package com.lantone.qc.trans.taizhou;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lantone.qc.pub.model.doc.operation.OperationDiscussionDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.model.doc.operation.OperationInformedConsentDoc;
import com.lantone.qc.pub.model.doc.operation.OperationRecordDoc;
import com.lantone.qc.pub.model.doc.operation.OperationSafetyChecklistDoc;
import com.lantone.qc.pub.model.doc.operation.PreoperativeDiscussionDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.dbanaly.facade.taizhou.TzXmlUtil;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @Description: 手术文档生成
 * @author: rengb
 * @time: 2020/3/20 17:11
 */
public class TaiZhouOperationDocTrans extends ModelDocTrans {

    /**
     * 手术文档包含：术后首次病程及谈话记录；手术记录；术前讨论、术前小结；手术知情同意书；手术安全核查表
     *
     * @param medrecVo
     * @return
     */
    @Override
    public List<OperationDoc> extract(MedrecVo medrecVo) {
        List<OperationDoc> retList = Lists.newArrayList();

        Map<String, List<String>> contentMap = (Map) medrecVo.getContent().get("content");
        if (contentMap == null) {
            return retList;
        }

        Map<String, OperationDiscussionDoc> operationDiscussionDocMap = getOperationDiscussionDocMap((List) contentMap.get("术后首次病程及谈话记录"));
        Map<String, OperationRecordDoc> operationRecordDocMap = getOperationRecordDocMap((List) contentMap.get("手术记录"));
        Map<String, PreoperativeDiscussionDoc> preoperativeDiscussionDocMap = getPreoperativeDiscussionDocMap((List) contentMap.get("术前讨论、术前小结"));
        Map<String, OperationInformedConsentDoc> operationInformedConsentDocMap = getOperationInformedConsentDocMap((List) contentMap.get("手术知情同意书"));
        Map<String, OperationSafetyChecklistDoc> operationSafetyChecklistDocMap = getOperationSafetyChecklistDocMap((List) contentMap.get("手术安全核查表"));

        Set<String> operationNameSet = Sets.newHashSet();
        operationNameSet.addAll(operationDiscussionDocMap.keySet());
        operationNameSet.addAll(operationRecordDocMap.keySet());
        operationNameSet.addAll(preoperativeDiscussionDocMap.keySet());
        operationNameSet.addAll(operationInformedConsentDocMap.keySet());
        operationNameSet.addAll(operationSafetyChecklistDocMap.keySet());

        operationNameSet.forEach(operationName -> {
            OperationDoc operationDoc = new OperationDoc();
            operationDoc.setOperationName(operationName);
            operationDoc.setOperationDiscussionDoc(operationDiscussionDocMap.get(operationName));
            operationDoc.setOperationRecordDoc(operationRecordDocMap.get(operationName));
            operationDoc.setPreoperativeDiscussionDoc(preoperativeDiscussionDocMap.get(operationName));
            operationDoc.setOperationInformedConsentDoc(operationInformedConsentDocMap.get(operationName));
            operationDoc.setOperationSafetyChecklistDoc(operationSafetyChecklistDocMap.get(operationName));
            retList.add(operationDoc);
        });
        return retList;
    }


