PoUserController.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package com.ruoyi.web.controller.system;
  2. import com.ruoyi.common.annotation.Log;
  3. import com.ruoyi.common.constant.UserConstants;
  4. import com.ruoyi.common.core.controller.BaseController;
  5. import com.ruoyi.common.core.domain.AjaxResult;
  6. import com.ruoyi.common.core.domain.entity.SysRole;
  7. import com.ruoyi.common.core.domain.entity.SysUser;
  8. import com.ruoyi.common.core.page.TableDataInfo;
  9. import com.ruoyi.common.enums.BusinessType;
  10. import com.ruoyi.common.utils.SecurityUtils;
  11. import com.ruoyi.common.utils.StringUtils;
  12. import com.ruoyi.common.utils.poi.ExcelUtil;
  13. import com.ruoyi.system.domain.PoUser;
  14. import com.ruoyi.system.service.IPoUserService;
  15. import org.apache.catalina.User;
  16. import org.apache.commons.lang3.ArrayUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.security.access.prepost.PreAuthorize;
  19. import org.springframework.validation.annotation.Validated;
  20. import org.springframework.web.bind.annotation.*;
  21. import javax.servlet.http.HttpServletResponse;
  22. import java.util.List;
  23. import java.util.stream.Collectors;
  24. /**
  25. * 被分配用户Controller
  26. *
  27. * @author ruoyi
  28. * @date 2023-01-17
  29. */
  30. @RestController
  31. @RequestMapping("/system/user")
  32. public class PoUserController extends BaseController
  33. {
  34. @Autowired
  35. private IPoUserService poUserService;
  36. /**
  37. * 查询用户列表
  38. */
  39. @PreAuthorize("@ss.hasPermi('system:user:list')")
  40. @GetMapping("/list")
  41. public TableDataInfo list(PoUser poUser)
  42. {
  43. startPage();
  44. List<PoUser> list = poUserService.selectPoUserList(poUser);
  45. return getDataTable(list);
  46. }
  47. /**
  48. * 导出用户列表
  49. */
  50. @PreAuthorize("@ss.hasPermi('system:user:export')")
  51. @Log(title = "被分配权限的用户", businessType = BusinessType.EXPORT)
  52. @PostMapping("/export")
  53. public void export(HttpServletResponse response, PoUser poUser)
  54. {
  55. List<PoUser> list = poUserService.selectPoUserList(poUser);
  56. ExcelUtil<PoUser> util = new ExcelUtil<PoUser>(PoUser.class);
  57. util.exportExcel(response, list, "用户数据");
  58. }
  59. /**
  60. * 根据用户编号获取详细信息
  61. */
  62. @PreAuthorize("@ss.hasPermi('system:user:query')")
  63. @GetMapping(value = { "/", "/{userId}" })
  64. public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
  65. {
  66. poUserService.checkUserDataScope(userId);
  67. AjaxResult ajax = AjaxResult.success();
  68. if (StringUtils.isNotNull(userId))
  69. {
  70. PoUser poUser = poUserService.selectPoUserByUserId(userId);
  71. ajax.put(AjaxResult.DATA_TAG, poUser);
  72. }
  73. return ajax;
  74. }
  75. /**
  76. * 新增分配权限用户
  77. */
  78. @PreAuthorize("@ss.hasPermi('system:user:add')")
  79. @Log(title = "获得权限用户", businessType = BusinessType.INSERT)
  80. @PostMapping("/add")
  81. public AjaxResult add(@Validated @RequestBody PoUser poUser)
  82. {
  83. if(UserConstants.NOT_UNIQUE.equals(poUserService.checkUserNameUnique(poUser))){
  84. return error("新增用户"+poUser.getUserName()+"失败,用户账号已经存在");
  85. } else if (StringUtils.isNotEmpty(poUser.getPhonenumber())&& UserConstants.NOT_UNIQUE.equals(poUserService.checkPhoneUnique(poUser))) {
  86. return error("新增用户"+poUser.getPhonenumber()+"失败,用户手机号已经存在");
  87. }else if(StringUtils.isNotEmpty(poUser.getEmail()) && UserConstants.NOT_UNIQUE.equals(poUserService.checkEmailUnique(poUser))){
  88. return error("新增用户"+poUser.getEmail()+"失败,用户邮箱账号已经存在");
  89. }
  90. poUser.setCreateBy(getUsername());
  91. poUser.setPassword(SecurityUtils.encryptPassword(poUser.getPassword()));
  92. return toAjax(poUserService.insertPoUser(poUser));
  93. }
  94. /**
  95. * 修改用户
  96. */
  97. @PreAuthorize("@ss.hasPermi('system:user:edit')")
  98. @Log(title = "修改权限用户", businessType = BusinessType.UPDATE)
  99. @PutMapping("/edit")
  100. public AjaxResult edit( @Validated @RequestBody PoUser poUser)
  101. {
  102. poUserService.checkUserAllowed(poUser);
  103. poUserService.checkUserDataScope(poUser.getUserId());
  104. if (UserConstants.NOT_UNIQUE.equals(poUserService.checkUserNameUnique(poUser)))
  105. {
  106. return error("修改用户'" + poUser.getUserName() + "'失败,登录账号已存在");
  107. }
  108. else if (StringUtils.isNotEmpty(poUser.getPhonenumber())
  109. && UserConstants.NOT_UNIQUE.equals(poUserService.checkPhoneUnique(poUser)))
  110. {
  111. return error("修改用户'" + poUser.getUserName() + "'失败,手机号码已存在");
  112. }
  113. else if (StringUtils.isNotEmpty(poUser.getEmail())
  114. && UserConstants.NOT_UNIQUE.equals(poUserService.checkEmailUnique(poUser)))
  115. {
  116. return error("修改用户'" + poUser.getUserName() + "'失败,邮箱账号已存在");
  117. }
  118. return toAjax(poUserService.updatePoUser(poUser));
  119. }
  120. /**
  121. * 删除用户
  122. */
  123. @PreAuthorize("@ss.hasPermi('system:user:remove')")
  124. @Log(title = "删除权限用户", businessType = BusinessType.DELETE)
  125. @DeleteMapping("/{userIds}")
  126. public AjaxResult remove(@PathVariable Long[] userIds)
  127. {
  128. if (ArrayUtils.contains(userIds, getUserId()))
  129. {
  130. return error("当前用户不能删除");
  131. }
  132. return toAjax(poUserService.deletePoUserByUserIds(userIds));
  133. }
  134. /**
  135. * 重置密码
  136. */
  137. @PreAuthorize("@ss.hasPermi('system:user:resetPwd')")
  138. @Log(title = "用户管理", businessType = BusinessType.UPDATE)
  139. @PutMapping("/resetPwd")
  140. public AjaxResult resetPwd(@RequestBody PoUser poUser)
  141. {
  142. //检验是否有操作权限
  143. poUserService.checkUserAllowed(poUser);
  144. //检验是否有访问数据权限
  145. poUserService.checkUserDataScope(poUser.getUserId());
  146. poUser.setPassword(SecurityUtils.encryptPassword(poUser.getPassword()));
  147. poUser.setUpdateBy(getUsername());
  148. return toAjax(poUserService.resetPwd(poUser));
  149. }
  150. /**
  151. * 状态修改
  152. */
  153. @PreAuthorize("@ss.hasPermi('system:user:edit')")
  154. @Log(title = "用户管理", businessType = BusinessType.UPDATE)
  155. @PutMapping("/changeStatus")
  156. public AjaxResult changeStatus(@RequestBody PoUser poUser)
  157. {
  158. poUserService.checkUserAllowed(poUser);
  159. poUserService.checkUserDataScope(poUser.getUserId());
  160. poUser.setUpdateBy(getUsername());
  161. return toAjax(poUserService.updateUserStatus(poUser));
  162. }
  163. }