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.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.StringUtil;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.taizhou.util.TzXmlUtil;

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

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

    @Override
    public List<TransferRecordDoc> extract(MedrecVo medrecVo) {
        List<TransferRecordDoc> retList = Lists.newArrayList();

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

        Map<String, TransferIntoDoc> transferIntoDocMap = getTransferIntoDocMap(contentMap.get("转入"));
        Map<String, TransferOutDoc> transferOutDocMap = getTransferOutDocMap(contentMap.get("转出"));

        Set<String> transferRecordNameSet = Sets.newHashSet();
        transferRecordNameSet.addAll(transferIntoDocMap.keySet());
        transferRecordNameSet.addAll(transferOutDocMap.keySet());

        transferRecordNameSet.forEach(transferRecordName -> {
            TransferRecordDoc transferRecordDoc = new TransferRecordDoc();
            transferRecordDoc.setTransferRecordName(transferRecordName);
            transferRecordDoc.setTransferIntoDoc(transferIntoDocMap.get(transferRecordName));
            transferRecordDoc.setTransferOutDoc(transferOutDocMap.get(transferRecordName));
            retList.add(transferRecordDoc);
        });

        return retList;
    }


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

    private TransferIntoDoc getTransferIntoDoc(String content) {
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(TzXmlUtil.getXmlToMapForTZ(content), transferInto_keyContrasts);

        TransferIntoDoc transferIntoDoc = new TransferIntoDoc();
        transferIntoDoc.setStructureMap(structureMap);

        return transferIntoDoc;
    }

    private List<String> transferInto_keyContrasts = Lists.newArrayList();


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

    private TransferOutDoc getTransferOutDoc(String content) {
        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(TzXmlUtil.getXmlToMapForTZ(content), transferOut_keyContrasts);

        TransferOutDoc transferOutDoc = new TransferOutDoc();
        transferOutDoc.setStructureMap(structureMap);

        return transferOutDoc;
    }

    private List<String> transferOut_keyContrasts = Lists.newArrayList();

}