|  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @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)); | 
 |  |  | 
 |  |  |         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("服务器内部错误"); | 
 |  |  |                 throw new CoolException("Internal Server Error"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return R.ok("添加成功"); | 
 |  |  |         return R.ok("Save Success"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @PreAuthorize("hasAuthority('system:user:update')") | 
 |  |  |     @OperationLog("修改用户") | 
 |  |  |     @OperationLog("Update User") | 
 |  |  |     @PostMapping("/user/update") | 
 |  |  |     @Transactional | 
 |  |  |     public R update(@RequestBody User user) { | 
 |  |  |         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"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @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("服务器内部错误"); | 
 |  |  |                 throw new CoolException("Internal Server Error"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return R.ok("删除成功"); | 
 |  |  |         return R.ok("Delete Success"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @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"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |