TmDepartmentServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package com.fdkankan.fusion.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import cn.hutool.core.util.IdUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  8. import com.fdkankan.fusion.common.ResultCode;
  9. import com.fdkankan.fusion.common.enums.IdPreEnum;
  10. import com.fdkankan.fusion.entity.*;
  11. import com.fdkankan.fusion.exception.BusinessException;
  12. import com.fdkankan.fusion.mapper.ITmDepartmentMapper;
  13. import com.fdkankan.fusion.service.ITmDepartmentService;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import com.fdkankan.fusion.service.ITmRoleService;
  16. import com.fdkankan.fusion.service.ITmUserRoleService;
  17. import com.fdkankan.fusion.service.ITmUserService;
  18. import org.apache.catalina.User;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.beans.BeanUtils;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23. import java.util.*;
  24. import java.util.logging.Level;
  25. import java.util.stream.Collectors;
  26. /**
  27. * <p>
  28. * 服务实现类
  29. * </p>
  30. *
  31. * @author
  32. * @since 2023-07-27
  33. */
  34. @Service
  35. public class TmDepartmentServiceImpl extends ServiceImpl<ITmDepartmentMapper, TmDepartment> implements ITmDepartmentService {
  36. @Autowired
  37. ITmUserService tmUserService;
  38. @Autowired
  39. ITmUserRoleService tmUserRoleService;
  40. @Autowired
  41. ITmRoleService tmRoleService;
  42. @Override
  43. public void insertDept(TmDepartment dept) {
  44. if(!checkDeptNameUnique(dept.getName(),null)){
  45. throw new BusinessException(ResultCode.DEPT_NAME_EXITS);
  46. }
  47. List<TmDepartment> tmDepartments = new ArrayList<>();
  48. this.getParentList(dept.getParentId(),tmDepartments);
  49. List<TmDepartment> collect = tmDepartments.stream().filter(entity -> !entity.getId().equals("0")).collect(Collectors.toList());
  50. if(collect.size() >=4){
  51. throw new BusinessException(ResultCode.DEPT_ADD_ERROR);
  52. }
  53. dept.setId(IdPreEnum.DEPARTMENT_PRE.getPre() +IdUtil.getSnowflake(1).nextId() );
  54. //dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
  55. dept.setCreatorId((String) StpUtil.getLoginId());
  56. this.save(dept);
  57. }
  58. private void getParentList(String deptId,List<TmDepartment> tmDepartments) {
  59. if(StringUtils.isNotBlank(deptId)){
  60. TmDepartment tmDepartment = this.getById(deptId);
  61. if(tmDepartment != null){
  62. tmDepartments.add(tmDepartment);
  63. getParentList(tmDepartment.getParentId(),tmDepartments);
  64. }
  65. }
  66. }
  67. @Override
  68. public void updateDept(TmDepartment dept) {
  69. if(!checkDeptNameUnique(dept.getName(),dept.getId())){
  70. throw new BusinessException(ResultCode.DEPT_NAME_EXITS);
  71. }
  72. if(dept.getId().equals(dept.getParentId())){
  73. throw new BusinessException(ResultCode.DEPT_EDIT_ERROR);
  74. }
  75. List<TmDepartment> tmDepartments = new ArrayList<>();
  76. this.getParentList(dept.getParentId(),tmDepartments);
  77. List<TmDepartment> collect2 = tmDepartments.stream().filter(entity -> !entity.getId().equals("0")).collect(Collectors.toList());
  78. if(collect2.size() >=4){
  79. throw new BusinessException(ResultCode.DEPT_ADD_ERROR);
  80. }
  81. List<TmDepartment> sonByDeptId = this.getSonByDeptId(dept.getId());
  82. if(sonByDeptId.size() >0){
  83. List<String> collect = sonByDeptId.stream().map(TmDepartment::getId).collect(Collectors.toList());
  84. if(collect.contains(dept.getParentId())){
  85. throw new BusinessException(ResultCode.DEPT_EDIT_ERROR);
  86. }
  87. }
  88. dept.setCreateTime(null);
  89. dept.setUpdateTime(null);
  90. this.updateById(dept);
  91. }
  92. @Override
  93. public void deleteDeptById(String deptId) {
  94. TmDepartment department = this.getById(deptId);
  95. if(department != null){
  96. List<TmDepartment> deptList = this.getDeptListByParentId(department);
  97. if(deptList .size() >0){
  98. throw new BusinessException(ResultCode.DEPT_DEL_ERROR1);
  99. }
  100. List<TmUser> userList = tmUserService.getByDeptId(deptId);
  101. if(userList.size() >0){
  102. throw new BusinessException(ResultCode.DEPT_DEL_ERROR2);
  103. }
  104. this.removeById(deptId);
  105. }
  106. }
  107. private boolean checkDeptNameUnique(String name, String id) {
  108. LambdaQueryWrapper<TmDepartment> wrapper = new LambdaQueryWrapper<>();
  109. wrapper.eq(TmDepartment::getName,name);
  110. if(StringUtils.isNotBlank(id)){
  111. wrapper.ne(TmDepartment::getId,id);
  112. }
  113. List<TmDepartment> list = this.list(wrapper);
  114. if(list.size() >0){
  115. return false;
  116. }
  117. return true;
  118. }
  119. @Override
  120. public List<TmDepartment> getDeptList() {
  121. String userId =(String) StpUtil.getLoginId();
  122. TmUser tmUser = tmUserService.getById(userId);
  123. if(tmUser == null){
  124. return new ArrayList<>();
  125. }
  126. List<TmUserRole> tmUserRoles = tmUserRoleService.getByUserId(tmUser.getId());
  127. if(tmUserRoles.size() >0){
  128. TmUserRole tmUserRole = tmUserRoles.get(0);
  129. TmRole tmRole = tmRoleService.getById(tmUserRole.getRoleId());
  130. if(tmRole != null && tmRole.getRoleKey().equals("admin-ordinary")){
  131. TmDepartment tmDepartment = this.getById(tmUser.getDeptId());
  132. TmDepartment parentDept = this.getById(tmDepartment.getParentId());
  133. if(parentDept !=null){
  134. tmDepartment.setParentName(parentDept.getName());
  135. }
  136. return Arrays.asList(tmDepartment);
  137. }
  138. }
  139. return getDeptList(tmUser.getDeptId());
  140. }
  141. public List<TmDepartment> getDeptList(String deptId) {
  142. TmDepartment dept = this.getById(deptId);
  143. if(dept == null){
  144. return new ArrayList<>();
  145. }
  146. // if("0".equals(dept.getId())){
  147. // return this.getDeptListByParentId(dept);
  148. // }
  149. dept.setChildren(this.getDeptListByParentId(dept));
  150. TmDepartment parentDept = this.getById(dept.getParentId());
  151. if(parentDept !=null){
  152. dept.setParentName(parentDept.getName());
  153. }
  154. return Arrays.asList(dept);
  155. }
  156. public List<TmDepartment> getDeptListByParentId(TmDepartment department) {
  157. LambdaQueryWrapper<TmDepartment> queryWrapper = new LambdaQueryWrapper<>();
  158. if(department.getId() == null){
  159. queryWrapper.isNull(TmDepartment::getParentId);
  160. }else {
  161. queryWrapper.eq(TmDepartment::getParentId,department.getId());
  162. }
  163. queryWrapper.orderByDesc(TmDepartment::getCreateTime);
  164. List<TmDepartment> list = this.list(queryWrapper);
  165. if(list.size() <=0){
  166. return list;
  167. }
  168. for (TmDepartment deptVo : list) {
  169. deptVo.setChildren(getDeptListByParentId(deptVo));
  170. deptVo.setParentName(department.getName());
  171. }
  172. return list;
  173. }
  174. @Override
  175. public List<String> getByZdDeptIds() {
  176. String deptId = this.getDeptId();
  177. String zdDeptId = this.getZdDeptId(deptId);
  178. List<TmDepartment> deptList = this.getSonByDeptId(zdDeptId);
  179. List<String> deptIds = deptList.stream().map(TmDepartment::getId).collect(Collectors.toList());
  180. deptIds.add(zdDeptId);
  181. return deptIds;
  182. }
  183. private String getZdDeptId(String deptId) {
  184. TmDepartment tmDepartment = this.getById(deptId);
  185. if(tmDepartment == null){
  186. throw new BusinessException(ResultCode.NOT_DEPT.code,ResultCode.NOT_DEPT.msg +":"+deptId);
  187. }
  188. if(tmDepartment.getDeptType()==0 || tmDepartment.getDeptType()==1 ){
  189. return tmDepartment.getId();
  190. }
  191. return getZdDeptId(tmDepartment.getParentId());
  192. }
  193. @Override
  194. public List<String> getDeptIds() {
  195. List<String> Ids = new ArrayList<>();
  196. List<TmDepartment> deptList = this.getDeptList();
  197. getDeptIds(deptList,Ids);
  198. return Ids;
  199. }
  200. @Override
  201. public String getDeptId() {
  202. String userId =(String) StpUtil.getLoginId();
  203. TmUser tmUser = tmUserService.getById(userId);
  204. if(tmUser == null){
  205. return null;
  206. }
  207. return tmUser.getDeptId();
  208. }
  209. public void getDeptIds(List<TmDepartment> departments, List<String > Ids) {
  210. for (TmDepartment department : departments) {
  211. Ids.add(department.getId());
  212. if(department.getChildren() != null && department.getChildren().size() >0){
  213. this.getDeptIds(department.getChildren(),Ids);
  214. }
  215. }
  216. }
  217. @Override
  218. public List<TmDepartment> getSonByDeptId(String deptId) {
  219. List<TmDepartment> listAll = new ArrayList<>();
  220. converSon(Arrays.asList(deptId),listAll);
  221. return listAll;
  222. }
  223. private void converSon(List<String> deptIds, List<TmDepartment> listAll){
  224. LambdaQueryWrapper<TmDepartment> wrapper = new LambdaQueryWrapper<>();
  225. wrapper.in(TmDepartment::getParentId,deptIds);
  226. List<TmDepartment> list = this.list(wrapper);
  227. if(list.size() <=0){
  228. return ;
  229. }
  230. listAll.addAll(list);
  231. List<String> ids = list.stream().map(TmDepartment::getId).collect(Collectors.toList());
  232. converSon(ids,listAll);
  233. }
  234. @Override
  235. public HashMap<String, TmDepartment> getMapByDeptIds(Set<String> deptIdSet) {
  236. HashMap<String,TmDepartment> map = new HashMap<>();
  237. if(deptIdSet.size() >0){
  238. List<TmDepartment> tmDepartments = this.listByIds(deptIdSet);
  239. tmDepartments.forEach(entity -> map.put(entity.getId(),entity));
  240. }
  241. return map;
  242. }
  243. @Override
  244. public List<TmDepartment> getLikeName(String organizerDeptName) {
  245. LambdaQueryWrapper<TmDepartment> wrapper = new LambdaQueryWrapper<>();
  246. wrapper.like(TmDepartment::getName,organizerDeptName);
  247. return this.list(wrapper);
  248. }
  249. @Override
  250. public List<String> getSonByDeptIdAndDeptIds(List<String> deptIds, String deptId) {
  251. List<String> deptIds2 = new ArrayList<>();
  252. if(StringUtils.isNotBlank(deptId)){
  253. List<TmDepartment> sonByDeptId = this.getSonByDeptId(deptId);
  254. deptIds2 = sonByDeptId.stream().map(TmDepartment::getId).collect(Collectors.toList());
  255. deptIds2.add(deptId);
  256. }
  257. if(!deptIds2.isEmpty()){
  258. List<String> collect = deptIds.stream().filter(deptIds2::contains).collect(Collectors.toList());
  259. if(collect.size()<=0){
  260. collect.add("empty-dept");
  261. }
  262. return collect;
  263. }
  264. if(deptIds.size()<=0){
  265. deptIds.add("empty-dept");
  266. }
  267. return deptIds;
  268. }
  269. }