mitao
2025-04-02 2f3d3fb97bd4ebdc00c40a2774465c8b3487b6d1
medicalWaste-system/src/main/java/com/sinata/system/service/impl/SysDepartmentServiceImpl.java
@@ -1,18 +1,28 @@
package com.sinata.system.service.impl;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinata.common.constant.CacheConstants;
import com.sinata.common.core.domain.entity.SysUser;
import com.sinata.common.entity.PageDTO;
import com.sinata.common.exception.ServiceException;
import com.sinata.common.utils.BeanUtils;
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.MwContract;
import com.sinata.system.domain.MwMicroEquipment;
import com.sinata.system.domain.MwProtectionEquipment;
import com.sinata.system.domain.MwProtectionRegulation;
import com.sinata.system.domain.MwProtectionTask;
import com.sinata.system.domain.MwStagingRoom;
import com.sinata.system.domain.MwTransitCar;
import com.sinata.system.domain.MwTransitRoute;
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;
@@ -24,6 +34,16 @@
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.MwApplicationService;
import com.sinata.system.service.MwContractService;
import com.sinata.system.service.MwMicroEquipmentService;
import com.sinata.system.service.MwProtectionEquipmentService;
import com.sinata.system.service.MwProtectionRegulationService;
import com.sinata.system.service.MwProtectionTaskService;
import com.sinata.system.service.MwStagingRoomService;
import com.sinata.system.service.MwTransitCarService;
import com.sinata.system.service.MwTransitRouteService;
import com.sinata.system.service.SysDepartmentInfoService;
import com.sinata.system.service.SysDepartmentService;
import com.sinata.system.service.SysUserDepartmentService;
@@ -58,6 +78,17 @@
    private final SysUserDepartmentService sysUserDepartmentService;
    private final RedisTemplate<Object, Object> redisTemplate;
    private final SysDepartmentInfoService sysDepartmentInfoService;
    private final ISysUserService sysUserService;
    private final MwApplicationService mwApplicationService;
    private final MwContractService mwContractService;
    private final MwMicroEquipmentService mwMicroEquipmentService;
    private final MwProtectionEquipmentService mwProtectionEquipmentService;
    private final MwProtectionTaskService mwProtectionTaskService;
    private final MwProtectionRegulationService mwProtectionRegulationService;
    private final MwStagingRoomService mwStagingRoomService;
    private final MwTransitCarService mwTransitCarService;
    private final MwTransitRouteService mwTransitRouteService;
    /**
     * 获取区域树
     * @return
@@ -86,12 +117,52 @@
            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;
            case 6:
                childrenMap = getChildrenDepartmentByOrgType(myDepartment, Arrays.asList(DepartmentEnum.REGION.getCode(), DepartmentEnum.MEDICAL_INSTITUTION.getCode(), DepartmentEnum.REGULATORY_UNIT.getCode()));
                break;
            case 7:
                childrenMap = getChildrenDepartmentByOrgType(myDepartment, Arrays.asList(DepartmentEnum.REGION.getCode(), DepartmentEnum.DISPOSAL_UNIT.getCode(), DepartmentEnum.REGULATORY_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();
            }
        }
    }
    /**
@@ -102,14 +173,26 @@
    @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()
                .ne(SysDepartment::getOrgType , 4)
                .ne(SysDepartment::getOrgType , 3)
                .ne(SysDepartment::getOrgType , 2)
                .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();
@@ -132,6 +215,80 @@
        }
        Map<Long, List<SysDepartment>> childrenMap = getChildrenDepartmentByOrgType(currentDepartment, Collections.singletonList(DepartmentEnum.REGION.getCode()));
        SysDepartmentVO sysDepartmentVO = fillChildrenTreeModel(currentDepartment, childrenMap);
        root.add(sysDepartmentVO);
        if (StringUtils.isNotBlank(keyword)) {
            treeMatch(root, keyword);
        }
        return root;
    }
    @Override
    public List<SysDepartmentVO> getRegionTree1(String keyword) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        List<SysDepartmentVO> root = new ArrayList<>();
        SysDepartment sysDepartment = this.baseMapper.selectById(sysUser.getDepartmentId());
        SysDepartment currentDepartment = this.baseMapper.selectById(sysDepartment.getParentId());
        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()));
        SysDepartmentVO sysDepartmentVO = fillChildrenTreeModel(currentDepartment, childrenMap);
        root.add(sysDepartmentVO);
        if (StringUtils.isNotBlank(keyword)) {
            treeMatch(root, keyword);
        }
        return root;
    }
    public List<SysDepartmentVO> getRegionTree2(String keyword) {
//        SysDepartment currentDepartment = getMyDepartment();
        List<SysDepartmentVO> root = new ArrayList<>();
        SysDepartment currentDepartment = getMyDepartment();
        if (Objects.isNull(currentDepartment)) {
            return root;
        }
//        if (!currentDepartment.getOrgType().equals(DepartmentEnum.REGION.getCode())) {
//            return root;
//        }
        Map<Long, List<SysDepartment>> childrenMap = getChildrenDepartmentMap(currentDepartment);
        SysDepartmentVO sysDepartmentVO = fillChildrenTreeModel(currentDepartment, childrenMap);
        Integer orgType = sysDepartmentVO.getOrgType();
        if (sysDepartmentVO.getOrgType()==4){
            SysDepartment byId = this.getById(sysDepartmentVO.getParentId());
            childrenMap = getChildrenDepartmentMap(byId);
            sysDepartmentVO = fillChildrenTreeModel(byId, childrenMap);
//            sysDepartmentVO.setChildren(new ArrayList<>());
        }
        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);
@@ -223,7 +380,7 @@
        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
        //获取部门树编码
        department.setOrgType(DepartmentEnum.REGION.getCode());
        department.setTreeCode(getTreeCode(dto.getParentId()));
        department.setTreeCode(generateTreeCode(dto.getParentId()));
        department.setOrgCode(getOrgCode(dto.getParentId(), DepartmentEnum.REGION.getCode()));
        save(department);
    }
@@ -258,7 +415,7 @@
        SysDepartment sysDepartment = getById(dto.getId());
        if (!dto.getParentId().equals(sysDepartment.getParentId())) {
            //获取部门树编码
            department.setTreeCode(getTreeCode(dto.getParentId()));
            department.setTreeCode(generateTreeCode(dto.getParentId()));
        }
        updateById(department);
    }
@@ -270,7 +427,7 @@
     * @return
     */
    @Override
    public String getTreeCode(Long parentId) {
    public String generateTreeCode(Long parentId) {
        String treeId;
        String preTreeCode = "";
@@ -348,9 +505,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);
    }
