SysDeptServiceImpl.java 17 KB

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