package com.lantone.qc.trans.ywzxyy.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.qc.dbanaly.facade.ywzxyy.YwzxyyXmlUtil;
import com.lantone.qc.pub.util.MapUtil;
import com.lantone.qc.trans.comsis.CommonAnalysisUtil;
import lombok.extern.slf4j.Slf4j;

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

/**
 * @Description:
 * @author: rengb
 * @time: 2021/1/25 14:04
 */
@Slf4j
public class YwzxyyClinicalBloodXmlAnalysis implements YwzxyyXmlAnalysis {

    @Override
    public Map<String, String> analysis(String... args) {
        Map<String, String> level1StructureMap = Maps.newHashMap();
        try {
            String xml = args[0];
            String recTitle = args[1];
            String level1Text = YwzxyyXmlUtil.bcXmlAna(xml);
            List<String> level1Titles = CommonAnalysisUtil.sortTitles(Lists.newArrayList("病历记录时间", "病历记录标题", "原始文本", "医师签名", "记录医师"), level1Text);
            CommonAnalysisUtil.cutByTitles(level1Text, level1Titles, 0, level1StructureMap);
            MapUtil.keyAssig(level1StructureMap, "记录医生", "记录医师", "医师签名");
            MapUtil.keyAssig(level1StructureMap, "病历内容", "原始文本");
            MapUtil.keyAssig(level1StructureMap, "标题", "病历记录标题");
            MapUtil.keyAssig(level1StructureMap, "病历日期", "病历记录时间");
            MapUtil.keyAssig(level1StructureMap, "记录时间", "病历记录时间");

            if (recTitle.equals("输血记录") || recTitle.equals("输血浆记录")) {
                level1StructureMap.putAll(sxjl(level1StructureMap.get("原始文本")));
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return level1StructureMap;
    }

    private Map<String, String> sxjl(String level2Text) {
        Map<String, String> level2StructureMap = Maps.newHashMap();
        List<String> level2Titles = CommonAnalysisUtil.sortTitles(
                Lists.newArrayList("输血指征", "输血前用药", "输血开始时间", "输血结束时间",
                        "输血血型种类及数量", "输血血型、种类及数量", "输血过程是否顺利", "输血反应", "输血后评估"),
                level2Text
        );
        CommonAnalysisUtil.cutByTitles(level2Text, level2Titles, 0, level2StructureMap);
        MapUtil.keyAssig(level2StructureMap, "输注开始", "输血开始时间");
        MapUtil.keyAssig(level2StructureMap, "输注结束", "输血结束时间");
        MapUtil.keyAssig(level2StructureMap, "输注种类、血型、数量", "输血血型种类及数量", "输血血型、种类及数量");

        CommonAnalysisUtil.repKey(level2StructureMap, "输血开始时间", " ");
        CommonAnalysisUtil.repKey(level2StructureMap, "输血结束时间", " ");
        CommonAnalysisUtil.repKey(level2StructureMap, "输注开始", " ");
        CommonAnalysisUtil.repKey(level2StructureMap, "输注结束", " ");
        return level2StructureMap;
    }

}