@@ -363,15 +520,22 @@
     */
    @Override
    public PageDTO<MedicalInstitutionVO> pageMedicalList(DepartmentQuery query) {
        String treeCode = "";
        String treeCode;
        if (Objects.isNull(query.getDepartmentId())) {
            SysDepartment myDepartment = getMyDepartment();
            if (Objects.isNull(myDepartment)) {
                return PageDTO.empty(0L, 0L);
            SysDepartment department = getMyDepartment();
            treeCode = department.getTreeCode();
        } else {
            SysDepartment department = getById(query.getDepartmentId());
            //如果是处置单位,则获取父级部门
            if (department.getOrgType().equals(DepartmentEnum.DISPOSAL_UNIT.getCode())) {
                department = getDepartmentByParentId(department.getParentId());
            }
            treeCode = myDepartment.getTreeCode();
            treeCode = department.getTreeCode();
        }
        Page<MedicalInstitutionVO> page = baseMapper.pageMedicalList(new Page<>(query.getPageCurr(), query.getPageSize()), query.getDepartmentId(), query.getDepartmentName(), query.getContactPerson(), query.getContactPhone(), treeCode);
        if (StringUtils.isBlank(treeCode)) {
            return PageDTO.empty(0L, 0L);
        }
        Page<MedicalInstitutionVO> page = baseMapper.pageMedicalList(new Page<>(query.getPageCurr(), query.getPageSize()), query.getDepartmentName(), query.getContactPerson(), query.getContactPhone(), treeCode);
        return PageDTO.of(page);
    }
@@ -401,7 +565,7 @@
            throw new ServiceException("医疗机构已存在");
        }
        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
        department.setTreeCode(getTreeCode(parent.getId()));
        department.setTreeCode(generateTreeCode(parent.getId()));
        department.setOrgCode(getOrgCode(parent.getId(), DepartmentEnum.MEDICAL_INSTITUTION.getCode()));
        department.setOrgType(DepartmentEnum.MEDICAL_INSTITUTION.getCode());
        //查询父级完整区域
