package com.lantone.qc.trans.beilun.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:
 * @author: HUJING
 * @time: 2020/9/9 11:24
 */
@Slf4j
public class BeiLunLeaveHospitalHtmlAnalysis implements BeiLunHtmlAnalysis {

    private String modeId = "5";

    @Override
    public Map<String, String> analysis(String... args) {
        Map<String, String> structureMap = Maps.newLinkedHashMap();
        try {
            List<String> titles = Lists.newArrayList("姓名", "出生年月", "性别", "年龄", "出生日期", "科室", "病区", "床位", "床号", "住院号 ", "住院号",
                    "入院日期", "出院日期", "地址", "住院天数", "婚姻", "入院诊断", "出院诊断", "分娩日期", "分娩方式", "手术指征",
                    "入院情况", "诊治经过", "入院后检查化验结果", "入院后化验检查结果", "诊疗经过", "治疗结果", "出院计划", "病理检查结果", "出院情况", "婴儿情况",
                    "治疗效果", "出院医嘱", "医师签名", "日期", "病理号", "X片号", "MR号", "CT号", "其它", "主治医师", "住院医师");
            String html = args[0];
            String recTitle = args[1];
            String recTypeId = args[2];
            Document doc = Jsoup.parse(html);
            String htmlContent = BeiLunHtmlAnalysisUtil.blockDivToStr(doc.selectFirst("body").child(0), true);
            if (StringUtil.isNotBlank(htmlContent)) {
                htmlContent = htmlContent.replace("下拉选择无或右键调用临床数据", "");
                htmlContent = htmlContent.replaceAll("[   ]", " ").replace("第1页", "");
                htmlContent = htmlContent.replace("\n", "");
                List<String> sortTitles = CommonAnalysisUtil.sortTitles(titles, htmlContent.replace("复诊时间", ""));
                sortTitles.add("时间");
                CommonAnalysisUtil.cutByTitles(htmlContent, sortTitles, 0, structureMap);
            }
            if (structureMap.containsKey("出院诊断") && structureMap.get("出院诊断").contains("入院情况")) {
                String discharge = structureMap.get("出院诊断");
                int index = discharge.indexOf("入院情况");
                structureMap.put("出院诊断", discharge.substring(0, index));
                structureMap.put("入院情况", discharge.substring(index + 4));
            }
            CommonAnalysisUtil.extractDateByTitle(structureMap, "时间");
            BeiLunHtmlAnalysisUtil.insertModuleId(modeId, recTypeId, structureMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return structureMap;
    }
}