package com.lantone.qc.trans.changx;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.CrisisValueReportDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
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;

/**
 * @Description: 危急值记录文档生成
 * @author: rengb
 * @time: 2020/3/19 19:41
 */
public class ChangxCrisisValueReportDocTrans extends ModelDocTrans {

    @Override
    public List<CrisisValueReportDoc> extract(MedrecVo medrecVo) {
        List<CrisisValueReportDoc> retList = Lists.newArrayList();
        List<Map<String, Object>> contentMaps = (List) medrecVo.getContent().get("content");
        contentMaps.forEach(contentMap -> {
            retList.add(getCrisisValueReportDoc(contentMap));
        });
        return retList;
    }

    private CrisisValueReportDoc getCrisisValueReportDoc(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, keyContrasts);
        structureMap.put("医生姓名", structureMap.get("医生签名"));

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

        CrisisValueReportDoc crisisValueReportDoc = ModelDocGenerate.crisisValueReportDocGen(cutWordMap);
        crisisValueReportDoc.setText(text);
        crisisValueReportDoc.setPageData((Map) cutWordMap);

        return crisisValueReportDoc;
    }

    private List<String> sourceTitles = Lists.newArrayList(
            "记录时间",
            "记录医师",
            "患者临床诊断",
            "危急值记录内容",
            "报告部门",
            "报告人姓名",
            "报告时间",
            "接收人姓名",
            "接收时间",
            "病情分析及处理",
            "医生签名",
            "医生姓名",
            "医生签名时间",
            "签名时间"
    );

    private List<String> keyContrasts = Lists.newArrayList(
            "记录日期=记录时间",
            "危急值记录=危急值记录内容",
            "接收时间=",
            "病情分析及处理=",
            "++++病情评估=",
            "签名++++=医生签名",
            "签名时间=医生签名时间"
    );

}