mitao
2025-03-27 b22f118eabab8f2f27dbd73e7c6cb1090cd82c82
medicalWaste-system/src/main/java/com/sinata/system/service/impl/SysDepartmentServiceImpl.java
@@ -1,7 +1,6 @@
package com.sinata.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinata.common.constant.CacheConstants;
@@ -12,9 +11,9 @@
import com.sinata.common.utils.CollUtils;
import com.sinata.common.utils.SecurityUtils;
import com.sinata.common.utils.StringUtils;
import com.sinata.system.domain.MwApplication;
import com.sinata.system.domain.SysDepartment;
import com.sinata.system.domain.SysDepartmentInfo;
import com.sinata.system.domain.SysUserDepartment;
import com.sinata.system.domain.dto.DisposalUnitDTO;
import com.sinata.system.domain.dto.MedicalInstitutionDTO;
import com.sinata.system.domain.dto.RegulatoryUnitDTO;
@@ -26,6 +25,7 @@
import com.sinata.system.domain.vo.SysDepartmentVO;
import com.sinata.system.enums.DepartmentEnum;
import com.sinata.system.mapper.SysDepartmentMapper;
import com.sinata.system.service.ISysUserService;
import com.sinata.system.service.SysDepartmentInfoService;
import com.sinata.system.service.SysDepartmentService;
import com.sinata.system.service.SysUserDepartmentService;
@@ -60,6 +60,7 @@
    private final SysUserDepartmentService sysUserDepartmentService;
    private final RedisTemplate<Object, Object> redisTemplate;
    private final SysDepartmentInfoService sysDepartmentInfoService;
    private final ISysUserService sysUserService;
    /**
     * 获取区域树
     * @return
@@ -88,12 +89,46 @@
            case 4:
                childrenMap = getChildrenDepartmentByOrgType(myDepartment, Arrays.asList(DepartmentEnum.REGION.getCode(), DepartmentEnum.REGULATORY_UNIT.getCode()));
                break;
            case 5:
                childrenMap = getChildrenDepartmentByOrgType(myDepartment, Arrays.asList(DepartmentEnum.REGION.getCode(), DepartmentEnum.MEDICAL_INSTITUTION.getCode(), DepartmentEnum.DISPOSAL_UNIT.getCode()));
                break;
            default:
                childrenMap = getChildrenDepartmentByOrgType(myDepartment, null);
        }
        SysDepartmentVO sysDepartmentVO = fillChildrenTreeModel(myDepartment, childrenMap);
        switch (type) {
            case 2:
            case 3:
            case 4:
                filterEmptyChildren(sysDepartmentVO.getChildren());
                break;
        }
        root.add(sysDepartmentVO);
        return root;
    }
    /**
     * 移除子节点为空的区域
     *
     * @param departments
     */
    private void filterEmptyChildren(List<SysDepartmentVO> departments) {
        if (departments == null) {
            return;
        }
        Iterator<SysDepartmentVO> iterator = departments.iterator();
        while (iterator.hasNext()) {
            SysDepartmentVO department = iterator.next();
            // 递归过滤子节点
            filterEmptyChildren(department.getChildren());
            // 如果 orgType == 1 且 children 为空,则移除当前节点
            if (department.getOrgType() == 1 && department.getChildren().isEmpty()) {
                iterator.remove();
            }
        }
    }
    /**
@@ -104,14 +139,23 @@
    @NotNull
    private Map<Long, List<SysDepartment>> getChildrenDepartmentByOrgType(SysDepartment myDepartment, List<Integer> orgTypes) {
        List<SysDepartment> sysDepartmentList = this.lambdaQuery()
                .likeRight(SysDepartment::getTreeCode, myDepartment.getTreeCode())
                .in(CollUtils.isNotEmpty(orgTypes), SysDepartment::getOrgType, orgTypes)
                .likeRight(SysDepartment::getTreeCode, myDepartment.getTreeCode())
                .orderByDesc(SysDepartment::getCreateTime)
                .list();
        return sysDepartmentList.stream()
                .collect(Collectors.groupingBy(SysDepartment::getParentId));
    }
    @NotNull
    private Map<Long, List<SysDepartment>> getChildrenDepartmentMap(SysDepartment myDepartment) {
        List<SysDepartment> sysDepartmentList = this.lambdaQuery()
                .likeRight(SysDepartment::getTreeCode, myDepartment.getTreeCode())
                .orderByDesc(SysDepartment::getCreateTime)
                .list();
        return sysDepartmentList.stream()
                .collect(Collectors.groupingBy(SysDepartment::getParentId));
    }
    @Override
    public SysDepartment getDepartmentByParentId(Long parentId) {
        return this.lambdaQuery().eq(SysDepartment::getId, parentId).one();
@@ -163,16 +207,44 @@
    }
    public List<SysDepartmentVO> getRegionTree2(String keyword) {
//        SysDepartment currentDepartment = getMyDepartment();
        List<SysDepartmentVO> root = new ArrayList<>();
        SysDepartment currentDepartment = this.baseMapper.selectById(-1);
        SysDepartment currentDepartment = getMyDepartment();
        if (Objects.isNull(currentDepartment)) {
            return root;
        }
        if (!currentDepartment.getOrgType().equals(DepartmentEnum.REGION.getCode())) {
            return root;
        }
        Map<Long, List<SysDepartment>> childrenMap = getChildrenDepartmentByOrgType(currentDepartment, Collections.singletonList(DepartmentEnum.REGION.getCode()));
//        if (!currentDepartment.getOrgType().equals(DepartmentEnum.REGION.getCode())) {
//            return root;
//        }
        Map<Long, List<SysDepartment>> childrenMap = getChildrenDepartmentMap(currentDepartment);
        SysDepartmentVO sysDepartmentVO = fillChildrenTreeModel(currentDepartment, childrenMap);
        if (sysDepartmentVO.getTreeCode().length()==10){
            SysDepartment sysDepartment = this.baseMapper.selectById(sysDepartmentVO.getParentId());
            SysDepartmentVO sysDepartmentVO1 = new SysDepartmentVO();
            BeanUtils.copyProperties(sysDepartment,sysDepartmentVO1);
            List<SysDepartmentVO> root1 = new ArrayList<>();
            root1.add(sysDepartmentVO);
            sysDepartmentVO1.setChildren(root1);
            sysDepartmentVO = sysDepartmentVO1;
        }
        if (sysDepartmentVO.getTreeCode().length()>=14){
            SysDepartment sysDepartment = this.baseMapper.selectById(sysDepartmentVO.getParentId());
            SysDepartmentVO sysDepartmentVO1 = new SysDepartmentVO();
            BeanUtils.copyProperties(sysDepartment,sysDepartmentVO1);
            List<SysDepartmentVO> root1 = new ArrayList<>();
            root1.add(sysDepartmentVO);
            sysDepartmentVO1.setChildren(root1);
            SysDepartment sysDepartment2 = this.baseMapper.selectById(sysDepartment.getParentId());
            SysDepartmentVO sysDepartmentVO2 = new SysDepartmentVO();
            BeanUtils.copyProperties(sysDepartment2,sysDepartmentVO2);
            List<SysDepartmentVO> root2 = new ArrayList<>();
            root2.add(sysDepartmentVO1);
            sysDepartmentVO2.setChildren(root2);
            sysDepartmentVO = sysDepartmentVO2;
        }
        root.add(sysDepartmentVO);
        if (StringUtils.isNotBlank(keyword)) {
            treeMatch(root, keyword);
@@ -389,9 +461,9 @@
                .orderByDesc(SysDepartment::getCreateTime)
                .list();
        List<Long> departmentIds = sysDepartmentList.stream().map(SysDepartment::getId).collect(Collectors.toList());
        Long count = sysUserDepartmentService.lambdaQuery().in(SysUserDepartment::getDepartmentId, departmentIds).count();
        Long count = sysUserService.lambdaQuery().in(SysUser::getDepartmentId, departmentIds).count();
        if (count > 0) {
            throw new ServiceException("该区域已存在用户,无法删除");
            throw new ServiceException("该区域已关联用户,无法删除");
        }
        removeById(id);
    }
@@ -484,7 +556,8 @@
     * @param department
     * @return
     */
    private String getRegionName(SysDepartment department) {
    @Override
    public String getRegionName(SysDepartment department) {
        String region = department.getDepartmentName();
        SysDepartment sysDepartment = this.lambdaQuery().eq(SysDepartment::getId, department.getParentId()).ne(SysDepartment::getId, -1).one();
        if (Objects.nonNull(sysDepartment)) {
@@ -563,9 +636,9 @@
     */
    @Override
    public void deleteMedical(Long id) {
        Long count = sysUserDepartmentService.lambdaQuery().eq(SysUserDepartment::getDepartmentId, id).count();
        Long count = sysUserService.lambdaQuery().eq(SysUser::getDepartmentId, id).count();
        if (count > 0) {
            throw new ServiceException("该医疗机构已存在用户,无法删除");
            throw new ServiceException("该医疗机构已关联用户,无法删除");
        }
        removeById(id);
    }
@@ -685,9 +758,9 @@
    @Override
    public void deleteDisposalUnit(Long id) {
        Long count = sysUserDepartmentService.lambdaQuery().eq(SysUserDepartment::getDepartmentId, id).count();
        Long count = sysUserService.lambdaQuery().eq(SysUser::getDepartmentId, id).count();
        if (count > 0) {
            throw new ServiceException("该处置单位构已存在用户,无法删除");
            throw new ServiceException("该处置单位构已关联用户,无法删除");
        }
        removeById(id);
    }
@@ -710,6 +783,7 @@
                .like(StringUtils.isNotBlank(query.getContactPerson()), SysDepartment::getContactPerson, query.getContactPerson())
                .like(StringUtils.isNotBlank(query.getContactPhone()), SysDepartment::getContactPhone, query.getContactPhone())
                .eq(SysDepartment::getOrgType, DepartmentEnum.REGULATORY_UNIT.getCode())
                .orderByDesc(SysDepartment::getCreateTime)
                .page(new Page<>(query.getPageCurr(), query.getPageSize()));
        return PageDTO.of(page, RegulatoryUnitVO.class);
    }
@@ -813,9 +887,9 @@
     */
    @Override
    public void deleteRegulatoryUnit(Long id) {
        Long count = sysUserDepartmentService.lambdaQuery().eq(SysUserDepartment::getDepartmentId, id).count();
        Long count = sysUserService.lambdaQuery().eq(SysUser::getDepartmentId, id).count();
        if (count > 0) {
            throw new ServiceException("该监管单位构已存在用户,无法删除");
            throw new ServiceException("该监管单位构已关联用户,无法删除");
        }
        removeById(id);
    }
@@ -850,4 +924,28 @@
    public List<MedicalInstitutionVO> getHospitalListByRouteId(Long id) {
        return baseMapper.getHospitalListByRouteId(id);
    }
    /**
     * 创建机构
     *
     * @param mwApplication
     */
    @Override
    public void createDepartment(MwApplication mwApplication) {
        SysDepartment parent = this.getById(mwApplication.getDepartmentId());
        if (Objects.isNull(parent)) {
            throw new ServiceException("找不到对应父级组织");
        }
        SysDepartment department = new SysDepartment();
        department.setParentId(mwApplication.getDepartmentId());
        department.setDepartmentName(mwApplication.getUnitName());
        department.setContactPerson(mwApplication.getConcat());
        department.setContactPhone(mwApplication.getPhone());
        department.setOrgType(mwApplication.getUnitType().equals(1) ? DepartmentEnum.MEDICAL_INSTITUTION.getCode() : DepartmentEnum.DISPOSAL_UNIT.getCode());
        department.setRegion(mwApplication.getRegion());
        department.setRelation(mwApplication.getRelation());
        department.setTreeCode(generateTreeCode(parent.getId()));
        department.setOrgCode(getOrgCode(parent.getId(), department.getOrgType()));
        save(department);
    }
}