|
|
@@ -0,0 +1,87 @@
|
|
|
+package cn.tr.module.sys.user.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.tr.core.exception.ServiceException;
|
|
|
+import cn.tr.core.exception.TRExcCode;
|
|
|
+import cn.tr.core.utils.JsonUtils;
|
|
|
+import cn.tr.module.sys.mapper.user.SysOrgMapper;
|
|
|
+import cn.tr.module.sys.user.dto.SysOrgDTO;
|
|
|
+import cn.tr.module.sys.user.dto.SysOrgQueryDTO;
|
|
|
+import cn.tr.module.sys.user.po.SysOrgPO;
|
|
|
+import cn.tr.module.sys.user.po.SysUserPO;
|
|
|
+import cn.tr.module.sys.user.repository.SysOrgRepository;
|
|
|
+import cn.tr.module.sys.user.repository.SysUserRepository;
|
|
|
+import cn.tr.module.sys.user.service.ISysOrgService;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.cache.annotation.CacheEvict;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ClassName : SysOrgServiceImpl
|
|
|
+ * @Description :
|
|
|
+ * @Author : LF
|
|
|
+ * @Date: 2023年03月31日
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class SysOrgServiceImpl implements ISysOrgService {
|
|
|
+ @Autowired
|
|
|
+ private SysOrgRepository orgRepository;
|
|
|
+ @Autowired
|
|
|
+ private SysUserRepository userRepository;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<SysOrgDTO> selectSysOrgList(SysOrgQueryDTO query) {
|
|
|
+ return SysOrgMapper.INSTANCE.toSysOrgDTOList(orgRepository.selectList(new LambdaQueryWrapper<SysOrgPO>()
|
|
|
+ .like(StrUtil.isNotEmpty(query.getName()), SysOrgPO::getName,query.getName())
|
|
|
+ .eq(StrUtil.isNotEmpty(query.getParentId()), SysOrgPO::getParentId,query.getParentId())
|
|
|
+ .eq(ObjectUtil.isNotNull(query.getDisable()), SysOrgPO::getDisable,query.getDisable()))
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SysOrgDTO selectSysOrgById(String id) {
|
|
|
+ return SysOrgMapper.INSTANCE.toSysOrgDTO(orgRepository.selectById(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @CacheEvict(value = "sys:org")
|
|
|
+ public boolean updateSysOrgById(SysOrgDTO source) {
|
|
|
+ return orgRepository.updateById(SysOrgMapper.INSTANCE.toSysOrgPO(source))!=0;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ @CacheEvict(value = "sys:org")
|
|
|
+ public boolean insertSysOrg(SysOrgDTO source) {
|
|
|
+ return orgRepository.insert(SysOrgMapper.INSTANCE.toSysOrgPO(source))!=0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @CacheEvict(value = "sys:org")
|
|
|
+ public int deleteSysOrgByIds(Collection<String> ids) {
|
|
|
+ List<SysOrgPO> orgs = orgRepository.selectBatchIds(ids);
|
|
|
+ if(CollectionUtil.isEmpty(orgs)){
|
|
|
+ return CollectionUtil.size(ids);
|
|
|
+ }
|
|
|
+ List<SysUserPO> orgUsers = userRepository.selectList(new LambdaQueryWrapper<SysUserPO>()
|
|
|
+ .in(SysUserPO::getOrgId, ids));
|
|
|
+ if(CollectionUtil.isNotEmpty(orgUsers)){
|
|
|
+ //部门用户
|
|
|
+ Map<String, SysOrgPO> orgMap = orgs.stream().collect(Collectors.groupingBy(SysOrgPO::getId, Collectors.collectingAndThen(Collectors.toList(), CollectionUtil::getFirst)));
|
|
|
+ Set<String> userOrgIds = orgUsers.stream().map(SysUserPO::getOrgId).collect(Collectors.toSet());
|
|
|
+ Set<String> existName = userOrgIds.stream()
|
|
|
+ .map(orgMap::get)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .map(SysOrgPO::getName)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,String.format("部门 %s 下存在用户,不可删除", JsonUtils.toJsonString(existName)));
|
|
|
+
|
|
|
+ }
|
|
|
+ return orgRepository.deleteBatchIds(ids);
|
|
|
+ }
|
|
|
+}
|