package com.lantone.qc.kernel.structure.ai;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.lantone.qc.kernel.client.CRFServiceClient;
import com.lantone.qc.kernel.structure.ai.process.EntityProcessDrug;
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.doc.operation.OperationRecordDoc;
import com.lantone.qc.pub.model.entity.Drug;
import com.lantone.qc.pub.model.label.OperationDiscussionLabel;
import com.lantone.qc.pub.model.label.OperationRecordLabel;
import com.lantone.qc.pub.util.StringUtil;

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

/**
 * @ClassName : OperationAI
 * @Description :
 * @Author : 胡敬
 * @Date: 2020-03-24 09:52
 */
public class OperationAI extends ModelAI {
    /**
     *
     */
    public static List<String> medicalTextType = Arrays.asList("CourseAfterOperation_cx", "CourseRecordSRR");
    public static String entityRelationObject = "entity_relation_object";
    public static String outputs = "outputs";
    public static String content = "content";
    public static List<String> discussionKey = Lists.newArrayList("术前诊断", "术中诊断", "手术名称", "麻醉方式"
            , "手术经过", "目前情况", "术后注意事项");

    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).getOperationRecordDoc() != null) {
                OperationRecordDoc operationRecordDoc = operationDocs.get(i).getOperationRecordDoc();
                Map<String, String> structureMap = operationRecordDoc.getStructureMap();
                //存放抓取的药品
                String content = CatalogueUtil.structureMapJoin(structureMap, Lists.newArrayList("手术经过"));
                putContent(crfContent, medicalTextType.get(1), content, "手术记录" + i + "药物");
            }
            if (operationDocs.get(i).getOperationDiscussionDoc() != null) {
                OperationDiscussionDoc operationDiscussionDoc = operationDocs.get(i).getOperationDiscussionDoc();
                Map<String, String> structureMap = operationDiscussionDoc.getStructureMap();
                String discussionText = CatalogueUtil.structureMapJoin(structureMap, discussionKey);
                putContent(crfContent, medicalTextType.get(0), discussionText, Content.operation_Discussion + i);
                //存放抓取的药品
                String content = CatalogueUtil.structureMapJoin(structureMap, Lists.newArrayList("治疗计划和措施"));
                putContent(crfContent, medicalTextType.get(1), content, Content.operation_Discussion + i + "药物");
            }
        }

        JSONObject midData = loadAI(inputInfo.isUseCrfCache(), inputInfo.getMedicalRecordInfoDoc().getStructureMap().get("behospitalCode"), crfContent, crfServiceClient);//crf返回数据

        for (int i = 0; i < operationDocs.size(); i++) {
            if (midData.get("手术记录" + i + "药物") != null) {
                OperationRecordDoc operationRecordDoc = operationDocs.get(i).getOperationRecordDoc();
                //手术记录药品
                OperationRecordLabel operationRecordLabel = new OperationRecordLabel();
                operationRecordLabel.setDrugs(putDrugCrfData(midData.getJSONObject("手术记录" + i + "药物")));
            }
            if (midData.get(Content.operation_Discussion + i) != null) {
                OperationDiscussionDoc discussionDoc = operationDocs.get(i).getOperationDiscussionDoc();
                OperationDiscussionLabel discussionLabel = putOperationDiscussionCrfData(midData.getJSONObject(Content.operation_Discussion + i));
                if (discussionLabel != null) {
                    discussionDoc.setOperationDiscussionLabel(discussionLabel);
                }
                //术后首程药品
                if (midData.get(Content.operation_Discussion + i + "药物") != null) {
                    discussionLabel = operationDocs.get(i).getOperationDiscussionDoc().getOperationDiscussionLabel();
                    if (discussionLabel == null) {
                        discussionLabel = new OperationDiscussionLabel();
                    }
                    discussionLabel.setDrugs(putDrugCrfData(midData.getJSONObject(Content.operation_Discussion + i + "药物")));
                }
            }
        }
    }

    /**
     * 处理手术记录
     *
     * @param jsonObject
     */
    public OperationDiscussionLabel putOperationDiscussionCrfData(JSONObject jsonObject) {
        JSONObject aiOut = loadEntity(jsonObject, entityRelationObject, outputs, content);
        if (aiOut == null) {
            return null;
        }
        String originalText = jsonObject.getString("originalText");
        EntityProcessOperationDiscussion entityProcessOperationDiscussion = new EntityProcessOperationDiscussion();
        return entityProcessOperationDiscussion.extractEntity(aiOut);
    }

    /**
     * 存放抓取的药品
     *
     * @param jsonObject
     */
    public List<Drug> putDrugCrfData(JSONObject jsonObject) {
        JSONObject aiOut = loadEntity(jsonObject, entityRelationObject, outputs, content);
        if (aiOut == null) {
            return new ArrayList<>();
        }
        EntityProcessDrug entityProcessDrug = new EntityProcessDrug();
        List<Drug> drugs = entityProcessDrug.extractEntity(aiOut);
        return drugs;
    }

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