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.StringUtil;
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 YwzxyyFirstCourseRecordXmlAnalysis implements YwzxyyXmlAnalysis {

    @Override
    public Map<String, String> analysis(String... args) {
        Map<String, String> level1StructureMap = Maps.newHashMap();
        try {
            String xml = args[0];
            xml = StringUtil.matRegx(xml, "(<病情摘要>)[\\s\\S]*(</病情摘要>)", "(<病人姓名>)[\\s\\S]*(</病情摘要>)", " </病情摘要>");
            String level1Text = YwzxyyXmlUtil.bcXmlAna(xml);
            List<String> level1Titles = CommonAnalysisUtil.sortTitles(Lists.newArrayList("病历记录时间", "病历记录标题", "原始文本", "记录医师"), level1Text);
            CommonAnalysisUtil.cutByTitles(level1Text, level1Titles, 0, level1StructureMap);

            Map<String, String> level2StructureMap = Maps.newHashMap();
            String level2Text = level1StructureMap.get("原始文本");
            List<String> level2Titles = CommonAnalysisUtil.sortTitles(Lists.newArrayList("病例特点", "诊断依据", "鉴别诊断", "初步诊断", "诊疗计划"), level2Text);
            CommonAnalysisUtil.cutByTitles(level2Text, level2Titles, 0, level2StructureMap);
            level2StructureMap.put("病例摘要", level2Text.substring(0, level2Text.indexOf("一、病例特点")));
            level2StructureMap.keySet().forEach(key -> {
                level2StructureMap.put(key, StringUtil.removeWN(level2StructureMap.get(key), "[一二三四五六七八九][、]", 2));
            });
            level1StructureMap.putAll(level2StructureMap);
            level1StructureMap.put("记录医生", level1StructureMap.get("记录医师"));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return level1StructureMap;
    }

}