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.ChiefPresentSimilarityServiceClient;
import com.lantone.qc.kernel.structure.ai.ModelAI;
import com.lantone.qc.dbanaly.util.KernelConstants;
import com.lantone.qc.dbanaly.util.SpecialStorageUtil;
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.model.label.ChiefLabel;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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


/**
 * @ClassName : BEH0002
 * @Description : 主诊断与主诉不符
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class BEH0002 extends QCCatalogue {
    @Autowired
    private SpecialStorageUtil specialStorageUtil;
    @Autowired
    ChiefPresentSimilarityServiceClient chiefPresentSimilarityServiceClient;

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        if (beHospitalizedDoc == null) {
            status.set("0");
            return;
        }
        boolean bool = false;
        ChiefLabel chiefLabel = beHospitalizedDoc.getChiefLabel();
        DiagLabel initialDiagLabel = beHospitalizedDoc.getInitialDiagLabel();
        if (chiefLabel == null || initialDiagLabel == null) {
            status.set("0");
            return;
        }
        List<Diag> chiefDiags = chiefLabel.getDiags();
        List<Diag> diags = initialDiagLabel.getDiags();
        if (chiefDiags.size() > 0) {
            for (Diag diag : chiefDiags) {
                String hospitalDiagName = diag.getHospitalDiagName();
                if (hospitalDiagName.contains("术后") || hospitalDiagName.contains("癌") ||
                        hospitalDiagName.contains("瘤") || hospitalDiagName.contains("复诊")) {
                    status.set("0");
                    break;
                }
                {
                    bool = true;
                }
            }
        } else {
            bool = true;
        }
        if (bool) {
            String firstDiag = diags.get(0).getName();
            String firstHDiag = diags.get(0).getHospitalDiagName();
            String mainDiag = StringUtil.isBlank(firstHDiag) ? firstDiag : firstHDiag;
            List<String> symptoms = null;
            //Map<String, String> hostpital_standDiag = specialStorageUtil.getJsonStringValue(KernelConstants.HOSPITAL_DIAG_MAP);
            //if (!hostpital_standDiag.containsKey(mainDiag)) {
            //    status.set("0");
            //    return;
            //}
            Map<String, Map<String, Object>> diagMap = specialStorageUtil.getJsonStringValue(KernelConstants.CONCEPT_DIAG_PROPERTY_MAP);
            Map<String, Object> map = (Map<String, Object>) diagMap.get(mainDiag);
            if (map != null && map.size() > 0) {
                symptoms = (List<String>) map.get("symptoms");
            }
            if (symptoms == null) {
                status.set("0");
                return;
            }
            List<Clinical> clinicals = inputInfo.getBeHospitalizedDoc().getChiefLabel().getClinicals();
            if (clinicals != null && clinicals.size() > 0) {
                String firstClinical = clinicals.get(0).getName();
                /* 主诉现病史相似度算法接口 */
                ModelAI modelAI = new ModelAI();
                JSONArray jsonArray = modelAI.loadChiefPresentSimilarAI(firstClinical, symptoms, false
                        , "chief_present", chiefPresentSimilarityServiceClient);
                if (jsonArray.size() == 2) {
                    /* 相似度最高症状 */
                    String symptom = jsonArray.getString(0);
                    /* 相似度分数 */
                    double likeRate = jsonArray.getDoubleValue(1);
                    if (likeRate > 0.9) {
                        status.set("0");
                    } else {
                        status.set("-1");
                        info.set(symptom);
                    }
                }

                //if (symptoms.contains(firstClinical)) {
                //    status.set("0");
                //}
                //for (Clinical clinical : clinicals) {
                //    String clinicalName = clinical.getName();
                //    if (symptoms != null && !symptoms.contains(clinicalName)) {
                //        if (StringUtils.isEmpty(info.get())) {
                //            info.set(clinicalName);
                //        } else {
                //            info.set(info.get() + "," + clinicalName);
                //        }
                //    }
                //}
            }
            //if (StringUtils.isEmpty(info.get())) {
            //    status.set("0");
            //}
        }
    }
}