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.EntityProcessRescue;
import com.lantone.structure.client.CRFServiceClient;
import com.lantone.structure.model.Content;
import com.lantone.structure.model.InputInfo;
import com.lantone.structure.model.doc.RescueDoc;
import com.lantone.structure.model.label.RescueLabel;

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

/**
 * @ClassName : RescueAI
 * @Description :抢救记录
 * @Author : wsy
 * @Date: 2021-01-25 17:16
 */
public class RescueAI extends ModelAI {

    public static List<String> medicalTextType = Arrays.asList("Taizhou_rescue_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<RescueDoc> resultsDocs = inputInfo.getRescueDocs();
        for (int i = 0; i < resultsDocs.size(); i++) {
            String content = resultsDocs.get(i).getText();
            putContent(crfContent, medicalTextType.get(0), content, Content.rescue);
        }

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

        for (int i = 0; i < resultsDocs.size(); i++) {
            if (midData.get(Content.rescue) == null) {
                continue;
            }
            RescueDoc resultsDoc = resultsDocs.get(i);
            RescueLabel rescueLabel = putRescueCrfData(midData.getJSONObject(Content.rescue), inputInfo);
            if (rescueLabel != null) {
                resultsDoc.setRescueLabel(rescueLabel);
            }
        }
    }


    /**
     * 处理抢救记录
     *
     * @param jsonObject
     */
    public RescueLabel putRescueCrfData(JSONObject jsonObject, InputInfo inputInfo) {
        RescueLabel rescueLabel = new RescueLabel();
        if (jsonObject == null) {
            return rescueLabel;
        }
        JSONObject aiOut = jsonObject.getJSONObject(entityRelationObject).getJSONObject(RescueAI.outputs);
        if (aiOut == null) {
            return rescueLabel;
        }
        EntityProcessRescue entityProcessRescue = new EntityProcessRescue();
        rescueLabel = entityProcessRescue.extractEntity(aiOut);
        return rescueLabel;
    }

    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);
        crfContent.add(detailContent);
    }
}