@@ -413,6 +577,12 @@
        sysDepartmentInfoService.save(sysDepartmentInfo);
    }
    /**
     * 根据父级区域id查询处置单位列表
     *
     * @param id
     * @return
     */
    @Override
    public List<DisposalUnitVO> getDisposalUnitListByParentId(Long id) {
        List<DisposalUnitVO> disposalUnitList = null;
@@ -425,12 +595,13 @@
    }
    /**
     * 获取完成区域
     * 获取完整区域
     *
     * @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)) {
@@ -475,7 +646,7 @@
        }
        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
        if (!department.getParentId().equals(sysDepartment.getParentId())) {
            department.setTreeCode(getTreeCode(parent.getId()));
            department.setTreeCode(generateTreeCode(parent.getId()));
            //查询父级完整区域
            String region = getRegionName(parent);
            department.setRegion(region);
@@ -486,6 +657,8 @@
        sysDepartmentInfo.setDepartmentId(department.getId());
        sysDepartmentInfo.setId(null);
        sysDepartmentInfoService.save(sysDepartmentInfo);
        //处理层级关系
        handleRelation(sysDepartment, parent, dto.getRelation());
    }
    /**
@@ -509,9 +682,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);
    }
@@ -524,13 +697,9 @@
     */
    @Override
    public PageDTO<DisposalUnitVO> pageDisposalUnitList(DepartmentQuery query) {
        String treeCode = "";
        if (Objects.isNull(query.getDepartmentId())) {
            SysDepartment myDepartment = getMyDepartment();
            if (Objects.isNull(myDepartment)) {
                return PageDTO.empty(0L, 0L);
            }
            treeCode = myDepartment.getTreeCode();
        String treeCode = getTreeCodeByDepartmentId(query.getDepartmentId());
        if (StringUtils.isBlank(treeCode)) {
            return PageDTO.empty(0L, 0L);
        }
        Page<DisposalUnitVO> page = baseMapper.pageRegulatoryUnitList(new Page<>(query.getPageCurr(), query.getPageSize()), query.getDepartmentId(), query.getDepartmentName(), query.getContactPerson(), query.getContactPhone(), treeCode);
        return PageDTO.of(page);
@@ -562,7 +731,7 @@
            throw new ServiceException("处置单位已存在");
        }
        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
        department.setTreeCode(getTreeCode(parent.getId()));
        department.setTreeCode(generateTreeCode(parent.getId()));
        department.setOrgCode(getOrgCode(parent.getId(), DepartmentEnum.DISPOSAL_UNIT.getCode()));
        department.setOrgType(DepartmentEnum.DISPOSAL_UNIT.getCode());
        //查询父级完整区域
@@ -609,7 +778,7 @@
        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
        if (!department.getParentId().equals(sysDepartment.getParentId())) {
            department.setTreeCode(getTreeCode(parent.getId()));
            department.setTreeCode(generateTreeCode(parent.getId()));
            //查询父级完整区域
            String region = getRegionName(parent);
            department.setRegion(region);
@@ -620,6 +789,26 @@
        sysDepartmentInfo.setId(null);
        sysDepartmentInfo.setDepartmentId(department.getId());
        sysDepartmentInfoService.save(sysDepartmentInfo);
        //如果修改了所属区域
        handleRelation(sysDepartment, parent, dto.getRelation());
    }
    private void handleRelation(SysDepartment sysDepartment, SysDepartment parent, String relation) {
        if (!sysDepartment.getParentId().equals(parent.getId())) {
            List<Long> relationList = Arrays.stream(relation.split(",")).map(Long::valueOf).collect(Collectors.toList());
            relationList.add(sysDepartment.getId());
            relation = JSONArray.toJSONString(relationList);
            //同步该单位所属器具的层级
            mwApplicationService.lambdaUpdate().set(MwApplication::getRelation, relation).eq(MwApplication::getDepartmentId, sysDepartment.getId()).update();
            mwContractService.lambdaUpdate().set(MwContract::getRelation, relation).eq(MwContract::getDepartmentId, sysDepartment.getId()).update();
            mwMicroEquipmentService.lambdaUpdate().set(MwMicroEquipment::getRelation, relation).eq(MwMicroEquipment::getDepartmentId, sysDepartment.getId()).update();
            mwProtectionEquipmentService.lambdaUpdate().set(MwProtectionEquipment::getRelation, relation).eq(MwProtectionEquipment::getDepartmentId, sysDepartment.getId()).update();
            mwProtectionTaskService.lambdaUpdate().set(MwProtectionTask::getRelation, relation).eq(MwProtectionTask::getDepartmentId, sysDepartment.getId()).update();
            mwProtectionRegulationService.lambdaUpdate().set(MwProtectionRegulation::getRelation, relation).eq(MwProtectionRegulation::getDepartmentId, sysDepartment.getId()).update();
            mwStagingRoomService.lambdaUpdate().set(MwStagingRoom::getRelation, relation).eq(MwStagingRoom::getDepartmentId, sysDepartment.getId()).update();
            mwTransitCarService.lambdaUpdate().set(MwTransitCar::getRelation, relation).eq(MwTransitCar::getDepartmentId, sysDepartment.getId()).update();
            mwTransitRouteService.lambdaUpdate().set(MwTransitRoute::getRelation, relation).eq(MwTransitRoute::getDepartmentId, sysDepartment.getId()).update();
        }
    }
    /**
@@ -635,9 +824,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);
    }
@@ -650,19 +839,17 @@
     */
    @Override
    public PageDTO<RegulatoryUnitVO> pageRegulatoryUnitList(DepartmentQuery query) {
        String treeCode = "";
        if (Objects.isNull(query.getDepartmentId())) {
            SysDepartment myDepartment = getMyDepartment();
            if (Objects.isNull(myDepartment)) {
                return PageDTO.empty(0L, 0L);
            }
            treeCode = myDepartment.getTreeCode();
        String treeCode = getTreeCodeByDepartmentId(query.getDepartmentId());
        if (StringUtils.isBlank(treeCode)) {
            return PageDTO.empty(0L, 0L);
        }
        Page<SysDepartment> page = this.lambdaQuery().eq(Objects.nonNull(query.getDepartmentId()), SysDepartment::getParentId, query.getDepartmentId())
                .likeRight(StringUtils.isNotBlank(treeCode), SysDepartment::getTreeCode, treeCode)
                .like(StringUtils.isNotEmpty(query.getDepartmentName()), SysDepartment::getDepartmentName, query.getDepartmentName())
                .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);
    }
@@ -692,7 +879,7 @@
            throw new ServiceException("监管单位已存在");
        }
        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
        department.setTreeCode(getTreeCode(parent.getId()));
        department.setTreeCode(generateTreeCode(parent.getId()));
        department.setOrgCode(getOrgCode(parent.getId(), DepartmentEnum.REGULATORY_UNIT.getCode()));
        department.setOrgType(DepartmentEnum.REGULATORY_UNIT.getCode());
        //查询父级完整区域
@@ -736,12 +923,13 @@
        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
        if (!department.getParentId().equals(sysDepartment.getParentId())) {
            department.setTreeCode(getTreeCode(parent.getId()));
            department.setTreeCode(generateTreeCode(parent.getId()));
            //查询父级完整区域
            String region = getRegionName(parent);
            department.setRegion(region);
        }
        updateById(department);
        handleRelation(sysDepartment, parent, dto.getRelation());
    }
    /**
@@ -766,10 +954,65 @@
     */
    @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);
    }
    /**
     * 根据部门id获取树编码,如果为空则获取当前登录用户所在区域树编码
     *
     * @param departmentId
     * @return
     */
    @Override
    public String getTreeCodeByDepartmentId(Long departmentId) {
        SysDepartment department;
        if (Objects.isNull(departmentId)) {
            department = getMyDepartment();
        } else {
            department = getById(departmentId);
        }
        if (Objects.nonNull(department)) {
            return department.getTreeCode();
        }
        return null;
    }
    /**
     * 路线关联医院列表
     *
     * @param id
     * @return
     */
    @Override
    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);
    }
}