package com.lantone.security.facade;

import cn.hutool.crypto.digest.BCrypt;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.collect.Lists;
import com.lantone.security.dto.DownBoxRegionDTO;
import com.lantone.security.dto.GetDictionaryInfoByTypeDTO;
import com.lantone.security.dto.GetHospitalByIdDTO;
import com.lantone.security.dto.GetHospitalListInfoDTO;
import com.lantone.security.dto.GetHospitalTreeDTO;
import com.lantone.security.dto.GetHospitalUserDTO;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.enums.StatusEnum;
import com.lantone.common.exception.Asserts;
import com.lantone.common.util.DateUtil;
import com.lantone.common.util.EntityUtil;
import com.lantone.common.util.ListUtil;
import com.lantone.common.util.StringUtil;
import com.lantone.common.util.SysUserUtils;
import com.lantone.security.vo.AddHospitalTreeVO;
import com.lantone.security.vo.AddHospitalVO;
import com.lantone.security.vo.AddRoleVO;
import com.lantone.security.vo.AddUserVO;
import com.lantone.security.vo.GetHospitalListInfoVO;
import com.lantone.security.vo.UpdateHospitalVO;
import com.lantone.security.vo.UpdateUserVO;
import com.lantone.security.entity.Dept;
import com.lantone.security.entity.Hospital;
import com.lantone.security.entity.HospitalUser;
import com.lantone.security.entity.Region;
import com.lantone.security.entity.RegionDept;
import com.lantone.security.entity.Role;
import com.lantone.security.entity.User;
import com.lantone.security.facade.base.DeptFacade;
import com.lantone.security.facade.base.DictionaryInfoFacade;
import com.lantone.security.facade.base.HospitalFacade;
import com.lantone.security.facade.base.HospitalUserFacade;
import com.lantone.security.facade.base.RegionDeptFacade;
import com.lantone.security.facade.base.RegionFacade;
import com.lantone.security.facade.base.RoleFacade;
import com.lantone.security.facade.base.UserFacade;
import com.lantone.security.facade.base.UserRoleFacade;
import com.lantone.security.enums.DictionaryEnum;
import com.lantone.security.enums.HospitalTypeEnum;
import com.lantone.security.enums.ReturnTypeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
 * 组织管理API
 */
@Component
public class HospitalManagementFacade {
    @Autowired
    private HospitalFacade hospitalFacade;
    @Autowired
    private DeptFacade deptFacade;
    @Autowired
    private HospitalUserFacade hospitalUserFacade;
    @Autowired
    private UserRoleFacade userRoleFacade;
    @Autowired
    private UserManagementFacade userManagementFacade;
    @Autowired
    private UserFacade userFacade;
    @Autowired
    private RoleManagementFacade roleManagementFacade;
    @Autowired
    private RoleFacade roleFacade;
    @Autowired
    private RegionFacade regionFacade;
    @Autowired
    private RegionDeptFacade regionDeptFacade;
    @Autowired
    private DictionaryInfoFacade dictionaryInfoFacade;

    /**
     * @param addHospitalVO
     * @Description添加组织
     * @Return boolean
     */
    public boolean addHospital(AddHospitalVO addHospitalVO) {

        //通过任务类型插入医院或科室
        if ((HospitalTypeEnum.DEPT.getKey() + "").equals(addHospitalVO.getType())) {
            Dept dept = new Dept();
            BeanUtils.copyProperties(addHospitalVO, dept);
            dept.setHospitalId(addHospitalVO.getParentId());
            dept.setCreator(SysUserUtils.getCurrentPrincipleId() + "");
            dept.setGmtCreate(DateUtil.now());
            //添加联系人手机号
            dept.setLiaison(addHospitalVO.getAddHospitalUserVO().getName());
            dept.setMobilePhone(addHospitalVO.getAddHospitalUserVO().getMobilePhone());
            if (deptFacade.save(dept)) {
                //如果选择病区的话保存不能过去和科室的关系
                if (addHospitalVO.getRegionId() != null) {
                    RegionDept regionDept = new RegionDept();
                    regionDept.setHospitalId(addHospitalVO.getParentId());
                    regionDept.setRegionId(addHospitalVO.getRegionId());
                    regionDept.setDeptId(dept.getId());
                    if (!regionDeptFacade.save(regionDept)) {
                        Asserts.fail("病区和科室关系插入失败");
                    }
                }
                //2.保存该组织的用户(不支持创建管理员)
                return true;
            } else {
                Asserts.fail("组织添加失败");
            }
        } else {
            //1.添加组织信息
            Hospital hospital = new Hospital();
            BeanUtils.copyProperties(addHospitalVO, hospital);
            hospital.setCreator(SysUserUtils.getCurrentPrincipleId() + "");
            hospital.setGmtCreate(DateUtil.now());
            if (hospitalFacade.save(hospital)) {
                //2.保存该组织的用户
                return saveUser(hospital.getId(), addHospitalVO);
            } else {
                Asserts.fail("组织添加失败");
            }
        }
        return false;
    }

