优化机构逻辑

This commit is contained in:
阿沐 2023-10-06 15:44:12 +08:00
parent ef21f030b8
commit 7c01b8b0db
2 changed files with 69 additions and 69 deletions

View File

@ -4,7 +4,6 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import net.maku.framework.common.constant.Constant;
import net.maku.framework.common.utils.Result; import net.maku.framework.common.utils.Result;
import net.maku.framework.operatelog.annotations.OperateLog; import net.maku.framework.operatelog.annotations.OperateLog;
import net.maku.framework.operatelog.enums.OperateTypeEnum; import net.maku.framework.operatelog.enums.OperateTypeEnum;
@ -47,7 +46,7 @@ public class SysOrgController {
SysOrgVO vo = SysOrgConvert.INSTANCE.convert(entity); SysOrgVO vo = SysOrgConvert.INSTANCE.convert(entity);
// 获取上级机构名称 // 获取上级机构名称
if (!Constant.ROOT.equals(entity.getPid())) { if (entity.getPid() != null) {
SysOrgEntity parentEntity = sysOrgService.getById(entity.getPid()); SysOrgEntity parentEntity = sysOrgService.getById(entity.getPid());
vo.setParentName(parentEntity.getName()); vo.setParentName(parentEntity.getName());
} }

View File

@ -1,11 +1,12 @@
package net.maku.system.service.impl; package net.maku.system.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import net.maku.framework.common.constant.Constant; import net.maku.framework.common.constant.Constant;
import net.maku.framework.common.exception.ServerException; import net.maku.framework.common.exception.ServerException;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.framework.common.utils.TreeUtils; import net.maku.framework.common.utils.TreeUtils;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.system.convert.SysOrgConvert; import net.maku.system.convert.SysOrgConvert;
import net.maku.system.dao.SysOrgDao; import net.maku.system.dao.SysOrgDao;
import net.maku.system.dao.SysUserDao; import net.maku.system.dao.SysUserDao;
@ -30,89 +31,89 @@ import java.util.Map;
@Service @Service
@AllArgsConstructor @AllArgsConstructor
public class SysOrgServiceImpl extends BaseServiceImpl<SysOrgDao, SysOrgEntity> implements SysOrgService { public class SysOrgServiceImpl extends BaseServiceImpl<SysOrgDao, SysOrgEntity> implements SysOrgService {
private final SysUserDao sysUserDao; private final SysUserDao sysUserDao;
@Override @Override
public List<SysOrgVO> getList() { public List<SysOrgVO> getList() {
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
// 数据权限 // 数据权限
params.put(Constant.DATA_SCOPE, getDataScope("t1", "id")); params.put(Constant.DATA_SCOPE, getDataScope("t1", "id"));
// 机构列表 // 机构列表
List<SysOrgEntity> entityList = baseMapper.getList(params); List<SysOrgEntity> entityList = baseMapper.getList(params);
return TreeUtils.build(SysOrgConvert.INSTANCE.convertList(entityList)); return TreeUtils.build(SysOrgConvert.INSTANCE.convertList(entityList));
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void save(SysOrgVO vo) { public void save(SysOrgVO vo) {
SysOrgEntity entity = SysOrgConvert.INSTANCE.convert(vo); SysOrgEntity entity = SysOrgConvert.INSTANCE.convert(vo);
baseMapper.insert(entity); baseMapper.insert(entity);
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void update(SysOrgVO vo) { public void update(SysOrgVO vo) {
SysOrgEntity entity = SysOrgConvert.INSTANCE.convert(vo); SysOrgEntity entity = SysOrgConvert.INSTANCE.convert(vo);
// 上级机构不能为自身 // 上级机构不能为自身
if(entity.getId().equals(entity.getPid())){ if (entity.getId().equals(entity.getPid())) {
throw new ServerException("上级机构不能为自身"); throw new ServerException("上级机构不能为自身");
} }
// 上级机构不能为下级 // 上级机构不能为下级
List<Long> subOrgList = getSubOrgIdList(entity.getId()); List<Long> subOrgList = getSubOrgIdList(entity.getId());
if(subOrgList.contains(entity.getPid())){ if (subOrgList.contains(entity.getPid())) {
throw new ServerException("上级机构不能为下级"); throw new ServerException("上级机构不能为下级");
} }
updateById(entity); updateById(entity);
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void delete(Long id) { public void delete(Long id) {
// 判断是否有子机构 // 判断是否有子机构
long orgCount = count(new QueryWrapper<SysOrgEntity>().eq("pid", id)); long orgCount = count(new QueryWrapper<SysOrgEntity>().eq("pid", id));
if(orgCount > 0){ if (orgCount > 0) {
throw new ServerException("请先删除子机构"); throw new ServerException("请先删除子机构");
} }
// 判断机构下面是否有用户 // 判断机构下面是否有用户
long userCount = sysUserDao.selectCount(new QueryWrapper<SysUserEntity>().eq("org_id", id)); long userCount = sysUserDao.selectCount(new QueryWrapper<SysUserEntity>().eq("org_id", id));
if(userCount > 0){ if (userCount > 0) {
throw new ServerException("机构下面有用户,不能删除"); throw new ServerException("机构下面有用户,不能删除");
} }
// 删除 // 删除
removeById(id); removeById(id);
} }
@Override @Override
public List<Long> getSubOrgIdList(Long id) { public List<Long> getSubOrgIdList(Long id) {
// 所有机构的idpid列表 // 所有机构的idpid列表
List<SysOrgEntity> orgList = baseMapper.getIdAndPidList(); List<SysOrgEntity> orgList = baseMapper.getIdAndPidList();
// 递归查询所有子机构ID列表 // 递归查询所有子机构ID列表
List<Long> subIdList = new ArrayList<>(); List<Long> subIdList = new ArrayList<>();
getTree(id, orgList, subIdList); getTree(id, orgList, subIdList);
// 本机构也添加进去 // 本机构也添加进去
subIdList.add(id); subIdList.add(id);
return subIdList; return subIdList;
} }
private void getTree(Long id, List<SysOrgEntity> orgList, List<Long> subIdList) { private void getTree(Long id, List<SysOrgEntity> orgList, List<Long> subIdList) {
for(SysOrgEntity org : orgList){ for (SysOrgEntity org : orgList) {
if (org.getPid().equals(id)){ if (ObjectUtil.equals(org.getPid(), id)) {
getTree(org.getId(), orgList, subIdList); getTree(org.getId(), orgList, subIdList);
subIdList.add(org.getId()); subIdList.add(org.getId());
} }
} }
} }
} }