SysDeptServiceImpl.java 10 KB

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