    /**
     * @param hospitalId
     * @param addHospitalVO
     * @Description添加组织的用户
     * @Return boolean
     */
    private boolean saveUser(Long hospitalId, AddHospitalVO addHospitalVO) {

        //1.添加角色
        AddRoleVO addRoleVO = new AddRoleVO();
        addRoleVO.setName(addHospitalVO.getName() + "组织管理角色");
        addRoleVO.setRemark("ManagementRole");
        addRoleVO.setStatus("1");
        Long insertHospitalId = 0l;
        if ((HospitalTypeEnum.DEPT.getKey() + "").equals(addHospitalVO.getType())) {
            insertHospitalId = addHospitalVO.getParentId();
        } else {
            insertHospitalId = hospitalId;
        }
        addRoleVO.setHospitalId(insertHospitalId);
        addRoleVO.setSoftwares(addHospitalVO.getSoftwares());
        if (roleManagementFacade.addRole(addRoleVO)) {
            //2.获取角色id完善新增用户信息
            Long roleId = roleFacade.list(new QueryWrapper<Role>()
                    .eq("name", addHospitalVO.getName() + "组织管理角色")
                    .eq("hospital_id", insertHospitalId)).get(0).getId();
            //拼装添加用户信息
            AddUserVO addUserVO = new AddUserVO();
            BeanUtils.copyProperties(addHospitalVO.getAddHospitalUserVO(), addUserVO);
            //用户与角色的关系
            addUserVO.setRoles(new ArrayList<>());
            addUserVO.getRoles().add(roleId);
            addUserVO.setName(addHospitalVO.getName() + "管理员");
            addUserVO.setRemark("Administrators");
            //用户与组织的关系(医院、科室)
            AddHospitalTreeVO addHospitalTreeVO = new AddHospitalTreeVO();
            if ((HospitalTypeEnum.DEPT.getKey() + "").equals(addHospitalVO.getType())) {
                addHospitalTreeVO.setDepts(new ArrayList<>());
                addHospitalTreeVO.getDepts().add(hospitalId);
                addUserVO.setAddHospitalTreeVO(addHospitalTreeVO);

                addHospitalTreeVO.setHospitals(new ArrayList<>());
                addHospitalTreeVO.getHospitals().add(addHospitalVO.getParentId());
                addUserVO.setAddHospitalTreeVO(addHospitalTreeVO);
            } else {
                addHospitalTreeVO.setHospitals(new ArrayList<>());
                addHospitalTreeVO.getHospitals().add(hospitalId);
                addUserVO.setAddHospitalTreeVO(addHospitalTreeVO);
            }
            return userManagementFacade.addUser(addUserVO);
        } else {
            Asserts.fail("角色插入失败");
        }
        return false;
    }


