xuhy
2024-09-25 842947e6ecf0bdf8fd98049c0e47eb3893a8cbdb
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java
@@ -13,6 +13,7 @@
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.SysUserUpdateStatusDTO;
import com.ruoyi.system.dto.UpdatePwdDTO;
import com.ruoyi.system.query.SysUserQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.SysUserVO;
@@ -22,6 +23,7 @@
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@@ -55,7 +57,7 @@
     */
    @ApiOperation(value = "获取用户列表")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody SysUserQuery query)
    public AjaxResult<PageInfo<SysUserVO>> list(@RequestBody SysUserQuery query)
    {
        PageInfo<SysUserVO> list = userService.pageList(query);
        return AjaxResult.success(list);
@@ -63,7 +65,7 @@
    @ApiOperation(value = "获取用户列表-不分页")
    @PostMapping("/listNotPage")
    public AjaxResult listNotPage()
    public AjaxResult<List<SysUser>> listNotPage()
    {
        List<SysUser> list = userService.selectList();
        return AjaxResult.success(list);
@@ -82,110 +84,11 @@
//    }
    /**
     * 人员借用列表
     */
//    @ApiOperation(value = "人员借用列表")
//    @GetMapping("/userBorrowList")
//    public AjaxResult userBorrowList(@RequestParam(required = false) String name,
//                                     @RequestParam(required = false) Integer type)
//    {
//
//        UserAddListVO userAddListVO = new UserAddListVO();
//
//        Long companyId = tokenService.getLoginUser().getUser().getCompanyId();
//
//        List<TCompany> companyList = new ArrayList<>();
//        List<TDept> deptList = new ArrayList<>();
//        List<SysUser> userList = new ArrayList<>();
//        // 查询公司
//        if(Objects.nonNull(type) && type == 1){
//            companyList = companyService.userAddListByCompanyName(name);
//        }
//        // 查询部门
//        if(Objects.nonNull(type) && type == 2){
//            deptList = tDeptService.userAddListByDeptName(name);
//        }
//        // 查询用户
//        if(Objects.nonNull(type) && type == 3){
//            userList = userService.selectListByNamePhone(name);
//        }
//
//        if(Objects.isNull(type)){
//            companyList = companyService.userAddListByCompanyName(name);
//            deptList = tDeptService.userAddListByDeptName(name);
//            userList = userService.selectListByNamePhone(name);
//        }
//
//        List<Long> companyIds = companyList.stream().map(TCompany::getId).collect(Collectors.toList());
//        List<Long> deptCompanyIds = deptList.stream().map(TDept::getCompanyId).collect(Collectors.toList());
//        List<Long> userCompanyIds = userList.stream().map(SysUser::getCompanyId).collect(Collectors.toList());
//        companyIds.addAll(deptCompanyIds);
//        companyIds.addAll(userCompanyIds);
//
//        companyIds = companyIds.stream().distinct().collect(Collectors.toList());
//
//        if(CollectionUtils.isEmpty(companyIds)){
//            return AjaxResult.success(userAddListVO);
//        }
//        SysUser user1 = tokenService.getLoginUser().getUser();
//        if(!user1.isAdmin()){
//            companyIds = companyIds.stream().filter(e->!e.equals(companyId)).collect(Collectors.toList());
//        }
//
//        // 查询符合要求的公司
//        List<UserLevelVO> parent = companyService.userAddListByCompanyIds(companyIds);
//
//        List<TDept> depts = tDeptService.selectList();
//
//        List<SysUser> sysUsers = userService.selectList();
//
//        for (UserLevelVO userLevelVO : parent) {
//
//            // 找到公司下的部门
//            List<TDept> tDepts = depts.stream().filter(e -> userLevelVO.getKey().equals(e.getCompanyId())).collect(Collectors.toList());
//            List<UserLevelVO> children = new ArrayList<>();
//            // 封装部门
//            for (TDept dept : tDepts) {
//                userLevelVO.setChildren(children);
//                UserLevelVO userLevelVO1 = new UserLevelVO();
//                userLevelVO1.setKey(dept.getId());
//                userLevelVO1.setTitle(dept.getDeptName());
//                // 找到部门下的人员
//                List<SysUser> users;
//                if(StringUtils.isNotEmpty(name) && type == 3){
//                    users = sysUsers.stream().filter(e -> userLevelVO1.getKey().equals(e.getDeptId())
//                            && ((StringUtils.isNotEmpty(e.getNickName()) && e.getNickName().contains(name)))
//                            || (StringUtils.isNotEmpty(e.getPhonenumber()) && e.getPhonenumber().contains(name))).collect(Collectors.toList());
//                }else {
//                    users = sysUsers.stream().filter(e -> userLevelVO1.getKey().equals(e.getDeptId())).collect(Collectors.toList());
//                }
//                List<UserLevelVO> children1 = new ArrayList<>();
//                // 封装人员
//                for (SysUser user : users) {
//                    UserLevelVO userLevelVO2 = new UserLevelVO();
//                    userLevelVO2.setKey(user.getUserId());
//                    userLevelVO2.setTitle(user.getNickName());
//                    userLevelVO2.setAvatar(user.getAvatar());
//                    userLevelVO2.setFlag(true);
//                    children1.add(userLevelVO2);
//                }
//                userLevelVO1.setChildren(children1);
//
//                children.add(userLevelVO1);
//            }
//            userLevelVO.setChildren(children);
//        }
//        userAddListVO.setUserLevelVOS(parent);
//        userAddListVO.setUserList(sysUsers);
//        return AjaxResult.success(userAddListVO);
//    }
    /**
     * 获取用户详情
     */
    @ApiOperation(value = "获取用户详情")
    @GetMapping("/getDetail")
    public AjaxResult getDetail(@RequestParam Long userId)
    public AjaxResult<SysUser> getDetail(@RequestParam Long userId)
    {
        SysUser sysUser = userService.selectUserById(userId);
        SysUserVO sysUserVO = new SysUserVO();
@@ -200,7 +103,7 @@
     */
    @ApiOperation(value = "获取用户数量统计")
    @PostMapping("/getUserCount")
    public AjaxResult getUserCount()
    public AjaxResult<Map<String,Integer>> getUserCount()
    {
        Map<String,Integer> map = new HashMap<>();
@@ -218,17 +121,17 @@
    /**
     * 移除黑名单
     */
    @GetMapping("/removeBlackList")
    public AjaxResult removeBlackList(@RequestParam String ids)
    {
        String[] split = ids.split(",");
        List<Long> id = new ArrayList<>();
        for (String s : split) {
            id.add(Long.valueOf(s));
        }
        userService.updateUserIfBlack(id);
        return AjaxResult.success();
    }
//    @GetMapping("/removeBlackList")
//    public AjaxResult<String> removeBlackList(@RequestParam String ids)
//    {
//        String[] split = ids.split(",");
//        List<Long> id = new ArrayList<>();
//        for (String s : split) {
//            id.add(Long.valueOf(s));
//        }
//        userService.updateUserIfBlack(id);
//        return AjaxResult.success();
//    }
//    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
@@ -270,7 +173,6 @@
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysUser user)
    {
        user.setUserName(user.getPhonenumber());
        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败,登录账号已存在");
@@ -279,6 +181,8 @@
        {
            return error("新增用户'" + user.getUserName() + "'失败,手机号码已存在");
        }
        user.setPassword("123456");
        user.setRePassword("123456");
        user.setCreateBy(getUsername());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        userService.insertUser(user);
@@ -294,7 +198,6 @@
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysUser user)
    {
        user.setUserName(user.getPhonenumber());
//        userService.checkUserAllowed(user);
//        userService.checkUserDataScope(user.getUserId());
        if (!userService.checkUserNameUnique(user))
@@ -307,7 +210,7 @@
        }
        user.setUpdateBy(getUsername());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
//        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return AjaxResult.success(userService.updateUser(user));
    }
@@ -345,6 +248,34 @@
//        userService.checkUserDataScope(user.getUserId());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setUpdateBy(getUsername());
        userService.resetPwd(user);
        return AjaxResult.success();
    }
    /**
     * 修改密码
     */
    // @PreAuthorize("@ss.hasPermi('system:user:resetPwd')")
    @ApiOperation(value = "修改密码")
    @Log(title = "用户信息-修改密码", businessType = BusinessType.UPDATE)
    @PostMapping("/updatePwd")
    public AjaxResult updatePwd(@RequestBody UpdatePwdDTO dto)
    {
        SysUser user = userService.selectUserByUserName(dto.getAccount());
        if(Objects.isNull(user)){
            return AjaxResult.error("未查询到该账号");
        }
        userService.checkUserAllowed(user);
        // 校验密码跟原密码是否匹配
        if (!SecurityUtils.matchesPassword(dto.getOldPassword(), user.getPassword())) {
            throw new BadCredentialsException("输入原密码不正确");
        }
        if (!dto.getPassword().equals(dto.getConfirmPassword())) {
            throw new BadCredentialsException("两次输入密码不一致");
        }
//        userService.checkUserDataScope(user.getUserId());
        user.setPassword(SecurityUtils.encryptPassword(dto.getPassword()));
        user.setUpdateBy(getUsername());
        return AjaxResult.success(userService.resetPwd(user));
    }
@@ -359,7 +290,7 @@
        SysUser user = new SysUser();
        user.setUserId(dto.getUserId());
        user.setStatus(String.valueOf(dto.getStatus()));
        user.setRemark(dto.getRemark());
        user.setDisableRemark(dto.getDisableRemark());
        user.setUpdateBy(getUsername());
        return AjaxResult.success(userService.updateUserStatus(user));
    }