package com.lantone.qc.trans.ninghaiyiyi;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.CriticallyIllNoticeDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.FastJsonUtils;
import com.lantone.qc.trans.ModelDocTrans;
import com.lantone.qc.trans.comsis.CommonAnalysisUtil;
import com.lantone.qc.trans.comsis.ModelDocGenerate;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.ninghaiyiyi.util.NingHaiYiYiCriticallyIllNoticeHtmlAnalysis;
import com.lantone.qc.trans.ninghaiyiyi.util.NingHaiYiYiHtmlAnalysis;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;

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

/**
 * @Description: 病危通知书文档生成
 * @author: rengb
 * @time: 2020/3/19 19:41
 */
@Slf4j
public class NingHaiYiYiCriticallyIllNoticeDocTrans extends ModelDocTrans {

    @Override
    public List<CriticallyIllNoticeDoc> extract(MedrecVo medrecVo) {
        List<CriticallyIllNoticeDoc> retList = Lists.newArrayList();
        List<Map<String, Object>> contentMaps = (List) medrecVo.getContent().get("content");
        contentMaps.forEach(contentMap -> {
            try {
                CriticallyIllNoticeDoc criticallyIllNoticeDoc = getCriticallyIllNoticeDoc(contentMap);
                if (criticallyIllNoticeDoc != null) {
                    retList.add(criticallyIllNoticeDoc);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
        return retList;
    }

    private CriticallyIllNoticeDoc getCriticallyIllNoticeDoc(Map<String, Object> contentMap) {
        String content = contentMap.get("htmlText").toString();
        Map<String, String> structureMap = null;
        if (contentMap.get("isParsed") != null && "1".equals(contentMap.get("isParsed").toString())) {
            structureMap = (Map) FastJsonUtils.getJsonToMap(content);
        } else {
            String recTitle = contentMap.get("recTitle").toString();
            String recTypeId = contentMap.get("recTypeId").toString();
            NingHaiYiYiHtmlAnalysis beiLunHtmlAnalysis = new NingHaiYiYiCriticallyIllNoticeHtmlAnalysis();
            Map<String, String> sourceMap = beiLunHtmlAnalysis.analysis(content, recTitle, recTypeId);
            if (MapUtils.isNotEmpty(sourceMap)) {
                structureMap = OrdinaryAssistant.mapKeyContrast(sourceMap, keyContrasts);
                structureMap.put("记录编号", contentMap.get("recId").toString());
                structureMap.put("病历号", contentMap.get("behospitalCode") == null ? null : contentMap.get("behospitalCode").toString());
            }
        }
        if (MapUtils.isNotEmpty(structureMap)) {
            CriticallyIllNoticeDoc criticallyIllNoticeDoc = ModelDocGenerate.criticallyIllNoticeDocGen(structureMap);
            criticallyIllNoticeDoc.setText(CommonAnalysisUtil.html2String(content));
            criticallyIllNoticeDoc.setPageData((Map) structureMap);
            return criticallyIllNoticeDoc;
        } else {
            return null;
        }
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "科室=科别",
            "临床诊断=当前诊断",
            "初步诊断=当前诊断",
            "患方签名=患者签名",
            "时间=医生签名时间"
    );

}