    /**
     * @param hospitalId
     * @param addHospitalVO
     * @Description修改组织的用户
     * @Return boolean
     */
    private boolean updateUser(Long hospitalId, AddHospitalVO addHospitalVO) {

        //1.添加角色
        AddRoleVO addRoleVO = new AddRoleVO();
        addRoleVO.setName(addHospitalVO.getName() + "组织管理角色");
        addRoleVO.setRemark("ManagementRole");
        addRoleVO.setStatus("1");
        Long insertHospitalId = 0l;
        if ((HospitalTypeEnum.DEPT.getKey() + "").equals(addHospitalVO.getType())) {
            insertHospitalId = addHospitalVO.getParentId();
        } else {
            insertHospitalId = hospitalId;
        }
        addRoleVO.setHospitalId(insertHospitalId);
        addRoleVO.setSoftwares(addHospitalVO.getSoftwares());
        if (roleManagementFacade.addRole(addRoleVO)) {
            //2.获取角色id完善新增用户信息
            Long roleId = roleFacade.list(new QueryWrapper<Role>()
                    .eq("name", addHospitalVO.getName() + "组织管理角色")
                    .eq("hospital_id", insertHospitalId)).get(0).getId();
            //拼装添加用户信息
            UpdateUserVO updateUserVO = new UpdateUserVO();
            BeanUtils.copyProperties(addHospitalVO.getAddHospitalUserVO(), updateUserVO);
            //用户与角色的关系
            updateUserVO.setRoles(new ArrayList<>());
            updateUserVO.getRoles().add(roleId);
            updateUserVO.setName(addHospitalVO.getName() + "管理员");
            //用户与组织的关系(医院、科室)
            AddHospitalTreeVO addHospitalTreeVO = new AddHospitalTreeVO();
            addHospitalTreeVO.setHospitals(new ArrayList<>());
            addHospitalTreeVO.getHospitals().add(hospitalId);
            updateUserVO.setAddHospitalTreeVO(addHospitalTreeVO);
            return userManagementFacade.updateUser(updateUserVO);
        } else {
            Asserts.fail("角色插入失败");
        }
        return false;
    }
    /**
     * @param hospitalId
     * @Description删除组织
     * @Return boolean
     */
    public boolean deleteHospital(Long hospitalId, String type) {
        AtomicBoolean out = new AtomicBoolean(false);
        //禁用科室
        if ((HospitalTypeEnum.DEPT.getKey() + "").equals(type)) {
            return deptFacade.update(new UpdateWrapper<Dept>()
                    .set("is_deleted", IsDeleteEnum.Y.getKey())
                    .in("id", hospitalId));
        } else {
            //1.获取该组织及其子组织
            List<Long> hospitals = hospitalFacade.getBaseMapper().getHospitalTreeInfo(hospitalId, null).stream()
                    .map(GetHospitalTreeDTO::getHospitalId).collect(Collectors.toList());
            //2.删除这些组织的用户、角色
            if (ListUtil.isNotEmpty(hospitals)) {
                hospitals.forEach(id -> {
                    //删除该组织角色
                    if (roleFacade.update(new UpdateWrapper<Role>()
                            .set("is_deleted", IsDeleteEnum.Y.getKey())
                            .in("id", roleFacade.list(new QueryWrapper<Role>()
                                    .eq("hospital_id", id))
                                    .stream().map(Role::getId).collect(Collectors.toList())))) {

                        //删除组织用户
                        if (userFacade.update(new UpdateWrapper<User>()
                                .set("is_deleted", IsDeleteEnum.Y.getKey())
                                .in("id", hospitalUserFacade.list(new QueryWrapper<HospitalUser>()
                                        .eq("hospital_id", id))
                                        .stream().map(HospitalUser::getUserId).collect(Collectors.toList())))) {
                            //删除组织
                            out.set(hospitalFacade.update(new UpdateWrapper<Hospital>()
                                    .set("is_deleted", IsDeleteEnum.Y.getKey())
                                    .eq("id", id)));
                        } else {
                            Asserts.fail("组织用户删除失败");
                        }
                    } else {
                        Asserts.fail("组织角色删除失败");
                    }

                });
            } else {
                Asserts.fail("要删除的组织不存在,请刷新重试~");
            }
        }

        return out.get();
    }

    /**
     * @param hospitalId
     * @Description删除该组织的管理用户、角色
     * @Return boolean
     */
    private boolean deleteHUserAndRole(Long hospitalId) {

        //1.获取用户id
        Hospital hospital = hospitalFacade.getById(hospitalId);
        if (hospital == null) {
            Asserts.fail("组织不存在");
        }
        List<Long> userIds = hospitalUserFacade.getBaseMapper().getHospitalUserById(hospitalId, hospital.getName() + "管理员");
        if (ListUtil.isEmpty(userIds)) {
            Asserts.fail("该组织的管理用户不存在，请联系管理员~");
        }
        if (userIds.size() > 1) {
            Asserts.fail("该组织的管理用户存在多个，请联系管理员~");
        }
        //这里的角色和用户都是真实删除的
        //2.获取角色id
        List<Long> roleIds = userRoleFacade.getBaseMapper().getUserRoleById(userIds.get(0));
        if (ListUtil.isEmpty(roleIds)) {
            Asserts.fail("要修改组织的管理角色不存在，请联系管理员~");
        }
        //通过角色id删除原来角色相关管理关系
        AtomicBoolean deleteFlag = new AtomicBoolean(false);
        roleIds.stream().forEach(roleId -> {
            deleteFlag.set(roleManagementFacade.delete(roleId));
        });
//        if (deleteFlag.get()) {
//            //3.删除用户
//            return userManagementFacade.deleteUser(userIds.get(0));
//        } else {
//            Asserts.fail("管理角色删除失败");
//        }

        return deleteFlag.get();
    }

