package com.ruoyi.predict.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author hlf
 * @date 2022/12/9 15:13
 * 文件说明：用户管理
 */
@Api(value = "用户管理Controller", tags = {"用户管理操作接口"})
@Controller
@RequestMapping("/predict/api/umc")
public class UserManagementController extends BaseController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysPasswordService passwordService;

    @ApiOperation("获取用户信息列表")
    @PostMapping("/getUserList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名称"),
            @ApiImplicitParam(name = "loginName", value = "登录名称"),
            @ApiImplicitParam(name = "roleName", value = "角色名称"),
    })
    public TableDataInfo getUserList(SysUser user) {
        startPage();
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    @ApiOperation("新增用户信息")
    @PostMapping("/addUser")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名称", required = true),
            @ApiImplicitParam(name = "loginName", value = "登录名称", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true),
            @ApiImplicitParam(name = "roleId", value = "用户角色", required = true),
    })
    public AjaxResult addUser(SysUser user) {
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(user.getLoginName()))) {
            return error("新增用户'" + user.getLoginName() + "'失败，登录账号已存在");
        }
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        user.setLoginNumber((long) 0);
        userService.insertUser(user);
        Long[] roleIds = {user.getRoleId()};
        userService.insertUserAuth(user.getUserId(), roleIds);
        return AjaxResult.success();
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/editUser")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户主键", required = true),
            @ApiImplicitParam(name = "userName", value = "用户名称"),
            @ApiImplicitParam(name = "roleId", value = "用户角色"),
    })
    public AjaxResult editUser(SysUser user) {
        Long[] roleIds = {user.getRoleId()};
        user.setRoleIds(roleIds);
        return toAjax(userService.updateUser(user));
    }

    @ApiOperation("删除用户信息")
    @PostMapping("/delUser")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户主键", required = true),
    })
    public AjaxResult delUser(@RequestParam(value = "userId") String userId) {
        return toAjax(userService.deleteUserByIds(userId));
    }
}