    /*******************************************术后首次病程及谈话记录***************************************************/
    private Map<String, OperationDiscussionDoc> getOperationDiscussionDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, OperationDiscussionDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String operationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            operationName = index + "";
            OperationDiscussionDoc operationDiscussionDoc = getOperationDiscussionDoc(contentMap);
            operationDiscussionDoc.setOperationName(operationName);
            retMap.put(operationName, operationDiscussionDoc);
            index++;
        }
        return retMap;
    }

    private OperationDiscussionDoc getOperationDiscussionDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZ(content);
        sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, operationDiscussion_keyContrasts);

        OperationDiscussionDoc operationDiscussionDoc = new OperationDiscussionDoc();
        operationDiscussionDoc.setStructureMap(structureMap);
        operationDiscussionDoc.setPageData((Map) structureMap);

        return operationDiscussionDoc;
    }

    private List<String> operationDiscussion_keyContrasts = Lists.newArrayList(
            "本人姓名=姓名",
            "开始时间=手术开始时间",
            "结束时间=手术结束时间",
            "书写日期=记录日期",
            "注意事项=术后注意事项",
            "治疗计划和措施=术后处理措施"
    );


    /**********************************************手术记录*************************************************************/
    private Map<String, OperationRecordDoc> getOperationRecordDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, OperationRecordDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String operationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            operationName = index + "";
            OperationRecordDoc operationRecordDoc = getOperationRecordDoc(contentMap);
            operationRecordDoc.setOperationName(operationName);
            retMap.put(operationName, operationRecordDoc);
            index++;
        }
        return retMap;
    }

    private OperationRecordDoc getOperationRecordDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZ(content);
        sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, operationRecord_keyContrasts);
        OrdinaryAssistant.removeBlank(structureMap);
        OperationRecordDoc operationRecordDoc = new OperationRecordDoc();
        operationRecordDoc.setStructureMap(structureMap);
        operationRecordDoc.setPageData((Map) structureMap);
        return operationRecordDoc;
    }

    // 手术记录
    private List<String> operationRecord_keyContrasts = Lists.newArrayList(
            "结束时间=手术结束时间",
            "开始时间=手术开始时间",
            "医生=主刀医师签名",
            "手术医师=主刀医师",
            "助手=一助",
            "本人姓名=姓名"
    );


    /**********************************************术前讨论、术前小结****************************************************/
    private Map<String, PreoperativeDiscussionDoc> getPreoperativeDiscussionDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, PreoperativeDiscussionDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String operationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            operationName = index + "";
            PreoperativeDiscussionDoc preoperativeDiscussionDoc = getPreoperativeDiscussionDoc(contentMap);
            preoperativeDiscussionDoc.setOperationName(operationName);
            retMap.put(operationName, preoperativeDiscussionDoc);
            index++;
        }
        return retMap;
    }

    private PreoperativeDiscussionDoc getPreoperativeDiscussionDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZ(content);
        sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, preoperativeDiscussion_keyContrasts);
        addKeyMapping(structureMap,"麻醉方式","拟施手术名称及麻醉方式");
        addKeyMapping(structureMap,"主刀医师签名","主刀医生");
        addKeyMapping(structureMap,"既往疾病史","其他");
        addKeyMapping(structureMap,"诊疗计划","治疗计划和措施");
        PreoperativeDiscussionDoc preoperativeDiscussionDoc = new PreoperativeDiscussionDoc();
        preoperativeDiscussionDoc.setStructureMap(structureMap);
        preoperativeDiscussionDoc.setPageData((Map) structureMap);
        return preoperativeDiscussionDoc;
    }

    private List<String> preoperativeDiscussion_keyContrasts = Lists.newArrayList(
            "参加人员及职称=参加人员",
            "主刀医师=主刀医师签名",
            "术前准备=术前准备内容",
            "注意事项=术前术后注意事项",
            "其他术前相关情况=术前术后注意事项",
            "辅助检查结果=辅助检查",
            "术中、术后防范措施=可能意外和防范措施",
            "现病史- 发病情况=发病经过",
            "本人姓名=姓名",
            "拟施手术名称和方式=拟行术式"
    );

    /**********************************************手术知情同意书****************************************************/
    private Map<String, OperationInformedConsentDoc> getOperationInformedConsentDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, OperationInformedConsentDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String operationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            operationName = index + "";
            OperationInformedConsentDoc operationInformedConsentDoc = getOperationInformedConsentDoc(contentMap);
            operationInformedConsentDoc.setOperationName(operationName);
            retMap.put(operationName, operationInformedConsentDoc);
            index++;
        }
        return retMap;
    }

    private OperationInformedConsentDoc getOperationInformedConsentDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZ(content);
        sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, operationInformedConsent_keyContrasts);

        OperationInformedConsentDoc operationInformedConsentDoc = new OperationInformedConsentDoc();
        operationInformedConsentDoc.setStructureMap(structureMap);
        operationInformedConsentDoc.setPageData((Map) structureMap);
        return operationInformedConsentDoc;
    }

    private List<String> operationInformedConsent_keyContrasts = Lists.newArrayList(
            "姓名++++患者姓名=姓名",
            "性别=",
            "病区++++病区名称=病区",
            "床号=",
            "病案号++++住院号=病案号",
            "手术医生=",
            "目前诊断=",
            "手术名称=",
            "手术指征=",
            "风险及并发症=",
            "++++保守治疗=保守治疗",
            "++++其他手术=其他手术",
            "签名++++=签名",
            "签名时间=签名时间"
    );


    /**********************************************手术安全核查表****************************************************/
    private Map<String, OperationSafetyChecklistDoc> getOperationSafetyChecklistDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, OperationSafetyChecklistDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String operationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            operationName = index + "";
            OperationSafetyChecklistDoc operationSafetyChecklistDoc = getOperationSafetyChecklistDoc(contentMap);
            operationSafetyChecklistDoc.setOperationName(operationName);
            retMap.put(operationName, operationSafetyChecklistDoc);
            index++;
        }
        return retMap;
    }

    private OperationSafetyChecklistDoc getOperationSafetyChecklistDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZ(content);
        sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, operationSafetyChecklist_keyContrasts);

        OperationSafetyChecklistDoc operationSafetyChecklistDoc = new OperationSafetyChecklistDoc();
        operationSafetyChecklistDoc.setStructureMap(structureMap);
        operationSafetyChecklistDoc.setPageData((Map) structureMap);

        return operationSafetyChecklistDoc;
    }

    private List<String> operationSafetyChecklist_keyContrasts = Lists.newArrayList(
            "姓名++++患者姓名=姓名",
            "性别",
            "病区",
            "床号",
            "病案号++++住院号=病案号",
            "签名++++=签名"
    );

    /**
     * 给structureMap添加一个key（映射）
     * @param structureMap
     * @param target    需要用到的key
     * @param source    structureMap中含有的key
     */
    protected void addKeyMapping(Map<String, String> structureMap, String target, String source) {
        if (StringUtils.isEmpty(structureMap.get(target)) && StringUtils.isNotEmpty(structureMap.get(source))) {
            structureMap.put(target, structureMap.get(source));
        }
    }

}