package com.lantone.qc.trans.shaoyf;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lantone.qc.dbanaly.facade.shaoyf.ShaoyfXmlUtil;
import com.lantone.qc.pub.model.doc.consultation.ConsultationApplicationDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationRecordDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationResultsDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.MapUtil;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;

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

/**
 * @Description: 会诊文档生成
 * @author: rengb
 * @time: 2020/3/17 13:22
 */
public class ShaoyfConsultationDocTrans extends ModelDocTrans {

    /**
     * 会诊记录包含3个模块：会诊申请单，会诊结果单，会诊记录
     * 配套放置，华卓需要注意顺序问题，没有字段可以关联，我们做不到check
     *
     * @param medrecVo
     * @return
     */
    @Override
    public List<ConsultationDoc> extract(MedrecVo medrecVo) {
        List<ConsultationDoc> retList = Lists.newArrayList();

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

        Map<String, ConsultationRecordDoc> consultationRecordDocMap = getConsultationRecordDocMap((List) contentMap.get("会诊记录"));
        Map<String, ConsultationResultsDoc> consultationResultsDocMap = getConsultationResultsDocMap((List) contentMap.get("会诊结果单"));
        Map<String, ConsultationApplicationDoc> consultationApplicationDocMap = getConsultationApplicationDocMap((List) contentMap.get("会诊申请单"));

        Set<String> consultationNameSet = Sets.newHashSet();
        consultationNameSet.addAll(consultationRecordDocMap.keySet());
        consultationNameSet.addAll(consultationResultsDocMap.keySet());
        consultationNameSet.addAll(consultationApplicationDocMap.keySet());

        consultationNameSet.forEach(consultationName -> {
            ConsultationDoc consultationDoc = new ConsultationDoc();
            consultationDoc.setConsultationName(consultationName);
            consultationDoc.setConsultationRecordDoc(consultationRecordDocMap.get(consultationName));
            consultationDoc.setConsultationResultsDoc(consultationResultsDocMap.get(consultationName));
            consultationDoc.setConsultationApplicationDoc(consultationApplicationDocMap.get(consultationName));
            retList.add(consultationDoc);
        });

        return retList;
    }


    /**************************************************会诊记录*********************************************************/
    private Map<String, ConsultationRecordDoc> getConsultationRecordDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, ConsultationRecordDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String consultationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            consultationName = index + "";
            ConsultationRecordDoc consultationRecordDoc = getConsultationRecordDoc(contentMap);
            consultationRecordDoc.setConsultationName(consultationName);
            retMap.put(consultationName, consultationRecordDoc);
            index++;
        }
        return retMap;
    }

    private ConsultationRecordDoc getConsultationRecordDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> sourceMap = ShaoyfXmlUtil.getXmlToMap(content);
        sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, consultationRecord_keyContrasts);

        ConsultationRecordDoc consultationRecordDoc = new ConsultationRecordDoc();
        consultationRecordDoc.setStructureMap(structureMap);
        consultationRecordDoc.setPageData((Map) structureMap);
        return consultationRecordDoc;
    }

    // 会诊记录映射字段
    private List<String> consultationRecord_keyContrasts = Lists.newArrayList(
            "事件日期=记录时间",
            "医生=记录医师",
            "临床科室=申请科室",
            "会诊执行情况=会诊情况"
    );


    /**************************************************会诊结果单*******************************************************/
    private Map<String, ConsultationResultsDoc> getConsultationResultsDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, ConsultationResultsDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String consultationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            consultationName = index + "";
            ConsultationResultsDoc consultationResultsDoc = getConsultationResultsDoc(contentMap);
            consultationResultsDoc.setConsultationName(consultationName);
            retMap.put(consultationName, consultationResultsDoc);
            index++;
        }
        return retMap;
    }

    private ConsultationResultsDoc getConsultationResultsDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> sourceMap = ShaoyfXmlUtil.getXmlToMap(content);
        sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, consultationResults_keyContrasts);

        MapUtil.keyAssig(structureMap, "申请科室", "临床科室");
        if (contentMap.get("recTitle").toString().equals("外请会诊回复单")) {
            MapUtil.keyAssig(structureMap, "会诊科室", "会诊医生");
            MapUtil.keyAssig(structureMap, "申请医生", "医师签名");
            MapUtil.keyAssig(structureMap, "申请日期", "事件日期");
        }
        ConsultationResultsDoc consultationResultsDoc = new ConsultationResultsDoc();
        consultationResultsDoc.setStructureMap(structureMap);
        consultationResultsDoc.setPageData((Map) structureMap);
        return consultationResultsDoc;
    }

    // 会诊结果映射字段
    private List<String> consultationResults_keyContrasts = Lists.newArrayList(
            "会诊诊断=当前诊断",
            "事件日期1(会诊申请日期)=申请日期",
            "医生=医师签名",
            "本人姓名=姓名",
            "病史描述=病史回顾描述",
            "简要病情=病史回顾描述",
            "病历日期=会诊日期及时间",
            "记录及建议=会诊意见",
            "临床科室1=临床科室",
            "申请医生1=申请医生"
    );


    /**************************************************会诊申请单*******************************************************/
    private Map<String, ConsultationApplicationDoc> getConsultationApplicationDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, ConsultationApplicationDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String consultationName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            consultationName = index + "";
            ConsultationApplicationDoc consultationApplicationDoc = getConsultationApplicationDoc(contentMap);
            consultationApplicationDoc.setConsultationName(consultationName);
            retMap.put(consultationName, consultationApplicationDoc);
            index++;
        }
        return retMap;
    }

    private ConsultationApplicationDoc getConsultationApplicationDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> sourceMap = ShaoyfXmlUtil.getXmlToMap(content);
        sourceMap.put("mode_id=" + contentMap.get("modeId").toString(), "");
        sourceMap.put("rec_title=" + contentMap.get("recTitle").toString(), "");
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, consultationApplication_keyContrasts);

        if (structureMap.get("会诊医生") != null) {
            String ra = structureMap.get("会诊医生").replaceAll("&amp;nbsp;", "");
            structureMap.put("会诊医生", ra);
        }
        ConsultationApplicationDoc consultationApplicationDoc = new ConsultationApplicationDoc();
        consultationApplicationDoc.setStructureMap(structureMap);
        consultationApplicationDoc.setPageData((Map) structureMap);

        return consultationApplicationDoc;
    }

    // 会诊申请单映射字段
    private List<String> consultationApplication_keyContrasts = Lists.newArrayList(
            "事件日期=申请日期",
            "会诊类别=会诊分类",
            "会诊理由和目的=申请理由目的",
            "病历日期=会诊日期及时间",
            "本人姓名=姓名",
            "病历号=病案号",
            "临床科室=申请科室"
    );

}