From ae7f04be9321ddbe17c46fae8ab05d34e7493f9f Mon Sep 17 00:00:00 2001
From: mitao <2763622819@qq.com>
Date: 星期三, 19 二月 2025 13:40:06 +0800
Subject: [PATCH] 管理后台bug修改

---
 medicalWaste-system/src/main/java/com/sinata/system/service/impl/SysDepartmentServiceImpl.java |  581 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 574 insertions(+), 7 deletions(-)

diff --git a/medicalWaste-system/src/main/java/com/sinata/system/service/impl/SysDepartmentServiceImpl.java b/medicalWaste-system/src/main/java/com/sinata/system/service/impl/SysDepartmentServiceImpl.java
index 9ad50a5..3ce68a4 100644
--- a/medicalWaste-system/src/main/java/com/sinata/system/service/impl/SysDepartmentServiceImpl.java
+++ b/medicalWaste-system/src/main/java/com/sinata/system/service/impl/SysDepartmentServiceImpl.java
@@ -1,25 +1,40 @@
 package com.sinata.system.service.impl;
 
 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.SysDepartment;
-import com.sinata.system.domain.SysUserDepartment;
+import com.sinata.system.domain.SysDepartmentInfo;
+import com.sinata.system.domain.dto.DisposalUnitDTO;
+import com.sinata.system.domain.dto.MedicalInstitutionDTO;
+import com.sinata.system.domain.dto.RegulatoryUnitDTO;
 import com.sinata.system.domain.dto.SysDepartmentDTO;
+import com.sinata.system.domain.query.DepartmentQuery;
+import com.sinata.system.domain.vo.DisposalUnitVO;
+import com.sinata.system.domain.vo.MedicalInstitutionVO;
+import com.sinata.system.domain.vo.RegulatoryUnitVO;
 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;
 import lombok.RequiredArgsConstructor;
 import org.jetbrains.annotations.NotNull;
+import org.springframework.context.annotation.Lazy;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -40,10 +55,12 @@
  * @since 2024-12-02
  */
 @Service
