package com.lantone.qc.trans.changx;

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.ListUtil;
import com.lantone.qc.pub.util.MapUtil;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.dbanaly.facade.changx.CxXmlUtil;
import com.lantone.qc.trans.comsis.ModelDocGenerate;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.comsis.Preproc;

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

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

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

        Map<String, List<String>> 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("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            consultationName = index + "";
            ConsultationRecordDoc consultationRecordDoc = getConsultationRecordDoc(contentMap);
            consultationRecordDoc.setConsultationName(consultationName);
            retMap.put(consultationName, consultationRecordDoc);
            index++;
        }
        return retMap;
    }

    private ConsultationRecordDoc getConsultationRecordDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue(content);
        xmlNodeValueMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        xmlNodeValueMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, consultationRecord_keyContrasts);

        String text = CxXmlUtil.getXmlText(content);
        Map<String, String> cutWordMap = Preproc.getCutWordMap(true, consultationRecord_sourceTitles, text);
        cutWordMap.putAll(structureMap);
        MapUtil.keyAssig(cutWordMap, "记录医师", "签名");

        ConsultationRecordDoc consultationRecordDoc = ModelDocGenerate.consultationRecordDocGen(cutWordMap);
        consultationRecordDoc.setText(text);
        consultationRecordDoc.setPageData((Map) cutWordMap);

        return consultationRecordDoc;
    }

    private List<String> consultationRecord_sourceTitles = Lists.newArrayList(
            "医师签名"
    );

    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("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            consultationName = index + "";
            ConsultationResultsDoc consultationResultsDoc = getConsultationResultsDoc(contentMap);
            consultationResultsDoc.setConsultationName(consultationName);
            retMap.put(consultationName, consultationResultsDoc);
            index++;
        }
        return retMap;
    }

    private ConsultationResultsDoc getConsultationResultsDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue(content);
        xmlNodeValueMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        xmlNodeValueMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, consultationApplication_keyContrasts);

        String text = CxXmlUtil.getXmlText(content);
        Map<String, String> cutWordMap = Preproc.getCutWordMap(true, consultationApplication_sourceTitles, text);
        cutWordMap.putAll(structureMap);

        ConsultationResultsDoc consultationResultsDoc = ModelDocGenerate.consultationResultsDocGen(cutWordMap);
        consultationResultsDoc.setText(content);
        consultationResultsDoc.setPageData((Map) cutWordMap);
        return consultationResultsDoc;
    }

    private List<String> consultationResults_pageDataTitles = Lists.newArrayList(
            "病程记录时间",
            "病程记录名称",
            "病程记录内容",
            "记录医师"
    );
    private List<String> consultationResults_sourceTitles = 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("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").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("xmlText").toString();
        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue(content);
        xmlNodeValueMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        xmlNodeValueMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, consultationApplication_keyContrasts);

        String text = CxXmlUtil.getXmlText(content);
        Map<String, String> cutWordMap = Preproc.getCutWordMap(true, consultationApplication_sourceTitles, text);
        cutWordMap.putAll(structureMap);

        ConsultationApplicationDoc consultationApplicationDoc = ModelDocGenerate.consultationApplicationDocGen(cutWordMap);
        consultationApplicationDoc.setText(text);
        consultationApplicationDoc.setPageData((Map) cutWordMap);

        return consultationApplicationDoc;
    }

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

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

}