package com.lantone.qc.kernel.analysis;

import com.google.common.collect.Maps;
import com.lantone.qc.dbanaly.util.SpecialStorageUtil;
import com.lantone.qc.kernel.client.CRFServiceClient;
import com.lantone.qc.kernel.client.SimilarityServiceClient;
import com.lantone.qc.kernel.structure.ai.AIAnalyze;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.exception.AIException;
import com.lantone.qc.pub.exception.CatalogueException;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.vo.QueryVo;
import com.lantone.qc.trans.TransDispatch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName : QCAnalysis
 * @Description : 质控业务处理总入口
 * @Author : 楼辉荣
 * @Date: 2020-03-04 11:48
 */
@Slf4j
@Component
public class QCAnalysis {
    @Autowired
    CRFServiceClient crfServiceClient;
    @Autowired
    private SpecialStorageUtil specialStorageUtil;
    @Autowired
    SimilarityServiceClient similarityServiceClient;

    public OutputInfo anlysis(QueryVo queryVo) throws AIException, CatalogueException, Exception {
        specialStorageUtil.getHospitalIdThreadLocal().set(queryVo.getHospitalId());
        OutputInfo outputInfo = new OutputInfo();
        long t1 = System.currentTimeMillis();
        InputInfo inputInfo = TransDispatch.trans(queryVo);
        long t2 = System.currentTimeMillis();
        inputInfo.setUseCrfCache(queryVo.isUseCrfCache());
        inputInfo.setInputCatalogueMap(queryVo.getInputCatalogueMap());
        AIAnalyze aiAnalyze = new AIAnalyze(crfServiceClient, similarityServiceClient);
        try {
            aiAnalyze.aiProcess(inputInfo);
        } catch (Exception e) {
            System.out.println("出错病历号：" + inputInfo.getMedicalRecordInfoDoc().getStructureMap().get("behospitalCode"));
            throw new AIException("AI模型执行错误：" + e.getMessage());
        }
        long t3 = System.currentTimeMillis();
        for (Map.Entry<String, Map<String, String>> entry : inputInfo.getInputCatalogueMap().entrySet()) {
            if (CatalogueUtil.qcCatalogueMap.get(entry.getKey()) == null) {
                continue;
            }
            CatalogueUtil.qcCatalogueMap.get(entry.getKey()).execute(inputInfo, outputInfo);
        }
//        inputInfo.getInputCatalogueMap().entrySet().parallelStream().forEach(entry -> {
//            if (CatalogueUtil.qcCatalogueMap.get(entry.getKey()) != null) {
//                CatalogueUtil.qcCatalogueMap.get(entry.getKey()).execute(inputInfo, outputInfo);
//            }
//        });
        long t4 = System.currentTimeMillis();

        log.error(queryVo.getBehospitalInfo().getBehospitalCode() + "-----" + "trans层  耗时：" + (t2 - t1));
        log.error(queryVo.getBehospitalInfo().getBehospitalCode() + "-----" + "CRF  耗时：" + (t3 - t2));
        log.error(queryVo.getBehospitalInfo().getBehospitalCode() + "-----" + "所有规则  耗时：" + (t4 - t3));

        outputInfo.setPageData(inputInfo.getPageData());
        Map<String, Map<String, Object>> result = outputInfo.getResult();
        Map<String, Map<String, Object>> resultNew = Maps.newHashMap();
        result.keySet().forEach(key -> {
            if (result.get(key).get("status").equals("-1")) {
                result.get(key).remove("extData");
                resultNew.put(key, result.get(key));
            }
        });
        outputInfo.setResult(resultNew);
        return outputInfo;
    }
}