    /**
     * @param updateHospitalVO
     * @Description修改组织信息
     * @Return boolean
     */
    public boolean updateHospital(UpdateHospitalVO updateHospitalVO) {
        //修改科室信息
        if ((HospitalTypeEnum.DEPT.getKey() + "").equals(updateHospitalVO.getType())) {
            boolean updateFlag = true;
            //1.删除原来科室和病区的管理关系
            List<Long> ids = regionDeptFacade.list(new QueryWrapper<RegionDept>()
                    .eq("dept_id", updateHospitalVO.getId())
                    .eq("hospital_id", updateHospitalVO.getParentId())
                    .eq("is_deleted", IsDeleteEnum.N.getKey())).stream().map(RegionDept::getId).collect(Collectors.toList());
            if (ListUtil.isNotEmpty(ids)) {
                updateFlag = regionDeptFacade.removeByIds(ids);
            }
            //2.修改科室
            if (updateFlag) {
                Dept dept = new Dept();
                BeanUtils.copyProperties(updateHospitalVO, dept);
                dept.setHospitalId(updateHospitalVO.getParentId());
                //添加联系人手机号
                dept.setLiaison(updateHospitalVO.getAddHospitalUserVO().getName());
                dept.setMobilePhone(updateHospitalVO.getAddHospitalUserVO().getMobilePhone());
                updateFlag = deptFacade.updateById(dept);
                //3.如果选择病区的话保存病区和科室的关系
                if (updateHospitalVO.getRegionId() != null && updateFlag) {
                    RegionDept regionDept = new RegionDept();
                    regionDept.setHospitalId(updateHospitalVO.getParentId());
                    regionDept.setRegionId(updateHospitalVO.getRegionId());
                    regionDept.setDeptId(updateHospitalVO.getId());
                    if (!regionDeptFacade.save(regionDept)) {
                        Asserts.fail("病区和科室关系插入失败");
                    }
                }
                return updateFlag;
            } else {
                Asserts.fail("原病区和科室的关联关系删除失败");
            }
        } else {
            //1.删除该组织关联的管理用户以及角色
            if (deleteHUserAndRole(updateHospitalVO.getId())) {
                Hospital hospital = new Hospital();
                BeanUtils.copyProperties(updateHospitalVO, hospital);
                //2.修改组织信息
                if (hospitalFacade.updateById(hospital)) {
                    //3.创建组织与管理用户、角色
                    AddHospitalVO addHospitalVO = new AddHospitalVO();
                    BeanUtils.copyProperties(updateHospitalVO, addHospitalVO);
                    addHospitalVO.setAddHospitalUserVO(updateHospitalVO.getAddHospitalUserVO());
                    addHospitalVO.setSoftwares(updateHospitalVO.getSoftwares());
                    return updateUser(updateHospitalVO.getId(), addHospitalVO);
                }
            } else {
                Asserts.fail("原组织管理用户信息清除失败,请联系管理员");
            }
        }
        return false;
    }

