SysDeptServiceImpl.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package com.dgtly.system.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Isolation;
  7. import org.springframework.transaction.annotation.Propagation;
  8. import org.springframework.transaction.annotation.Transactional;
  9. import com.dgtly.common.annotation.DataScope;
  10. import com.dgtly.common.constant.UserConstants;
  11. import com.dgtly.common.core.domain.Ztree;
  12. import com.dgtly.common.exception.BusinessException;
  13. import com.dgtly.common.utils.StringUtils;
  14. import com.dgtly.system.domain.SysDept;
  15. import com.dgtly.system.domain.SysRole;
  16. import com.dgtly.system.mapper.SysDeptMapper;
  17. import com.dgtly.system.service.ISysDeptService;
  18. /**
  19. * 部门管理 服务实现
  20. *
  21. * @author ruoyi
  22. */
  23. @Service
  24. public class SysDeptServiceImpl implements ISysDeptService
  25. {
  26. @Autowired
  27. private SysDeptMapper deptMapper;
  28. /**
  29. * 查询部门管理数据
  30. *
  31. * @param dept 部门信息
  32. * @return 部门信息集合
  33. */
  34. @Override
  35. @DataScope(deptAlias = "d")
  36. public List<SysDept> selectDeptList(SysDept dept)
  37. {
  38. return deptMapper.selectDeptList(dept);
  39. }
  40. /**
  41. * 查询部门管理树
  42. *
  43. * @param dept 部门信息
  44. * @return 所有部门信息
  45. */
  46. @Override
  47. @DataScope(deptAlias = "d")
  48. public List<Ztree> selectDeptTree(SysDept dept)
  49. {
  50. List<SysDept> deptList = deptMapper.selectDeptList(dept);
  51. List<Ztree> ztrees = initZtree(deptList);
  52. return ztrees;
  53. }
  54. /**
  55. * 根据角色ID查询部门(数据权限)
  56. *
  57. * @param role 角色对象
  58. * @return 部门列表(数据权限)
  59. */
  60. @Override
  61. @DataScope(deptAlias = "d")
  62. public List<Ztree> roleDeptTreeData(SysRole role)
  63. {
  64. Long roleId = role.getRoleId();
  65. List<Ztree> ztrees = new ArrayList<Ztree>();
  66. /*处理 注解添加的DataScope代码 传递一下*/
  67. SysDept sysDept = new SysDept();
  68. sysDept.setParams(role.getParams());
  69. List<SysDept> deptList = deptMapper.selectDeptList(sysDept);
  70. if (StringUtils.isNotNull(roleId))
  71. {
  72. List<String> roleDeptList = deptMapper.selectRoleDeptTree(roleId);
  73. ztrees = initZtree(deptList, roleDeptList);
  74. }
  75. else
  76. {
  77. ztrees = initZtree(deptList);
  78. }
  79. return ztrees;
  80. }
  81. /**
  82. * 对象转部门树
  83. *
  84. * @param deptList 部门列表
  85. * @return 树结构列表
  86. */
  87. public List<Ztree> initZtree(List<SysDept> deptList)
  88. {
  89. return initZtree(deptList, null);
  90. }
  91. /**
  92. * 对象转部门树
  93. *
  94. * @param deptList 部门列表
  95. * @param roleDeptList 角色已存在菜单列表
  96. * @return 树结构列表
  97. */
  98. public List<Ztree> initZtree(List<SysDept> deptList, List<String> roleDeptList)
  99. {
  100. List<Ztree> ztrees = new ArrayList<Ztree>();
  101. boolean isCheck = StringUtils.isNotNull(roleDeptList);
  102. for (SysDept dept : deptList)
  103. {
  104. if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
  105. {
  106. Ztree ztree = new Ztree();
  107. ztree.setId(dept.getDeptId());
  108. ztree.setpId(dept.getParentId());
  109. ztree.setName(dept.getDeptName());
  110. ztree.setTitle(dept.getDeptName());
  111. if (isCheck)
  112. {
  113. ztree.setChecked(roleDeptList.contains(dept.getDeptId() + dept.getDeptName()));
  114. }
  115. ztrees.add(ztree);
  116. }
  117. }
  118. return ztrees;
  119. }
  120. /**
  121. * 查询部门人数
  122. *
  123. * @param parentId 部门ID
  124. * @return 结果
  125. */
  126. @Override
  127. public int selectDeptCount(Long parentId)
  128. {
  129. SysDept dept = new SysDept();
  130. dept.setParentId(parentId);
  131. return deptMapper.selectDeptCount(dept);
  132. }
  133. /**
  134. * 查询部门是否存在用户
  135. *
  136. * @param deptId 部门ID
  137. * @return 结果 true 存在 false 不存在
  138. */
  139. @Override
  140. public boolean checkDeptExistUser(Long deptId)
  141. {
  142. int result = deptMapper.checkDeptExistUser(deptId);
  143. return result > 0 ? true : false;
  144. }
  145. /**
  146. * 删除部门管理信息
  147. *
  148. * @param deptId 部门ID
  149. * @return 结果
  150. */
  151. @Override
  152. public int deleteDeptById(Long deptId)
  153. {
  154. return deptMapper.deleteDeptById(deptId);
  155. }
  156. /**
  157. * 新增保存部门信息,
  158. * qxp 修改 新增部门用于创建部门时将部门的祖籍列表中加入自身id方便做公司全数据权限
  159. *
  160. * @param dept 部门信息
  161. * @return 结果
  162. */
  163. @Override
  164. @Transactional (isolation=Isolation.READ_UNCOMMITTED)
  165. public int insertDept(SysDept dept)
  166. {
  167. SysDept info = deptMapper.selectDeptById(dept.getParentId());
  168. deptMapper.insertDeptReturnId(dept);
  169. SysDept upInfo = new SysDept();
  170. if(info!=null){
  171. // 如果父节点不为"正常"状态,则不允许新增子节点
  172. if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
  173. {
  174. throw new BusinessException("部门停用,不允许新增");
  175. }
  176. upInfo.setAncestors(info.getAncestors()+","+dept.getDeptId());
  177. }else{
  178. upInfo.setAncestors(0+","+dept.getDeptId());
  179. }
  180. upInfo.setDeptId(dept.getDeptId());
  181. int i =deptMapper.updateDept(upInfo);
  182. if(i>0){
  183. return i;
  184. }else{
  185. throw new RuntimeException("新增部门失败!");
  186. }
  187. }
  188. /**
  189. * 修改保存部门信息
  190. *
  191. * @param dept 部门信息
  192. * @return 结果
  193. */
  194. @Override
  195. @Transactional
  196. public int updateDept(SysDept dept)
  197. {
  198. SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
  199. SysDept oldDept = selectDeptById(dept.getDeptId());
  200. if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
  201. {
  202. String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
  203. String oldAncestors = oldDept.getAncestors();
  204. dept.setAncestors(newAncestors);
  205. updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
  206. }
  207. int result = deptMapper.updateDept(dept);
  208. if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
  209. {
  210. // 如果该部门是启用状态,则启用该部门的所有上级部门
  211. updateParentDeptStatus(dept);
  212. }
  213. return result;
  214. }
  215. /**
  216. * 修改该部门的父级部门状态
  217. *
  218. * @param dept 当前部门
  219. */
  220. private void updateParentDeptStatus(SysDept dept)
  221. {
  222. String updateBy = dept.getUpdateBy();
  223. dept = deptMapper.selectDeptById(dept.getDeptId());
  224. dept.setUpdateBy(updateBy);
  225. deptMapper.updateDeptStatus(dept);
  226. }
  227. /**
  228. * 修改子元素关系
  229. *
  230. * @param deptId 被修改的部门ID
  231. * @param newAncestors 新的父ID集合
  232. * @param oldAncestors 旧的父ID集合
  233. */
  234. public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors)
  235. {
  236. List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
  237. for (SysDept child : children)
  238. {
  239. child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
  240. }
  241. if (children.size() > 0)
  242. {
  243. deptMapper.updateDeptChildren(children);
  244. }
  245. }
  246. /**
  247. * 根据部门ID查询信息
  248. *
  249. * @param deptId 部门ID
  250. * @return 部门信息
  251. */
  252. @Override
  253. public SysDept selectDeptById(Long deptId)
  254. {
  255. return deptMapper.selectDeptById(deptId);
  256. }
  257. /**
  258. * 校验部门名称是否唯一
  259. *
  260. * @param dept 部门信息
  261. * @return 结果
  262. */
  263. @Override
  264. public String checkDeptNameUnique(SysDept dept)
  265. {
  266. Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
  267. SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
  268. if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
  269. {
  270. return UserConstants.DEPT_NAME_NOT_UNIQUE;
  271. }
  272. return UserConstants.DEPT_NAME_UNIQUE;
  273. }
  274. }