package com.lantone.structure.ai;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.lantone.common.util.CatalogueUtil;
import com.lantone.common.util.StringUtil;
import com.lantone.structure.ai.process.EntityProcessBlood;
import com.lantone.structure.model.Content;
import com.lantone.structure.model.InputInfo;
import com.lantone.structure.client.CRFServiceClient;
import com.lantone.structure.model.doc.ClinicalBloodDoc;
import com.lantone.structure.model.label.ClinicalBloodLabel;

import java.util.*;

/**
 * @ClassName : ClinicalbloodAI
 * @Description : 输血记录
 * @Author : wsy
 * @Date: 2021-01-19 17:16
 */
public class ClinicalbloodAI extends ModelAI {

    public static List<String> medicalTextType = Arrays.asList("Taizhou_blood_record");
    public static String entityRelationObject = "entity_relation_object";
    public static String outputs = "outputs";
    public static String content = "content";
    public static List<String> mapKey = Lists.newArrayList("病历内容");

    public void medrec(InputInfo inputInfo, CRFServiceClient crfServiceClient) {
        JSONArray crfContent = new JSONArray();
        List<ClinicalBloodDoc> resultsDocs = inputInfo.getClinicalBloodDocs();
        for (int i = 0; i < resultsDocs.size(); i++) {
            String content = resultsDocs.get(i).getText();
            putContent(crfContent, medicalTextType.get(0), content, Content.bloodRecord);
        }

        JSONObject midData = loadAI(crfContent, crfServiceClient);//crf返回数据

        for (int i = 0; i < resultsDocs.size(); i++) {
            if (midData.get(Content.bloodRecord) == null) {
                continue;
            }
            ClinicalBloodDoc resultsDoc = resultsDocs.get(i);
            ClinicalBloodLabel clinicalBloodLabel = putBloodRecordCrfData(midData.getJSONObject(Content.bloodRecord), inputInfo);
            if (clinicalBloodLabel != null) {
                resultsDoc.setClinicalBloodLabel(clinicalBloodLabel);
            }
        }
    }


    /**
     * 处理输血记录
     *
     * @param jsonObject
     */
    public ClinicalBloodLabel putBloodRecordCrfData(JSONObject jsonObject, InputInfo inputInfo) {
        ClinicalBloodLabel clinicalBloodLabel = new ClinicalBloodLabel();
        if (jsonObject == null) {
            return clinicalBloodLabel;
        }
        JSONObject aiOut = jsonObject.getJSONObject(entityRelationObject).getJSONObject(BeHospitalizedAI.outputs);
        if (aiOut == null) {
            return clinicalBloodLabel;
        }
        EntityProcessBlood entityProcessBlood = new EntityProcessBlood();
        clinicalBloodLabel = entityProcessBlood.extractEntity(aiOut);
        return clinicalBloodLabel;
    }

    protected void putContent(JSONArray crfContent, String medicalTextType, String text, String sign) {
        String move_text = CatalogueUtil.removeSpecialChar(text);
        if (StringUtil.isEmpty(move_text)) {
            return;
        }
        JSONObject detailContent = new JSONObject();
        detailContent.put("medical_text_type", medicalTextType);
        detailContent.put("content", move_text);
        detailContent.put("detail_title", sign);
        detailContent.put("originalText", text);
        crfContent.add(detailContent);
    }
}