SysDeptServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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.HttpResult;
  8. import com.ydtech.core.page.PageRequest;
  9. import com.ydtech.core.page.PageResult;
  10. import com.ydtech.exception.SystemException;
  11. import com.ydtech.modules.admin.dao.SysDeptMapper;
  12. import com.ydtech.modules.admin.model.SysDept;
  13. import com.ydtech.modules.admin.model.vo.SysDeptVo;
  14. import com.ydtech.modules.admin.model.vo.TreeSysDeptVO;
  15. import com.ydtech.modules.admin.service.SysDeptService;
  16. import com.ydtech.modules.protocol.utils.AssertionUtils;
  17. import com.ydtech.utils.StringUtils;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import org.springframework.util.ObjectUtils;
  22. import java.util.ArrayList;
  23. import java.util.Comparator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.stream.Collectors;
  27. @Service
  28. public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
  29. implements SysDeptService {
  30. @Override
  31. public List<SysDept> findTree(String deptId,String managementSource) {
  32. if(null == deptId || "".equals(deptId) || "undefined".equals(deptId)){
  33. deptId = "9";
  34. }
  35. SysDept sysDept = getById(deptId);
  36. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  37. lqw.eq(SysDept::getDelFlag, 0);
  38. lqw.likeRight(SysDept::getId, deptId);
  39. lqw.orderByAsc(SysDept::getOrderNum);
  40. List<SysDept> sysDeptList = list(lqw);
  41. //判断机构来源条件 进行遍历
  42. if (null != managementSource && !"undefined".equals(managementSource) && !"".equals(managementSource)) {
  43. lqw.eq(SysDept::getManagementSource, managementSource);
  44. //条件
  45. List<SysDept> conditionList = list(lqw);
  46. Map<String, List<SysDept>> conditionMap = conditionList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  47. Map<String, List<SysDept>> map = conditionList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  48. //先查第6层
  49. conditionList.forEach(i ->
  50. i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>()))
  51. );
  52. List<SysDept> result = new ArrayList<>();
  53. //再查前4层
  54. int level = 5;
  55. while (level > 0) {
  56. List<SysDept> resultTmp = recursion(sysDeptList, conditionMap);
  57. if (resultTmp.size() == 0) {
  58. break;
  59. } else {
  60. result.clear();
  61. result.addAll(resultTmp);
  62. }
  63. conditionMap = result.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  64. level--;
  65. }
  66. sysDeptList = result;
  67. } else {
  68. Map<String, List<SysDept>> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  69. sysDeptList.forEach(i -> i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>())));
  70. sysDeptList = map.get(sysDept.getParentId());
  71. }
  72. return sysDeptList;
  73. }
  74. private List<SysDept> recursion(List<SysDept> sysDeptList, Map<String, List<SysDept>> conditionMap) {
  75. List<SysDept> result = new ArrayList<>();
  76. sysDeptList.forEach(i ->
  77. conditionMap.forEach((key, value) -> {
  78. if (i.getId().equals(key)) {
  79. if (i.getChildren() == null) {
  80. i.setChildren(new ArrayList<>());
  81. }
  82. i.getChildren().addAll(value);
  83. result.add(i);
  84. }
  85. })
  86. );
  87. result.sort(Comparator.comparing(SysDept::getOrderNum));
  88. return result;
  89. }
  90. @Override
  91. public List<SysDept> getUserList(String deptId) {
  92. SysDept sysDept = getById(deptId);
  93. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  94. lqw.eq(SysDept::getDelFlag, 0);
  95. lqw.likeRight(SysDept::getId, deptId);
  96. List<SysDept> sysDeptList = list(lqw);
  97. Map<String, List<SysDept>> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  98. return map.get(sysDept.getParentId());
  99. }
  100. @Override
  101. @Transactional
  102. public void addDept(SysDeptVo sysDeptVo) {
  103. SysDept sysDept = new SysDept();
  104. BeanUtils.copyProperties(sysDeptVo, sysDept);
  105. String comlevel = ObjectUtils.isEmpty(sysDept.getComlevel()) ? "" : sysDept.getComlevel();
  106. String parentId = sysDept.getParentId();
  107. String number;
  108. if (comlevel.equals("2")) {
  109. AssertionUtils.isStringEmpty(sysDept.getProvince(), "二级机构份必填");
  110. number = sysDept.getProvince().substring(0, 2);
  111. } else if (comlevel.equals("3")) {
  112. AssertionUtils.isStringEmpty(sysDept.getCity(), "三级机构市必填");
  113. number = sysDept.getCity().substring(0, 4);
  114. } else if (comlevel.equals("4")) {
  115. AssertionUtils.isStringEmpty(sysDept.getArea(), "四级机构区必填");
  116. number = sysDept.getArea().substring(0, 6);
  117. } else {
  118. if (comlevel.equals("5")) {
  119. sysDeptVo.setDeptType("M");
  120. }
  121. String maxId = baseMapper.getMaxId(sysDept.getParentId());
  122. if (!maxId.equals("0")) {
  123. String substring = maxId.substring(sysDept.getParentId().length() + 1);
  124. int i = Integer.parseInt("1" + substring) + 1;
  125. number = String.valueOf(i);
  126. } else {
  127. number = "101";
  128. }
  129. }
  130. String substring = number.substring(number.length() - 2);
  131. String serialNumber;
  132. if ("M,D".contains(sysDeptVo.getDeptType())) {
  133. serialNumber = parentId + sysDeptVo.getDeptType() + substring;
  134. } else {
  135. serialNumber = parentId + substring;
  136. }
  137. sysDept.setId(serialNumber);
  138. SysDept dept = getById(sysDept.getId());
  139. if (!ObjectUtils.isEmpty(dept)) {
  140. throw new SystemException("机构编号为:" + dept.getId() + " " + dept.getName() + "已被占用");
  141. }
  142. SysDept parentDept = baseMapper.selectById(sysDept.getParentId());
  143. if (parentDept.getComlevel().equals("5")){
  144. sysDept.setManagementSource(parentDept.getManagementSource());
  145. }
  146. // 查询当前部门序号是否存在
  147. LambdaQueryWrapper<SysDept> qw = new LambdaQueryWrapper<>();
  148. qw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
  149. qw.eq(SysDept::getParentId, sysDept.getParentId()); // 父级部门ID相同
  150. qw.eq(SysDept::getOrderNum, sysDept.getOrderNum()); // 排序号相同
  151. boolean exists = baseMapper.exists(qw);
  152. if (exists) {
  153. // 如果存在,查询该父级部门下排序号大于当前部门的部门列表
  154. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  155. lqw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
  156. lqw.eq(SysDept::getParentId, sysDept.getParentId()); // 父级部门ID相同
  157. lqw.ge(SysDept::getOrderNum, sysDept.getOrderNum()); // 排序号大于等于当前部门
  158. List<SysDept> sysDeptList = list(lqw);
  159. // 更新这些部门的排序号
  160. sysDeptList.forEach(i -> {
  161. i.setOrderNum(i.getOrderNum() + 1);
  162. updateById(i);
  163. });
  164. }
  165. boolean b = save(sysDept);
  166. AssertionUtils.isSucceed(b, "删除失败");
  167. }
  168. @Override
  169. @Transactional
  170. public HttpResult updateDept(SysDept record) {
  171. // 查询修改的序号是否存在
  172. LambdaQueryWrapper<SysDept> qw = new LambdaQueryWrapper<>();
  173. qw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
  174. qw.eq(SysDept::getParentId, record.getParentId()); // 父级部门ID相同
  175. qw.eq(SysDept::getOrderNum, record.getOrderNum()); // 排序号相同
  176. boolean exists = baseMapper.exists(qw);
  177. //不存在则直接修改
  178. if (!exists) {
  179. baseMapper.updateById(record);
  180. return HttpResult.ok();
  181. }
  182. //查询机构
  183. SysDept sysDept = getById(record.getId());
  184. //原本的序号
  185. Integer orderNum = sysDept.getOrderNum();
  186. //本次修改的序号
  187. Integer newOrderNum = record.getOrderNum();
  188. // 向上换位
  189. if (newOrderNum < orderNum) {
  190. // 查询该父级部门下排序号大于当前部门的部门列表
  191. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  192. lqw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
  193. lqw.eq(SysDept::getParentId, record.getParentId()); // 父级部门ID相同
  194. lqw.ge(SysDept::getOrderNum, record.getOrderNum()); // 排序号大于等于当前部门
  195. List<SysDept> sysDeptList = list(lqw);
  196. sysDeptList.sort(Comparator.comparing(SysDept::getOrderNum));
  197. for (SysDept dept : sysDeptList) {
  198. dept.setOrderNum(dept.getOrderNum() + 1);
  199. updateById(dept);
  200. if (dept.getOrderNum().equals(orderNum)) {
  201. break;
  202. }
  203. }
  204. baseMapper.updateById(record);
  205. }
  206. //向下换位
  207. if (newOrderNum > orderNum) {
  208. // 查询该父级部门下排序号小于当前部门的部门列表
  209. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  210. lqw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
  211. lqw.eq(SysDept::getParentId, record.getParentId()); // 父级部门ID相同
  212. lqw.le(SysDept::getOrderNum, record.getOrderNum()); // 排序号大于等于当前部门
  213. List<SysDept> sysDeptList = list(lqw);
  214. sysDeptList.sort(Comparator.comparing(SysDept::getOrderNum).reversed());
  215. for (SysDept dept : sysDeptList) {
  216. dept.setOrderNum(dept.getOrderNum() - 1);
  217. updateById(dept);
  218. if (dept.getOrderNum().equals(orderNum)) {
  219. break;
  220. }
  221. }
  222. baseMapper.updateById(record);
  223. }
  224. return HttpResult.ok();
  225. }
  226. @Override
  227. public void deleteDept(String deptId) {
  228. SysDept sysDept = getById(deptId);
  229. List<SysDept> list = lambdaQuery().eq(SysDept::getParentId, sysDept.getId()).list();
  230. if (list.isEmpty()) {
  231. boolean b = removeById(deptId);
  232. AssertionUtils.isSucceed(b, "删除失败");
  233. } else {
  234. throw new SystemException(sysDept.getName() + "下还有其他隶属机构");
  235. }
  236. }
  237. @Override
  238. public PageResult<SysDept> findSubDepts(PageRequest pageRequest) {
  239. String id = pageRequest.getColumnFilterValue(pageRequest, "id");
  240. String managementSource = pageRequest.getColumnFilterValue(pageRequest, "managementSource");
  241. Page<SysDept> p = PageRequest.buildPageRequest(pageRequest);
  242. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  243. lqw.eq(StringUtils.isNotEmpty(id), SysDept::getParentId, id);
  244. lqw.eq(StringUtils.isNotEmpty(managementSource), SysDept::getManagementSource, managementSource);
  245. IPage<SysDept> pResult = page(p, lqw);
  246. //传入查询条件
  247. return PageResult.buildPageResult(pResult);
  248. }
  249. public String findMaxDeptId(String deptId) {
  250. if (deptId == null) return "";
  251. String result = "00";
  252. QueryWrapper<SysDept> qw = new QueryWrapper<>();
  253. qw.likeRight(StringUtils.isNotEmpty(deptId), "id", deptId + "D%");
  254. qw.select("IFNULL(MAX(id),0) maxid");
  255. Map<String, Object> m = getMap(qw);
  256. String maxId = m.get("maxid").toString();
  257. if (!maxId.equalsIgnoreCase("0") && !maxId.equalsIgnoreCase(deptId)) {
  258. result = maxId.substring(deptId.length() + 1);
  259. }
  260. return result;
  261. }
  262. public String findMaxHouseId(String deptId) {
  263. if (deptId == null) {
  264. return "";
  265. }
  266. String result = "00";
  267. QueryWrapper<SysDept> qw = new QueryWrapper<>();
  268. qw.like(StringUtils.isNotEmpty(deptId), "id", deptId + "M%");
  269. qw.select("IFNULL(MAX(id),0) maxid");
  270. Map<String, Object> m = getMap(qw);
  271. String maxId = m.get("maxid").toString();
  272. if (!maxId.equalsIgnoreCase("0") && !maxId.equalsIgnoreCase(deptId)) {
  273. result = maxId.substring(deptId.length() + 1);
  274. }
  275. return result;
  276. }
  277. //
  278. @Override
  279. public List<TreeSysDeptVO> selectListWhere(String where, Object[] paramValues) {
  280. 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 " +
  281. "where del_flag='0' " + where;
  282. QueryWrapper<SysDept> qw = new QueryWrapper<>();
  283. qw.select(sql);
  284. List<SysDept> sysDeptList = list(qw);
  285. List<TreeSysDeptVO> recordList = new ArrayList<TreeSysDeptVO>();
  286. sysDeptList.forEach(a -> {
  287. TreeSysDeptVO model = new TreeSysDeptVO();
  288. model.setId(a.getId());
  289. model.setName(a.getName());
  290. model.setParentId(a.getParentId());
  291. model.setDeptCode(a.getDeptCode());
  292. model.setProvince(a.getProvince());
  293. model.setCity(a.getCity());
  294. model.setArea(a.getArea());
  295. model.setComlevel(a.getComlevel());
  296. recordList.add(model);
  297. });
  298. return recordList;
  299. }
  300. /**
  301. * 获取机构树(不包含团队 dept_type=C)
  302. */
  303. @Override
  304. public List<SysDept> findCompanyTree(String deptId) {
  305. SysDept sysDept = getById(deptId);
  306. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  307. lqw.eq(SysDept::getDelFlag, 0);
  308. lqw.likeRight(SysDept::getId, deptId);
  309. lqw.and((wrapper)->{wrapper.eq(SysDept::getDeptType,'C').or().eq(SysDept::getId,'9');});
  310. List<SysDept> sysDeptList = list(lqw);
  311. Map<String, List<SysDept>> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
  312. sysDeptList.forEach(i -> i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>())));
  313. return map.get(sysDept.getParentId());
  314. }
  315. /**
  316. * 根据传入机构,查询归属所有团队
  317. */
  318. @Override
  319. public List<SysDept> findTeamList(String deptId) {
  320. SysDept sysDept = getById(deptId);
  321. LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
  322. lqw.eq(SysDept::getDelFlag, 0);
  323. lqw.likeRight(SysDept::getId, deptId);
  324. lqw.eq(SysDept::getDeptType,'D');
  325. lqw.apply(" exists (SELECT 1 FROM `sys_dept` b WHERE sys_dept.parent_id = b.`id` AND b.`comlevel`='5') ");
  326. List<SysDept> sysDeptList = list(lqw);
  327. return sysDeptList;
  328. }
  329. }