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.util.CatalogueUtil;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.doc.consultation.ConsultationDoc;
import com.lantone.qc.pub.model.doc.consultation.ConsultationResultsDoc;
import com.lantone.qc.pub.model.entity.Drug;
import com.lantone.qc.pub.model.label.ConsultationResultLabel;
import com.lantone.qc.pub.util.StringUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName : ConsultationAI
 * @Description :
 * @Author : 胡敬
 * @Date: 2020-09-01 16:40
 */
public class ConsultationAI extends ModelAI {
    /**
     *
     */
    public static List<String> medicalTextType = Arrays.asList("CourseRecordSRR");
    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<ConsultationDoc> docs = inputInfo.getConsultationDocs();
        List<ConsultationResultsDoc> resultsDocs = docs.stream().map(ConsultationDoc::getConsultationResultsDoc).filter(Objects::nonNull).collect(Collectors.toList());
        for (int i = 0; i < resultsDocs.size(); i++) {
            Map<String, String> structureMap = resultsDocs.get(i).getStructureMap();
            //存放抓取的药品
            String content = CatalogueUtil.structureMapJoin(structureMap, mapKey);
            putContent(crfContent, medicalTextType.get(0), content, i + "药物");
        }

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

        for (int i = 0; i < resultsDocs.size(); i++) {
            if (midData.get(i + "药物") == null) {
                continue;
            }
            ConsultationResultsDoc resultsDoc = resultsDocs.get(i);
            ConsultationResultLabel consultationResultLabel = new ConsultationResultLabel();
            List<Drug> drugs = putDrugCrfData(midData.getJSONObject(i + "药物"));
            consultationResultLabel.setDrugs(drugs);
            resultsDoc.setConsultationResultLabel(consultationResultLabel);
        }
    }


    /**
     * 存放抓取的药品
     *
     * @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);
    }
}