package com.lantone.qc.trans.ninghaiyiyi;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lantone.qc.pub.model.doc.consultation.ConsultationApplicationDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationRecordDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationResultsDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.FastJsonUtils;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.comsis.ModelDocGenerate;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.ninghaiyiyi.util.NingHaiYiYiConsultationHtmlAnalysis;
import com.lantone.qc.trans.ninghaiyiyi.util.NingHaiYiYiConsultationRecordHtmlAnalysis;
import com.lantone.qc.trans.ninghaiyiyi.util.NingHaiYiYiHtmlAnalysis;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 会诊文档生成
 * @author: rengb
 * @time: 2020/3/17 13:22
 */
@Slf4j
public class NingHaiYiYiConsultationDocTrans extends ModelDocTrans {

    @Override
    public List<ConsultationDoc> extract(MedrecVo medrecVo) {
        List<ConsultationDoc> retList = Lists.newArrayList();

        Map<String, List<Object>> contentMap = (Map) medrecVo.getContent().get("content");
        if (contentMap == null) {
            return retList;
        }

        Map<String, ConsultationRecordDoc> consultationRecordDocMap = getConsultationRecordDocMap((List) contentMap.get("会诊记录"));
        Map<String, ConsultationResultsDoc> consultationResultsDocMap = getConsultationResultsDocMap((List) contentMap.get("会诊结果单"));
        Map<String, ConsultationApplicationDoc> consultationApplicationDocMap = getConsultationApplicationDocMap((List) contentMap.get("会诊申请单"));

        Set<String> consultationNameSet = Sets.newHashSet();
        consultationNameSet.addAll(consultationRecordDocMap.keySet());
        consultationNameSet.addAll(consultationResultsDocMap.keySet());
        consultationNameSet.addAll(consultationApplicationDocMap.keySet());

        consultationNameSet.forEach(consultationName -> {
            ConsultationDoc consultationDoc = new ConsultationDoc();
            consultationDoc.setConsultationName(consultationName);
            consultationDoc.setConsultationRecordDoc(consultationRecordDocMap.get(consultationName));
            consultationDoc.setConsultationResultsDoc(consultationResultsDocMap.get(consultationName));
            consultationDoc.setConsultationApplicationDoc(consultationApplicationDocMap.get(consultationName));
            retList.add(consultationDoc);
        });

        return retList;
    }


