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

import com.alibaba.fastjson.JSONArray;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.client.ChiefPresentSimilarityServiceClient;
import com.lantone.qc.kernel.client.SimilarityServiceClient;
import com.lantone.qc.kernel.structure.ai.ModelAI;
import com.lantone.qc.pub.Content;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.label.DiagLabel;
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;

/**
 * @ClassName : FIRP0178
 * @Description :出院其他诊断不完全
 * @Author : 胡敬
 * @Date: 2020-03-16 14:01
 */
@Component
public class FIRP0178 extends QCCatalogue {
    @Autowired
    SimilarityServiceClient similarityServiceClient;
    @Autowired
    ChiefPresentSimilarityServiceClient chiefPresentSimilarityServiceClient;

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getFirstPageRecordDoc() != null && inputInfo.getLeaveHospitalDoc() != null) {
            Map<String, Object> firstpageStructureMap = inputInfo.getFirstPageRecordDoc().getStructureExtMap();
            List<Map<String, String>> dischargeDiag = (List) firstpageStructureMap.get(Content.dischargeDiag);
            if (ListUtil.isEmpty(dischargeDiag)) {
                return;
            }
            /* 病案首页出院小结诊断 除去主诊断*/
            List<String> firstpageLeaveDiags = getFirstPageDiag(dischargeDiag);

            DiagLabel leaveDiagLabel = inputInfo.getLeaveHospitalDoc().getLeaveDiagLabel();
            if (leaveDiagLabel == null) {
                return;
            }
            List<Diag> leaveDiags = leaveDiagLabel.getDiags();
            List<String> leaveDiagsStr = putDiagToList(leaveDiags);

            /* 病案首页出院诊断会把主诊断去掉，所以size会少一个，这里将出院小结里的出院诊断size也-1 */
            if (firstpageLeaveDiags.size() != leaveDiagsStr.size() - 1) {
                status.set("-1");
                return;
            }

            /* 修改为疾病相似度模型 */
            ModelAI modelAI = new ModelAI();
            int matchDiagSum = 0;
            for (String firstpageLeaveDiag : firstpageLeaveDiags) {
                JSONArray jsonArray = modelAI.loadChiefPresentSimilarAI(firstpageLeaveDiag, leaveDiagsStr, false
                        , "diagnose", chiefPresentSimilarityServiceClient);
                if (jsonArray.size() == 2) {
                    /* 相似度最高症状 */
                    String symptom = jsonArray.getString(0);
                    /* 相似度分数 */
                    double likeRate = jsonArray.getDoubleValue(1);
                    if (likeRate > 0.9) {
                        matchDiagSum++;
                    }
                }
                /*
                for (String leaveDiag : leaveDiagsStr) {
                    if (firstpageLeaveDiag.equals(leaveDiag) || leaveDiag.contains(firstpageLeaveDiag)) {
                        matchDiagSum++;
                        break;
                    }
                }

                 */

            }

            if (matchDiagSum != firstpageLeaveDiags.size()) {
                status.set("-1");
            }

        }
    }

    private List<String> getFirstPageDiag(List<Map<String, String>> dischargeDiag) {
        List<String> firstpageDiag = new ArrayList<>();
        for (int i = 1; i < dischargeDiag.size(); i++) {
            String diagnoseName = dischargeDiag.get(i).get(Content.diagnoseName);
            if (StringUtil.isBlank(diagnoseName)) {
                continue;
            }
            firstpageDiag.add(diagnoseName);
        }
        return firstpageDiag;
    }

    private List<String> putDiagToList(List<Diag> diagList) {
        List<String> diags = new ArrayList<>();
        for (Diag diag : diagList) {
            diags.add(diag.getHospitalDiagName());
        }
        return diags;
    }
}