package api.controller.system; import api.entity.database.system.*; import api.entity.input.system.CustomerInput; import api.entity.view.system.CustomerView; import api.entity.view.system.UserView; import api.service.system.*; import api.service.system.impl.UserPostServiceImpl; import api.util.annotation.Anonymous; import com.alibaba.fastjson2.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import api.controller.BaseController; import api.entity.input.PageInput; import api.model.AjaxResult; import api.util.annotation.Log; import api.util.enums.BusinessType; import api.util.helper.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.var; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.*; import java.util.stream.Collectors; @Api(value = "用户信息表", tags = "用户信息表") @RestController @RequestMapping("/system/user") public class UserController extends BaseController { @Autowired public StringRedisTemplate stringRedisTemplate; @Autowired private IUserService userService; @Autowired private IDeptService deptService; @Autowired private IRoleService roleService; @Autowired private IUserPostService userPostService; @ApiOperation("列表") @Log(title = "查询用户信息表列表", businessType = BusinessType.QUERY) @GetMapping public AjaxResult getList(User input, PageInput pageInput) { LambdaQueryWrapper qw = new LambdaQueryWrapper(); qw.eq(input.getUserId() != null && input.getUserId() > 0, User::getUserId, input.getUserId()); // qw.eq(input.getDeptId() != null && input.getDeptId() > 0, User::getDeptId, input.getDeptId()); if (input.getDeptId() != null &&input.getDeptId()>0) { //查询下级部门所有人员 qw.and(wq->{ wq.inSql(User::getDeptId, "select dept_id from sys_dept where " + " ancestors REGEXP CONCAT((select ancestors from sys_dept where dept_id="+input.getDeptId()+"),','," +input.getDeptId()+") ") ; wq.or().eq(User::getDeptId,input.getDeptId()); }); } qw.like(!StringHelper.isEmpty(input.getUserName()), User::getUserName, input.getUserName()); qw.like(!StringHelper.isEmpty(input.getNickName()), User::getNickName, input.getNickName()); qw.eq(!StringHelper.isEmpty(input.getSex()), User::getSex, input.getSex()); qw.eq(User::getDeleteFlag, "0"); qw.orderByDesc(User::getUserId); Page page = GetPage(pageInput); if (page != null) { IPage iPage = userService.selectUserDeptList(page, qw); List userViewList = new ArrayList<>(); for (User user : iPage.getRecords()) { UserView uv = JSON.parseObject(JSON.toJSONString(user), UserView.class); Role role = roleService.getEntity(user.getRoleId());//.selectRoleByUserId(user.getUserId()); if (role != null) { uv.setRoleName(role.getRoleName()); } userViewList.add(uv); } return Success("成功", userViewList, iPage.getTotal()); } else { return Success("成功", userService.selectUserDeptList(qw)); } } @ApiOperation("详情") @Log(title = "查询用户详情", businessType = BusinessType.QUERY) @GetMapping("/{id}") public AjaxResult getInfo(@PathVariable Long id) { User user = userService.selectUserById(id); if (user == null) { return Error("用户不存在"); } return Success("成功", user); } @ApiOperation("新增") @Log(title = "新增用户", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody User user) { if (!userService.checkUserNameUnique(user)) { return Error("新增用户'" + user.getUserName() + "'失败,登录账号已存在"); } else if (StringHelper.isNotEmpty(user.getTelephone()) && !userService.checkPhoneUnique(user)) { return Error("新增用户'" + user.getUserName() + "'失败,手机号码已存在或者手机号格式错误"); } if (StringHelper.isEmpty(user.getPassword())) return Error("请输入密码"); if (StringHelper.isEmpty(user.getUserName())) return Error("请输入姓名"); if (StringHelper.isEmpty(user.getNickName())) return Error("请输入工号"); user.setMobileShow(user.getMobile().substring(0,3)+"****"+user.getMobile().substring(7)); user.setMobile(SecretHelper.AesEncrypt( user.getMobile())); user.setPassword(SecretHelper.MD5(user.getPassword())); user.setCreateBy(CurrentUser().getUserName()); boolean result = userService.insert(user); if (result) { return Success("新增成功"); } else { return Error("新增失败"); } } @ApiOperation("编辑") @Log(title = "修改角色信息", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody User user) { userService.checkUserAllowed(user); if (!userService.checkUserNameUnique(user)) { return Error("修改用户'" + user.getUserName() + "'失败,登录账号已存在"); } else if (StringHelper.isNotEmpty(user.getTelephone()) && !userService.checkPhoneUnique(user)) { return Error("修改用户'" + user.getUserName() + "'失败,手机号码已存在或者手机号格式错误"); } User use = userService.selectUserById(user.getUserId()); if (use==null) return Error("用户不存在"); user.setPassword(use.getPassword()); user.setCreateBy(CurrentUser().getUserName()); user.setCreateTime(new Date()); Dept dept= deptService.getEntity( user.getDeptId()); boolean result = userService.update(user); if (result) { return Success("修改成功"); } else { return Error("修改失败"); } } /** * 删除用户 * * @param ids * @return */ @ApiOperation("删除") @Log(title = "删除用户", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult delete(@PathVariable Long[] ids) { LambdaUpdateWrapper uw = new LambdaUpdateWrapper<>(); uw.set(User::getDeleteFlag, "2").in(User::getUserId, ids); boolean result = userService.updateBatch(uw); if (result) { return Success("删除成功"); } else { return Error("删除失败"); } } /** * 重置密码 */ @ApiOperation("重置密码") @Log(title = "重置密码", businessType = BusinessType.OTHER) @PutMapping("/resetPwd") public AjaxResult resetPwd(@RequestBody User user) { //校验用户操作 //校验用户权限 // userService.checkUserAllowed(user); //userService.checkUserDataScope(user); LambdaQueryWrapper qw = new LambdaQueryWrapper<>(); qw.eq(User::getUserId, user.getUserId()) .eq(User::getDeleteFlag, "0"); User entity = userService.getEntity(qw); userService.checkUserAllowed(entity); // if (!FormatHelper.checkPassword(user.getPassword())) { // return Error("密码长度为8到20位,必须包含字母和数字,字母区分大小写!"); // } //md5加密 String password = SecretHelper.MD5(user.getPassword()); entity.setPassword(password); if (userService.update(entity)) { return Success("修改成功!"); } return Error("修改失败!"); } /** * 修改密码 */ @ApiOperation("修改密码") @Log(title = "修改密码", businessType = BusinessType.OTHER) @PutMapping("/UpdatePwd") public AjaxResult UpdatePwd(String oldPwd ,String Pwd) { UserView currentUser=CurrentUser(); if (!currentUser.getPassword().equals(SecretHelper.MD5(oldPwd))) { return Error("旧密码不正确"); } var entity=userService.getEntity(currentUser.getUserId()); String password = SecretHelper.MD5(Pwd); entity.setPassword(password); if (userService.update(entity)) { return Success("修改成功!"); } return Error("修改失败!"); } // /** // * 修改图像 // */ // @ApiOperation("修改图像") // @Log(title = "修改图像", businessType = BusinessType.OTHER) // @GetMapping("/avatar") // public AjaxResult avatar( String url) { // UserView currentUser=CurrentUser(); // var entity=userService.getEntity(currentUser.getUserId()); // entity.setAvatar(url); // if (userService.update(entity)) { // return Success("修改成功!"); // } // return Error("修改失败!"); // } /** * 修改状态 * * @param user * @return */ @ApiOperation("修改状态 禁用用户") @PutMapping("/changeStatus") public AjaxResult changeStatus(@RequestBody User user) { //校验用户操作 //校验用户权限 userService.checkUserAllowed(user); //userService.checkUserDataScope(user); LambdaQueryWrapper qw = new LambdaQueryWrapper<>(); qw.eq(User::getUserId, user.getUserId()).eq(User::getDeleteFlag, "0"); User entity = userService.getEntity(qw); entity.setDeleteFlag(user.getDeleteFlag()); if (entity == null) { return Error("用户不存在"); } if (userService.update(entity)) { return Success("修改成功!"); } return Error("修改失败!"); } /** * 获取部门树列表 */ @GetMapping("/deptTree") public AjaxResult deptTree(Dept dept) { return Success("查询成功", deptService.selectDeptTreeList(dept)); } @GetMapping("/deptUserTree") @Anonymous public AjaxResult deptUserTree(Dept dept) { return Success("查询成功", deptService.selectUserDeptTreeList(dept)); } /** * 导出excel * */ @ApiOperation("导出Excel") @Log(title = "导出excel", businessType = BusinessType.EXPORT) @PostMapping("/exportExcel") public void ExportExcel(User input) { LambdaQueryWrapper qw = new LambdaQueryWrapper(); qw.eq(input.getUserId() != null && input.getUserId() > 0, User::getUserId, input.getUserId()); qw.eq(input.getDeptId() != null && input.getDeptId() > 0, User::getDeptId, input.getDeptId()); qw.like(!StringHelper.isEmpty(input.getUserName()), User::getUserName, input.getUserName()); qw.like(!StringHelper.isEmpty(input.getNickName()), User::getNickName, input.getNickName()); qw.like(!StringHelper.isEmpty(input.getTelephone()), User::getTelephone, input.getTelephone()); qw.like(!StringHelper.isEmpty(input.getSex()), User::getSex, input.getSex()); qw.like(!StringHelper.isEmpty(input.getPassword()), User::getPassword, input.getPassword()); qw.eq(User::getDeleteFlag, "0"); qw.like(!StringHelper.isEmpty(input.getCreateBy()), User::getCreateBy, input.getCreateBy()); qw.eq(input.getCreateTime() != null, User::getCreateTime, input.getCreateTime()); qw.eq(input.getUpdateTime() != null, User::getUpdateTime, input.getUpdateTime()); qw.like(!StringHelper.isEmpty(input.getRemark()), User::getRemark, input.getRemark()); ExcelHelper excel = new ExcelHelper<>(User.class); excel.exportExcel("xlsx", userService.getList(qw)); } /** * excel导入 * * @param file * @return * @throws Exception */ @ApiOperation("导入Excel") @Log(title = "excel导入", businessType = BusinessType.IMPORT) @PostMapping("/importData") public AjaxResult importExcel(MultipartFile file, Boolean updateSupport) throws Exception { ExcelHelper excel = new ExcelHelper<>(User.class); List list = excel.importExcel(file.getInputStream()); if (list == null) { return Error("导入失败"); } String operName = CurrentUser().getUserName(); Long dept_id = CurrentUser().getDeptId(); String result = userService.importUser(list, updateSupport, operName, dept_id); if (result == null) { return Error("导入失败"); } return Success("导入完成", result); } /** * 删除用户与角色直接的关联 */ @Log(title = "删除用户与角色直接的关联", businessType = BusinessType.DELETE) @DeleteMapping("/deleteUserRole/{userId}") public AjaxResult deleteUserRole(@PathVariable Long userId) { if (userService.deleteUserRoleByUserId(userId) > 0) { return Success("删除成功"); } else { return Error("删除失败"); } } /** * 批量在某角色下添加用户 */ @Log(title = "批量在某角色下添加用户", businessType = BusinessType.OTHER) @PostMapping("/addUsersByRoleId") public AjaxResult addUsersByRoleId(Long[] userIds, Long roleId) { userService.addUsersRole(userIds, roleId); return Success("成功"); } /** * 根据roleId获取用户列表 */ @Log(title = "根据roleId获取用户列表", businessType = BusinessType.QUERY) @GetMapping("/getUserListByRoleId/{roleId}") public AjaxResult getUserListByRoleId(@PathVariable("roleId") Long roleId) { List userList = userService.selectUserListByRoleId(roleId); if (userList != null) { return Success("成功", userList); } return Error("该角色暂无用户!"); } /** * 分配角色 * @param userId * @return */ @Log(title = "分配角色", businessType = BusinessType.OTHER) @GetMapping("/authRole/{userId}") public AjaxResult authRole(@PathVariable("userId") Long userId) { Map map = new HashMap<>(); User user = userService.getEntity(userId); LambdaQueryWrapper qw2 = new LambdaQueryWrapper<>(); qw2.eq(Role::getRoleId, userId); List roles = roleService.selectRolesByUserId(userId); map.put("user", user); map.put("roles", user.isAdmin() ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); return Success("成功", map); } /** * 用户授权角色 */ @Log(title = "用户授权角色,改成单选roleid传int", businessType = BusinessType.GRANT) @PutMapping("/authRole") public AjaxResult insertAuthRole(Long userId, Long roleId) { userService.checkUserDataScope(userService.getEntity(userId)); // userService.insertUserAuth(userId, roleIds); Role role= roleService.getEntity(roleId); //给user表的roleid赋值 LambdaUpdateWrapper uw=new LambdaUpdateWrapper<>(); uw.eq(User::getUserId,userId); uw.set(User::getRoleId,roleId).set(User::getRoleCode,role.getRoleCode()); userService.updateBatch(uw); return Success("授权成功!"); } @Log(title = "根据name获取用户部门的name,tel", businessType = BusinessType.QUERY) @GetMapping("/getnametelbyname") public AjaxResult getNameTelListByName(String name) { HashMap hashMap=new HashMap<>(); LambdaQueryWrapper qw2 = new LambdaQueryWrapper<>(); qw2.eq(User::getDeleteFlag, 0).like(User::getNickName, name); List userList =userService.getList(qw2); for (User user:userList) { hashMap.put(user.getNickName(),user.getTelephone()); } LambdaQueryWrapper qw = new LambdaQueryWrapper<>(); qw.eq(Dept::getDelFlag, 0).like(Dept::getDeptName, name); List depts =deptService.getList(qw); for (Dept dept:depts) { hashMap.put(dept.getDeptName(),dept.getPhone()); } return Success("成功",hashMap); } }