buzhanyi 2 лет назад
Родитель
Сommit
c6e79c4f49

+ 28 - 20
hazard-admin/src/main/java/com/ozs/web/controller/system/SysDeptController.java

@@ -65,16 +65,8 @@ public class SysDeptController extends BaseController {
     @PreAuthorize("@ss.hasPermi('system:dept:listAll')")
     @GetMapping("/listAll")
     public AjaxResult listAll(SysDept dept) {
-        List<SysDept> depts = deptService.selectDeptListAll(dept);
-        for (SysDept sysDept : depts) {
-            for (SysDept dept1 : depts) {
-                if (dept1.getDeptId().equals(sysDept.getParentId())) {
-                    sysDept.setParentName(dept1.getDeptName());
-                    break;
-                }
-            }
-        }
-        return success(depts);
+        List<SysDept> sysDeptS = forDeptList(dept);
+        return success(sysDeptS);
     }
 
     /**
@@ -91,7 +83,7 @@ public class SysDeptController extends BaseController {
      */
     @PreAuthorize("@ss.hasPermi('system:dept:list')")
     @GetMapping("/list/exclude/{deptId}")
-    public AjaxResult excludeChild(@PathVariable(value = "deptId" , required = false) Long deptId) {
+    public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
         List<SysDept> depts = deptService.selectDeptList(new SysDept());
         depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
         return success(depts);
@@ -113,18 +105,18 @@ public class SysDeptController extends BaseController {
      * 新增部门
      */
     @PreAuthorize("@ss.hasPermi('system:dept:add')")
-    @Log(title = "部门管理" , businessType = BusinessType.INSERT)
+    @Log(title = "部门管理", businessType = BusinessType.INSERT)
     @PostMapping
     public AjaxResult add(@Validated @RequestBody SysDept dept) {
         if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
             return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
         }
-        LambdaQueryWrapper<SysDept> wq=new LambdaQueryWrapper<SysDept>();
+        LambdaQueryWrapper<SysDept> wq = new LambdaQueryWrapper<SysDept>();
         if (ObjectUtils.isNotEmpty(dept.getDeptCode())) {
             wq.eq(SysDept::getDeptCode, dept.getDeptCode());
         }
         SysDept sysDept = deptService.getOne(wq);
-        if (ObjectUtils.isNotEmpty(sysDept)){
+        if (ObjectUtils.isNotEmpty(sysDept)) {
             return error("新增部门'" + dept.getDeptCode() + "'失败,部门编码已存在");
         }
         dept.setCreateBy(getUserId());
@@ -136,7 +128,7 @@ public class SysDeptController extends BaseController {
      * 修改部门
      */
     @PreAuthorize("@ss.hasPermi('system:dept:edit')")
-    @Log(title = "部门管理" , businessType = BusinessType.UPDATE)
+    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
     @PutMapping
     public AjaxResult edit(@Validated @RequestBody SysDept dept) {
         Long deptId = dept.getDeptId();
@@ -148,13 +140,13 @@ public class SysDeptController extends BaseController {
         } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0) {
             return error("该部门包含未停用的子部门!");
         }
-        LambdaQueryWrapper<SysDept> wq=new LambdaQueryWrapper<SysDept>();
+        LambdaQueryWrapper<SysDept> wq = new LambdaQueryWrapper<SysDept>();
         if (ObjectUtils.isNotEmpty(dept.getDeptCode())) {
             wq.eq(SysDept::getDeptCode, dept.getDeptCode());
-            wq.ne(SysDept::getDeptId,dept.getDeptId());
+            wq.ne(SysDept::getDeptId, dept.getDeptId());
         }
         SysDept sysDept = deptService.getOne(wq);
-        if (ObjectUtils.isNotEmpty(sysDept)){
+        if (ObjectUtils.isNotEmpty(sysDept)) {
             return error("新增部门'" + dept.getDeptCode() + "'失败,部门编码已存在");
         }
         dept.setUpdateBy(getUserId());
@@ -165,7 +157,7 @@ public class SysDeptController extends BaseController {
      * 删除部门
      */
     @PreAuthorize("@ss.hasPermi('system:dept:remove')")
-    @Log(title = "部门管理" , businessType = BusinessType.DELETE)
+    @Log(title = "部门管理", businessType = BusinessType.DELETE)
     @DeleteMapping("/{deptId}")
     public AjaxResult remove(@PathVariable Long deptId) {
         if (deptService.hasChildByDeptId(deptId)) {
@@ -183,8 +175,24 @@ public class SysDeptController extends BaseController {
     @PostMapping("/export")
     @ApiOperation("部门导出")
     public void export(HttpServletResponse response, SysDept dept) {
-        List<SysDept> list = deptService.selectDeptListAll(dept);
+        List<SysDept> list = forDeptList(dept);
         ExcelUtil<SysDept> util = new ExcelUtil<SysDept>(SysDept.class);
         util.exportExcel(response, list, "部门数据");
     }
+
+    /**
+     * 遍历列表,给字段赋值
+     */
+    private List<SysDept> forDeptList(SysDept dept) {
+        List<SysDept> list = deptService.selectDeptListAll(dept);
+        for (SysDept sysDept : list) {
+            for (SysDept dept1 : list) {
+                if (dept1.getDeptId().equals(sysDept.getParentId())) {
+                    sysDept.setParentName(dept1.getDeptName());
+                    break;
+                }
+            }
+        }
+        return list;
+    }
 }