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.EntityProcessStagesSummary;
import com.lantone.structure.client.CRFServiceClient;
import com.lantone.structure.model.Content;
import com.lantone.structure.model.InputInfo;
import com.lantone.structure.model.doc.StagesSummaryDoc;
import com.lantone.structure.model.label.StagesSummaryLabel;

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

/**
 * @ClassName : StagesSummaryAI
 * @Description : 阶段小结
 * @Author : wsy
 * @Date: 2021-01-25 17:16
 */
public class StagesSummaryAI extends ModelAI {

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

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

        for (int i = 0; i < resultsDocs.size(); i++) {
            if (midData.get(Content.stagesSummary) == null) {
                continue;
            }
            StagesSummaryDoc resultsDoc = resultsDocs.get(i);
            StagesSummaryLabel stagesSummaryLabel = putStagesSummaryCrfData(midData.getJSONObject(Content.stagesSummary), inputInfo);
            if (stagesSummaryLabel != null) {
                resultsDoc.setStagesSummaryLabel(stagesSummaryLabel);
            }
        }
    }


    /**
     * 处理阶段小结
     *
     * @param jsonObject
     */
    public StagesSummaryLabel putStagesSummaryCrfData(JSONObject jsonObject, InputInfo inputInfo) {
        StagesSummaryLabel stagesSummaryLabel = new StagesSummaryLabel();
        if (jsonObject == null) {
            return stagesSummaryLabel;
        }
        JSONObject aiOut = jsonObject.getJSONObject(entityRelationObject).getJSONObject(StagesSummaryAI.outputs);
        if (aiOut == null) {
            return stagesSummaryLabel;
        }
        EntityProcessStagesSummary entityProcessStagesSummary = new EntityProcessStagesSummary();
        stagesSummaryLabel = entityProcessStagesSummary.extractEntity(aiOut);
        return stagesSummaryLabel;
    }

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