SysDeptController.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package com.jiayue.center.controller;
  2. import cn.hutool.core.util.NumberUtil;
  3. import com.jiayue.center.annotation.OperateLog;
  4. import com.jiayue.center.annotation.PreventReplay;
  5. import com.jiayue.center.backenum.AuditType;
  6. import com.jiayue.center.backenum.BusinessType;
  7. import com.jiayue.center.constant.CustomException;
  8. import com.jiayue.center.constant.UserConstants;
  9. import com.jiayue.center.entity.SysDept;
  10. import com.jiayue.center.service.SysDeptService;
  11. import com.jiayue.center.util.ResponseVO;
  12. import com.jiayue.center.util.RyStringUtils;
  13. import com.jiayue.center.util.SecurityContextUtil;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.security.access.prepost.PreAuthorize;
  18. import org.springframework.web.bind.annotation.*;
  19. import java.util.List;
  20. /**
  21. * 部门接口
  22. *
  23. * @author xsl
  24. * @since 2023/03/21
  25. */
  26. @RestController
  27. @RequestMapping("/sysDeptController")
  28. @Slf4j
  29. public class SysDeptController {
  30. @Autowired
  31. private SysDeptService sysDeptService;
  32. /**
  33. * 获取菜单列表
  34. */
  35. @GetMapping("/list")
  36. @PreAuthorize("@ss.hasPermi('system:dept:list')")
  37. @OperateLog(title = "部门管理", businessType = BusinessType.QUERY, auditType = AuditType.SYS,operdesc = "查询部门")
  38. @PreventReplay
  39. public ResponseVO list(SysDept dept) throws CustomException {
  40. try {
  41. if (StringUtils.isNotEmpty(dept.getDeptName())) {
  42. if (dept.getDeptName().length() > 50) {
  43. return ResponseVO.fail("部门名长度不能超过50个字符!");
  44. }
  45. }
  46. List<SysDept> deptList = sysDeptService.selectDeptList(dept);
  47. return ResponseVO.success(deptList);
  48. } catch (Exception e) {
  49. throw new CustomException("获取部门列表异常", e);
  50. }
  51. }
  52. /**
  53. * 新增菜单
  54. */
  55. @PostMapping(value = "/addDept")
  56. @OperateLog(title = "部门管理", businessType = BusinessType.INSERT, auditType = AuditType.SYS,operdesc = "新增部门")
  57. @PreAuthorize("@ss.hasPermi('system:dept:add')")
  58. @PreventReplay
  59. public ResponseVO addDept(@RequestBody SysDept dept) throws CustomException {
  60. try {
  61. if (RyStringUtils.isEmpty(dept.getDeptName()) || RyStringUtils.isEmpty(dept.getDeptName().trim())) {
  62. return ResponseVO.fail("部门名称不能为空!");
  63. } else {
  64. String deptName = dept.getDeptName().trim();
  65. if (deptName.length() > 50) {
  66. return ResponseVO.fail("部门名长度不能超过50个字符!");
  67. }
  68. }
  69. if (dept.getOrderNum() == null) {
  70. return ResponseVO.fail("排序不能为空!");
  71. } else if (!NumberUtil.isInteger(dept.getOrderNum() + "")) {
  72. return ResponseVO.fail("排序不是整型数值!");
  73. }
  74. if (UserConstants.NOT_UNIQUE.equals(sysDeptService.checkDeptNameUnique(dept))) {
  75. return ResponseVO.fail(dept.getDeptName() + "'失败,部门名称已存在!");
  76. }
  77. dept.setCreateBy(SecurityContextUtil.getSysUser().getUsername());
  78. int bo = sysDeptService.insertDept(dept);
  79. if (bo == 1) {
  80. return ResponseVO.success("添加部门成功");
  81. } else {
  82. log.error("添加部门失败");
  83. return ResponseVO.fail("添加部门失败");
  84. }
  85. } catch (Exception e) {
  86. throw new CustomException("添加部门异常", e);
  87. }
  88. }
  89. /**
  90. * 更新系统参数
  91. *
  92. * @param dept 参数
  93. * @return 执行结果
  94. */
  95. @PostMapping(value = "/updateDept")
  96. @OperateLog(title = "部门管理", businessType = BusinessType.UPDATE, auditType = AuditType.SYS,operdesc = "修改部门")
  97. @PreAuthorize("@ss.hasPermi('system:dept:edit')")
  98. @PreventReplay
  99. public ResponseVO updateDept(@RequestBody SysDept dept) throws CustomException {
  100. try {
  101. if (dept.getDeptId() == null) {
  102. return ResponseVO.fail("主键为空不能修改!");
  103. }
  104. String deptName = dept.getDeptName().trim();
  105. if (RyStringUtils.isEmpty(deptName)) {
  106. return ResponseVO.fail("部门名称不能为空!");
  107. } else if (deptName.length() > 50) {
  108. return ResponseVO.fail("部门名长度不能超过50个字符!");
  109. }
  110. if (dept.getOrderNum() == null) {
  111. return ResponseVO.fail("排序不能为空!");
  112. } else if (!NumberUtil.isInteger(dept.getOrderNum() + "")) {
  113. return ResponseVO.fail("排序不是整型数值!");
  114. }
  115. if (sysDeptService.selectDeptById(dept.getDeptId()) == null) {
  116. return ResponseVO.fail("非法访问不能修改!");
  117. }
  118. if (UserConstants.NOT_UNIQUE.equals(sysDeptService.checkDeptNameUnique(dept))) {
  119. return ResponseVO.fail("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在!");
  120. }
  121. int bo = sysDeptService.updateDept(dept);
  122. if (bo == 1) {
  123. return ResponseVO.success("修改部门成功");
  124. } else {
  125. log.error("修改部门失败");
  126. return ResponseVO.fail("修改部门失败");
  127. }
  128. } catch (Exception e) {
  129. throw new CustomException("修改部门异常", e);
  130. }
  131. }
  132. /**
  133. * 删除菜单信息
  134. */
  135. @PostMapping(value = "delDept")
  136. @OperateLog(title = "部门管理", businessType = BusinessType.DELETE, auditType = AuditType.SYS,operdesc = "删除部门")
  137. @PreAuthorize("@ss.hasPermi('system:dept:remove')")
  138. @PreventReplay
  139. public ResponseVO delDept(String deptId) throws CustomException {
  140. try {
  141. if (StringUtils.isEmpty(deptId)) {
  142. return ResponseVO.fail("删除部门的id不能为空!");
  143. }
  144. if (sysDeptService.hasChildByDeptId(Long.parseLong(deptId))) {
  145. return ResponseVO.fail("存在子部门,不允许删除");
  146. }
  147. int bo = sysDeptService.deleteDeptById(Long.parseLong(deptId));
  148. if (bo == 1) {
  149. return ResponseVO.success("删除部门成功");
  150. } else {
  151. log.error("删除部门失败");
  152. return ResponseVO.fail("删除部门失败");
  153. }
  154. } catch (Exception e) {
  155. throw new CustomException("删除部门异常", e);
  156. }
  157. }
  158. /**
  159. * 根据菜单编号获取详细信息
  160. */
  161. @GetMapping(value = "/{getDetailInfo}")
  162. @OperateLog(title = "部门管理", businessType = BusinessType.QUERY, auditType = AuditType.SYS,operdesc = "获取部门详细信息")
  163. @PreventReplay
  164. public ResponseVO getDetailInfo(String deptId) throws CustomException {
  165. try {
  166. Long menuid;
  167. if (StringUtils.isNotEmpty(deptId)) {
  168. if (NumberUtil.isLong(deptId)){
  169. menuid = Long.parseLong(deptId);
  170. }
  171. else{
  172. return ResponseVO.fail("部门id不是类型不对!");
  173. }
  174. }
  175. else{
  176. return ResponseVO.fail("部门id不能为空!");
  177. }
  178. SysDept sysDept = sysDeptService.selectDeptById(menuid);
  179. return ResponseVO.success(sysDept);
  180. } catch (Exception e) {
  181. throw new CustomException("获取部门明细异常", e);
  182. }
  183. }
  184. }