SysDeptServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package com.ydtech.modules.admin.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.ydtech.core.page.PageRequest;
  8. import com.ydtech.core.page.PageResult;
  9. import com.ydtech.exception.SystemException;
  10. import com.ydtech.modules.admin.dao.SysDeptMapper;
  11. import com.ydtech.modules.admin.model.SysDept;
  12. import com.ydtech.modules.admin.model.vo.SysDeptVo;
  13. import com.ydtech.modules.admin.model.vo.TreeSysDeptVO;
  14. import com.ydtech.modules.admin.service.SysDeptService;
  15. import com.ydtech.modules.protocol.utils.AssertionUtils;
  16. import com.ydtech.utils.StringUtils;
  17. import org.springframework.beans.BeanUtils;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.util.ObjectUtils;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.stream.Collectors;
  24. @Service
  25. public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
  26. implements SysDeptService {
  27. @Override
  28. public List<SysDept> findTree(String deptId,String managementSource) {
  29. if(null == deptId || "".equals(deptId) || "undefined".equals(deptId)){
  30. deptId = "9";
  31. }
  32. SysDept sysDept = getById(deptId);
  33. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  34. lqw.eq(SysDept::getDelFlag, 0);
  35. lqw.likeRight(SysDept::getId, deptId);
  36. lqw.orderByDesc(SysDept::getOrderNum);
  37. List<SysDept> sysDeptList = list(lqw);
  38. //判断机构来源条件 进行遍历
  39. if(null != managementSource && !"undefined".equals(managementSource) && !"".equals(managementSource)){
  40. lqw.eq(SysDept::getManagementSource,managementSource);
  41. //条件
  42. List<SysDept> conditionList = list(lqw);
  43. Map<String, List<SysDept>> conditionMap = conditionList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  44. Map<String, List<SysDept>> map = conditionList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  45. //先查第6层
  46. conditionList.forEach(i ->
  47. i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>()))
  48. );
  49. List<SysDept> result = new ArrayList<>();
  50. //再查前4层
  51. int level = 5;
  52. while(level > 0){
  53. List<SysDept> resultTmp = recursion(sysDeptList, conditionMap);
  54. if(resultTmp.size() == 0){
  55. break;
  56. }else{
  57. result.clear();
  58. result.addAll(resultTmp);
  59. }
  60. conditionMap = result.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  61. level--;
  62. }
  63. sysDeptList = result;
  64. }else{
  65. Map<String, List<SysDept>> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  66. sysDeptList.forEach(i -> i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>())));
  67. sysDeptList = map.get(sysDept.getParentId());
  68. }
  69. return sysDeptList;
  70. }
  71. private List<SysDept> recursion(List<SysDept> sysDeptList, Map<String, List<SysDept>> conditionMap) {
  72. List<SysDept> result = new ArrayList<>();
  73. sysDeptList.forEach(i ->
  74. conditionMap.forEach((key,value) -> {
  75. if (i.getId().equals(key)) {
  76. if(i.getChildren() == null){
  77. i.setChildren(new ArrayList<>());
  78. }
  79. i.getChildren().addAll(value);
  80. result.add(i);
  81. }
  82. })
  83. );
  84. return result;
  85. }
  86. @Override
  87. public List<SysDept> getUserList(String deptId) {
  88. SysDept sysDept = getById(deptId);
  89. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  90. lqw.eq(SysDept::getDelFlag, 0);
  91. lqw.likeRight(SysDept::getId, deptId);
  92. List<SysDept> sysDeptList = list(lqw);
  93. Map<String, List<SysDept>> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  94. return map.get(sysDept.getParentId());
  95. }
  96. @Override
  97. public void addDept(SysDeptVo sysDeptVo) {
  98. SysDept sysDept = new SysDept();
  99. BeanUtils.copyProperties(sysDeptVo, sysDept);
  100. String comlevel = ObjectUtils.isEmpty(sysDept.getComlevel()) ? "" : sysDept.getComlevel();
  101. String parentId = sysDept.getParentId();
  102. String number;
  103. if (comlevel.equals("2")) {
  104. AssertionUtils.isStringEmpty(sysDept.getProvince(), "二级机构份必填");
  105. number = sysDept.getProvince().substring(0, 2);
  106. } else if (comlevel.equals("3")) {
  107. AssertionUtils.isStringEmpty(sysDept.getCity(), "三级机构市必填");
  108. number = sysDept.getCity().substring(0, 4);
  109. } else if (comlevel.equals("4")) {
  110. AssertionUtils.isStringEmpty(sysDept.getArea(), "四级机构区必填");
  111. number = sysDept.getArea().substring(0, 6);
  112. } else {
  113. if (comlevel.equals("5")) {
  114. sysDeptVo.setDeptType("M");
  115. }
  116. String maxId = baseMapper.getMaxId(sysDept.getParentId());
  117. if (!maxId.equals("0")) {
  118. String substring = maxId.substring(sysDept.getParentId().length() + 1);
  119. int i = Integer.parseInt("1" + substring) + 1;
  120. number = String.valueOf(i);
  121. } else {
  122. number = "101";
  123. }
  124. }
  125. String substring = number.substring(number.length() - 2);
  126. String serialNumber;
  127. if ("M,D".contains(sysDeptVo.getDeptType())) {
  128. serialNumber = parentId + sysDeptVo.getDeptType() + substring;
  129. } else {
  130. serialNumber = parentId + substring;
  131. }
  132. sysDept.setId(serialNumber);
  133. SysDept dept = getById(sysDept.getId());
  134. if (!ObjectUtils.isEmpty(dept)) {
  135. throw new SystemException("机构编号为:" + dept.getId() + " " + dept.getName() + "已被占用");
  136. }
  137. SysDept parentDept = baseMapper.selectById(sysDept.getParentId());
  138. if (parentDept.getComlevel().equals("5")){
  139. sysDept.setManagementSource(parentDept.getManagementSource());
  140. }
  141. boolean b = save(sysDept);
  142. AssertionUtils.isSucceed(b, "删除失败");
  143. }
  144. @Override
  145. public void deleteDept(String deptId) {
  146. SysDept sysDept = getById(deptId);
  147. List<SysDept> list = lambdaQuery().eq(SysDept::getParentId, sysDept.getId()).list();
  148. if (list.isEmpty()) {
  149. boolean b = removeById(deptId);
  150. AssertionUtils.isSucceed(b, "删除失败");
  151. } else {
  152. throw new SystemException(sysDept.getName() + "下还有其他隶属机构");
  153. }
  154. }
  155. @Override
  156. public PageResult<SysDept> findSubDepts(PageRequest pageRequest) {
  157. String id = pageRequest.getColumnFilterValue(pageRequest, "id");
  158. String managementSource = pageRequest.getColumnFilterValue(pageRequest, "managementSource");
  159. Page<SysDept> p = PageRequest.buildPageRequest(pageRequest);
  160. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  161. lqw.eq(StringUtils.isNotEmpty(id), SysDept::getParentId, id);
  162. lqw.eq(StringUtils.isNotEmpty(managementSource), SysDept::getManagementSource, managementSource);
  163. IPage<SysDept> pResult = page(p, lqw);
  164. //传入查询条件
  165. return PageResult.buildPageResult(pResult);
  166. }
  167. //查找自已有权限的部门树
  168. public List<TreeSysDeptVO> selectListTreeSysDeptVO(String id) {
  169. if (id == null) {
  170. id = "";
  171. }
  172. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  173. lqw.like(StringUtils.isNotEmpty(id), SysDept::getId, id);
  174. List<SysDept> deptList = list(lqw);
  175. List<TreeSysDeptVO> recordList = new ArrayList<>();
  176. deptList.forEach(a -> {
  177. TreeSysDeptVO model = new TreeSysDeptVO();
  178. model.setId(a.getId());
  179. model.setName(a.getName());
  180. model.setParentId(a.getParentId());
  181. model.setDeptCode(a.getDeptCode());
  182. model.setProvince(a.getProvince());
  183. model.setCity(a.getCity());
  184. model.setArea(a.getArea());
  185. model.setComlevel(a.getComlevel());
  186. recordList.add(model);
  187. });
  188. return recordList;
  189. }
  190. public String findMaxDeptId(String deptId) {
  191. if (deptId == null) return "";
  192. String result = "00";
  193. QueryWrapper<SysDept> qw = new QueryWrapper<>();
  194. qw.likeRight(StringUtils.isNotEmpty(deptId), "id", deptId + "D%");
  195. qw.select("IFNULL(MAX(id),0) maxid");
  196. Map<String, Object> m = getMap(qw);
  197. String maxId = m.get("maxid").toString();
  198. if (!maxId.equalsIgnoreCase("0") && !maxId.equalsIgnoreCase(deptId)) {
  199. result = maxId.substring(deptId.length() + 1);
  200. }
  201. return result;
  202. }
  203. public String findMaxHouseId(String deptId) {
  204. if (deptId == null) {
  205. return "";
  206. }
  207. String result = "00";
  208. QueryWrapper<SysDept> qw = new QueryWrapper<>();
  209. qw.like(StringUtils.isNotEmpty(deptId), "id", deptId + "M%");
  210. qw.select("IFNULL(MAX(id),0) maxid");
  211. Map<String, Object> m = getMap(qw);
  212. String maxId = m.get("maxid").toString();
  213. if (!maxId.equalsIgnoreCase("0") && !maxId.equalsIgnoreCase(deptId)) {
  214. result = maxId.substring(deptId.length() + 1);
  215. }
  216. return result;
  217. }
  218. //
  219. @Override
  220. public List<TreeSysDeptVO> selectListWhere(String where, Object[] paramValues) {
  221. String sql = "select t1.id, t1.name, t1.parent_id, t1.dept_code, t1.province, t1.city, t1.area, t1.comlevel from sys_dept t1 " +
  222. "where del_flag='0' " + where;
  223. QueryWrapper<SysDept> qw = new QueryWrapper<>();
  224. qw.select(sql);
  225. List<SysDept> sysDeptList = list(qw);
  226. List<TreeSysDeptVO> recordList = new ArrayList<TreeSysDeptVO>();
  227. sysDeptList.forEach(a -> {
  228. TreeSysDeptVO model = new TreeSysDeptVO();
  229. model.setId(a.getId());
  230. model.setName(a.getName());
  231. model.setParentId(a.getParentId());
  232. model.setDeptCode(a.getDeptCode());
  233. model.setProvince(a.getProvince());
  234. model.setCity(a.getCity());
  235. model.setArea(a.getArea());
  236. model.setComlevel(a.getComlevel());
  237. recordList.add(model);
  238. });
  239. return recordList;
  240. }
  241. /**
  242. * 获取机构树(不包含团队 dept_type=C)
  243. */
  244. @Override
  245. public List<SysDept> findCompanyTree(String deptId) {
  246. SysDept sysDept = getById(deptId);
  247. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  248. lqw.eq(SysDept::getDelFlag, 0);
  249. lqw.likeRight(SysDept::getId, deptId);
  250. lqw.and((wrapper)->{wrapper.eq(SysDept::getDeptType,'C').or().eq(SysDept::getId,'9');});
  251. List<SysDept> sysDeptList = list(lqw);
  252. Map<String, List<SysDept>> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  253. sysDeptList.forEach(i -> i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>())));
  254. return map.get(sysDept.getParentId());
  255. }
  256. /**
  257. * 根据传入机构,查询归属所有团队
  258. */
  259. @Override
  260. public List<SysDept> findTeamList(String deptId) {
  261. SysDept sysDept = getById(deptId);
  262. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  263. lqw.eq(SysDept::getDelFlag, 0);
  264. lqw.likeRight(SysDept::getId, deptId);
  265. lqw.eq(SysDept::getDeptType,'D');
  266. lqw.apply(" exists (SELECT 1 FROM `sys_dept` b WHERE sys_dept.parent_id = b.`id` AND b.`comlevel`='5') ");
  267. List<SysDept> sysDeptList = list(lqw);
  268. return sysDeptList;
  269. }
  270. }