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.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 YwzxyyOperationRecordXmlAnalysis implements YwzxyyXmlAnalysis {

    @Override
    public Map<String, String> analysis(String... args) {
        Map<String, String> structureMap = Maps.newHashMap();
        try {
            String xml = args[0];
            String text = YwzxyyXmlUtil.xml2Txt(xml);
            List<String> titles = CommonAnalysisUtil.sortTitles(
                    Lists.newArrayList("手术开始时间", "手术结束时间", "手术前诊断", "手术名称", "手术后诊断",
                            "手术人员", "麻醉方式", "麻醉医师", "手术标本", "冰冻切片结果", "术中并发症", "术中出血量估计",
                            "术中输血情况", "输血不良反应", "手术经过及处理（包括患者的体位、切口处理、病灶所见及手术步骤）",
                            "手术经过", "医师签名", "记录时间"),
                    text
            );
            CommonAnalysisUtil.cutByTitles(text, titles, 0, structureMap);

            MapUtil.keyAssig(structureMap, "开始时间", "手术开始时间");
            MapUtil.keyAssig(structureMap, "结束时间", "手术结束时间");
            MapUtil.keyAssig(structureMap, "术前诊断", "手术前诊断");
            MapUtil.keyAssig(structureMap, "术中后诊断", "手术后诊断");
            MapUtil.keyAssig(structureMap, "手术经过及处理", "手术经过", "手术经过及处理（包括患者的体位、切口处理、病灶所见及手术步骤）");
            MapUtil.keyAssig(structureMap, "记录医生", "医师签名", "记录医师");

            CommonAnalysisUtil.repKey(structureMap, "记录时间", "时间");
            CommonAnalysisUtil.repKey(structureMap, "记录医生", "记录医师");
            if (StringUtil.isNotBlank(structureMap.get("手术开始时间"))) {
                if (structureMap.get("手术开始时间").contains(".")) {
                    structureMap.put("手术开始时间", structureMap.get("手术开始时间").replace(".", "/"));
                } else {
                    CommonAnalysisUtil.repKey(structureMap, "手术开始时间", " ");
                }
                structureMap.put("手术开始时间", structureMap.get("手术开始时间").replace("点", "时"));
            }

            if (StringUtil.isNotBlank(structureMap.get("手术结束时间"))) {
                if (structureMap.get("手术结束时间").contains(".")) {
                    structureMap.put("手术结束时间", structureMap.get("手术结束时间").replace(".", "/"));
                } else {
                    CommonAnalysisUtil.repKey(structureMap, "手术结束时间", " ");
                }
                structureMap.put("手术结束时间", structureMap.get("手术结束时间").replace("点", "时"));
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return structureMap;
    }

}