|  |  |  | 
|---|
|  |  |  | import com.zy.acs.manager.common.domain.BaseParam; | 
|---|
|  |  |  | import com.zy.acs.manager.common.domain.KeyValVo; | 
|---|
|  |  |  | import com.zy.acs.manager.common.domain.PageParam; | 
|---|
|  |  |  | import com.zy.acs.manager.common.domain.PageResult; | 
|---|
|  |  |  | import com.zy.acs.manager.common.utils.ExcelUtil; | 
|---|
|  |  |  | import com.zy.acs.manager.system.controller.param.ResetPwdParam; | 
|---|
|  |  |  | import com.zy.acs.manager.system.entity.User; | 
|---|
|  |  |  | 
|---|
|  |  |  | public R page(@RequestBody Map<String, Object> map) { | 
|---|
|  |  |  | BaseParam baseParam = buildParam(map, BaseParam.class); | 
|---|
|  |  |  | PageParam<User, BaseParam> pageParam = new PageParam<>(baseParam, User.class); | 
|---|
|  |  |  | return R.ok().add(userService.page(pageParam, pageParam.buildWrapper(true, wrapper -> wrapper.orderByAsc("create_time")))); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PreAuthorize("hasAuthority('system:user:list')") | 
|---|
|  |  |  | @PostMapping("/user/page1") | 
|---|
|  |  |  | public R page1(@RequestBody Map<String, Object> map) { | 
|---|
|  |  |  | BaseParam baseParam = buildParam(map, BaseParam.class); | 
|---|
|  |  |  | PageParam<User, BaseParam> pageParam = new PageParam<>(baseParam, User.class); | 
|---|
|  |  |  | return R.ok().add(userService.pageRel(pageParam)); | 
|---|
|  |  |  | PageResult<User> pageResult = userService.pageRel(pageParam); | 
|---|
|  |  |  | for (User record : pageResult.getRecords()) { | 
|---|
|  |  |  | record.setRoles(userRoleService.listByUserId(record.getId())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok().add(pageResult); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PreAuthorize("hasAuthority('system:user:list')") | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PreAuthorize("hasAuthority('system:user:save')") | 
|---|
|  |  |  | @OperationLog("添加用户") | 
|---|
|  |  |  | @OperationLog("Save User") | 
|---|
|  |  |  | @PostMapping("/user/save") | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public R save(@RequestBody User user) { | 
|---|
|  |  |  | if (!Cools.isEmpty(user.getUsername()) | 
|---|
|  |  |  | && userService.count(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername())) > 0) { | 
|---|
|  |  |  | return R.error("登录账号已存在"); | 
|---|
|  |  |  | return R.error("the username already exist"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(user.getNickname()) | 
|---|
|  |  |  | && userService.count(new LambdaQueryWrapper<User>().eq(User::getNickname, user.getNickname())) > 0) { | 
|---|
|  |  |  | return R.error("用户名称已存在"); | 
|---|
|  |  |  | return R.error("the nickname already exist"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(user.getPhone()) | 
|---|
|  |  |  | && userService.count(new LambdaQueryWrapper<User>().eq(User::getPhone, user.getPhone())) > 0) { | 
|---|
|  |  |  | return R.error("手机号已存在"); | 
|---|
|  |  |  | return R.error("the phone already exist"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | user.setPassword(userService.encodePassword(COMMON_PASSWORD)); | 
|---|
|  |  |  | if (Cools.isEmpty(user.getPassword())) { | 
|---|
|  |  |  | user.setPassword(userService.encodePassword(COMMON_PASSWORD)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | user.setCreateBy(getLoginUserId()); | 
|---|
|  |  |  | user.setCreateTime(new Date()); | 
|---|
|  |  |  | 
|---|
|  |  |  | user.setUpdateTime(new Date()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!userService.save(user)) { | 
|---|
|  |  |  | throw new CoolException("服务器内部错误"); | 
|---|
|  |  |  | throw new CoolException("Internal Server Error"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | userRoleService.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId())); | 
|---|
|  |  |  | for (Long roleId : user.getRoleIds()) { | 
|---|
|  |  |  | if (!userRoleService.save(new UserRole(user.getId(), roleId))) { | 
|---|
|  |  |  | throw new CoolException("服务器内部错误"); | 
|---|
|  |  |  | if (!Cools.isEmpty((Object) user.getRoleIds())) { | 
|---|
|  |  |  | for (Long roleId : user.getRoleIds()) { | 
|---|
|  |  |  | if (!userRoleService.save(new UserRole(user.getId(), roleId))) { | 
|---|
|  |  |  | throw new CoolException("Internal Server Error"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return R.ok("添加成功"); | 
|---|
|  |  |  | return R.ok("Save Success").add(user); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PreAuthorize("hasAuthority('system:user:update')") | 
|---|
|  |  |  | @OperationLog("修改用户") | 
|---|
|  |  |  | @OperationLog("Update User") | 
|---|
|  |  |  | @PostMapping("/user/update") | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public R update(@RequestBody User user) { | 
|---|
|  |  |  | if (user.getPassword() != null && user.getPassword().equals("")) { | 
|---|
|  |  |  | user.setPassword(null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(user.getUsername()) && userService.count(new LambdaQueryWrapper<User>() | 
|---|
|  |  |  | .eq(User::getUsername, user.getUsername()) | 
|---|
|  |  |  | .ne(User::getId, user.getId())) > 0) { | 
|---|
|  |  |  | return R.error("登录账号已存在"); | 
|---|
|  |  |  | return R.error("the username already exist"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(user.getNickname()) && userService.count(new LambdaQueryWrapper<User>() | 
|---|
|  |  |  | .eq(User::getNickname, user.getNickname()) | 
|---|
|  |  |  | .ne(User::getId, user.getId())) > 0) { | 
|---|
|  |  |  | return R.error("用户名称已存在"); | 
|---|
|  |  |  | return R.error("the nickname already exist"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(user.getPhone()) && userService.count(new LambdaQueryWrapper<User>() | 
|---|
|  |  |  | .eq(User::getPhone, user.getPhone()) | 
|---|
|  |  |  | .ne(User::getId, user.getId())) > 0) { | 
|---|
|  |  |  | return R.error("手机号已存在"); | 
|---|
|  |  |  | return R.error("the phone already exist"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!Cools.isEmpty(user.getPassword())) { | 
|---|
|  |  |  | 
|---|
|  |  |  | user.setUpdateTime(new Date()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!userService.updateById(user)) { | 
|---|
|  |  |  | throw new CoolException("服务器内部错误"); | 
|---|
|  |  |  | throw new CoolException("Internal Server Error"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (user.getRoleIds() != null && user.getRoleIds().length > 0) { | 
|---|
|  |  |  | userRoleService.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId())); | 
|---|
|  |  |  | for (Long roleId : user.getRoleIds()) { | 
|---|
|  |  |  | if (!userRoleService.save(new UserRole(user.getId(), roleId))) { | 
|---|
|  |  |  | throw new CoolException("服务器内部错误"); | 
|---|
|  |  |  | throw new CoolException("Internal Server Error"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return R.ok("修改成功"); | 
|---|
|  |  |  | return R.ok("Update Success").add(user); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PreAuthorize("hasAuthority('system:user:remove')") | 
|---|
|  |  |  | @OperationLog("删除用户") | 
|---|
|  |  |  | @OperationLog("Remove User") | 
|---|
|  |  |  | @PostMapping("/user/remove/{ids}") | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public R remove(@PathVariable Long[] ids) { | 
|---|
|  |  |  | for (Long userId : ids) { | 
|---|
|  |  |  | if (!userService.removeById(userId)) { | 
|---|
|  |  |  | throw new CoolException("服务器内部错误"); | 
|---|
|  |  |  | throw new CoolException("Internal Server Error"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!userRoleService.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userId))) { | 
|---|
|  |  |  | throw new CoolException("服务器内部错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | userRoleService.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userId)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok("删除成功"); | 
|---|
|  |  |  | return R.ok("Delete Success").add(ids); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PreAuthorize("hasAuthority('system:user:list')") | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PreAuthorize("hasAuthority('system:user:update')") | 
|---|
|  |  |  | @OperationLog("重置密码") | 
|---|
|  |  |  | @OperationLog("Reset Password") | 
|---|
|  |  |  | @PostMapping("/user/reset/pwd") | 
|---|
|  |  |  | public R resetPwd(@RequestBody ResetPwdParam param) { | 
|---|
|  |  |  | User user = userService.getById(param.getId()); | 
|---|
|  |  |  | if (!Cools.isEmpty(param.getOldPwd())) { | 
|---|
|  |  |  | if (!userService.comparePassword(user.getPassword(), param.getOldPwd())) { | 
|---|
|  |  |  | return R.error("当前密码错误"); | 
|---|
|  |  |  | return R.error("The old password is incorrect"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!Cools.isEmpty(param.getPassword())) { | 
|---|
|  |  |  | 
|---|
|  |  |  | user.setUpdateBy(getLoginUserId()); | 
|---|
|  |  |  | user.setUpdateTime(new Date()); | 
|---|
|  |  |  | if (!userService.updateById(user)) { | 
|---|
|  |  |  | return R.error("重置失败"); | 
|---|
|  |  |  | return R.error("Reset Fail"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok("重置成功"); | 
|---|
|  |  |  | return R.ok("Reset Success"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|