package com.hs.api.service.Impl;

import com.hs.api.common.exceptions.DBConfigurationError;
import com.hs.api.mapper.DicOrgMapper;
import com.hs.api.mapper.SerDiseaseMapper;
import com.hs.api.mapper.SysRoleMapper;
import com.hs.api.model.DicOrg;
import com.hs.api.model.SerDepartment;
import com.hs.api.model.SerDisease;
import com.hs.api.model.SysRole;
import com.hs.api.model.respmodel.DiseaseLevel;
import com.hs.api.model.respmodel.DiseaseName;
import com.hs.api.service.SerDiseaseService;
import com.hs.common.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SerDiseaseServiceImpl implements SerDiseaseService {
    @Resource
    private SerDiseaseMapper serDiseaseMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private DicOrgMapper dicOrgMapper;

    @Override
    public List<DiseaseLevel> selectListByIdorLevel(String date, String diseaseCode, Integer level, String docCode, String deptCode, Long userId) {
        List<DiseaseLevel> list = new ArrayList<>();
        docCode = StringUtil.isNullOrEmpty(docCode) ? null : docCode;
        deptCode = StringUtil.isNullOrEmpty(deptCode) ? null : deptCode;
        //判断用户角色
        SysRole role = sysRoleMapper.selectByUser(userId);
        if (role != null) {
            if (role.getRoleCode().equals(2) && StringUtil.isNullOrBlank(deptCode)) {
                //科室账号科室入参为空时自动分配科室
                List<SerDepartment> deptList = dicOrgMapper.selectDeptByUser(userId, null);
                if (deptList != null && deptList.size() > 0)
                    deptCode = deptList.get(0).getKsbm();
            }
        } else {
            throw new DBConfigurationError("用户未分配角色！");
        }

        if (diseaseCode != null && diseaseCode != "") {
            list = selectByCode(date, diseaseCode, docCode, deptCode, userId);
        } else {
            list = selectByLevel(date, level, docCode, deptCode, userId);
        }
        return list;
    }

    @Override
    public List<DiseaseName> selectByMdcName(Long userId, String date, String mdcName, String docCode, String deptCode) {
        List<DicOrg> orgList = dicOrgMapper.selectByUser(userId);
        String orgCode = orgList != null && orgList.size() > 0 ? orgList.get(0).getOrgCode() : null;

        mdcName = StringUtil.isNullOrEmpty(mdcName) ? null : mdcName;
        docCode = StringUtil.isNullOrEmpty(docCode) ? null : docCode;
        deptCode = StringUtil.isNullOrEmpty(deptCode) ? null : deptCode;
        String[] mdcNameList = null;
        if (mdcName != null) {
            mdcNameList = mdcName.split("\\s+");
        }
        List<DiseaseName> result = serDiseaseMapper.selectByMdcName(date, mdcNameList, orgCode, deptCode);
        return result;
    }

    /**
     * 根据Code查询父子级别列表
     *
     * @param date        时间
     * @param diseaseCode ID
     * @return
     */
    private List<DiseaseLevel> selectByCode(String date, String diseaseCode, String docCode, String deptCode, Long userId) {


        SerDisease disease = serDiseaseMapper.selectByCode(diseaseCode, date, docCode, deptCode);
        if (disease == null) {
            return null;
        }

        boolean isParent = true;
        DiseaseLevel diseaseLevelResult = null;
        Integer diseaseId = disease.getId().intValue();
        //查询父级菜单对象(直到查询到顶级)
        while (isParent) {
            DiseaseLevel diseaseLevel = null;
            if (disease.getLevel().intValue() == 0) {
                diseaseLevel = serDiseaseMapper.selectParentById(date, diseaseId);
            } else {
                diseaseLevel = serDiseaseMapper.selectParentByIdNoZbz(date, diseaseId);
            }
            if (diseaseLevel != null) {
                List<DiseaseLevel> sonList = new ArrayList<>();
                if (diseaseLevelResult != null) {
                    sonList.add(diseaseLevelResult);
                }
                diseaseLevel.setDiseaseLevelList(sonList);
                diseaseId = diseaseLevel.getId().intValue();
                diseaseLevelResult = diseaseLevel;
            } else {
                isParent = false;
            }
        }

        List<DiseaseLevel> parentList = new ArrayList<>();
        //查询同级菜单列表
        List<DiseaseLevel> serDiseaseList = null;
        if (disease.getLevel().intValue() == 0) {
            serDiseaseList = serDiseaseMapper.selectByParentId(date, disease.getParentId().intValue(), docCode, deptCode, userId);
        } else {
            serDiseaseList = serDiseaseMapper.selectByParentIdNoZbz(date, disease.getParentId().intValue(), docCode, deptCode, userId);
        }
        List<DiseaseLevel> diseaseLevelList = new ArrayList<>();
        for (DiseaseLevel serDiseases : serDiseaseList) {
            DiseaseLevel diseaseLevel1 = new DiseaseLevel();
            //查询子级菜单列表
            if (serDiseases.getMdcCode().equals(diseaseCode)) {
                List<DiseaseLevel> diseaseLevelList1 = serDiseaseMapper.selectSonById(date, disease.getId().intValue(), docCode, deptCode);
                diseaseLevel1.setDiseaseLevelList(diseaseLevelList1);
            }
            diseaseLevel1.setDate(serDiseases.getDate());
            diseaseLevel1.setId(serDiseases.getId());
            diseaseLevel1.setLevel(serDiseases.getLevel());
            diseaseLevel1.setMdcCode(serDiseases.getMdcCode());
            diseaseLevel1.setMdcName(serDiseases.getMdcName());
            diseaseLevel1.setMdcNum(serDiseases.getMdcNum());
            diseaseLevel1.setOrgId(serDiseases.getOrgId());
            diseaseLevel1.setOrgName(serDiseases.getOrgName());
            diseaseLevel1.setParentId(serDiseases.getParentId());
            diseaseLevel1.setGroupType(serDiseases.getGroupType());
            diseaseLevel1.setZls(serDiseases.getZls());
            diseaseLevelList.add(diseaseLevel1);
        }
        if (diseaseLevelResult != null) {
            diseaseLevelResult = getSonDiseaseList(diseaseLevelResult, null, diseaseLevelList);
//            diseaseLevelResult.setDiseaseLevelList(diseaseLevelList);
            parentList.add(diseaseLevelResult);
            return parentList;
        } else {
            return diseaseLevelList;
        }
    }


    private DiseaseLevel getSonDiseaseList(DiseaseLevel diseaseLevel, List<DiseaseLevel> diseaseLevelList,
                                           List<DiseaseLevel> diseaseLevelListSon) {
        if (diseaseLevel != null && diseaseLevel.getDiseaseLevelList() != null && diseaseLevel.getDiseaseLevelList().size() > 0) {
            getSonDiseaseList(null, diseaseLevel.getDiseaseLevelList(), diseaseLevelListSon);
        } else if (diseaseLevelList != null && diseaseLevelList.size() > 0) {
            for (DiseaseLevel info : diseaseLevelList) {
                getSonDiseaseList(info, null, diseaseLevelListSon);
            }
        } else {
            if (diseaseLevel != null && diseaseLevelListSon != null && diseaseLevelListSon.size() > 0) {
                diseaseLevel.setDiseaseLevelList(diseaseLevelListSon);
            }
        }
        return diseaseLevel;
    }

    /**
     * 根据等级查询所有该等级数据
     *
     * @param date
     * @param level
     * @return
     */
    private List<DiseaseLevel> selectByLevel(String date, Integer level, String docCode, String deptCode, Long userId) {
        if (level == null) {
            level = 0;
        }
        if (level == 0) {
            return serDiseaseMapper.selectByLevel(date, level, docCode, deptCode, null, userId);
        } else {
            return serDiseaseMapper.selectByLevelNoZBZ(date, level, docCode, deptCode, null, userId);
        }
    }
}

