package com.lantone.qc.trans.ninghaiyiyi;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.BeHospitalizedDoc;
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.ModelDocGenerate;
import com.lantone.qc.trans.comsis.OrdinaryAssistant;
import com.lantone.qc.trans.ninghaiyiyi.util.NingHaiYiYiBeHospitalizedHtmlAnalysis;
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/5 15:47
 */
@Slf4j
public class NingHaiYiYiBeHospitalizedDocTrans extends ModelDocTrans {

    @Override
    public BeHospitalizedDoc extract(MedrecVo medrecVo) {
        BeHospitalizedDoc beHospitalizedDoc = null;
        try {
            Map<String, Object> contentMap = ((List<Map>) medrecVo.getContent().get("content")).get(0);
            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 NingHaiYiYiBeHospitalizedHtmlAnalysis();
                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 (structureMap.containsKey("初步诊断")) {
                        String initDiag = structureMap.get("初步诊断");
                        if (initDiag.contains("<img")) {
                            initDiag = initDiag.split("<img")[0];
                            structureMap.put("初步诊断", initDiag);
                        }
                    }
                }
            }
            if (MapUtils.isNotEmpty(structureMap)) {
                beHospitalizedDoc = ModelDocGenerate.beHospitalizedDocGen(structureMap);
                beHospitalizedDoc.setPageData((Map) structureMap);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return beHospitalizedDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "出生地=籍贯",
            "联系地址=住址",
            "入院时间=入院日期",
            "月经婚育史=婚育史",
            "初步诊断医师签名:=初步诊断医师签名",
            "初步诊断医师签名:时 间:=初步诊断签名时间",
            "补充诊断医师签名:时 间:=补充诊断签名时间",
            "补充诊断医师签名:=补充诊断医师签名"
    );

}