package com.lantone.qc.kernel.analysis;

import com.google.common.collect.Maps;
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.kernel.util.RedisUtil;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

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

    @Autowired
    SimilarityServiceClient similarityServiceClient;

    @Autowired
    private RedisUtil redisUtil;

    public OutputInfo anlysis(QueryVo queryVo) {
        OutputInfo outputInfo = new OutputInfo();
        InputInfo inputInfo = TransDispatch.trans(queryVo);
        inputInfo.setInputCatalogueMap(queryVo.getInputCatalogueMap());
        AIAnalyze aiAnalyze = new AIAnalyze(crfServiceClient, similarityServiceClient);
        try{
            aiAnalyze.aiProcess(inputInfo);
        }catch (Exception e){
            e.printStackTrace();
        }
        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);
        }
        outputInfo.setPageData(inputInfo.getPageData());
        Map<String, Map<String, String>> result = outputInfo.getResult();
        Map<String, Map<String, String>> resultNew = Maps.newHashMap();
        result.keySet().forEach(key->{
            if (result.get(key).get("status").equals("-1")) {
                resultNew.put(key,result.get(key));
            }
        });
        outputInfo.setResult(resultNew);
        return outputInfo;
    }
}