SysDeptServiceImpl.java 16 KB

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