package com.lantone.qc.trans.beilun.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 14:56
 */
@Slf4j
public class BeiLunOperationHtmlAnalysis implements BeiLunHtmlAnalysis {

    private String modeId = "18";

    @Override
    public Map<String, String> analysis(String... args) {
        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);
            analysisGeneral(recTitle, bigDivElement, map);
            CommonAnalysisUtil.makeEmpty(map, "医生签名");
            BeiLunHtmlAnalysisUtil.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 = BeiLunHtmlAnalysisUtil.blockDivToStr(bigDivElement, true);
        CommonAnalysisUtil.extractWardInfo(recTitle, text, map);
        String bqjlText = "病情记录" + map.get("病情记录")
                .replace("提醒：有创诊疗操作记录内容包括操作名称、时间、步骤、结果及患者的一般情况，记录操作过","")
                .replace("程是否顺利，有无不良反应，术后注意事项，操作医师签名、记录时间等。","")
                .replace("手术室完成的、治疗性质的及全麻下完成的有创诊疗操作参照手术管理。（包括介入治疗、胃肠镜","")
                .replace("下肿瘤切除/粘膜下肿瘤剥除等）","");
        List<String> titles = Lists.newArrayList(
                "病情记录", "患者手术后情况", "手术简要经过(包括手术方式、术中所见、过程是否顺利、有无并发症等)", "术后去向", "术后去向", "术后诊断", "术后处理措施", "术后应当特别注意观察的事项：", "术后治疗计划(包括术后应当特别注意观察的事项)", "医生签名", "医师签名"
        );
        titles = CommonAnalysisUtil.sortTitlesNoColon(titles, bqjlText);
        CommonAnalysisUtil.cutByTitlesNoColon(bqjlText, titles, 0, map);
    }

}