SysDeptServiceImpl.java 17 KB

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