package com.lantone.qc.trans.changx;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.CriticallyIllNoticeDoc;
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 ChangxCriticallyIllNoticeDocTrans extends ModelDocTrans {

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

    private CriticallyIllNoticeDoc getCriticallyIllNoticeDoc(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);

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

        CriticallyIllNoticeDoc criticallyIllNoticeDoc = ModelDocGenerate.criticallyIllNoticeDocGen(cutWordMap);
        criticallyIllNoticeDoc.setText(text);
        criticallyIllNoticeDoc.setPageData((Map) cutWordMap);

        return criticallyIllNoticeDoc;
    }

    private List<String> sourceTitles = Lists.newArrayList(
            "记录时间",
            "记录医师",
            "姓名",
            "性别",
            "病区",
            "床号",
            "病案号",
            "目前诊断",
            "签名",
            "签名时间"
    );

    private List<String> keyContrasts = Lists.newArrayList(
            "姓名++++患者姓名=姓名",
            "性别=",
            "病区++++病区名称=病区",
            "床号=",
            "病案号++++住院号=病案号",
            "目前诊断++++初步诊断=目前诊断",
            "签名++++=签名",
            "签名时间="
    );

}