package com.org.system.controller;

import com.org.system.annotation.Log;
import com.org.system.config.validation.Update;
import com.org.system.entity.SysUser;
import com.org.system.enums.BusinessType;
import com.org.system.params.SysUserGrantRoleParam;
import com.org.system.service.SysUserService;
import com.org.utils.AjaxResult;
import com.org.utils.StringUtils;
import com.org.utils.page.TableDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("sys/user")
public class SysUserController extends BaseController{

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('sys:user:add')")
    @Log(title = "系统用户新增", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody @Validated SysUser sysUser, @RequestParam String password){
        sysUser.setPassword(password);
        log.info("SysUserController - add sysUser:{}", sysUser);

        return toAjax(sysUserService.saveOrUpdate(sysUser));
    }

    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPermi('sys:user:edit')")
    @Log(title = "系统用户修改", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody @Validated(Update.class) SysUser sysUser){
        log.info("SysUserController - edit sysUser:{}", sysUser);

        return toAjax(sysUserService.saveOrUpdate(sysUser));
    }

    @PostMapping("/del")
    @PreAuthorize("@ss.hasPermi('sys:user:del')")
    @Log(title = "系统用户删除", businessType = BusinessType.DELETE)
    public AjaxResult del(String ids, String deleteReason){
        log.info("SysUserController - del ids:{}", ids);
        log.info("SysUserController - del deleteReason:{}", deleteReason);

        if(StringUtils.isEmpty(ids)) return AjaxResult.success();

        List<Long> idList = Arrays.asList(ids.split(",")).stream().map(idStr -> Long.parseLong(idStr)).collect(Collectors.toList());

        return toAjax(sysUserService.del(idList, deleteReason));
    }

    @GetMapping("/pageList")
    @PreAuthorize("@ss.hasPermi('sys:user:list')")
    @Log(title = "系统用户查询", businessType = BusinessType.SELECT)
    public TableDataInfo pageList(SysUser sysUser){
        log.info("SysUserController - pageList sysUser:{}", sysUser);

        startPage();
        List<SysUser> list = sysUserService.findList(sysUser);

        return getDataTable(list);
    }


    // 查询用户拥有角色
    @PostMapping("/ownRole")
    @PreAuthorize("@ss.hasPermi('sys:user:ownRole')")
    @Log(title = "用户_拥有角色", businessType = BusinessType.SELECT)
    public AjaxResult ownRole(Long id){
        log.info("SysUserController - ownMenu id:{}", id);

        return AjaxResult.success(sysUserService.ownRole(id));
    }

    // 用户授权角色
    @PostMapping("/grantRole")
    @PreAuthorize("@ss.hasPermi('sys:user:grantRole')")
    @Log(title = "用户授权角色", businessType = BusinessType.SELECT)
    public AjaxResult grantRole(@RequestBody @Validated SysUserGrantRoleParam param){
        log.info("SysUserController - grantMenu id:{}, grantRoleIdList{}", param.getId(), param.getGrantRoleIdList());

        sysUserService.grantRole(param.getId(), param.getGrantRoleIdList());
        return AjaxResult.success();
    }

    // 重置密码
    @GetMapping("/resetPwd")
    @PreAuthorize("@ss.hasPermi('sys:user:resetPwd')")
    @Log(title = "重置密码", businessType = BusinessType.SELECT)
    public AjaxResult resetPwd(@RequestParam Long id) {
        log.info("SysUserController - resetPwd id:{}", id);

        return toAjax(sysUserService.resetPwd(id));
    }

    // 修改密码
    @GetMapping("/changePwd")
//    @PreAuthorize("@ss.hasPermi('sys:user:changePwd')")
    @Log(title = "修改密码", businessType = BusinessType.SELECT)
    public AjaxResult changePwd(@RequestParam String oldPassword, @RequestParam("password") String password) {
        log.info("SysUserController - changePwd oldPassword:{}", oldPassword);
        log.info("SysUserController - changePwd password:{}", password);

        return toAjax(sysUserService.changePwd(oldPassword, password));
    }
}
