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:28
 */
@Slf4j
public class NingHaiYiYiCriticallyIllNoticeHtmlAnalysis implements NingHaiYiYiHtmlAnalysis {

    private String modeId = "25";

    @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"), true);
            if (StringUtil.isNotBlank(htmlContent)) {
                if (htmlContent.contains("宁海县第一医院重、危病员通知单（交患方）")) {
                    htmlContent = htmlContent.substring(0, htmlContent.indexOf("宁海县第一医院重、危病员通知单（交患方）"));
                }
                htmlContent = htmlContent.replaceAll("[   ]", " ").replace("--", "");
                CommonAnalysisUtil.html2StructureMap(titles, htmlContent, structureMap);
            }
            CommonAnalysisUtil.extractDateByTitle(structureMap, "告知时间");
            NingHaiYiYiHtmlAnalysisUtil.insertModuleId(modeId, recTypeId, structureMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return structureMap;
    }

}