package cn.hnthyy.thmz.service.impl.his.mz;

import cn.hnthyy.thmz.Utils.PyWbUtil;
import cn.hnthyy.thmz.common.Constants;
import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.zd.ZdIcdCode;
import cn.hnthyy.thmz.entity.thmz.TCommonParams;
import cn.hnthyy.thmz.enums.ParamsTypeEnum;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.mapper.his.mz.MzBlRecordMapper;
import cn.hnthyy.thmz.mapper.his.mz.MzVisitTableMapper;
import cn.hnthyy.thmz.mapper.his.zd.ZdIcdCodeMapper;
import cn.hnthyy.thmz.mapper.thmz.TCommonParamsMapper;
import cn.hnthyy.thmz.service.his.mz.TCommonParamsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class TCommonParamsServiceImpl implements TCommonParamsService {
    @SuppressWarnings("all")
    @Autowired
    private TCommonParamsMapper tCommonParamsMapper;
    @SuppressWarnings("all")
    @Autowired
    private ZdIcdCodeMapper zdIcdCodeMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzVisitTableMapper mzVisitTableMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzBlRecordMapper mzBlRecordMapper;


    @Override
    public int saveTCommonParams(TCommonParams tCommonParams) {
        tCommonParams.setCreateTime(new Date());
        tCommonParams.setParamsStatus(YesNoEnum.NO.code);
        return tCommonParamsMapper.insertTCommonParams(tCommonParams);
    }

    @Override
    public TCommonParams queryById(Long id) {
        return tCommonParamsMapper.selectById(id);
    }


    @Override
    public TCommonParams queryByParamsName(String paramsName, Integer paramsType, String createIdCode) {
        return tCommonParamsMapper.selectByParamsName(paramsName, paramsType, createIdCode);
    }

    @Override
    public List<TCommonParams> queryTCommonParamsByParamsType(Integer paramsType, String createIdCode) {
        return tCommonParamsMapper.selectTCommonParamsByParamsType(paramsType, null, null, createIdCode);
    }

    @Override
    public List<TCommonParams> queryTCommonParamsByParamsType(Integer paramsType, String commonParams, String createIdCode) {
//        Integer pageSize = null;
//        if (ParamsTypeEnum.DIAGNOSE.code.equals(paramsType)) {
//            pageSize = 100;
//        }
        if(StringUtils.isNotBlank(commonParams)){
            commonParams="%"+commonParams+"%";
        }else {
            commonParams=null;
        }
        return tCommonParamsMapper.selectTCommonParamsByParamsType(paramsType, commonParams, null, createIdCode);
    }

    @Override
    public List<TCommonParams> queryTCommonParamsByParamsType(String commonParams, Integer paramsType, Integer pageSize, String createIdCode) {
        if(StringUtils.isNotBlank(commonParams)){
            commonParams="%"+commonParams+"%";
        }else {
            commonParams=null;
        }
        List<TCommonParams> tCommonParamsList = tCommonParamsMapper.selectTCommonParamsByParamsType(paramsType, commonParams, pageSize, createIdCode);
        if(ParamsTypeEnum.DIAGNOSE_LIST.code.equals(paramsType) && tCommonParamsList!=null && tCommonParamsList.size()>0){
            for(TCommonParams tCommonParams:tCommonParamsList){
                if(StringUtils.isBlank(tCommonParams.getParamsName())){
                    continue;
                }
                String [] codes =tCommonParams.getParamsName().split(",");
                if(codes==null || codes.length==0){
                    continue;
                }
//                List<TCommonParams> diagnoseList=tCommonParamsMapper.selectByParamsCodes(Arrays.asList(codes));
//                if(diagnoseList==null || diagnoseList.size()==0){
//                    continue;
//                }
                String paramsContent =null;
                for (String code:codes){
                    TCommonParams diagnose=tCommonParamsMapper.selectByParamsCode(code);
                    if(diagnose==null){
                        continue;
                    }
                    if(paramsContent==null){
                        paramsContent=diagnose.getParamsName()+"("+diagnose.getParamsCode()+")";
                    }else {
                        paramsContent+=","+diagnose.getParamsName()+"("+diagnose.getParamsCode()+")";
                    }
                }
                tCommonParams.setParamsContent(paramsContent);
            }
        }
        return tCommonParamsList;
    }

    @Override
    public int modifyTCommonParams(TCommonParams tCommonParams) {
        return tCommonParamsMapper.updateTCommonParams(tCommonParams);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public int initDiagnose() {
        List<ZdIcdCode> zdIcdCodeList = zdIcdCodeMapper.selectAllZdIcdCode();
        if (zdIcdCodeList == null || zdIcdCodeList.size() == 0) {
            return 0;
        }
        List<Map<String, Object>> visitTableList = mzVisitTableMapper.selectIcdNum("mz_visit_table");
        Map<String, Integer> icdNumMap = new HashMap<>();
        if (visitTableList != null && visitTableList.size() > 0) {
            for (Map<String, Object> map : visitTableList) {
                icdNumMap.put((String) map.get("icd_code"), (Integer) map.get("num"));
            }
        }
        visitTableList = mzVisitTableMapper.selectIcdNum("mz_visit_table_b");
        if (visitTableList != null && visitTableList.size() > 0) {
            for (Map<String, Object> map : visitTableList) {
                Integer num = icdNumMap.get(map.get("icd_code"));
                if (num == null) {
                    icdNumMap.put((String) map.get("icd_code"), (Integer) map.get("num"));
                } else {
                    Integer numTemp = (Integer) map.get("num");
                    num += numTemp == null ? 0 : numTemp;
                    icdNumMap.put((String) map.get("icd_code"), num);
                }
            }
        }
        TCommonParams tCommonParams = new TCommonParams();
        tCommonParams.setParamsType(ParamsTypeEnum.DIAGNOSE.code);
        tCommonParams.setCreateIdCode(Constants.QYZH_CODE);
        tCommonParams.setCreateTime(new Date());
        for (ZdIcdCode zdIcdCode : zdIcdCodeList) {
            if (StringUtils.isNotBlank(zdIcdCode.getName())) {
                tCommonParams.setParamsName(zdIcdCode.getName());
                tCommonParams.setWbCode(PyWbUtil.getWBCode(tCommonParams.getParamsName()));
                tCommonParams.setPyCode(PyWbUtil.toBigFirstChar(tCommonParams.getParamsName()));
                if (StringUtils.isNotBlank(zdIcdCode.getDelFlag()) && YesNoEnum.YES.code.equals(Integer.valueOf(zdIcdCode.getDelFlag()))) {
                    tCommonParams.setParamsStatus(YesNoEnum.YES.code);
                } else {
                    tCommonParams.setParamsStatus(YesNoEnum.NO.code);
                }
                tCommonParams.setParamsCode(zdIcdCode.getCode());
                if (icdNumMap.size() > 0) {
                    Integer num = icdNumMap.get(tCommonParams.getParamsCode());
                    if (num != null && num > 0) {
                        tCommonParams.setUsedTimes(Long.valueOf(num));
                    }
                }
                try{
                    tCommonParamsMapper.insertTCommonParams(tCommonParams);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return zdIcdCodeList.size();
    }

    @Override
    public int initEmrChiefComplaint() throws MzException {
        List<TCommonParams> tCommonParamsList =tCommonParamsMapper.selectTCommonParamsByParamsType(ParamsTypeEnum.SYMPTOM.code, null, null, null);
        if (tCommonParamsList != null && tCommonParamsList.size() > 0) {
            throw new MzException("已经初始化主诉记录,请勿重复操作");
        }
        List<Map<String,Object>> emrChiefComplaintCountMap= mzBlRecordMapper.selectEmrChiefComplaintCount();
        if(emrChiefComplaintCountMap!=null && emrChiefComplaintCountMap.size()>0){
            TCommonParams tCommonParams = new TCommonParams();
            tCommonParams.setParamsType(ParamsTypeEnum.SYMPTOM.code);
            tCommonParams.setCreateTime(new Date());
            for(Map<String,Object> map:emrChiefComplaintCountMap){
               String emrChiefComplaint= (String) map.get("emr_chief_complaint");
                if(StringUtils.isBlank(emrChiefComplaint)){
                    continue;
                }
                tCommonParams.setParamsName(emrChiefComplaint);
                tCommonParams.setCreateIdCode((String) map.get("doctor_code"));
                tCommonParams.setWbCode(PyWbUtil.getWBCode(tCommonParams.getParamsName()));
                tCommonParams.setPyCode(PyWbUtil.toBigFirstChar(tCommonParams.getParamsName()));
                tCommonParams.setParamsStatus(YesNoEnum.NO.code);
                tCommonParams.setUsedTimes(Long.valueOf((Integer)map.get("num")));
                try{
                    tCommonParamsMapper.insertTCommonParams(tCommonParams);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return emrChiefComplaintCountMap.size();
    }

    @Override
    public int initEmrHpi() throws MzException {
        List<TCommonParams> tCommonParamsList =tCommonParamsMapper.selectTCommonParamsByParamsType(ParamsTypeEnum.HISTORY_OF_PRESENT_ILLNESS.code, null, null, null);
        if (tCommonParamsList != null && tCommonParamsList.size() > 0) {
            throw new MzException("已经初始化现病史记录,请勿重复操作");
        }
        List<Map<String,Object>> emrHpiCountMap= mzBlRecordMapper.selectEmrHpiCount();
        if(emrHpiCountMap!=null && emrHpiCountMap.size()>0){
            TCommonParams tCommonParams = new TCommonParams();
            tCommonParams.setParamsType(ParamsTypeEnum.HISTORY_OF_PRESENT_ILLNESS.code);
            tCommonParams.setCreateTime(new Date());
            for(Map<String,Object> map:emrHpiCountMap){
                String emrHpi= (String) map.get("emr_hpi");
                if(emrHpi==null || StringUtils.isBlank(emrHpi)){
                    continue;
                }
                tCommonParams.setParamsContent(emrHpi);
                if(emrHpi.length()>20){
                    tCommonParams.setParamsName(emrHpi.substring(0,20));
                }else {
                    tCommonParams.setParamsName(emrHpi);
                }
                tCommonParams.setCreateIdCode((String) map.get("doctor_code"));
                tCommonParams.setWbCode(PyWbUtil.getWBCode(tCommonParams.getParamsName()));
                tCommonParams.setPyCode(PyWbUtil.toBigFirstChar(tCommonParams.getParamsName()));
                tCommonParams.setParamsStatus(YesNoEnum.NO.code);
                tCommonParams.setUsedTimes(Long.valueOf((Integer)map.get("num")));
                try{
                    tCommonParamsMapper.insertTCommonParams(tCommonParams);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return emrHpiCountMap.size();
    }

    @Override
    public int initEmrPs() throws MzException {
        List<TCommonParams> tCommonParamsList =tCommonParamsMapper.selectTCommonParamsByParamsType(ParamsTypeEnum.FAMILY_HISTORY.code, null, null, null);
        if (tCommonParamsList != null && tCommonParamsList.size() > 0) {
            throw new MzException("已经初始化既往史记录,请勿重复操作");
        }
        List<Map<String,Object>> emrPsCountMap= mzBlRecordMapper.selectEmrPsCount();
        if(emrPsCountMap!=null && emrPsCountMap.size()>0){
            TCommonParams tCommonParams = new TCommonParams();
            tCommonParams.setParamsType(ParamsTypeEnum.FAMILY_HISTORY.code);
            tCommonParams.setCreateTime(new Date());
            for(Map<String,Object> map:emrPsCountMap){
                String emrPs= (String) map.get("emr_ps");
                if(emrPs==null || StringUtils.isBlank(emrPs)){
                    continue;
                }
                tCommonParams.setParamsContent(emrPs);
                if(emrPs.length()>20){
                    tCommonParams.setParamsName(emrPs.substring(0,20));
                }else {
                    tCommonParams.setParamsName(emrPs);
                }
                tCommonParams.setCreateIdCode((String) map.get("doctor_code"));
                tCommonParams.setWbCode(PyWbUtil.getWBCode(tCommonParams.getParamsName()));
                tCommonParams.setPyCode(PyWbUtil.toBigFirstChar(tCommonParams.getParamsName()));
                tCommonParams.setParamsStatus(YesNoEnum.NO.code);
                tCommonParams.setUsedTimes(Long.valueOf((Integer)map.get("num")));
                try{
                    tCommonParamsMapper.insertTCommonParams(tCommonParams);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return emrPsCountMap.size();
    }
}
