package com.lantone.qc.trans.shaoyf;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.qc.dbanaly.facade.shaoyf.ShaoyfXmlUtil;
import com.lantone.qc.pub.model.doc.transferrecord.TransferIntoDoc;
import com.lantone.qc.pub.model.doc.transferrecord.TransferOutDoc;
import com.lantone.qc.pub.model.doc.transferrecord.TransferRecordDoc;
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.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Description: 转科记录文档生成
 * @author: rengb
 * @time: 2020/3/20 16:10
 */
public class ShaoyfTransferRecordDocTrans extends ModelDocTrans {

    public TransferRecordDoc extract(MedrecVo medrecVo) {
        TransferRecordDoc transferRecordDoc = new TransferRecordDoc();

        Map<String, List<String>> contentMap = (Map) medrecVo.getContent().get("content");
        if (contentMap == null) {
            return transferRecordDoc;
        }
        List<Map<String, Object>> into = (List) contentMap.get("转入记录");
        List<Map<String, Object>> out = (List) contentMap.get("转出记录");
        List<Map<String, Object>> all = new ArrayList<>();
        if (ListUtil.isNotEmpty(into)) {
            all.addAll(into);
        }
        if (ListUtil.isNotEmpty(out)) {
            all.addAll(out);
        }
        Map<String, TransferIntoDoc> transferIntoDocMap = getTransferIntoDocMap(into);
        Map<String, TransferOutDoc> transferOutDocMap = getTransferOutDocMap(out);
        Map<String, TransferRecordDoc> transferAllDocMap = getTransferAllDocMap(all);

        //转入
        Map<Date, TransferIntoDoc> dateRecordIn = new TreeMap<>(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                // 升序排列
                return o1.compareTo(o2);
            }
        });
        for (TransferIntoDoc transferIntoDoc : transferIntoDocMap.values()) {
            //            Map<String, String> structureMap = transferIntoDoc.getStructureMap();
            //            String inDateStr = structureMap.get("转科日期");
            //            if (StringUtil.isBlank(inDateStr)) {
            //                continue;
            //            }
            //            Date inDate = StringUtil.parseDateTime(inDateStr);
            //            if (inDate == null) {
            //                continue;
            //            }
            //            dateRecordIn.put(inDate, transferIntoDoc);
            transferRecordDoc.getTransferIntoDocs().add(transferIntoDoc);
        }
        //        transferRecordDoc.setTransferIntoDocs(new ArrayList<>(dateRecordIn.values()));
        //转出
        Map<Date, TransferOutDoc> dateRecordOut = new TreeMap<>(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                // 升序排列
                return o1.compareTo(o2);
            }
        });
        for (TransferOutDoc transferOutDoc : transferOutDocMap.values()) {
            //            Map<String, String> structureMap = transferOutDoc.getStructureMap();
            //            String outDateStr = structureMap.get("转科日期");
            //            if (StringUtil.isBlank(outDateStr)) {
            //                continue;
            //            }
            //            Date outDate = StringUtil.parseDateTime(outDateStr);
            //            if (outDate == null) {
            //                continue;
            //            }
            //            dateRecordOut.put(outDate, transferOutDoc);
            transferRecordDoc.getTransferOutDocs().add(transferOutDoc);
        }
        //        transferRecordDoc.setTransferOutDocs(new ArrayList<>(dateRecordOut.values()));

        //全部转科记录
        Map<Date, TransferRecordDoc> dateRecordAll = new TreeMap<>(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                // 升序排列
                return o1.compareTo(o2);
            }
        });
        for (TransferRecordDoc transferRecordAllDoc : transferAllDocMap.values()) {
            Map<String, String> structureMap = transferRecordAllDoc.getStructureMap();
            String transferDateStr = structureMap.get("转科日期");
            if (StringUtil.isBlank(transferDateStr)) {
                continue;
            }
            Date transferDate = StringUtil.parseDateTime(transferDateStr);
            if (transferDate == null) {
                continue;
            }
            dateRecordAll.put(transferDate, transferRecordAllDoc);
        }
        transferRecordDoc.setAllTransferDocs(new ArrayList<>(dateRecordAll.values()));

        return transferRecordDoc;
    }


    /**************************************************转入*******************************************************/
    private Map<String, TransferIntoDoc> getTransferIntoDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, TransferIntoDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String transferRecordName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            transferRecordName = index + "";
            TransferIntoDoc transferIntoDoc = getTransferIntoDoc(contentMap);
            transferIntoDoc.setTransferRecordName(transferRecordName);
            retMap.put(transferRecordName, transferIntoDoc);
            index++;
        }
        return retMap;
    }

    private TransferIntoDoc getTransferIntoDoc(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, transferInto_keyContrasts);

        TransferIntoDoc transferIntoDoc = new TransferIntoDoc();
        transferIntoDoc.setText(content);
        transferIntoDoc.setStructureMap(structureMap);
        transferIntoDoc.setPageData((Map) structureMap);

        return transferIntoDoc;
    }

    private List<String> transferInto_keyContrasts = Lists.newArrayList(
            "书写日期=病历日期",
            "转科目的=转科原因",
            "入ICU前诊治经过=诊治经过",
            "辅助检查=辅助检查结果",
            "治疗计划=治疗计划和措施",
            "书写内容=转运途中情况",
            "本人姓名=姓名"
    );

    /**************************************************全部*******************************************************/
    private Map<String, TransferRecordDoc> getTransferAllDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, TransferRecordDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String transferRecordName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            transferRecordName = index + "";
            TransferRecordDoc transferRecordDoc = getTransferAllDoc(contentMap);
            transferRecordDoc.setTransferRecordName(transferRecordName);
            retMap.put(transferRecordName, transferRecordDoc);
            index++;
        }
        return retMap;
    }

    private TransferRecordDoc getTransferAllDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("xmlText").toString();
        Map<String, String> sourceMap = ShaoyfXmlUtil.getXmlToMap(content);
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, transferInto_keyContrasts);
        TransferRecordDoc transferAllDoc = new TransferRecordDoc();
        transferAllDoc.setText(content);
        transferAllDoc.setStructureMap(structureMap);
        transferAllDoc.setPageData((Map) sourceMap);
        return transferAllDoc;
    }

    /**************************************************转出*******************************************************/
    private Map<String, TransferOutDoc> getTransferOutDocMap(List<Map<String, Object>> contentMaps) {
        Map<String, TransferOutDoc> retMap = Maps.newHashMap();
        if (ListUtil.isEmpty(contentMaps)) {
            return retMap;
        }
        int index = 1;
        String transferRecordName = null;
        for (Map<String, Object> contentMap : contentMaps) {
            if (contentMap.get("xmlText") == null || StringUtil.isBlank(contentMap.get("xmlText").toString())) {
                continue;
            }
            transferRecordName = index + "";
            TransferOutDoc transferOutDoc = getTransferOutDoc(contentMap);
            transferOutDoc.setTransferRecordName(transferRecordName);
            retMap.put(transferRecordName, transferOutDoc);
            index++;
        }
        return retMap;
    }

    private TransferOutDoc getTransferOutDoc(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, transferOut_keyContrasts);
        MapUtil.keyAssig(structureMap, "记录日期", "书写日期", "病历日期");

        TransferOutDoc transferOutDoc = new TransferOutDoc();
        transferOutDoc.setText(content);
        transferOutDoc.setStructureMap(structureMap);
        transferOutDoc.setPageData((Map) structureMap);

        return transferOutDoc;
    }

    private List<String> transferOut_keyContrasts = Lists.newArrayList(
            "书写日期=病历日期",
            "转科目的=转科原因",
            "本人姓名=姓名"
    );

}