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.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.BeHospitalizedDoc;
import com.lantone.qc.pub.model.doc.FirstPageRecordDoc;
import com.lantone.qc.pub.model.entity.Allergy;
import com.lantone.qc.pub.model.label.PastLabel;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 药物过敏史与病案首页不一致
 * @author: 胡敬
 * @time: 2020/06/03 17:28
 */
@Component
public class BEH0724 extends QCCatalogue {
    @Autowired
    ChiefPresentSimilarityServiceClient chiefPresentSimilarityServiceClient;

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        FirstPageRecordDoc firstPageRecordDoc = inputInfo.getFirstPageRecordDoc();
        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        if (firstPageRecordDoc == null || beHospitalizedDoc == null) {
            status.set("0");
            return;
        }
        Map<String, String> firpStructureMap = firstPageRecordDoc.getStructureMap();
        String firpAllergyMedicine = firpStructureMap.get("过敏药物");
        if (StringUtil.isBlank(firpAllergyMedicine)) {
            status.set("0");
            return;
        }
        PastLabel pastLabel = beHospitalizedDoc.getPastLabel();
        if (pastLabel == null) {
            status.set("0");
            return;
        }

        List<Allergy> allergies = pastLabel.getAllergies();
        if (ListUtil.isNotEmpty(allergies)) {
            if (firpAllergyMedicine.contains("无")) {
                allergies = allergies.stream().filter(i -> i != null
                        && i.getNegative() != null
                        && StringUtil.isNotBlank(i.getName())
                        && i.getAllergyMedicine() != null
                        && StringUtil.isNotBlank(i.getAllergyMedicine().getName())
                ).collect(Collectors.toList());
                /* 模型提出的药物过敏史内容有一个否定就算与病案首页的药物过敏：无 相同 */
                if (allergies.size() > 0) {
                    status.set("0");
                }
            } else {
                allergies = allergies.stream().filter(i -> i != null
                        && StringUtil.isNotBlank(i.getName())
                        && i.getAllergyMedicine() != null
                        && StringUtil.isNotBlank(i.getAllergyMedicine().getName())
                ).collect(Collectors.toList());

                List<String> allergyMedicine = getAllergyMedicine(allergies);
                /* 主诉现病史相似度算法接口 */
                ModelAI modelAI = new ModelAI();
                JSONArray jsonArray = modelAI.loadChiefPresentSimilarAI(firpAllergyMedicine, allergyMedicine,
                        false, "chief_present", chiefPresentSimilarityServiceClient);
                if (jsonArray.size() == 2) {
                    /* 相似度分数 */
                    double likeRate = jsonArray.getDoubleValue(1);
                    if (likeRate > 0.9) {
                        status.set("0");
                    } else {
                        status.set("-1");
                    }
                }
            }
        }
    }

    private List<String> getAllergyMedicine(List<Allergy> allergies) {
        List<String> allergieStrList = new ArrayList<>();
        for (Allergy allergieStr : allergies) {
            allergieStrList.add(allergieStr.getAllergyMedicine().getName());
        }
        return allergieStrList;
    }

}