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

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

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

/**
 * @Description:
 * @author: rengb
 * @time: 2020/9/28 17:56
 */
@Slf4j
public class NingHaiYiYiClinicalBloodHtmlAnalysis implements NingHaiYiYiHtmlAnalysis {


    private String modeId = "10";

    @Override
    public Map<String, String> analysis(String... args) {
        List<String> titles = Lists.newArrayList("输血前症状", "输血前实验室指标", "输血目的", "输注成分", "输血时间",
                "开始时间", "结束时间", "输血过程");
        Map<String, String> map = Maps.newLinkedHashMap();
        try {
            String html = args[0];
            String recTitle = args[1];
            String recTypeId = args[2];
            Element bigDivElement = Jsoup.parse(html).selectFirst("body").child(0).getElementById("main");
            analysisGeneral(recTitle,bigDivElement, map);
            if (map.containsKey("病情记录")) {
                String content = map.get("病情记录");
                CommonAnalysisUtil.cutByTitles(content, titles, 0, map);
            }
            if (titles.size() > 6) {
                titles.remove(5);
            }
            titles.forEach(i -> {
                        if (map.get(i).length() > 2) {
                            map.put(i, map.get(i).substring(0, map.get(i).length() - 2));
                        }
                    }
            );
            NingHaiYiYiHtmlAnalysisUtil.insertModuleId(modeId, recTypeId, map);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return map;
    }

    private void analysisGeneral(String recTitle,Element bigDivElement, Map<String, String> map) {
        if (bigDivElement.childNodeSize() == 1) {
            bigDivElement = bigDivElement.child(0);
        }
        if (bigDivElement.selectFirst("hr") != null) {
            bigDivElement.selectFirst("hr").previousElementSiblings().remove();
        }
        String text = NingHaiYiYiHtmlAnalysisUtil.blockDivToStr(bigDivElement, true);
        CommonAnalysisUtil.NHExtractWardInfo(recTitle,text, map);
        String bqjlText = "病情记录" + map.get("病情记录");
        List<String> titles = Lists.newArrayList(
                "病情记录","医生签名","医师签名"
        );
        titles = CommonAnalysisUtil.sortTitlesNoColon(titles, bqjlText);
        CommonAnalysisUtil.cutByTitlesNoColon(bqjlText, titles, 0, map);
    }

}