    /**
     * @param hospitalId
     * @param status
     * @Description禁用启用组织
     * @Return boolean
     */
    public boolean disableHospital(Long hospitalId, String status, String type) {
        AtomicBoolean out = new AtomicBoolean(false);
        String selectStatus = "0";
        if (StatusEnum.Enable.getKey().equals(status)) {
            selectStatus = StatusEnum.Disable.getKey();
        } else {
            selectStatus = StatusEnum.Enable.getKey();
        }
        //禁用科室
        if ((HospitalTypeEnum.DEPT.getKey() + "").equals(type)) {
            return deptFacade.update(new UpdateWrapper<Dept>()
                    .set("status", status)
                    .in("id", hospitalId));
        } else {
            //1.获取该组织及其子组织
            List<Long> hospitals = hospitalFacade.getBaseMapper().getHospitalTreeInfo(hospitalId, selectStatus).stream()
                    .map(GetHospitalTreeDTO::getHospitalId).collect(Collectors.toList());
            //2.禁用这些组织的用户、角色
            if (ListUtil.isNotEmpty(hospitals)) {
                hospitals.forEach(id -> {
                    //禁用该组织角色
                    if (roleFacade.update(new UpdateWrapper<Role>()
                            .set("status", status)
                            .in("id", roleFacade.list(new QueryWrapper<Role>()
                                    .eq("hospital_id", id))
                                    .stream().map(Role::getId).collect(Collectors.toList())))) {

                        //禁用组织用户
                        if (userFacade.update(new UpdateWrapper<User>()
                                .set("status", status)
                                .in("id", hospitalUserFacade.list(new QueryWrapper<HospitalUser>()
                                        .eq("hospital_id", id))
                                        .stream().map(HospitalUser::getUserId).collect(Collectors.toList())))) {
                            //删除组织
                            out.set(hospitalFacade.update(new UpdateWrapper<Hospital>()
                                    .set("status", status).eq("id", id)));
                        } else {
                            Asserts.fail("组织用户禁用失败");
                        }
                    } else {
                        Asserts.fail("组织角色禁用失败");
                    }

                });
            } else {
                Asserts.fail("该组织不存在，请联系管理员");
            }
        }

        return out.get();
    }

    /**
     * @param hospitalId
     * @Description通过组织id获取该组织及用户详情
     * @Return com.lantone.security.dto.GetHospitalByIdDTO
     */
    public GetHospitalByIdDTO getHospitalById(Long hospitalId, int type) {
        GetHospitalByIdDTO getHospitalByIdDTO = new GetHospitalByIdDTO();
        //1.获取科室详情
        if (HospitalTypeEnum.DEPT.getKey() == type) {
            Dept dept = deptFacade.getById(hospitalId);
            if (dept == null) {
                Asserts.fail("该组织不存在，请联系管理员~");
            }
            BeanUtils.copyProperties(dept, getHospitalByIdDTO);
            getHospitalByIdDTO.setParentId(dept.getHospitalId());
            //添加联系人、联系电话
            GetHospitalUserDTO getHospitalUserDTO = new GetHospitalUserDTO();
            getHospitalUserDTO.setName(dept.getLiaison());
            getHospitalUserDTO.setMobilePhone(dept.getMobilePhone());
            getHospitalByIdDTO.setGetHospitalUserDTO(getHospitalUserDTO);

        } else {
            //1.获取医院详情
            Hospital hospital = hospitalFacade.getById(hospitalId);
            if (hospital == null) {
                Asserts.fail("该组织不存在，请联系管理员~");
            }
            BeanUtils.copyProperties(hospital, getHospitalByIdDTO);
            //2.获取该医院的管理用户详情
            List<Long> userIds = hospitalUserFacade.getBaseMapper().getHospitalUserById(hospitalId, getHospitalByIdDTO.getName() + "管理员");
            if (ListUtil.isEmpty(userIds)) {
                Asserts.fail("该组织的管理用户不存在，请联系管理员~");
            }
            if (userIds.size() > 1) {
                Asserts.fail("该组织的管理用户存在多个，请联系管理员~");
            }
            User user = userFacade.getById(userIds.get(0));
            if (user == null) {
                Asserts.fail("组织管理用户不存在~");
            }
            GetHospitalUserDTO getHospitalUserDTO = new GetHospitalUserDTO();
            user.setPassword(user.getPassword());
            BeanUtils.copyProperties(user, getHospitalUserDTO);
            getHospitalByIdDTO.setGetHospitalUserDTO(getHospitalUserDTO);
            //3.获取管理角色相关信息(新增组织查询时type为0）
            List<Long> roleIds = userRoleFacade.getBaseMapper().getUserRoleById(user.getId());
            if (ListUtil.isEmpty(roleIds)) {
                Asserts.fail("该组织的管理角色不存在，请联系管理员~");
            }
            if (roleIds.size() > 1) {
                Asserts.fail("该组织的管理角色存在多个，请联系管理员~");
            }
            getHospitalByIdDTO.setGetRoleDTO(roleManagementFacade.getRoleById(roleIds.get(0), "0"));
        }
        Hospital parentHospital = hospitalFacade.getById(getHospitalByIdDTO.getParentId());
        if (parentHospital != null) {
            getHospitalByIdDTO.setParentName(parentHospital.getName());
        }
        return getHospitalByIdDTO;
    }

