|
@@ -1,22 +1,28 @@
|
|
|
package com.template.controller;
|
|
package com.template.controller;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
import com.template.api.SmartDepartmentControllerAPI;
|
|
import com.template.api.SmartDepartmentControllerAPI;
|
|
|
import com.template.common.utils.paramUtils;
|
|
import com.template.common.utils.paramUtils;
|
|
|
import com.template.model.pojo.SmartDepartment;
|
|
import com.template.model.pojo.SmartDepartment;
|
|
|
-import com.template.model.pojo.SmartDepartment;
|
|
|
|
|
|
|
+import com.template.model.request.insertDepartmentRequest;
|
|
|
|
|
+import com.template.model.request.updateDepartmentRequest;
|
|
|
import com.template.model.result.CommonResult;
|
|
import com.template.model.result.CommonResult;
|
|
|
import com.template.model.result.PageUtils;
|
|
import com.template.model.result.PageUtils;
|
|
|
|
|
+import com.template.model.vo.DepartmentTreeVo;
|
|
|
import com.template.services.SmartDepartmentService;
|
|
import com.template.services.SmartDepartmentService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.validation.BindingResult;
|
|
import org.springframework.validation.BindingResult;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* <p>
|
|
* <p>
|
|
|
- * 前端控制器
|
|
|
|
|
|
|
+ * 前端控制器
|
|
|
* </p>
|
|
* </p>
|
|
|
*
|
|
*
|
|
|
* @author ceshi
|
|
* @author ceshi
|
|
@@ -30,44 +36,68 @@ public class SmartDepartmentController implements SmartDepartmentControllerAPI {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 新增部门
|
|
* 新增部门
|
|
|
- * @param smartApply 部门数据
|
|
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param smartDepartment 部门数据
|
|
|
* @param bindingResult
|
|
* @param bindingResult
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
- public CommonResult insertSmartDepartment(SmartDepartment smartApply, BindingResult bindingResult) {
|
|
|
|
|
|
|
+ public CommonResult insertSmartDepartment(insertDepartmentRequest smartDepartment, BindingResult bindingResult) {
|
|
|
if (bindingResult.hasErrors()) {
|
|
if (bindingResult.hasErrors()) {
|
|
|
String st = paramUtils.getParamError(bindingResult);
|
|
String st = paramUtils.getParamError(bindingResult);
|
|
|
return CommonResult.fail(st);
|
|
return CommonResult.fail(st);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- int result = smartDepartmentService.insertSmartDepartment(smartApply);
|
|
|
|
|
|
|
+ //新增的时候判断是否存在重复部门名称数据
|
|
|
|
|
+ int exist = smartDepartmentService.existDataByName(smartDepartment.getName());
|
|
|
|
|
+ if (exist > 0) {
|
|
|
|
|
+
|
|
|
|
|
+ return CommonResult.fail(smartDepartment.getName() + "部门名称数据已存在,请勿重复添加!");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ SmartDepartment sd = new SmartDepartment();
|
|
|
|
|
+ sd.setParentId(smartDepartment.getParentId());
|
|
|
|
|
+ sd.setName(smartDepartment.getName());
|
|
|
|
|
+
|
|
|
|
|
+ int result = smartDepartmentService.insertSmartDepartment(sd);
|
|
|
|
|
|
|
|
return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
|
|
return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 更新部门
|
|
* 更新部门
|
|
|
- * @param sa 部门数据
|
|
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param udr 部门数据
|
|
|
* @param bindingResult
|
|
* @param bindingResult
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
- public CommonResult updateSmartDepartmentById(SmartDepartment sa, BindingResult bindingResult) {
|
|
|
|
|
|
|
+ public CommonResult updateSmartDepartmentById(updateDepartmentRequest udr, BindingResult bindingResult) {
|
|
|
if (bindingResult.hasErrors()) {
|
|
if (bindingResult.hasErrors()) {
|
|
|
String st = paramUtils.getParamError(bindingResult);
|
|
String st = paramUtils.getParamError(bindingResult);
|
|
|
return CommonResult.fail(st);
|
|
return CommonResult.fail(st);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- int result = smartDepartmentService.updateSmartDepartment(sa);
|
|
|
|
|
|
|
+ SmartDepartment exist = smartDepartmentService.getSmartByName(udr.getName());
|
|
|
|
|
+ if (exist != null && !exist.getId().equals(udr.getId())) {
|
|
|
|
|
+ return CommonResult.fail(udr.getName() + "部门名称数据已存在,请勿重复添加!");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ SmartDepartment sd = new SmartDepartment();
|
|
|
|
|
+ sd.setId(udr.getId());
|
|
|
|
|
+ sd.setParentId(udr.getParentId());
|
|
|
|
|
+ sd.setName(udr.getName());
|
|
|
|
|
+
|
|
|
|
|
+ int result = smartDepartmentService.updateSmartDepartment(sd);
|
|
|
return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
|
|
return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 部门分页数据查询
|
|
* 部门分页数据查询
|
|
|
|
|
+ *
|
|
|
* @param currentPage 当前页数
|
|
* @param currentPage 当前页数
|
|
|
- * @param pageCount 一页数据条数
|
|
|
|
|
- * @param name 查询名称
|
|
|
|
|
|
|
+ * @param pageCount 一页数据条数
|
|
|
|
|
+ * @param name 查询名称
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
@@ -83,7 +113,7 @@ public class SmartDepartmentController implements SmartDepartmentControllerAPI {
|
|
|
|
|
|
|
|
SmartDepartment data = smartDepartmentService.getSmartById(id);
|
|
SmartDepartment data = smartDepartmentService.getSmartById(id);
|
|
|
|
|
|
|
|
- if(data == null){
|
|
|
|
|
|
|
+ if (data == null) {
|
|
|
return CommonResult.fail("当前数据不存在,删除失败!");
|
|
return CommonResult.fail("当前数据不存在,删除失败!");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -91,5 +121,74 @@ public class SmartDepartmentController implements SmartDepartmentControllerAPI {
|
|
|
|
|
|
|
|
return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
|
|
return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据部门名称查询部门树形图结构数据
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param name 部门名称
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CommonResult queryDepartmentTree(String name) {
|
|
|
|
|
+ List<SmartDepartment> result = smartDepartmentService.getSmartsByName(name);
|
|
|
|
|
+
|
|
|
|
|
+ List<SmartDepartment> zeroResult = result.stream().filter(e -> e.getParentId().intValue() == 0).collect(Collectors.toList());
|
|
|
|
|
+ List<DepartmentTreeVo> departments = new ArrayList<>();
|
|
|
|
|
+ if (ObjectUtils.isEmpty(name)) {
|
|
|
|
|
+ for (SmartDepartment data : zeroResult) {
|
|
|
|
|
+ DepartmentTreeVo newData = new DepartmentTreeVo().builder()
|
|
|
|
|
+ .id(data.getId())
|
|
|
|
|
+ .name(data.getName())
|
|
|
|
|
+ .parentId(data.getParentId())
|
|
|
|
|
+ .build();
|
|
|
|
|
+
|
|
|
|
|
+ List<DepartmentTreeVo> departmentTrees = QueryDepartmentTreeRecords(newData.getId(), result);
|
|
|
|
|
+ newData.setChildren(departmentTrees);
|
|
|
|
|
+ departments.add(newData);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ for (SmartDepartment data : result) {
|
|
|
|
|
+ DepartmentTreeVo newData = new DepartmentTreeVo().builder()
|
|
|
|
|
+ .id(data.getId())
|
|
|
|
|
+ .name(data.getName())
|
|
|
|
|
+ .parentId(data.getParentId())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ departments.add(newData);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return CommonResult.ok(departments);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据父级ID获取树形数据
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param parentID 父级ID
|
|
|
|
|
+ * @param lists 数据集合
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ private List<DepartmentTreeVo> QueryDepartmentTreeRecords(Integer parentID, List<SmartDepartment> lists) {
|
|
|
|
|
+ List<DepartmentTreeVo> newTrees = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ List<SmartDepartment> datas = lists.stream().filter(e -> e.getParentId().equals(parentID)).collect(Collectors.toList());
|
|
|
|
|
+
|
|
|
|
|
+ for (SmartDepartment data : datas) {
|
|
|
|
|
+ DepartmentTreeVo item = DepartmentTreeVo.builder()
|
|
|
|
|
+ .id(data.getId())
|
|
|
|
|
+ .parentId(parentID)
|
|
|
|
|
+ .name(data.getName())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ List<DepartmentTreeVo> news = QueryDepartmentTreeRecords(item.getId(), lists);
|
|
|
|
|
+ if (news == null || news.size() == 0) {
|
|
|
|
|
+ newTrees.add(item);
|
|
|
|
|
+ continue;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ item.setChildren(news);
|
|
|
|
|
+ newTrees.add(item);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return newTrees;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|