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.EntityProcessOperationDiscussion;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.Content;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.doc.operation.OperationDiscussionDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.model.label.OperationDiscussionLabel;
import com.lantone.qc.pub.util.StringUtil;

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

/**
 * @ClassName : OperationAI
 * @Description :
 * @Author : 胡敬
 * @Date: 2020-03-24 09:52
 */
public class OperationAI extends ModelAI {
    /**
     *
     */
    public static List<String> medicalTextType = Arrays.asList("CourseAfterOperation_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();
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        for (int i = 0; i < operationDocs.size(); i++) {
            if (operationDocs.get(i).getOperationDiscussionDoc() == null) {
                continue;
            }
            OperationDiscussionDoc operationDiscussionDoc = operationDocs.get(i).getOperationDiscussionDoc();
            putContent(crfContent, medicalTextType.get(0), operationDiscussionDoc.getText(), Content.operation_Discussion + i);
        }

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

        for (int i = 0; i < operationDocs.size(); i++) {
            if (midData.get(Content.operation_Discussion + i) == null) {
                continue;
            }
            putOperationDiscussionCrfData(midData.getJSONObject(Content.operation_Discussion + i), inputInfo);
        }
    }

    /**
     * 处理手术记录
     *
     * @param jsonObject
     * @param inputInfo
     */
    public void putOperationDiscussionCrfData(JSONObject jsonObject, InputInfo inputInfo) {
        JSONObject aiOut = loadEntity(jsonObject, entityRelationObject, outputs, content);
        if (aiOut == null) {
            return;
        }
        String originalText = jsonObject.getString("originalText");
        //使用现病史结构来处理病历特点
        EntityProcessOperationDiscussion entityProcessOperationDiscussion = new EntityProcessOperationDiscussion();
        OperationDiscussionLabel operationDiscussionLabel = entityProcessOperationDiscussion.extractEntity(aiOut);
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        for (OperationDoc operationDoc : operationDocs) {
            OperationDiscussionDoc operationDiscussionDoc = operationDoc.getOperationDiscussionDoc();
            String text = operationDiscussionDoc.getText();
            if (StringUtil.isNotBlank(text) && StringUtil.isNotBlank(originalText) && text.equals(originalText)) {
                operationDiscussionDoc.setOperationDiscussionLabel(operationDiscussionLabel);
                break;
            }
        }
    }

    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);
    }
}