-@RequiredArgsConstructor
+@RequiredArgsConstructor(onConstructor_ = {@Lazy})
 public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentMapper, SysDepartment> implements SysDepartmentService {
     private final SysUserDepartmentService sysUserDepartmentService;
     private final RedisTemplate<Object, Object> redisTemplate;
+    private final SysDepartmentInfoService sysDepartmentInfoService;
+    private final ISysUserService sysUserService;
     /**
      * 获取区域树
      * @return
@@ -76,8 +93,39 @@
                 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();
+            }
+        }
     }
 
     /**
@@ -110,6 +158,45 @@
     public List<SysDepartmentVO> getRegionTree(String keyword) {
         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 = 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) {
+
+        List<SysDepartmentVO> root = new ArrayList<>();
+        SysDepartment currentDepartment = this.baseMapper.selectById(-1);
         if (Objects.isNull(currentDepartment)) {
             return root;
         }
@@ -209,7 +296,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);
     }
@@ -244,7 +331,7 @@
         SysDepartment sysDepartment = getById(dto.getId());
         if (!dto.getParentId().equals(sysDepartment.getParentId())) {
             //获取部门树编码
-            department.setTreeCode(getTreeCode(dto.getParentId()));
+            department.setTreeCode(generateTreeCode(dto.getParentId()));
         }
         updateById(department);
     }
@@ -256,7 +343,7 @@
      * @return
      */
     @Override
-    public String getTreeCode(Long parentId) {
+    public String generateTreeCode(Long parentId) {
 
         String treeId;
         String preTreeCode = "";
@@ -334,10 +421,490 @@
                 .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);
     }
+
+    /**
+     * 医疗机构分页列表
+     *
+     * @param query
+     * @return
+     */
+    @Override
+    public PageDTO<MedicalInstitutionVO> pageMedicalList(DepartmentQuery query) {
+        String treeCode;
+        if (Objects.isNull(query.getDepartmentId())) {
+            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 = department.getTreeCode();
+        }
+        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);
+    }
+
+    /**
+     * 新增医疗机构
+     *
+     * @param dto
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void addMedical(MedicalInstitutionDTO dto) {
+        SysDepartment currentDepartment = getMyDepartment();
+        if (Objects.isNull(currentDepartment)) {
+            throw new ServiceException("无操作权限");
+        }
+        SysDepartment parent = this.getById(dto.getParentId());
+        if (Objects.isNull(parent)) {
+            throw new ServiceException("找不到对应父级组织");
+        }
+        if (!parent.getTreeCode().startsWith(currentDepartment.getTreeCode())) {
+            throw new ServiceException("无操作权限");
+        }
+        Long count = this.lambdaQuery().eq(SysDepartment::getDepartmentName, dto.getDepartmentName())
+                .eq(SysDepartment::getOrgType, DepartmentEnum.MEDICAL_INSTITUTION.getCode())
+                .count();
+        if (count > 0) {
+            throw new ServiceException("医疗机构已存在");
+        }
+        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
+        department.setTreeCode(generateTreeCode(parent.getId()));
+        department.setOrgCode(getOrgCode(parent.getId(), DepartmentEnum.MEDICAL_INSTITUTION.getCode()));
+        department.setOrgType(DepartmentEnum.MEDICAL_INSTITUTION.getCode());
+        //查询父级完整区域
+        String region = getRegionName(parent);
+        department.setRegion(region);
+        save(department);
+        SysDepartmentInfo sysDepartmentInfo = BeanUtils.copyBean(dto, SysDepartmentInfo.class);
+        sysDepartmentInfo.setDepartmentId(department.getId());
+        sysDepartmentInfoService.save(sysDepartmentInfo);
+    }
+
+    /**
+     * 根据父级区域id查询处置单位列表
+     *
+     * @param id
+     * @return
+     */
+    @Override
+    public List<DisposalUnitVO> getDisposalUnitListByParentId(Long id) {
+        List<DisposalUnitVO> disposalUnitList = null;
+        SysDepartment parent = getById(id);
+        if (Objects.nonNull(parent)) {
+            //查询处置单位
+            disposalUnitList = baseMapper.getDisposalUnitListByTreeCode(parent.getTreeCode());
+        }
+        return disposalUnitList;
+    }
+
+    /**
+     * 获取完整区域
+     *
+     * @param department
+     * @return
+     */
+    @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)) {
+            region = getRegionName(sysDepartment) + region;
+        }
+        return region;
+    }
+
+    /**
+     * 编辑医疗机构
+     *
+     * @param dto
+     * @return
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void editMedical(MedicalInstitutionDTO dto) {
+        if (Objects.isNull(dto.getId())) {
+            throw new ServiceException("id不能为空");
+        }
+        SysDepartment sysDepartment = getById(dto.getId());
+        if (Objects.isNull(sysDepartment)) {
+            throw new ServiceException("医疗机构不存在");
+        }
+        SysDepartment currentDepartment = getMyDepartment();
+        if (Objects.isNull(currentDepartment)) {
+            throw new ServiceException("无操作权限");
+        }
+
+        SysDepartment parent = this.getById(dto.getParentId());
+        if (Objects.isNull(parent)) {
+            throw new ServiceException("找不到对应父级组织");
+        }
+        if (!parent.getTreeCode().startsWith(currentDepartment.getTreeCode())) {
+            throw new ServiceException("无操作权限");
+        }
+        Long count = this.lambdaQuery().eq(SysDepartment::getDepartmentName, dto.getDepartmentName())
+                .eq(SysDepartment::getOrgType, DepartmentEnum.MEDICAL_INSTITUTION.getCode()).ne(SysDepartment::getId, dto.getId())
+                .count();
+        if (count > 0) {
+            throw new ServiceException("机构已存在");
+        }
+        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
+        if (!department.getParentId().equals(sysDepartment.getParentId())) {
+            department.setTreeCode(generateTreeCode(parent.getId()));
+            //查询父级完整区域
+            String region = getRegionName(parent);
+            department.setRegion(region);
+        }
+        updateById(department);
+        sysDepartmentInfoService.lambdaUpdate().eq(SysDepartmentInfo::getDepartmentId, sysDepartment.getId()).remove();
+        SysDepartmentInfo sysDepartmentInfo = BeanUtils.copyBean(dto, SysDepartmentInfo.class);
+        sysDepartmentInfo.setDepartmentId(department.getId());
+        sysDepartmentInfo.setId(null);
+        sysDepartmentInfoService.save(sysDepartmentInfo);
+    }
+
+    /**
+     * 医疗机构详情
+     *
+     * @param id
+     * @return
+     */
+    @Override
+    public MedicalInstitutionVO getMedicalDetailById(Long id) {
+        MedicalInstitutionVO vo = baseMapper.getMedicalDetailById(id);
+        List<DisposalUnitVO> disposalUnitList = getDisposalUnitListByParentId(vo.getParentId());
+        vo.setDisposalUnitList(disposalUnitList);
+        return vo;
+    }
+
+    /**
+     * 删除医疗机构
+     *
+     * @param id
+     */
+    @Override
+    public void deleteMedical(Long id) {
+        Long count = sysUserService.lambdaQuery().eq(SysUser::getDepartmentId, id).count();
+        if (count > 0) {
+            throw new ServiceException("该医疗机构已关联用户,无法删除");
+        }
+        removeById(id);
+    }
+
+    /**
+     * 处置单位分页列表
+     *
+     * @param query
+     * @return
+     */
+    @Override
+    public PageDTO<DisposalUnitVO> pageDisposalUnitList(DepartmentQuery query) {
+        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);
+    }
+
+    /**
+     * 新增处置单位
+     *
+     * @param dto
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void addDisposalUnit(DisposalUnitDTO dto) {
+        SysDepartment currentDepartment = getMyDepartment();
+        if (Objects.isNull(currentDepartment)) {
+            throw new ServiceException("无操作权限");
+        }
+        SysDepartment parent = this.getById(dto.getParentId());
+        if (Objects.isNull(parent)) {
+            throw new ServiceException("找不到对应父级组织");
+        }
+        if (!parent.getTreeCode().startsWith(currentDepartment.getTreeCode())) {
+            throw new ServiceException("无操作权限");
+        }
+        Long count = this.lambdaQuery().eq(SysDepartment::getDepartmentName, dto.getDepartmentName())
+                .eq(SysDepartment::getOrgType, DepartmentEnum.DISPOSAL_UNIT.getCode())
+                .count();
+        if (count > 0) {
+            throw new ServiceException("处置单位已存在");
+        }
+        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
+        department.setTreeCode(generateTreeCode(parent.getId()));
+        department.setOrgCode(getOrgCode(parent.getId(), DepartmentEnum.DISPOSAL_UNIT.getCode()));
+        department.setOrgType(DepartmentEnum.DISPOSAL_UNIT.getCode());
+        //查询父级完整区域
+        String region = getRegionName(parent);
+        department.setRegion(region);
+        save(department);
+        SysDepartmentInfo sysDepartmentInfo = BeanUtils.copyBean(dto, SysDepartmentInfo.class);
+        sysDepartmentInfo.setDepartmentId(department.getId());
+        sysDepartmentInfoService.save(sysDepartmentInfo);
+    }
+
+    /**
+     * 编辑处置单位
+     *
+     * @param dto
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void editDisposalUnit(DisposalUnitDTO dto) {
+        if (Objects.isNull(dto.getId())) {
+            throw new ServiceException("id不能为空");
+        }
+        SysDepartment sysDepartment = getById(dto.getId());
+        if (Objects.isNull(sysDepartment)) {
+            throw new ServiceException("处置单位不存在");
+        }
+        SysDepartment currentDepartment = getMyDepartment();
+        if (Objects.isNull(currentDepartment)) {
+            throw new ServiceException("无操作权限");
+        }
+        SysDepartment parent = this.getById(dto.getParentId());
+        if (Objects.isNull(parent)) {
+            throw new ServiceException("找不到对应父级组织");
+        }
+        if (!parent.getTreeCode().startsWith(currentDepartment.getTreeCode())) {
+            throw new ServiceException("无操作权限");
+        }
+        Long count = this.lambdaQuery().eq(SysDepartment::getDepartmentName, dto.getDepartmentName())
+                .eq(SysDepartment::getOrgType, DepartmentEnum.DISPOSAL_UNIT.getCode()).ne(SysDepartment::getId, dto.getId())
+                .count();
+        if (count > 0) {
+            throw new ServiceException("处置单位已存在");
+        }
+        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
+
+        if (!department.getParentId().equals(sysDepartment.getParentId())) {
+            department.setTreeCode(generateTreeCode(parent.getId()));
+            //查询父级完整区域
+            String region = getRegionName(parent);
+            department.setRegion(region);
+        }
+        updateById(department);
+        sysDepartmentInfoService.lambdaUpdate().eq(SysDepartmentInfo::getDepartmentId, sysDepartment.getId()).remove();
+        SysDepartmentInfo sysDepartmentInfo = BeanUtils.copyBean(dto, SysDepartmentInfo.class);
+        sysDepartmentInfo.setId(null);
+        sysDepartmentInfo.setDepartmentId(department.getId());
+        sysDepartmentInfoService.save(sysDepartmentInfo);
+    }
+
+    /**
+     * 处置单位详情
+     *
+     * @param id
+     * @return
+     */
+    @Override
+    public DisposalUnitVO getDisposalUnitDetailById(Long id) {
+        return baseMapper.getDisposalUnitDetailById(id);
+    }
+
+    @Override
+    public void deleteDisposalUnit(Long id) {
+        Long count = sysUserService.lambdaQuery().eq(SysUser::getDepartmentId, id).count();
+        if (count > 0) {
+            throw new ServiceException("该处置单位构已关联用户,无法删除");
+        }
+        removeById(id);
+    }
+
+    /**
+     * 监管单位分页列表
+     *
+     * @param query
+     * @return
+     */
+    @Override
+    public PageDTO<RegulatoryUnitVO> pageRegulatoryUnitList(DepartmentQuery query) {
+        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())
+                .page(new Page<>(query.getPageCurr(), query.getPageSize()));
+        return PageDTO.of(page, RegulatoryUnitVO.class);
+    }
+
+    /**
+     * 新增监管单位
+     *
+     * @param dto
+     */
+    @Override
+    public void addRegulatoryUnit(RegulatoryUnitDTO dto) {
+        SysDepartment currentDepartment = getMyDepartment();
+        if (Objects.isNull(currentDepartment)) {
+            throw new ServiceException("无操作权限");
+        }
+        SysDepartment parent = this.getById(dto.getParentId());
+        if (Objects.isNull(parent)) {
+            throw new ServiceException("找不到对应父级组织");
+        }
+        if (!parent.getTreeCode().startsWith(currentDepartment.getTreeCode())) {
+            throw new ServiceException("无操作权限");
+        }
+        Long count = this.lambdaQuery().eq(SysDepartment::getDepartmentName, dto.getDepartmentName())
+                .eq(SysDepartment::getOrgType, DepartmentEnum.REGULATORY_UNIT.getCode())
+                .count();
+        if (count > 0) {
+            throw new ServiceException("监管单位已存在");
+        }
+        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
+        department.setTreeCode(generateTreeCode(parent.getId()));
+        department.setOrgCode(getOrgCode(parent.getId(), DepartmentEnum.REGULATORY_UNIT.getCode()));
+        department.setOrgType(DepartmentEnum.REGULATORY_UNIT.getCode());
+        //查询父级完整区域
+        String region = getRegionName(parent);
+        department.setRegion(region);
+        save(department);
+    }
+
+    /**
+     * 编辑监管单位
+     *
+     * @param dto
+     * @return
+     */
+    @Override
+    public void editRegulatoryUnit(RegulatoryUnitDTO dto) {
+        if (Objects.isNull(dto.getId())) {
+            throw new ServiceException("id不能为空");
+        }
+        SysDepartment sysDepartment = getById(dto.getId());
+        if (Objects.isNull(sysDepartment)) {
+            throw new ServiceException("监管单位不存在");
+        }
+        SysDepartment currentDepartment = getMyDepartment();
+        if (Objects.isNull(currentDepartment)) {
+            throw new ServiceException("无操作权限");
+        }
+        SysDepartment parent = this.getById(dto.getParentId());
+        if (Objects.isNull(parent)) {
+            throw new ServiceException("找不到对应父级组织");
+        }
+        if (!parent.getTreeCode().startsWith(currentDepartment.getTreeCode())) {
+            throw new ServiceException("无操作权限");
+        }
+        Long count = this.lambdaQuery().eq(SysDepartment::getDepartmentName, dto.getDepartmentName())
+                .eq(SysDepartment::getOrgType, DepartmentEnum.REGULATORY_UNIT.getCode()).ne(SysDepartment::getId, dto.getId())
+                .count();
+        if (count > 0) {
+            throw new ServiceException("监管单位已存在");
+        }
+        SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class);
+
+        if (!department.getParentId().equals(sysDepartment.getParentId())) {
+            department.setTreeCode(generateTreeCode(parent.getId()));
+            //查询父级完整区域
+            String region = getRegionName(parent);
+            department.setRegion(region);
+        }
+        updateById(department);
+    }
+
+    /**
+     * 监管单位详情
+     *
+     * @param id
+     * @return
+     */
+    @Override
+    public RegulatoryUnitVO getRegulatoryUnitDetailById(Long id) {
+        SysDepartment department = this.lambdaQuery().eq(SysDepartment::getId, id).eq(SysDepartment::getOrgType, DepartmentEnum.REGULATORY_UNIT.getCode()).one();
+        if (Objects.nonNull(department)) {
+            return BeanUtils.copyBean(department, RegulatoryUnitVO.class);
+        }
+        return null;
+    }
+
+    /**
+     * 删除监管单位
+     *
+     * @param id
+     */
+    @Override
+    public void deleteRegulatoryUnit(Long id) {
+        Long count = sysUserService.lambdaQuery().eq(SysUser::getDepartmentId, id).count();
+        if (count > 0) {
+            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);
+    }
 }

--
Gitblit v1.7.1