package com.lantone.qc.trans.changx;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.ClinicalBloodDoc;
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/17 13:21
 */
public class ChangxClinicalBloodDocTrans extends ModelDocTrans {

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

    private ClinicalBloodDoc getClinicalBloodDoc(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, "");
        cutWordMap.putAll(structureMap);
        if (StringUtil.isNotBlank(structureMap.get("输注种类、血型、数量"))) {
            cutWordMap.put("输血成分及数量", structureMap.get("输注种类、血型、数量"));
        }

        ClinicalBloodDoc clinicalBloodDoc = ModelDocGenerate.clinicalBloodDocGen(cutWordMap);
        clinicalBloodDoc.setText(text);
        clinicalBloodDoc.setPageData((Map) cutWordMap);

        return clinicalBloodDoc;
    }

    private List<String> sourceTitles = Lists.newArrayList(
            "输注原因",
            "输注时间",
            "输注开始",
            "输注结束",
            "输注种类、血型、数量",
            "输注过程",
            "输注后效果评价",
            "记录医师",
            "记录时间"
    );

    private List<String> keyContrasts = Lists.newArrayList(
            "记录日期=记录时间",
            "输注原因=",
            "输注开始=",
            "输注结束=",
            "输注种类、血型、数量=",
            "输注过程=",
            "++++复选框=输注方式",
            "签名++++=记录医师",
            "签名时间="
    );

}