SysDeptServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. package com.ruoyi.system.service.impl;
  2. import com.ruoyi.common.core.constant.UserConstants;
  3. import com.ruoyi.common.core.exception.ServiceException;
  4. import com.ruoyi.common.core.text.Convert;
  5. import com.ruoyi.common.core.utils.SpringUtils;
  6. import com.ruoyi.common.core.utils.StringUtils;
  7. import com.ruoyi.common.datascope.annotation.DataScope;
  8. import com.ruoyi.common.security.utils.SecurityUtils;
  9. import com.ruoyi.system.api.domain.SysDept;
  10. import com.ruoyi.system.api.domain.SysRole;
  11. import com.ruoyi.system.api.domain.SysUser;
  12. import com.ruoyi.system.domain.vo.TreeSelect;
  13. import com.ruoyi.system.mapper.SysDeptMapper;
  14. import com.ruoyi.system.mapper.SysRoleMapper;
  15. import com.ruoyi.system.service.ISysDeptService;
  16. import org.apache.ibatis.annotations.Param;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Service;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.stream.Collectors;
  23. /**
  24. * 部门管理 服务实现
  25. *
  26. * @author ruoyi
  27. */
  28. @Service
  29. public class SysDeptServiceImpl implements ISysDeptService
  30. {
  31. @Autowired
  32. private SysDeptMapper deptMapper;
  33. @Autowired
  34. private SysRoleMapper roleMapper;
  35. /**
  36. * 查询部门管理数据
  37. *
  38. * @param dept 部门信息
  39. * @return 部门信息集合
  40. */
  41. @Override
  42. @DataScope(deptAlias = "d")
  43. public List<SysDept> selectDeptList(SysDept dept)
  44. {
  45. return deptMapper.selectDeptList(dept);
  46. }
  47. /**
  48. * 根据DEPT_ID查询下级的乡镇或区县部门列表,如果传入市级部门则查询区县,如果传入区县部门则查询乡镇街道(不带数据权限)
  49. *
  50. * @param dept 部门信息
  51. * @return 部门信息集合
  52. */
  53. @Override
  54. public List<SysDept> selectChildrenCountyOrVillagesDeptListByDeptId(SysDept dept){
  55. return deptMapper.selectChildrenCountyOrVillagesDeptListByDeptId(dept);
  56. }
  57. /**
  58. * 查询部门管理数据(本部门及以下)
  59. *
  60. * @param dept 部门信息
  61. * @return 部门信息集合
  62. */
  63. @Override
  64. @DataScope(deptAlias = "d")
  65. public List<SysDept> selectDeptBelowList(SysDept dept)
  66. {
  67. return deptMapper.selectDeptBelowList(dept);
  68. }
  69. /**
  70. * 查询全部部门管理数据
  71. *
  72. * @param dept 部门信息
  73. * @return 部门信息集合
  74. */
  75. @Override
  76. public List<SysDept> selectDeptAllList(SysDept dept){
  77. return deptMapper.selectDeptAllList(dept);
  78. }
  79. /**
  80. * 构建前端所需要树结构
  81. *
  82. * @param depts 部门列表
  83. * @return 树结构列表
  84. */
  85. @Override
  86. public List<SysDept> buildDeptTree(List<SysDept> depts)
  87. {
  88. List<SysDept> returnList = new ArrayList<SysDept>();
  89. List<Long> tempList = new ArrayList<Long>();
  90. for (SysDept dept : depts)
  91. {
  92. tempList.add(dept.getDeptId());
  93. }
  94. for (SysDept dept : depts)
  95. {
  96. // 如果是顶级节点, 遍历该父节点的所有子节点
  97. if (!tempList.contains(dept.getParentId()))
  98. {
  99. recursionFn(depts, dept);
  100. returnList.add(dept);
  101. }
  102. }
  103. if (returnList.isEmpty())
  104. {
  105. returnList = depts;
  106. }
  107. return returnList;
  108. }
  109. /**
  110. * 构建前端所需要下拉树结构
  111. *
  112. * @param depts 部门列表
  113. * @return 下拉树结构列表
  114. */
  115. @Override
  116. public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts)
  117. {
  118. List<SysDept> deptTrees = buildDeptTree(depts);
  119. return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
  120. }
  121. /**
  122. * 根据角色ID查询部门树信息
  123. *
  124. * @param roleId 角色ID
  125. * @return 选中部门列表
  126. */
  127. @Override
  128. public List<Long> selectDeptListByRoleId(Long roleId)
  129. {
  130. SysRole role = roleMapper.selectRoleById(roleId);
  131. return deptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
  132. }
  133. /**
  134. * 根据部门ID查询信息
  135. *
  136. * @param deptId 部门ID
  137. * @return 部门信息
  138. */
  139. @Override
  140. public SysDept selectDeptById(Long deptId)
  141. {
  142. return deptMapper.selectDeptById(deptId);
  143. }
  144. /**
  145. * 根据部门ID查询信息
  146. *
  147. * @param deptIdList 部门ID集合
  148. * @return 部门信息
  149. */
  150. @Override
  151. public List<SysDept> selectDeptByIds(List<Long> deptIdList)
  152. {
  153. return deptMapper.selectDeptByIds(deptIdList);
  154. }
  155. /**
  156. * 根据ID查询所有子部门(正常状态)
  157. *
  158. * @param deptId 部门ID
  159. * @return 子部门数
  160. */
  161. @Override
  162. public int selectNormalChildrenDeptById(Long deptId)
  163. {
  164. return deptMapper.selectNormalChildrenDeptById(deptId);
  165. }
  166. /**
  167. * 是否存在子节点
  168. *
  169. * @param deptId 部门ID
  170. * @return 结果
  171. */
  172. @Override
  173. public boolean hasChildByDeptId(Long deptId)
  174. {
  175. int result = deptMapper.hasChildByDeptId(deptId);
  176. return result > 0 ? true : false;
  177. }
  178. /**
  179. * 查询部门是否存在用户
  180. *
  181. * @param deptId 部门ID
  182. * @return 结果 true 存在 false 不存在
  183. */
  184. @Override
  185. public boolean checkDeptExistUser(Long deptId)
  186. {
  187. int result = deptMapper.checkDeptExistUser(deptId);
  188. return result > 0 ? true : false;
  189. }
  190. /**
  191. * 校验部门名称是否唯一
  192. *
  193. * @param dept 部门信息
  194. * @return 结果
  195. */
  196. @Override
  197. public String checkDeptNameUnique(SysDept dept)
  198. {
  199. Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
  200. SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
  201. if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
  202. {
  203. return UserConstants.NOT_UNIQUE;
  204. }
  205. return UserConstants.UNIQUE;
  206. }
  207. /**
  208. * 校验部门是否有数据权限
  209. *
  210. * @param deptId 部门id
  211. */
  212. @Override
  213. public void checkDeptDataScope(Long deptId)
  214. {
  215. if (!SysUser.isAdmin(SecurityUtils.getUserId()))
  216. {
  217. SysDept dept = new SysDept();
  218. dept.setDeptId(deptId);
  219. List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
  220. if (StringUtils.isEmpty(depts))
  221. {
  222. throw new ServiceException("没有权限访问部门数据!");
  223. }
  224. }
  225. }
  226. /**
  227. * 新增保存部门信息
  228. *
  229. * @param dept 部门信息
  230. * @return 结果
  231. */
  232. @Override
  233. public int insertDept(SysDept dept)
  234. {
  235. SysDept info = deptMapper.selectDeptById(dept.getParentId());
  236. // 如果父节点不为正常状态,则不允许新增子节点
  237. if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
  238. {
  239. throw new ServiceException("部门停用,不允许新增");
  240. }
  241. dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
  242. return deptMapper.insertDept(dept);
  243. }
  244. /**
  245. * 修改保存部门信息
  246. *
  247. * @param dept 部门信息
  248. * @return 结果
  249. */
  250. @Override
  251. public int updateDept(SysDept dept)
  252. {
  253. SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
  254. SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
  255. if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
  256. {
  257. String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
  258. String oldAncestors = oldDept.getAncestors();
  259. dept.setAncestors(newAncestors);
  260. updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
  261. }
  262. int result = deptMapper.updateDept(dept);
  263. if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
  264. && !StringUtils.equals("0", dept.getAncestors()))
  265. {
  266. // 如果该部门是启用状态,则启用该部门的所有上级部门
  267. updateParentDeptStatusNormal(dept);
  268. }
  269. return result;
  270. }
  271. /**
  272. * 修改该部门的父级部门状态
  273. *
  274. * @param dept 当前部门
  275. */
  276. private void updateParentDeptStatusNormal(SysDept dept)
  277. {
  278. String ancestors = dept.getAncestors();
  279. Long[] deptIds = Convert.toLongArray(ancestors);
  280. deptMapper.updateDeptStatusNormal(deptIds);
  281. }
  282. /**
  283. * 修改子元素关系
  284. *
  285. * @param deptId 被修改的部门ID
  286. * @param newAncestors 新的父ID集合
  287. * @param oldAncestors 旧的父ID集合
  288. */
  289. public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors)
  290. {
  291. List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
  292. for (SysDept child : children)
  293. {
  294. child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
  295. }
  296. if (children.size() > 0)
  297. {
  298. deptMapper.updateDeptChildren(children);
  299. }
  300. }
  301. /**
  302. * 删除部门管理信息
  303. *
  304. * @param deptId 部门ID
  305. * @return 结果
  306. */
  307. @Override
  308. public int deleteDeptById(Long deptId)
  309. {
  310. return deptMapper.deleteDeptById(deptId);
  311. }
  312. /**
  313. * 递归列表
  314. */
  315. private void recursionFn(List<SysDept> list, SysDept t)
  316. {
  317. // 得到子节点列表
  318. List<SysDept> childList = getChildList(list, t);
  319. t.setChildren(childList);
  320. for (SysDept tChild : childList)
  321. {
  322. if (hasChild(list, tChild))
  323. {
  324. recursionFn(list, tChild);
  325. }
  326. }
  327. }
  328. /**
  329. * 得到子节点列表
  330. */
  331. private List<SysDept> getChildList(List<SysDept> list, SysDept t)
  332. {
  333. List<SysDept> tlist = new ArrayList<SysDept>();
  334. Iterator<SysDept> it = list.iterator();
  335. while (it.hasNext())
  336. {
  337. SysDept n = (SysDept) it.next();
  338. if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
  339. {
  340. tlist.add(n);
  341. }
  342. }
  343. return tlist;
  344. }
  345. /**
  346. * 判断是否有子节点
  347. */
  348. private boolean hasChild(List<SysDept> list, SysDept t)
  349. {
  350. return getChildList(list, t).size() > 0 ? true : false;
  351. }
  352. /**
  353. * 获取当前登录人父ID的兄弟IDList
  354. * */
  355. public List<SysDept> selectParentSiblingsDeptsById(Long deptId){
  356. return deptMapper.selectParentSiblingsDeptsById(deptId);
  357. }
  358. /**
  359. * 根据DEPTID获取指挥中心
  360. * */
  361. public SysDept getCommandCenter(String deptId, String postId){
  362. return deptMapper.getCommandCenter(deptId, postId);
  363. }
  364. /**
  365. * 根据DEPTID获取区级指挥中心对象
  366. * */
  367. public SysDept selectDistrictDeptByAnyDeptId(Long deptId){
  368. return deptMapper.selectDistrictDeptByAnyDeptId(deptId);
  369. }
  370. /**
  371. * 根据ID查询所有子部门
  372. *
  373. * @param deptId 部门ID
  374. * @return 部门列表
  375. */
  376. public List<SysDept> selectChildrenDeptById(Long deptId){
  377. return deptMapper.selectChildrenDeptById(deptId);
  378. }
  379. /**
  380. * 根据ID查询所有子部门
  381. *
  382. * @param deptId 部门ID
  383. * @param postId 岗位ID
  384. * @return 部门列表
  385. */
  386. public List<SysDept> selectChildrenDeptById(String deptId, String postId){
  387. return deptMapper.selectChildrenDeptByIdAndPostId(deptId,postId);
  388. }
  389. /**
  390. * 根据ID查询所有子部门
  391. *
  392. * @return 部门列表
  393. */
  394. public List<SysDept> userAllDeptSelectIncludeChildren(){
  395. List<SysDept> list = SecurityUtils.getLoginUser().getSysUser().getDepts();
  396. return deptMapper.userAllDeptSelectIncludeChildren(list);
  397. }
  398. /**
  399. * 根据POSTID查询所有子部门
  400. *
  401. * @param postId 部门类型
  402. * @return 部门列表
  403. */
  404. public List<SysDept> getDeptsByPostId(Long postId){
  405. return deptMapper.getDeptsByPostId(postId);
  406. }
  407. /**
  408. * 根据部门deptType和deptId查询部门集合
  409. *
  410. * @param sysDept 部门
  411. * @return 部门列表
  412. */
  413. @DataScope(deptAlias = "d")
  414. public List<SysDept> getDeptsByDeptType(SysDept sysDept){
  415. return deptMapper.getDeptsByDeptType(sysDept);
  416. }
  417. /**
  418. * 获取行管部门下可接收短信人员
  419. *
  420. * @param sysDept 部门类型
  421. * @return 部门列表
  422. */
  423. public List<SysDept> selectDeptAndUsersByDeptType(SysDept sysDept){
  424. return deptMapper.selectDeptAndUsersByDeptType(sysDept);
  425. }
  426. /**
  427. * 判断DEPT上级中是否有除消防支队外的行管局
  428. * */
  429. public Integer deptParentsHasBureau(Long deptId){
  430. return deptMapper.deptParentsHasBureau(deptId);
  431. }
  432. /**
  433. * 根据子deptId和父级类型递归获取上级部门
  434. * */
  435. public List<Long> findParentIdByChildDeptIdAndParentType(Long deptId, String deptType){
  436. return deptMapper.findParentIdByChildDeptIdAndParentType(deptId,Convert.toStrArray(deptType));
  437. }
  438. /**
  439. * 判断DEPT_ID是否是PARENT_ID的子节点
  440. * */
  441. @Override
  442. public Integer thisDeptIdIsParentIdChild(Long deptId,Long parentId){
  443. return deptMapper.thisDeptIdIsParentIdChild(deptId,parentId);
  444. }
  445. /**
  446. * 查询部门管理数据
  447. *
  448. * @param deptName 部门信息
  449. * @return 部门信息集合
  450. */
  451. public List<SysDept> selectDeptListByDeptNames(String[] deptName){
  452. return deptMapper.selectDeptListImportExcel(deptName);
  453. }
  454. }