package com.lantone.qc.kernel.catalogue.behospitalized;

import com.alibaba.fastjson.JSONArray;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.client.SimilarityServiceClient;
import com.lantone.qc.kernel.structure.ai.FirstCourseRecordAI;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.BeHospitalizedDoc;
import com.lantone.qc.pub.model.entity.Clinical;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.util.ListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @ClassName : BEH0449
 * @Description : 现病史主症状与主诉症状不一致
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class BEH0449 extends QCCatalogue {
    @Autowired
    SimilarityServiceClient similarityServiceClient;

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        String chief_text = beHospitalizedDoc.getChiefLabel().getText();
        if (chief_text.contains("检查") || chief_text.contains("术后") || chief_text.contains("药物")
                || chief_text.contains("发现")) {
            status.set("0");
            return;
        } else {
            List<Clinical> clinicals_chief = beHospitalizedDoc.getChiefLabel().getClinicals();
            List<Clinical> clinicals_present = beHospitalizedDoc.getPresentLabel().getClinicals();
            if (ListUtil.isNotEmpty(clinicals_chief) && ListUtil.isNotEmpty(clinicals_present)) {
                Clinical clinical_chief = clinicals_chief.get(0);
                clinicals_present = clinicals_present.stream().filter(clinical -> clinical.getNegative() == null).collect(Collectors.toList());
                for (Clinical cliPre : clinicals_present) {
                    String name_pre = cliPre.getName();
                    String name_chief = clinical_chief.getName();
                    if (name_chief.equals(name_pre)) {
                        status.set("0");
                        return;
                    } else if (name_pre.contains(name_chief) || name_chief.contains(name_pre)) {
                        status.set("0");
                        return;
                    } else {
                        JSONArray similarContent = new JSONArray();
                        FirstCourseRecordAI firstCourseRecordAI = new FirstCourseRecordAI();
                        firstCourseRecordAI.putContent(similarContent, name_chief, name_pre);
                        double likeRate = firstCourseRecordAI.loadSimilarAI(similarContent, similarityServiceClient);
                        if (likeRate > 0.9) {
                            status.set("0");
                            return;
                        }
                    }
                }
            }
        }

        String chiefDiag = "";
        List<Diag> diags = beHospitalizedDoc.getChiefLabel().getDiags();
        if (diags != null && diags.size() > 0) {
            chiefDiag = diags.get(0).getHospitalDiagName();
        }
        String presentDiag = "";
        diags = beHospitalizedDoc.getPresentLabel().getDiags();
        if (diags != null && diags.size() > 0) {
            presentDiag = diags.get(0).getHospitalDiagName();
        }
        if (StringUtils.isNotEmpty(chiefDiag) && StringUtils.isNotEmpty(presentDiag) && chiefDiag.equals(presentDiag)) {
            status.set("0");
        }
    }
}