优化机构逻辑

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

View File

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