package com.lantone.qc.trans.yiwu;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.qc.dbanaly.facade.yiwu.YiWuXmlUtil;
import com.lantone.qc.dbanaly.util.ModuleMappingUtil;
import com.lantone.qc.pub.model.doc.FirstCourseRecordDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.FastJsonUtils;
import com.lantone.qc.pub.util.MapUtil;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.comsis.CommonAnalysisUtil;
import com.lantone.qc.trans.comsis.ModelDocGenerate;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.lantone.qc.trans.comsis.ModelDocGenerate.structureMapJoin;

/**
 * @Description: 首次病程录文档生成
 * @author: rengb
 * @time: 2020/3/17 13:25
 */
public class YiWuFirstCourseRecordDocTrans extends ModelDocTrans {
    private String modeId = "2";

    @Override
    public FirstCourseRecordDoc extract(MedrecVo medrecVo) {
        Map<String, Object> contentMap = ((List<Map>) medrecVo.getContent().get("content")).get(0);
        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));
            List<String> titles = Lists.newArrayList("病历记录时间", "一般情况", "一、病例特点", "二、初步诊断", "三、诊断依据"
                    , "四、鉴别诊断", "五、诊疗计划", "主诊医师", "记录医师", "审核医师");
            String text = sourceMap.get("原始文本");
            titles = CommonAnalysisUtil.sortTitles(titles, text);
            CommonAnalysisUtil.cutByTitles(text, titles, 0, sourceMap);
            //新生儿科特殊处理
            if (StringUtils.isNotEmpty(sourceMap.get("病历记录时间")) && sourceMap.get("病历记录时间").split("患者").length > 1) {
                sourceMap.put("病历内容", "患者" + sourceMap.get("病历记录时间").split("患者")[1]);
            }
            Map<String, String> changeKeyMap = Maps.newHashMap();
            Iterator<Map.Entry<String, String>> it = sourceMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> entry = it.next();
                if (entry.getKey().contains("、")) {
                    changeKeyMap.put(entry.getKey().split("、")[1], sourceMap.get(entry.getKey()));
                    it.remove();
                }
            }
            sourceMap.putAll(changeKeyMap);
            //首次病程记录医师处理
            if (sourceMap.containsKey("记录医师")) {
                sourceMap.put("记录医师", sourceMap.get("记录医师").replace("主治", "").replace("主任", "").replace("医生", "").replace("医师", ""));
            }
            sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
            sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
            structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, keyContrasts, modeId);
        }
        /*
        // 最后的主诊医师处理
        //经主诊医师XXX同意后确定以下诊疗计划：......
        //......。以下诊疗方案由主诊医师/诊疗组长XXX确认
        if (StringUtils.isNotEmpty(structureMap.get("诊疗计划"))) {
            if (structureMap.get("诊疗计划").contains("诊疗方案") || structureMap.get("诊疗计划").contains("诊疗规范")) {
                String temp = structureMap.get("诊疗计划");
                List<String> titles = Lists.newArrayList("。", "；");
                for (String str : titles) {
                    String[] s = temp.split(str);
                    temp = s[s.length - 1];
                }
                List<String> titles1 = Lists.newArrayList("方案", "由", "组长", "医师", "主诊", "诊疗", "确认", "审核", "医生", "规范", "以上", "经", "审核", "、", "同意", "1", "2", "3", "4", "副主任");
                for (String str : titles1) {
                    temp = temp.replaceAll(str, "");
                }
                structureMap.put("主诊医师", temp);
                structureMap.put("诊疗计划", structureMap.get("诊疗计划").split("诊疗方案")[0]);
            }
            if (structureMap.get("诊疗计划").contains("诊疗计划") || structureMap.get("诊疗计划").contains("治疗计划")) {
                String temp = structureMap.get("诊疗计划");
                List<String> titles = Lists.newArrayList("。", "；", "：");
                for (String str : titles) {
                    String[] s = temp.split(str);
                    temp = s[0];
                }
                List<String> titles1 = Lists.newArrayList("经", "主诊", "同意", "医师", "确定", "以下", "诊疗计划", "后", "如下", "审核", "治疗计划", "医", "确认", "，");
                for (String str : titles1) {
                    temp = temp.replaceAll(str, "");
                }
                structureMap.put("主诊医师", temp);
                if (structureMap.get("诊疗计划").split("1").length > 1) {
                    structureMap.put("诊疗计划", "1" + structureMap.get("诊疗计划").split("1")[1]);
                }
            }
        }

         */
        // 病例特点=主诉+现病史-发病情况+专科检查+实验室检查+影像学检查+辅助检查结果+呼吸+脉搏+体温+血压
        //        structureMap.put("病例特点", MapUtil.getSelectString(structureMap, Arrays.asList(
        //                "主诉",
        //                "现病史-发病情况",
        //                "专科检查",
        //                "实验室检查",
        //                "影像学检查",
        //                "辅助检查结果",
        //                "呼吸",
        //                "脉搏",
        //                "体温",
        //                "血压"
        //                ))
        //        );
        // 诊断依据=性别+年龄+主诉+现病史-发病情况+专科检查+实验室检查+影像学检查+辅助检查结果+呼吸+脉搏+体温+血压
        //        structureMap.put("诊断依据", MapUtil.getSelectString(structureMap, Arrays.asList(
        //                "性别",
        //                "年龄",
        //                "主诉",
        //                "现病史-发病情况",
        //                "专科检查",
        //                "实验室检查",
        //                "影像学检查",
        //                "辅助检查结果",
        //                "呼吸",
        //                "脉搏",
        //                "体温",
        //                "血压"
        //                ))
        //        );
        // 诊疗计划=检查计划+治疗计划和措施
        structureMap.put("诊疗计划-crf", MapUtil.getSelectString(structureMap, Arrays.asList(
                "检查计划",
                "治疗计划和措施"
                ))
        );

        // 诊疗计划
        FirstCourseRecordDoc firstCourseRecordDoc = ModelDocGenerate.firstCourseRecordDocGen(structureMap);
        firstCourseRecordDoc.setPageData((Map) structureMap);
        List<String> keys = Lists.newArrayList("检查计划", "治疗计划", "诊疗计划");
        String treatPlanJoin = structureMapJoin(structureMap, keys);
        firstCourseRecordDoc.getTreatPlanLabel().setAiText(treatPlanJoin);
        return firstCourseRecordDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "本人姓名=姓名",
            "现病史- 发病情况=现病史",
            "现病史- 发病情况=现病史",
            "医生=记录医生",
            "治疗计划和措施=治疗计划",
            "体温(耳)=体温耳",
            "体温(口)=体温口",
            "病程时间=病历日期",
            "记录医师=记录医生",
            "病例日期=病历日期",
            "一般情况=病历内容"
    );

}