    /**
     * @param
     * @Description组织管理列表页管理员下拉框筛选类型、病区查询
     * @Return java.util.Map<java.lang.String, java.lang.Object>
     */
    public Map<String, Object> getManagerBoxInfo() {
        Map<String, Object> outMap = new HashMap<>();
        String boxId = dictionaryInfoFacade.getOneDicMap(ReturnTypeEnum.INTERFACE.getKey(),DictionaryEnum.MANAGE_HIDE_MENU.getKey()+"").get("box");
        Map<String, List<GetDictionaryInfoByTypeDTO>> dicMapObj = dictionaryInfoFacade.getDicMapObj(ReturnTypeEnum.INTERFACE.getKey(),
                Lists.newArrayList(boxId.split(",")));
        outMap.putAll(dicMapObj);
        outMap.remove(DictionaryEnum.HOSPITAL_TYPE.getKey() + "");
        //1.获取当前用户的组织类型
        int hospitalType = -1;
        Hospital hospital = hospitalFacade.getById(SysUserUtils.getCurrentHospitalId());
        if (hospital != null) {
            hospitalType = Integer.parseInt(hospital.getType());
        }
        //单家医院
        if (HospitalTypeEnum.SINGLE_HOSPITAL.getKey() == hospitalType) {
            //获取病区信息
            List<DownBoxRegionDTO> downBoxRegions = new ArrayList<>();
            regionFacade.list(new QueryWrapper<Region>()
                    .eq("hospital_id", hospital.getId())
                    .eq("is_deleted", IsDeleteEnum.N.getKey())).stream().forEach(region -> {
                DownBoxRegionDTO downBoxRegionDTO = new DownBoxRegionDTO();
                downBoxRegionDTO.setVal(region.getName());
                downBoxRegionDTO.setName(region.getId());
                downBoxRegions.add(downBoxRegionDTO);
            });
            outMap.put(DictionaryEnum.REGION.getKey() + "", downBoxRegions);
        } else {
            String type = dicMapObj.get(DictionaryEnum.HOSPITAL_TYPE.getKey() + "").get(hospitalType).getVal();
            if (StringUtil.isNotEmpty(type)) {
                outMap.put(DictionaryEnum.HOSPITAL_TYPE.getKey() + "", JSONArray.parseArray(type));
            }
        }
        return outMap;
    }

