| package com.ruoyi.web.controller.system; | 
|   | 
| import com.ruoyi.common.annotation.Log; | 
| import com.ruoyi.common.basic.PageInfo; | 
| import com.ruoyi.common.core.controller.BaseController; | 
| import com.ruoyi.common.core.domain.AjaxResult; | 
| import com.ruoyi.common.core.domain.entity.SysDept; | 
| import com.ruoyi.common.core.domain.entity.SysRole; | 
| import com.ruoyi.common.core.domain.entity.SysUser; | 
| import com.ruoyi.common.enums.BusinessType; | 
| import com.ruoyi.common.utils.SecurityUtils; | 
| import com.ruoyi.common.utils.StringUtils; | 
| import com.ruoyi.framework.web.service.TokenService; | 
| import com.ruoyi.system.dto.SysUserUpdateStatusDTO; | 
| import com.ruoyi.system.query.SysUserQuery; | 
| import com.ruoyi.system.service.*; | 
| import com.ruoyi.system.vo.SysUserVO; | 
| import io.swagger.annotations.Api; | 
| import io.swagger.annotations.ApiOperation; | 
| import org.springframework.beans.BeanUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.validation.annotation.Validated; | 
| import org.springframework.web.bind.annotation.*; | 
|   | 
| import java.util.*; | 
| import java.util.stream.Collectors; | 
|   | 
| /** | 
|  * 用户信息 | 
|  *  | 
|  * @author ruoyi | 
|  */ | 
| @Api(tags = "用户信息") | 
| @RestController | 
| @RequestMapping("/system/user") | 
| public class SysUserController extends BaseController | 
| { | 
|     @Autowired | 
|     private ISysUserService userService; | 
|   | 
|     @Autowired | 
|     private ISysRoleService roleService; | 
|   | 
|     @Autowired | 
|     private ISysDeptService deptService; | 
|     @Autowired | 
|     private TokenService tokenService; | 
|   | 
|     /** | 
|      * 获取用户列表 | 
|      */ | 
|     @ApiOperation(value = "获取用户列表") | 
|     @PostMapping("/list") | 
|     public AjaxResult list(@RequestBody SysUserQuery query) | 
|     { | 
|         PageInfo<SysUserVO> list = userService.pageList(query); | 
|         return AjaxResult.success(list); | 
|     } | 
|   | 
|     @ApiOperation(value = "获取用户列表-不分页") | 
|     @PostMapping("/listNotPage") | 
|     public AjaxResult listNotPage() | 
|     { | 
|         List<SysUser> list = userService.selectList(); | 
|         return AjaxResult.success(list); | 
|     } | 
|   | 
|     /** | 
|      * 获取用户黑名单列表 | 
|      */ | 
| //    @ApiOperation(value = "获取用户黑名单列表") | 
| //    @PostMapping("/blacklist") | 
| //    public AjaxResult blacklist(@RequestBody SysUserQuery query) | 
| //    { | 
| //        startPage(query.getPageNum(), query.getPageSize()); | 
| //        List<SysUserVO> list = userService.selectBlackPageList(query); | 
| //        return AjaxResult.success(getDataTable(list)); | 
| //    } | 
|   | 
|     /** | 
|      * 人员借用列表 | 
|      */ | 
| //    @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) | 
|     { | 
|         SysUser sysUser = userService.selectUserById(userId); | 
|         SysUserVO sysUserVO = new SysUserVO(); | 
|         BeanUtils.copyProperties(sysUser,sysUserVO); | 
|   | 
|         return AjaxResult.success(sysUser); | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 获取用户数量统计 | 
|      */ | 
|     @ApiOperation(value = "获取用户数量统计") | 
|     @PostMapping("/getUserCount") | 
|     public AjaxResult getUserCount() | 
|     { | 
|         Map<String,Integer> map = new HashMap<>(); | 
|   | 
|         Integer userCountSum = userService.selectCount(null); | 
|         Integer normalCount = userService.selectCount(0);// 正常 | 
|         Integer stopCount = userService.selectCount(1);// 停用 | 
|   | 
|         map.put("all",userCountSum); | 
|         map.put("normal",normalCount); | 
|         map.put("stop",stopCount); | 
|   | 
|         return AjaxResult.success(map); | 
|     } | 
|   | 
|     /** | 
|      * 移除黑名单 | 
|      */ | 
|     @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(); | 
|     } | 
|   | 
|   | 
| //    @Log(title = "用户管理", businessType = BusinessType.EXPORT) | 
| //    @PostMapping("/export") | 
| //    public void export(HttpServletResponse response, SysUser user) | 
| //    { | 
| //        List<SysUser> list = userService.selectUserList(user); | 
| //        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class); | 
| //        util.exportExcel(response, list, "用户数据"); | 
| //    } | 
|   | 
| //    @Log(title = "用户管理", businessType = BusinessType.IMPORT) | 
| //    @PostMapping("/importData") | 
| //    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception | 
| //    { | 
| //        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class); | 
| //        List<SysUser> userList = util.importExcel(file.getInputStream()); | 
| //        String operName = getUsername(); | 
| //        String message = userService.importUser(userList, updateSupport, operName); | 
| //        return AjaxResult.success(message); | 
| //    } | 
|   | 
| //    @PostMapping("/importTemplate") | 
| //    public void importTemplate(HttpServletResponse response) | 
| //    { | 
| //        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class); | 
| //        util.importTemplateExcel(response, "用户数据"); | 
| //    } | 
|   | 
|   | 
|     /** | 
|      * 新增用户 | 
|      */ | 
|     @ApiOperation(value = "新增用户管理") | 
|     @Log(title = "用户信息-新增用户", businessType = BusinessType.INSERT) | 
|     @PostMapping("/add") | 
|     public AjaxResult add(@Validated @RequestBody SysUser user) | 
|     { | 
|         user.setUserName(user.getPhonenumber()); | 
|         if (!userService.checkUserNameUnique(user)) | 
|         { | 
|             return error("新增用户'" + user.getUserName() + "'失败,登录账号已存在"); | 
|         } | 
|         else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) | 
|         { | 
|             return error("新增用户'" + user.getUserName() + "'失败,手机号码已存在"); | 
|         } | 
|         user.setCreateBy(getUsername()); | 
|         user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); | 
|         userService.insertUser(user); | 
|         return AjaxResult.success(); | 
|     } | 
|   | 
|     /** | 
|      * 修改用户 | 
|      */ | 
|     @ApiOperation(value = "修改用户管理") | 
|     @Log(title = "用户信息-修改用户", businessType = BusinessType.UPDATE) | 
|     @PostMapping("/edit") | 
|     public AjaxResult edit(@Validated @RequestBody SysUser user) | 
|     { | 
|         user.setUserName(user.getPhonenumber()); | 
| //        userService.checkUserAllowed(user); | 
| //        userService.checkUserDataScope(user.getUserId()); | 
|         if (!userService.checkUserNameUnique(user)) | 
|         { | 
|             return error("修改用户'" + user.getUserName() + "'失败,登录账号已存在"); | 
|         } | 
|         else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) | 
|         { | 
|             return error("修改用户'" + user.getUserName() + "'失败,手机号码已存在"); | 
|         } | 
|   | 
|         user.setUpdateBy(getUsername()); | 
|         user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); | 
|         return AjaxResult.success(userService.updateUser(user)); | 
|     } | 
|   | 
|     /** | 
|      * 删除用户 | 
|      */ | 
|     @ApiOperation(value = "批量删除用户") | 
|     @Log(title = "用户信息-批量删除用户", businessType = BusinessType.DELETE) | 
|     @DeleteMapping("/deleteById/{ids}") | 
|     public AjaxResult remove(@PathVariable String ids) | 
|     { | 
|         String[] split = ids.split(","); | 
|         List<Long> userIds = new ArrayList<>(); | 
|         for (String s : split) { | 
|             userIds.add(Long.valueOf(s)); | 
|         } | 
|         if (userIds.contains(getUserId())) | 
|         { | 
|             return error("当前用户不能删除"); | 
|         } | 
|         return AjaxResult.success(userService.deleteUserByIds(userIds)); | 
|     } | 
|   | 
|     /** | 
|      * 重置密码 | 
|      */ | 
|     @ApiOperation(value = "重置密码") | 
|     @Log(title = "用户信息-重置密码", businessType = BusinessType.UPDATE) | 
|     @PostMapping("/resetPwd") | 
|     public AjaxResult resetPwd(@RequestBody SysUser user) | 
|     { | 
|         userService.checkUserAllowed(user); | 
| //        userService.checkUserDataScope(user.getUserId()); | 
|         user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); | 
|         user.setUpdateBy(getUsername()); | 
|         return AjaxResult.success(userService.resetPwd(user)); | 
|     } | 
|   | 
|     /** | 
|      * 状态修改 | 
|      */ | 
|     @ApiOperation(value = "状态修改") | 
|     @Log(title = "用户信息-状态修改", businessType = BusinessType.UPDATE) | 
|     @PutMapping("/changeStatus") | 
|     public AjaxResult changeStatus(@RequestBody SysUserUpdateStatusDTO dto) | 
|     { | 
|         SysUser user = new SysUser(); | 
|         user.setUserId(dto.getUserId()); | 
|         user.setStatus(String.valueOf(dto.getStatus())); | 
|         user.setRemark(dto.getRemark()); | 
|         user.setUpdateBy(getUsername()); | 
|         return AjaxResult.success(userService.updateUserStatus(user)); | 
|     } | 
|   | 
|     /** | 
|      * 根据用户编号获取授权角色 | 
|      */ | 
|     @GetMapping("/authRole/{userId}") | 
|     public AjaxResult authRole(@PathVariable("userId") Long userId) | 
|     { | 
|         AjaxResult ajax = AjaxResult.success(); | 
|         SysUser user = userService.selectUserById(userId); | 
|         List<SysRole> roles = roleService.selectRolesByUserId(userId); | 
|         ajax.put("user", user); | 
|         ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); | 
|         return ajax; | 
|     } | 
|   | 
|     /** | 
|      * 用户授权角色 | 
|      */ | 
|     @Log(title = "用户管理", businessType = BusinessType.GRANT) | 
|     @PutMapping("/authRole") | 
|     public AjaxResult insertAuthRole(Long userId, Long[] roleIds) | 
|     { | 
|         userService.checkUserDataScope(userId); | 
|         userService.insertUserAuth(userId, roleIds); | 
|         return AjaxResult.success(); | 
|     } | 
|   | 
|     /** | 
|      * 获取部门树列表 | 
|      */ | 
|     @GetMapping("/deptTree") | 
|     public AjaxResult deptTree(SysDept dept) | 
|     { | 
|         return AjaxResult.success(deptService.selectDeptTreeList(dept)); | 
|     } | 
| } |