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.StagesSummaryDoc;
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/19 19:41
 */
public class YiWuStagesSummaryDocTrans extends ModelDocTrans {
    private String modeId = "28";

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

    private StagesSummaryDoc getStagesSummaryDoc(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(), "");
            List<String> titles = Lists.newArrayList("入院时情况", "入院诊断", "辅助检查",
                    "诊治经过", "目前情况", "目前诊断", "进一步诊疗方案",
                    "记录医师");
            String text = sourceMap.get("原始文本");
            List<String> sortTitles = CommonAnalysisUtil.sortTitles(titles, text);
            CommonAnalysisUtil.cutByTitles(text, sortTitles, 0, sourceMap);
            structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, keyContrasts, modeId);
            if(structureMap.containsKey("病程内容")){
                structureMap.put("病程内容",structureMap.get("病程内容").split("入院时情况")[0]);
            }
        }

        StagesSummaryDoc stagesSummaryDoc = new StagesSummaryDoc();
        stagesSummaryDoc.setStructureMap(structureMap);
        stagesSummaryDoc.setPageData((Map) structureMap);

        return stagesSummaryDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "本人姓名=姓名",
            "现病史- 发病情况=发病情况",
            "体温(耳)=体温耳",
            "医师签名=记录医生",
            "进一步诊疗方案=诊疗计划",
            "入院时情况=入院情况",
            "记录医师=记录医生",
            "病程时间=记录时间"
    );

}