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:
 * @author: HUJING
 * @time: 2020/9/9 11:24
 */
@Slf4j
public class NingHaiYiYiLeaveHospitalHtmlAnalysis implements NingHaiYiYiHtmlAnalysis {

    private String modeId = "5";

    @Override
    public Map<String, String> analysis(String... args) {
        Map<String, String> structureMap = Maps.newLinkedHashMap();
        try {
            List<String> titles = Lists.newArrayList("姓名", "出生年月", "性别", "年龄", "出生日期", "科室", "床位", "床号", "病区", "住院号 ", "住院号",
                    "病理号", "B超", "X片号", "CT号", "MRI号", "内镜", "入院诊断", "出院诊断", "入院日期", "出院日期", "地址", "住院天数", "婚姻", "分娩日期",
                    "分娩方式", "手术指征", "入院情况", "辅助检查","诊治经过", "住院经过", "入院后检查化验结果", "入院后化验检查结果", "诊疗经过", "治疗结果", "转归情况", "出院计划",
                    "病理检查结果","出院情况","出院时情况", "婴儿情况", "治疗效果", "出院医嘱", "医师签名", "日期", "时 间", "其它", "主治医师", "住院医师");
            String html = args[0];
            String recTitle = args[1];
            String recTypeId = args[2];
            Document doc = Jsoup.parse(html);
            String htmlContent = NingHaiYiYiHtmlAnalysisUtil.blockDivToStr(doc.selectFirst("body").child(0).getElementById("main"), true);
            if (StringUtil.isNotBlank(htmlContent)) {
                htmlContent = htmlContent.replaceAll("[   ]", " ")
                        .replace("第1页", "")
                        .replace("出院后请注意接听本院的随访电话：825592278255916565578435","");
                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));
            }
            //产科出院记录 出院时情况、诊治经过(入院情况、治疗经过及重要辅助检查) title没有 ：故在此特殊处理
            try
            {
                String headerContent = NingHaiYiYiHtmlAnalysisUtil.blockDivToStr(doc.selectFirst("body").child(0).getElementById("header"), true);
                if("产科".equals(StringUtil.trim(headerContent.substring(headerContent.indexOf("科室：")+"科室：".length()
                        ,headerContent.indexOf("床号")))))
                {
                    if(htmlContent.contains("诊治经过(入院情况、治疗经过及重要辅助检查)")&&htmlContent.contains("辅助检查："))
                    {
                        structureMap.put("诊疗经过",htmlContent.substring(htmlContent.indexOf("诊治经过(入院情况、治疗经过及重要辅助检查)")
                                +"诊治经过(入院情况、治疗经过及重要辅助检查)".length(),htmlContent.indexOf("辅助检查：")));
                    }
                    if(htmlContent.contains("出院时情况")&&htmlContent.contains("出院医嘱："))
                    {
                        structureMap.put("出院情况",htmlContent.substring(htmlContent.indexOf("出院时情况")
                                +"出院时情况".length(),htmlContent.indexOf("出院医嘱：")));
                    }
                }
            }
            catch (Exception e)
            {
                log.error(e.getMessage(), e);
            }

            CommonAnalysisUtil.extractDateByTitle(structureMap, "时间");
            NingHaiYiYiHtmlAnalysisUtil.insertModuleId(modeId, recTypeId, structureMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return structureMap;
    }

}