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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.comsis.CommonAnalysisUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

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

/**
 * @Description: 死亡记录html解析
 * @author: HUJING
 * @time: 2020/9/14 15:41
 */
@Slf4j
public class NingHaiYiYiDeathRecordHtmlAnalysis implements NingHaiYiYiHtmlAnalysis {

    private String modeId = "24";

    @Override
    public Map<String, String> analysis(String... args) {
        Map<String, String> structureMap = Maps.newLinkedHashMap();
        try {
            List<String> titles = Lists.newArrayList("入院日期", "入院时间", "死亡时间", "记录时间", "入院诊断", "入院情况",
                    "诊疗经过（重点记录病情演变、抢救经过）", "诊治经过", "死亡原因", "死亡诊断", "医师签字");
            String html = args[0];
            String recTitle = args[1];
            String recTypeId = args[2];
            Document doc = Jsoup.parse(html);
            NingHaiYiYiHtmlAnalysisUtil.tableStyle1InsertMap(doc.selectFirst("body").child(0).getElementById("header"), structureMap);
            structureMap.forEach((key, value) -> structureMap.put(key, value.replace("\n", "")));
            String htmlContent = NingHaiYiYiHtmlAnalysisUtil.blockDivToStr(doc.selectFirst("body").child(0).getElementById("main"), false);
            if (StringUtil.isNotBlank(htmlContent)) {
                htmlContent = htmlContent.replaceAll("[   ]", " ");
                CommonAnalysisUtil.html2StructureMap(titles, htmlContent, structureMap);
            }
            CommonAnalysisUtil.extractDateByTitle(structureMap, "记录时间");
            NingHaiYiYiHtmlAnalysisUtil.insertModuleId(modeId, recTypeId, structureMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return structureMap;
    }
}