    /**
     * @param getHospitalListInfoVO
     * @Description获取当前登录用户的组织结构树
     * @Return java.util.List<com.lantone.security.dto.GetHospitalListInfoDTO>
     */
    public List<GetHospitalListInfoDTO> getHospitalListInfo(GetHospitalListInfoVO getHospitalListInfoVO) {
        //1.获取当前用户的组织类型
        int hospitalType = -1;
        getHospitalListInfoVO.setHospitalId(SysUserUtils.getCurrentHospitalId());
        getHospitalListInfoVO.setCreator(SysUserUtils.getCurrentPrincipleId());
        Hospital hospital = hospitalFacade.getById(SysUserUtils.getCurrentHospitalId());
        if (hospital != null) {
            hospitalType = Integer.parseInt(hospital.getType());
        }
        //单家医院
        if (HospitalTypeEnum.SINGLE_HOSPITAL.getKey() == hospitalType) {
            //1.获取当前登陆用户所在医院的科室信息
            GetHospitalListInfoDTO getHospitalListInfoDTO = new GetHospitalListInfoDTO();
            getHospitalListInfoDTO.setHospitalId(hospital.getId());
            getHospitalListInfoDTO.setHospitalName(hospital.getName());
            BeanUtils.copyProperties(hospital, getHospitalListInfoDTO);
            //添加根目录标志
            getHospitalListInfoDTO.setRootFlag(true);
            getHospitalListInfoDTO.setTypeName(HospitalTypeEnum.getName(hospitalType));
            if (StringUtil.isNotEmpty(getHospitalListInfoDTO.getStatus())) {
                getHospitalListInfoDTO.setStatusName(StatusEnum.getName(Integer.parseInt(getHospitalListInfoDTO.getStatus())));
            }
            getHospitalListInfoDTO.setDepts(deptFacade.getBaseMapper().getDeptRegion(getHospitalListInfoVO));
            List<GetHospitalListInfoDTO> hospitalListInfos = new ArrayList<>();
            //科室不为空则表示有子组织
            if (ListUtil.isNotEmpty(getHospitalListInfoDTO.getDepts())) {
                getHospitalListInfoDTO.setHasHospitalFlag(true);
            }
            //该组织是否有用户
            if (hospitalUserFacade.list(new QueryWrapper<HospitalUser>()
                    .eq("hospital_id", hospital.getId())
                    .eq("is_deleted", IsDeleteEnum.N.getKey())).size() > 1) {
                getHospitalListInfoDTO.setHasUserFlag(true);
            }
            hospitalListInfos.add(getHospitalListInfoDTO);
            return hospitalListInfos;
            //虚拟医院
        } else if (hospitalType >= 0) {
            List<GetHospitalListInfoDTO> hospitalListInfoDTOS = hospitalFacade.getBaseMapper().getHospitalById(getHospitalListInfoVO);
            Map<Long, List<GetHospitalListInfoDTO>> hospitalMaps = EntityUtil.makeEntityListMap(hospitalListInfoDTOS, "parentId");
            List<GetHospitalListInfoDTO> hospitalListInfos = hospitalMaps.get(hospital.getParentId());
            for (GetHospitalListInfoDTO bean : hospitalListInfos) {
                //设置根目录
                bean.setRootFlag(true);
                //获取该组织是否有子组织、用户
                getHospitalSonORUser(bean);
                getSonHospital(bean, hospitalMaps);
            }
            return hospitalListInfos;
        }

        return null;
    }

    /**
     * 递归获取医院结构
     *
     * @param hospital    当前医院
     * @param hospitalMap 医院集
     * @return 空
     */
    public void getSonHospital(GetHospitalListInfoDTO hospital, Map<Long, List<GetHospitalListInfoDTO>> hospitalMap) {
        //获取该组织是否有子组织、用户
        getHospitalSonORUser(hospital);
        List<GetHospitalListInfoDTO> hospitals = hospitalMap.get(hospital.getHospitalId());
        if (ListUtil.isNotEmpty(hospitals)) {
            hospital.setChildren(hospitals);
            for (GetHospitalListInfoDTO bean : hospitals) {
                getSonHospital(bean, hospitalMap);
            }
        }
    }

    private void getHospitalSonORUser(GetHospitalListInfoDTO hospital) {
        if (hospital != null && hospital.getHospitalId() != null) {
            //获取该组织用户
            if (hospitalUserFacade.list(new QueryWrapper<HospitalUser>()
                    .eq("hospital_id", hospital.getHospitalId())
                    .eq("is_deleted", IsDeleteEnum.N.getKey())).size() > 1) {
                hospital.setHasUserFlag(true);
            }
            //获取该组织子组织
            if (hospitalFacade.list(new QueryWrapper<Hospital>()
                    .eq("parent_id", hospital.getHospitalId())
                    .eq("status", StatusEnum.Enable.getKey())
                    .eq("is_deleted", IsDeleteEnum.N.getKey())).size() > 0) {
                hospital.setHasHospitalFlag(true);
            }
        }
    }

    /**
     * @param hospitalId
     * @Description重置密码
     * @Return boolean
     */
    public boolean resetPassword(Long hospitalId) {
        User user = new User();
        String password = dictionaryInfoFacade.getDicMap(ReturnTypeEnum.INTERFACE.getKey(),
                Lists.newArrayList(DictionaryEnum.PASSWORD.getKey() + ""))
                .get(DictionaryEnum.PASSWORD.getKey() + "").get("password");
        if (StringUtil.isEmpty(password)) {
            Asserts.fail("重置密码失败，请联系管理员");
        }
        user.setPassword(BCrypt.hashpw(password));
        user.setId(userFacade.getBaseMapper().getUserByHospitalId(hospitalId));
        return userFacade.updateById(user);
    }
}
