package com.lantone.qc.trans.yiwu;

import com.google.common.collect.Lists;
import com.lantone.qc.dbanaly.facade.yiwu.YiWuXmlUtil;
import com.lantone.qc.dbanaly.util.ModuleMappingUtil;
import com.lantone.qc.pub.model.doc.RescueDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.FastJsonUtils;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.comsis.CommonAnalysisUtil;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;

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

/**
 * @Description: 抢救记录文档生成
 * @author: rengb
 * @time: 2020/3/17 17:34
 */
public class YiWuRescueDocTrans extends ModelDocTrans {
    private String modeId = "22";

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

    private RescueDoc getRescueDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> structureMap = null;
        if (contentMap.get("isParsed") != null && "1".equals(contentMap.get("isParsed").toString())) {
            structureMap = (Map) FastJsonUtils.getJsonToMap(content);
        } else {
            Map<String, String> sourceMap = YiWuXmlUtil.xmlToMap(content);
            sourceMap.put("mode_id", ModuleMappingUtil.getDeptModuleId(modeId));
            sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
            sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
            if (sourceMap.containsKey("原始文本")) {
                String originalText = sourceMap.get("原始文本");
                List<String> titles = Lists.newArrayList("抢救记录","抢救实施的时间及措施", "抢救后的状况", "参加抢救人员姓名及专业技术职称", "医师签名");
                CommonAnalysisUtil.cutByTitlesNoColon(originalText, titles, 0, sourceMap);
            }
            structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, keyContrasts, modeId);
            if (structureMap.containsKey("抢救措施") && structureMap.containsKey("抢救实施的时间及措施")) {
                structureMap.put("抢救措施", structureMap.get("抢救措施").replace(structureMap.get("抢救实施的时间及措施"), ""));
            }
        }

        RescueDoc rescueDoc = new RescueDoc();
        rescueDoc.setStructureMap(structureMap);
        rescueDoc.setPageData((Map) structureMap);

        return rescueDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "事件日期=开始时间",
            "抢救病情=病情变化",
            "本人姓名=姓名",
            "抢救结束日期=结束时间",
            "抢救开始日期=开始时间",
            "抢救经过与措施=抢救措施",
            "病情变化情况=简要病情",
            "是否成功=抢救是否成功",
            "参与抢救人员名单=参加抢救的医务人员姓名及专业技术职称",
            "参加抢救人员名单=参加抢救的医务人员姓名及专业技术职称",
            "记录医师=记录医生",
            "患者签名时间=记录时间",
            "抢救记录=简要病情",
            "医师签名=记录医生",
            "抢救实施的时间及措施=抢救措施",
            "病程内容=抢救措施"
    );

}