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.EntityProcessCaesareanSection;
import com.lantone.structure.client.CRFServiceClient;
import com.lantone.structure.model.Content;
import com.lantone.structure.model.InputInfo;
import com.lantone.structure.model.doc.CaesareanSectionDoc;
import com.lantone.structure.model.label.CaesareanSectionLabel;

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

/**
 * @ClassName : CaesareanSectionAI
 * @Description : 剖宫产手术
 * @Author : wsy
 * @Date: 2021-01-26 17:52
 */
public class CaesareanSectionAI extends ModelAI {

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

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

        for (int i = 0; i < resultsDocs.size(); i++) {
            if (midData.get(Content.caesareanSection) == null) {
                continue;
            }
            CaesareanSectionDoc caesareanSectionDoc = resultsDocs.get(i);
            CaesareanSectionLabel caesareanSectionLabel = putCaesareanSectionCrfData(midData.getJSONObject(Content.caesareanSection));
            if (caesareanSectionLabel != null) {
                caesareanSectionDoc.setCaesareanSectionLabel(caesareanSectionLabel);
            }
        }
    }

    /**
     * 处理剖宫产手术
     *
     * @param jsonObject
     */
    public CaesareanSectionLabel putCaesareanSectionCrfData(JSONObject jsonObject) {
        CaesareanSectionLabel caesareanSectionLabel = new CaesareanSectionLabel();
        if (jsonObject == null) {
            return caesareanSectionLabel;
        }
        JSONObject aiOut = jsonObject.getJSONObject(entityRelationObject).getJSONObject(RescueAI.outputs);
        if (aiOut == null) {
            return caesareanSectionLabel;
        }
        EntityProcessCaesareanSection entityProcessCaesareanSection = new EntityProcessCaesareanSection();
        caesareanSectionLabel = entityProcessCaesareanSection.extractEntity(aiOut);
        return caesareanSectionLabel;
    }

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