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

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.kernel.util.CatalogueUtil;
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.Diag;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : LEA0146
 * @Description :出院诊断不完整
 * @Author : 胡敬
 * @Date: 2020-03-16 14:01
 */
@Component
public class LEA0146 extends QCCatalogue {
    @Autowired
    ChiefPresentSimilarityServiceClient chiefPresentSimilarityServiceClient;
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getBeHospitalizedDoc() != null && inputInfo.getLeaveHospitalDoc() != null) {
            DiagLabel leaveDiagLabel = inputInfo.getLeaveHospitalDoc().getLeaveDiagLabel();
            if (leaveDiagLabel == null) {
                return;
            }
            List<Diag> dischargeDiag = leaveDiagLabel.getDiags();
            if (ListUtil.isEmpty(dischargeDiag)) {
                return;
            }
            List<String> leaveDiags = new ArrayList<>();
            putDiagToList(dischargeDiag, leaveDiags);

            BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
            List<String> beHospitalDiag = new ArrayList<>();//收集入院记录中初步诊断、修正诊断、其他诊断所有诊断
//            if (beHospitalizedDoc.getInitialDiagLabel() != null) {
//                putDiagToList(beHospitalizedDoc.getInitialDiagLabel().getDiags(), beHospitalDiag); //初步诊断
//            }
            if (beHospitalizedDoc.getRevisedDiagLabel() != null) {
                putDiagToList(beHospitalizedDoc.getRevisedDiagLabel().getDiags(), beHospitalDiag); //修正诊断
            }
            if (beHospitalizedDoc.getSuppleDiagLabel() != null) {
                putDiagToList(beHospitalizedDoc.getSuppleDiagLabel().getDiags(), beHospitalDiag); //其他诊断
            }

            String infoStr = "";
            int matchSum = 0;
            ModelAI modelAI = new ModelAI();
            for (String beDiag : beHospitalDiag) {
                JSONArray jsonArray = modelAI.loadChiefPresentSimilarAI(beDiag, leaveDiags, false
                        , "diagnose", chiefPresentSimilarityServiceClient);
                if (jsonArray.size() == 2) {
                    /* 相似度最高诊断 */
                    String symptom = jsonArray.getString(0);
                    /* 相似度分数 */
                    double likeRate = jsonArray.getDoubleValue(1);
                    if (likeRate > 0.85) {
                        matchSum++;
                    } else {
                        infoStr = CatalogueUtil.concatInfo(infoStr, beDiag);
                    }
                } else {
                    infoStr = CatalogueUtil.concatInfo(infoStr, beDiag);
                }
            }
            info.set(infoStr);
            if (matchSum != beHospitalDiag.size()) {
                status.set("-1");
            }
        }
    }

    private void putDiagToList(List<Diag> diagList, List<String> beHospitalDiag) {
        for (Diag diag : diagList) {
            beHospitalDiag.add(diag.getHospitalDiagName());
        }
    }
}