package com.lantone.qc.trans.xiamen;

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 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 XiamenLeaveHospitalDocTrans 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("xmlText").toString();
            Map<String, String> structureMap = null;
            if (contentMap.get("isParsed") != null && "1".equals(contentMap.get("isParsed").toString())) {
                structureMap = (Map) FastJsonUtils.getJsonToMap(content);
                structureMap = OrdinaryAssistant.mapKeyContrast(structureMap, keyContrasts, "5");
            }
            if (MapUtils.isNotEmpty(structureMap)) {
                if (!structureMap.containsKey("出院医嘱") && structureMap.containsKey("病历|出院带药")) {
                    structureMap.put("出院医嘱", structureMap.get("病历|出院带药"));
                }
                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(
            "病历|出院诊断=出院诊断",
            "实际住院天数=住院天数",
            "入院情况=病史摘要",
            "诊疗经过=诊治经过",
            "病历|医技检查=主要实验室检查及器械检验结果",
            "出院医嘱=",
            "病历|出院复诊=出院复诊",
            "治疗结果=诊疗结果"
    );

}