package com.lantone.qc.kernel.structure.ai;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lantone.qc.kernel.client.CRFServiceClient;
import com.lantone.qc.kernel.structure.ai.process.EntityProcessLeaveHospital;
import com.lantone.qc.pub.Content;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.doc.LeaveHospitalDoc;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.model.label.LeaveHospitalLabel;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @ClassName : DischargeAI
 * @Description :
 * @Author : 胡敬
 * @Date: 2020-03-20 11:00
 */
public class LeaveHospitalAI extends ModelAI {
    /**
     *
     */
    public static List<String> medicalTextType = Arrays.asList("DischargeQA_cx", "Diagnoses_cx");
    public static String entityRelationObject = "entity_relation_object";
    public static String outputs = "outputs";
    public static String content = "content";

    public void medrec(InputInfo inputInfo, CRFServiceClient crfServiceClient) {
        JSONArray crfContent = new JSONArray();
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        if (leaveHospitalDoc != null) {
            if (StringUtils.isNotEmpty(leaveHospitalDoc.getText())) {
                //出院小结全部文本(之后考虑删除传全部文本，改为传相应结构化文本)
                putContent(crfContent, medicalTextType.get(0), leaveHospitalDoc.getText(), Content.discharge);
            }

            Map<String, String> leaveHospitalStructureMap = leaveHospitalDoc.getStructureMap();
            if (leaveHospitalStructureMap != null) {
                //                //入院情况
                //                if (leaveHospitalStructureMap.get(Content.inStatus) != null) {
                //                    putContent(crfContent, medicalTextType.get(1), leaveHospitalStructureMap.get(Content.inStatus), Content.inStatus);
                //                }
                //出院诊断
                if (leaveHospitalStructureMap.get(Content.dischargeDiag) != null) {
                    putContent(crfContent, medicalTextType.get(1), leaveHospitalStructureMap.get(Content.dischargeDiag), Content.dischargeDiag);
                }
            }
        }
        JSONObject midData = loadAI(crfContent, crfServiceClient);//crf返回数据
        if (midData.containsKey(Content.discharge)) {
            putDischargeCrfData(midData.getJSONObject(Content.discharge), inputInfo);//出院小结
        }
        if (midData.containsKey(Content.dischargeDiag)) {
            putDischargeCrfData(midData.getJSONObject(Content.dischargeDiag), inputInfo);//出院诊断
        }
    }


    /**
     * 病历特点
     *
     * @param jsonObject
     * @param inputInfo
     */
    public void putDischargeCrfData(JSONObject jsonObject, InputInfo inputInfo) {
        JSONObject aiOut = loadEntity(jsonObject, entityRelationObject, outputs, content);
        if (aiOut == null) {
            return;
        }
        //使用现病史结构来处理病历特点
        EntityProcessLeaveHospital entityProcessLeaveHospital = new EntityProcessLeaveHospital();
        LeaveHospitalLabel leaveHospitalLabel = entityProcessLeaveHospital.extractEntity(aiOut);
        if (inputInfo.getLeaveHospitalDoc().getLeaveDiagLabel() == null) {
            DiagLabel diagLabel = new DiagLabel();
            diagLabel.setDiags(leaveHospitalLabel.getDiags());
        } else {
            inputInfo.getLeaveHospitalDoc().getLeaveDiagLabel().setDiags(leaveHospitalLabel.getDiags());
        }
        //临床表现
        inputInfo.getLeaveHospitalDoc().setLeaveHospitalLabel(leaveHospitalLabel);
    }
}