package com.lantone.qc.trans.ninghaiyiyi;

import com.google.common.collect.Lists;
import com.lantone.qc.pub.model.doc.LeaveHospitalDoc;
import com.lantone.qc.pub.model.label.LeaveHospitalDoctorAdviceLabel;
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.NingHaiYiYiHtmlAnalysis;
import com.lantone.qc.trans.ninghaiyiyi.util.NingHaiYiYiLeaveHospitalHtmlAnalysis;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @Description: 出院小结文档生成
 * @author: rengb
 * @time: 2020/3/17 13:25
 */
@Slf4j
public class NingHaiYiYiLeaveHospitalDocTrans extends ModelDocTrans {

    @Override
    public LeaveHospitalDoc extract(MedrecVo medrecVo) {
        LeaveHospitalDoc leaveHospitalDoc = 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 NingHaiYiYiLeaveHospitalHtmlAnalysis();
                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)) {
                leaveHospitalDoc = ModelDocGenerate.leaveHospitalDocGen(structureMap);
                if (StringUtils.isNotEmpty(structureMap.get("出院医嘱"))) {
                    LeaveHospitalDoctorAdviceLabel leaveHospitalDoctorAdviceLabel = new LeaveHospitalDoctorAdviceLabel();
                    leaveHospitalDoctorAdviceLabel.setText(structureMap.get("出院医嘱"));
                    leaveHospitalDoctorAdviceLabel.setAiText(structureMap.get("出院医嘱"));
                    leaveHospitalDoc.setLeaveHospitalDoctorAdviceLabel(leaveHospitalDoctorAdviceLabel);
                }
                leaveHospitalDoc.setText(CommonAnalysisUtil.html2String(content));
                leaveHospitalDoc.setPageData((Map) structureMap);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return leaveHospitalDoc;
    }

    private List<String> keyContrasts = Lists.newArrayList(
            "患者姓名=姓名",
            "性别=",
            "床号=",
            "年龄=",
            "科室=科别",
            "入院诊断=入院诊断",
            "出院诊断=出院诊断",
            "实际住院天数=住院天数",
            "入院情况=入院情况",
            "诊疗经过=诊治经过",
            "住院经过=诊治经过",
            "入院情况=病史摘要",
            "辅助检查=主要实验室检查及器械检验结果",
            "出院情况=",
            "出院医嘱=",
            "随访计划=",
            "医师签名=记录医生",
            "时间=记录时间",
            "其它=其他",
            "出生年月=出生日期",
            "床位=床号",
            "时间=记录时间",
            "转归情况=治疗结果"
    );

}