    /**************************************************会诊记录*********************************************************/
    private Map<String, ConsultationRecordDoc> getConsultationRecordDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, ConsultationRecordDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String consultationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("htmlText") == null || StringUtil.isBlank(contentMap.get("htmlText").toString())) {
                continue;
            }
            try {
                ConsultationRecordDoc consultationRecordDoc = getConsultationRecordDoc(contentMap);
                if (consultationRecordDoc != null) {
                    consultationName = index + "";
                    consultationRecordDoc.setConsultationName(consultationName);
                    retMap.put(consultationName, consultationRecordDoc);
                    index++;
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        return retMap;
    }

    private ConsultationRecordDoc getConsultationRecordDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("htmlText").toString();
        Map<String, String> structureMap = null;
        if (contentMap.get("isParsed") != null && "1".equals(contentMap.get("isParsed").toString())) {
            structureMap = (Map) FastJsonUtils.getJsonToMap(content);
        } else {
            String recTitle = contentMap.get("recTitle").toString();
            String recTypeId = contentMap.get("recTypeId").toString();
            NingHaiYiYiHtmlAnalysis beiLunHtmlAnalysis = new NingHaiYiYiConsultationRecordHtmlAnalysis();
            Map<String, String> sourceMap = beiLunHtmlAnalysis.analysis(content, recTitle, recTypeId);
            if (MapUtils.isNotEmpty(sourceMap)) {
                structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, consultationRecord_keyContrasts);
                structureMap.put("记录编号", contentMap.get("recId").toString());
                structureMap.put("病历号", contentMap.get("behospitalCode") == null ? null : contentMap.get("behospitalCode").toString());
            }
        }
        if (MapUtils.isNotEmpty(structureMap)) {
            ConsultationRecordDoc consultationRecordDoc = ModelDocGenerate.consultationRecordDocGen(structureMap);
            consultationRecordDoc.setPageData((Map) structureMap);
            return consultationRecordDoc;
        } else {
            return null;
        }
    }

    private List<String> consultationRecord_keyContrasts = Lists.newArrayList(
            "执行情况=会诊情况",
            "记录日期=会诊申请日期",
            "签名++++=签名",
            "病情记录=简要病情",
            "医师签名=签名",
            "医生签名=记录医生",
            "医师签名=记录医生"
    );


    /**************************************************会诊结果单*******************************************************/
    private Map<String, ConsultationResultsDoc> getConsultationResultsDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, ConsultationResultsDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String consultationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("htmlText") == null || StringUtil.isBlank(contentMap.get("htmlText").toString())) {
                continue;
            }
            try {
                ConsultationResultsDoc consultationResultsDoc = getConsultationResultsDoc(contentMap);
                if (consultationResultsDoc != null) {
                    consultationName = index + "";
                    consultationResultsDoc.setConsultationName(consultationName);
                    retMap.put(consultationName, consultationResultsDoc);
                    index++;
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        return retMap;
    }

    private ConsultationResultsDoc getConsultationResultsDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("htmlText").toString();
        Map<String, String> structureMap = null;
        if (contentMap.get("isParsed") != null && "1".equals(contentMap.get("isParsed").toString())) {
            structureMap = (Map) FastJsonUtils.getJsonToMap(content);
        } else {
            String recTitle = contentMap.get("recTitle").toString();
            String recTypeId = contentMap.get("recTypeId").toString();
            NingHaiYiYiHtmlAnalysis beiLunHtmlAnalysis = new NingHaiYiYiConsultationHtmlAnalysis();
            Map<String, String> sourceMap = beiLunHtmlAnalysis.analysis(content, recTitle, recTypeId);
            if (MapUtils.isNotEmpty(sourceMap)) {
                structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, consultationResults_pageDataTitles);
                structureMap.put("记录编号", contentMap.get("recId").toString());
                structureMap.put("病历号", contentMap.get("behospitalCode") == null ? null : contentMap.get("behospitalCode").toString());
            }
        }
        if (StringUtil.isNotBlank(structureMap.get("会诊科室"))) {
            structureMap.put("会诊科室", structureMap.get("会诊科室").replaceAll("XXXX", "").trim());
        }
        if (MapUtils.isNotEmpty(structureMap)) {
            ConsultationResultsDoc consultationResultsDoc = ModelDocGenerate.consultationResultsDocGen(structureMap);
            consultationResultsDoc.setPageData((Map) structureMap);
            return consultationResultsDoc;
        } else {
            return null;
        }
    }

    private List<String> consultationResults_pageDataTitles = Lists.newArrayList(
            "科室=科别",
            "专业=科别",
            "申请会诊科别=申请科室",
            "申请时间=申请日期",
            "会诊日期=申请日期",
            //"会诊诊断=当前诊断",
            "患者病情及诊疗经过、申请会诊的理由及目的=会诊目的",
            "病情摘要=会诊目的",
            "出生年月=出生日期",
            "床位=床号",
            "外来专家意见=会诊意见",
            "由受邀医生填写，本次会诊是否必要=由受邀医生填写本次会诊是否必要",
            "单位=会诊医师所在医疗机构名称",
            "记录医师"
    );


    /**************************************************会诊申请单*******************************************************/
    private Map<String, ConsultationApplicationDoc> getConsultationApplicationDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, ConsultationApplicationDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String consultationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("htmlText") == null || StringUtil.isBlank(contentMap.get("htmlText").toString())) {
                continue;
            }
            consultationName = index + "";
            ConsultationApplicationDoc consultationApplicationDoc = getConsultationApplicationDoc(contentMap);
            consultationApplicationDoc.setConsultationName(consultationName);
            retMap.put(consultationName, consultationApplicationDoc);
            index++;
        }
        return retMap;
    }

    private ConsultationApplicationDoc getConsultationApplicationDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("htmlText").toString();
        Map<String, String> structureMap = null;
        if (contentMap.get("isParsed") != null && "1".equals(contentMap.get("isParsed").toString())) {
            structureMap = (Map) FastJsonUtils.getJsonToMap(content);
        } else {
            String recTitle = contentMap.get("recTitle").toString();
            String recTypeId = contentMap.get("recTypeId").toString();
            NingHaiYiYiHtmlAnalysis beiLunHtmlAnalysis = new NingHaiYiYiConsultationHtmlAnalysis();
            Map<String, String> sourceMap = beiLunHtmlAnalysis.analysis(content, recTitle, recTypeId);
            structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, consultationApplication_keyContrasts);
            structureMap.put("记录编号", contentMap.get("recId").toString());
            structureMap.put("病历号", contentMap.get("behospitalCode") == null ? null : contentMap.get("behospitalCode").toString());
        }

        ConsultationApplicationDoc consultationApplicationDoc = ModelDocGenerate.consultationApplicationDocGen(structureMap);
        consultationApplicationDoc.setText(content);
        consultationApplicationDoc.setPageData((Map) structureMap);

        return consultationApplicationDoc;
    }

    private List<String> consultationApplication_sourceTitles = Lists.newArrayList(
            "姓名",
            "性别",
            "病区",
            "床号",
            "病案号",
            "简要病情及诊疗",
            "申请理由目的",
            "受邀科室",
            "会诊分类",
            "会诊时间",
            "会诊地点",
            "签名"
    );

    private List<String> consultationApplication_keyContrasts = Lists.newArrayList(
            "++++姓名=姓名",
            "申请时间=申请日期",
            "性别=性别",
            "++++病区=病区",
            "床号=",
            "++++病案号=病案号",
            "新会诊-简要病情及诊疗=简要病情及诊疗",
            "新会诊-申请理由目的=会诊目的",
            "新会诊-受邀科室=受邀科室",
            "新会诊-会诊分类=会诊分类",
            "新会诊-会诊时间=会诊时间",
            "新会诊-会诊地点=会诊地点",
            "新会诊-接待医生=会诊医师",
            "新会诊-会诊意见=会诊意见",
            "新会诊-会诊科室=会诊科室",
            "签名++++=签名",
            "++++会诊医师=会诊医师"
    );

}