SysDeptService.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package cc.uncarbon.module.sys.service;
  2. import cc.uncarbon.framework.core.constant.HelioConstant;
  3. import cc.uncarbon.framework.core.exception.BusinessException;
  4. import cc.uncarbon.framework.crud.service.impl.HelioBaseServiceImpl;
  5. import cc.uncarbon.module.sys.annotation.SysLog;
  6. import cc.uncarbon.module.sys.constant.SysConstant;
  7. import cc.uncarbon.module.sys.entity.SysDeptEntity;
  8. import cc.uncarbon.module.sys.entity.SysUserDeptRelationEntity;
  9. import cc.uncarbon.module.sys.enums.SysErrorEnum;
  10. import cc.uncarbon.module.sys.mapper.SysDeptMapper;
  11. import cc.uncarbon.module.sys.model.request.AdminInsertOrUpdateSysDeptDTO;
  12. import cc.uncarbon.module.sys.model.request.AdminListSysDeptDTO;
  13. import cc.uncarbon.module.sys.model.response.SysDeptBO;
  14. import cn.hutool.core.bean.BeanUtil;
  15. import cn.hutool.core.collection.CollUtil;
  16. import cn.hutool.core.util.ObjectUtil;
  17. import cn.hutool.core.util.StrUtil;
  18. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import javax.annotation.Resource;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. /**
  26. * 部门
  27. * @author Uncarbon
  28. */
  29. @Slf4j
  30. @Service
  31. public class SysDeptService extends HelioBaseServiceImpl<SysDeptMapper, SysDeptEntity> {
  32. @Resource
  33. private SysUserDeptRelationService sysUserDeptRelationService;
  34. /**
  35. * 后台管理-列表
  36. */
  37. public List<SysDeptBO> adminList(AdminListSysDeptDTO dto) {
  38. List<SysDeptEntity> entityList = this.list(
  39. new QueryWrapper<SysDeptEntity>()
  40. .lambda()
  41. // 名称
  42. .like(StrUtil.isNotBlank(dto.getTitle()), SysDeptEntity::getTitle, StrUtil.cleanBlank(dto.getTitle()))
  43. // 上级ID
  44. .eq(ObjectUtil.isNotNull(dto.getParentId()), SysDeptEntity::getParentId, dto.getParentId())
  45. // 排序
  46. .orderByAsc(SysDeptEntity::getSort)
  47. );
  48. return this.entityList2BOs(entityList);
  49. }
  50. /**
  51. * 通用-详情
  52. */
  53. public SysDeptBO getOneById(Long entityId) {
  54. SysDeptEntity entity = this.getById(entityId);
  55. SysErrorEnum.INVALID_ID.assertNotNull(entity);
  56. return this.entity2BO(entity, false);
  57. }
  58. /**
  59. * 后台管理-新增
  60. */
  61. @SysLog(value = "新增部门")
  62. @Transactional(rollbackFor = Exception.class)
  63. public Long adminInsert(AdminInsertOrUpdateSysDeptDTO dto) {
  64. this.checkExist(dto);
  65. if (ObjectUtil.isNull(dto.getParentId())) {
  66. dto.setParentId(0L);
  67. }
  68. dto.setId(null);
  69. SysDeptEntity entity = new SysDeptEntity();
  70. BeanUtil.copyProperties(dto, entity);
  71. this.save(entity);
  72. return entity.getId();
  73. }
  74. /**
  75. * 后台管理-编辑
  76. */
  77. @SysLog(value = "编辑部门")
  78. @Transactional(rollbackFor = Exception.class)
  79. public void adminUpdate(AdminInsertOrUpdateSysDeptDTO dto) {
  80. this.checkExist(dto);
  81. if (ObjectUtil.isNull(dto.getParentId())) {
  82. dto.setParentId(0L);
  83. }
  84. SysDeptEntity entity = new SysDeptEntity();
  85. BeanUtil.copyProperties(dto, entity);
  86. this.updateById(entity);
  87. }
  88. /**
  89. * 后台管理-删除
  90. */
  91. @SysLog(value = "删除部门")
  92. @Transactional(rollbackFor = Exception.class)
  93. public void adminDelete(List<Long> ids) {
  94. this.removeByIds(ids);
  95. }
  96. /**
  97. * 取所属部门简易信息
  98. * @param userId 用户ID
  99. */
  100. public SysDeptBO getPlainDeptByUserId(Long userId) {
  101. SysUserDeptRelationEntity relationEntity = sysUserDeptRelationService.getOne(
  102. new QueryWrapper<SysUserDeptRelationEntity>()
  103. .lambda()
  104. .eq(SysUserDeptRelationEntity::getUserId, userId)
  105. .last(HelioConstant.CRUD.SQL_LIMIT_1)
  106. );
  107. if (relationEntity == null) {
  108. return null;
  109. }
  110. SysDeptEntity entity = this.getById(relationEntity.getDeptId());
  111. return this.entity2BO(entity, false);
  112. }
  113. /*
  114. 私有方法
  115. ------------------------------------------------------------------------------------------------
  116. */
  117. private SysDeptBO entity2BO(SysDeptEntity entity, boolean traverseChildren) {
  118. if (entity == null) {
  119. return null;
  120. }
  121. SysDeptBO bo = new SysDeptBO();
  122. BeanUtil.copyProperties(entity, bo);
  123. // 可以在此处为BO填充字段
  124. if (SysConstant.ROOT_PARENT_ID.equals(bo.getParentId())) {
  125. bo.setParentId(null);
  126. }
  127. if (traverseChildren) {
  128. List<SysDeptBO> children = this.adminList(
  129. AdminListSysDeptDTO.builder()
  130. .parentId(bo.getId())
  131. .build()
  132. );
  133. if (CollUtil.isEmpty(children)) {
  134. children = null;
  135. }
  136. bo.setChildren(children);
  137. }
  138. return bo;
  139. }
  140. private List<SysDeptBO> entityList2BOs(List<SysDeptEntity> entityList) {
  141. // 深拷贝
  142. List<SysDeptBO> ret = new ArrayList<>(entityList.size());
  143. entityList.forEach(
  144. entity -> ret.add(this.entity2BO(entity, true))
  145. );
  146. return ret;
  147. }
  148. /**
  149. * 检查是否已存在同名数据
  150. * @param dto DTO
  151. */
  152. private void checkExist(AdminInsertOrUpdateSysDeptDTO dto) {
  153. SysDeptEntity existEntity = this.getOne(
  154. new QueryWrapper<SysDeptEntity>()
  155. .select(" id ")
  156. .lambda()
  157. .eq(SysDeptEntity::getTitle, dto.getTitle())
  158. .last(HelioConstant.CRUD.SQL_LIMIT_1)
  159. );
  160. if (existEntity != null && !existEntity.getId().equals(dto.getId())) {
  161. throw new BusinessException(400, "已存在相同部门,请重新输入");
  162. }
  163. }
  164. }