SysDeptServiceImpl.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package com.emato.system.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import com.emato.common.annotation.DataScope;
  9. import com.emato.common.constant.UserConstants;
  10. import com.emato.common.core.domain.TreeSelect;
  11. import com.emato.common.core.domain.entity.SysDept;
  12. import com.emato.common.core.domain.entity.SysRole;
  13. import com.emato.common.exception.CustomException;
  14. import com.emato.common.utils.StringUtils;
  15. import com.emato.system.mapper.SysDeptMapper;
  16. import com.emato.system.mapper.SysRoleMapper;
  17. import com.emato.system.service.ISysDeptService;
  18. /**
  19. * 部门管理 服务实现
  20. *
  21. * @author cadmin
  22. */
  23. @Service
  24. public class SysDeptServiceImpl implements ISysDeptService
  25. {
  26. @Autowired
  27. private SysDeptMapper deptMapper;
  28. @Autowired
  29. private SysRoleMapper roleMapper;
  30. /**
  31. * 查询部门管理数据
  32. *
  33. * @param dept 部门信息
  34. * @return 部门信息集合
  35. */
  36. @Override
  37. @DataScope(deptAlias = "d")
  38. public List<SysDept> selectDeptList(SysDept dept)
  39. {
  40. return deptMapper.selectDeptList(dept);
  41. }
  42. /**
  43. * 构建前端所需要树结构
  44. *
  45. * @param depts 部门列表
  46. * @return 树结构列表
  47. */
  48. @Override
  49. public List<SysDept> buildDeptTree(List<SysDept> depts)
  50. {
  51. List<SysDept> returnList = new ArrayList<SysDept>();
  52. List<Long> tempList = new ArrayList<Long>();
  53. for (SysDept dept : depts)
  54. {
  55. tempList.add(dept.getDeptId());
  56. }
  57. for (Iterator<SysDept> iterator = depts.iterator(); iterator.hasNext();)
  58. {
  59. SysDept dept = (SysDept) iterator.next();
  60. // 如果是顶级节点, 遍历该父节点的所有子节点
  61. if (!tempList.contains(dept.getParentId()))
  62. {
  63. recursionFn(depts, dept);
  64. returnList.add(dept);
  65. }
  66. }
  67. if (returnList.isEmpty())
  68. {
  69. returnList = depts;
  70. }
  71. return returnList;
  72. }
  73. /**
  74. * 构建前端所需要下拉树结构
  75. *
  76. * @param depts 部门列表
  77. * @return 下拉树结构列表
  78. */
  79. @Override
  80. public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts)
  81. {
  82. List<SysDept> deptTrees = buildDeptTree(depts);
  83. return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
  84. }
  85. /**
  86. * 根据角色ID查询部门树信息
  87. *
  88. * @param roleId 角色ID
  89. * @return 选中部门列表
  90. */
  91. @Override
  92. public List<Integer> selectDeptListByRoleId(Long roleId)
  93. {
  94. SysRole role = roleMapper.selectRoleById(roleId);
  95. return deptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
  96. }
  97. /**
  98. * 根据部门ID查询信息
  99. *
  100. * @param deptId 部门ID
  101. * @return 部门信息
  102. */
  103. @Override
  104. public SysDept selectDeptById(Long deptId)
  105. {
  106. return deptMapper.selectDeptById(deptId);
  107. }
  108. /**
  109. * 根据ID查询所有子部门(正常状态)
  110. *
  111. * @param deptId 部门ID
  112. * @return 子部门数
  113. */
  114. @Override
  115. public int selectNormalChildrenDeptById(Long deptId)
  116. {
  117. return deptMapper.selectNormalChildrenDeptById(deptId);
  118. }
  119. /**
  120. * 是否存在子节点
  121. *
  122. * @param deptId 部门ID
  123. * @return 结果
  124. */
  125. @Override
  126. public boolean hasChildByDeptId(Long deptId)
  127. {
  128. int result = deptMapper.hasChildByDeptId(deptId);
  129. return result > 0 ? true : false;
  130. }
  131. /**
  132. * 查询部门是否存在用户
  133. *
  134. * @param deptId 部门ID
  135. * @return 结果 true 存在 false 不存在
  136. */
  137. @Override
  138. public boolean checkDeptExistUser(Long deptId)
  139. {
  140. int result = deptMapper.checkDeptExistUser(deptId);
  141. return result > 0 ? true : false;
  142. }
  143. /**
  144. * 校验部门名称是否唯一
  145. *
  146. * @param dept 部门信息
  147. * @return 结果
  148. */
  149. @Override
  150. public String checkDeptNameUnique(SysDept dept)
  151. {
  152. Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
  153. SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
  154. if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
  155. {
  156. return UserConstants.NOT_UNIQUE;
  157. }
  158. return UserConstants.UNIQUE;
  159. }
  160. /**
  161. * 新增保存部门信息
  162. *
  163. * @param dept 部门信息
  164. * @return 结果
  165. */
  166. @Override
  167. public int insertDept(SysDept dept)
  168. {
  169. SysDept info = deptMapper.selectDeptById(dept.getParentId());
  170. // 如果父节点不为正常状态,则不允许新增子节点
  171. if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
  172. {
  173. throw new CustomException("部门停用,不允许新增");
  174. }
  175. //TODO 后面动态添加 cw,sys_dept 字段merch_sn 写死 mhbs764449385500180480
  176. dept.setMerchSn("mhbs764449385500180480");
  177. dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
  178. return deptMapper.insertDept(dept);
  179. }
  180. /**
  181. * 修改保存部门信息
  182. *
  183. * @param dept 部门信息
  184. * @return 结果
  185. */
  186. @Override
  187. public int updateDept(SysDept dept)
  188. {
  189. SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
  190. SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
  191. if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
  192. {
  193. String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
  194. String oldAncestors = oldDept.getAncestors();
  195. dept.setAncestors(newAncestors);
  196. updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
  197. }
  198. int result = deptMapper.updateDept(dept);
  199. if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
  200. {
  201. // 如果该部门是启用状态,则启用该部门的所有上级部门
  202. updateParentDeptStatus(dept);
  203. }
  204. return result;
  205. }
  206. /**
  207. * 修改该部门的父级部门状态
  208. *
  209. * @param dept 当前部门
  210. */
  211. private void updateParentDeptStatus(SysDept dept)
  212. {
  213. String updateBy = dept.getUpdateBy();
  214. dept = deptMapper.selectDeptById(dept.getDeptId());
  215. dept.setUpdateBy(updateBy);
  216. deptMapper.updateDeptStatus(dept);
  217. }
  218. /**
  219. * 修改子元素关系
  220. *
  221. * @param deptId 被修改的部门ID
  222. * @param newAncestors 新的父ID集合
  223. * @param oldAncestors 旧的父ID集合
  224. */
  225. public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors)
  226. {
  227. List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
  228. for (SysDept child : children)
  229. {
  230. child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
  231. }
  232. if (children.size() > 0)
  233. {
  234. deptMapper.updateDeptChildren(children);
  235. }
  236. }
  237. /**
  238. * 删除部门管理信息
  239. *
  240. * @param deptId 部门ID
  241. * @return 结果
  242. */
  243. @Override
  244. public int deleteDeptById(Long deptId)
  245. {
  246. return deptMapper.deleteDeptById(deptId);
  247. }
  248. /**
  249. * 递归列表
  250. */
  251. private void recursionFn(List<SysDept> list, SysDept t)
  252. {
  253. // 得到子节点列表
  254. List<SysDept> childList = getChildList(list, t);
  255. t.setChildren(childList);
  256. for (SysDept tChild : childList)
  257. {
  258. if (hasChild(list, tChild))
  259. {
  260. recursionFn(list, tChild);
  261. }
  262. }
  263. }
  264. /**
  265. * 得到子节点列表
  266. */
  267. private List<SysDept> getChildList(List<SysDept> list, SysDept t)
  268. {
  269. List<SysDept> tlist = new ArrayList<SysDept>();
  270. Iterator<SysDept> it = list.iterator();
  271. while (it.hasNext())
  272. {
  273. SysDept n = (SysDept) it.next();
  274. if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
  275. {
  276. tlist.add(n);
  277. }
  278. }
  279. return tlist;
  280. }
  281. /**
  282. * 判断是否有子节点
  283. */
  284. private boolean hasChild(List<SysDept> list, SysDept t)
  285. {
  286. return getChildList(list, t).size() > 0 ? true : false;
  287. }
  288. }