RecallService.java 3.61 KB
package com.yoho.search.recall.scene;

import com.yoho.search.recall.scene.builder.request.BrandRecallRequestBuilder;
import com.yoho.search.recall.scene.builder.request.CommonRecallRequestBuilder;
import com.yoho.search.recall.scene.builder.request.SortPriceRecallRequestBuilder;
import com.yoho.search.recall.scene.builder.response.RecallResponseBatchBuilder;
import com.yoho.search.recall.scene.builder.response.RecallSknResultBuilder;
import com.yoho.search.recall.scene.component.BatchRecallComponent;
import com.yoho.search.recall.scene.models.*;
import com.yoho.search.recall.scene.persional.PersionalFactor;
import com.yoho.search.recall.scene.persional.RecallPersionalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class RecallService {

    @Autowired
    private RecallPersionalService recallPersionalService;
    @Autowired
    private BatchRecallComponent bacthRecallComponent;
    @Autowired
    private RecallResponseBatchBuilder recallResponseBatchBuilder;
    @Autowired
    private RecallSknResultBuilder recallSknResultBuilder;
    @Autowired
    private CommonRecallRequestBuilder commonRequestBuilder;
    @Autowired
    private BrandRecallRequestBuilder brandRequestBuilder;
    @Autowired
    private SortPriceRecallRequestBuilder sortPriceRequestBuilder;

    public RecallSknResult getRecallSknResult(RecallSknParams recallSknParams){
        RecallSknResult recallSknResult = this.doRecall(recallSknParams);
        return recallSknResult;
    }

    /**
     * 召回入口
     * @param param
     * @return
     */
    private RecallSknResult doRecall(RecallSknParams param) {
        //1、获取个性化因子
        PersionalFactor persionalFactor = recallPersionalService.queryPersionalFactor(param);
        //2、构造请求
        List<RecallRequest> batchRequests = this.buildBatchRequests(param, persionalFactor);
        //3、批量召回
        List<CacheRecallRequestResponse> requestResponses = bacthRecallComponent.batchRecallAndCache(batchRequests);
        //4、获取skn列表[去重]
        RecallResponseBatch recallResponseBatch = recallResponseBatchBuilder.buildRecallResponseBatch(requestResponses);
        //5、构造真实结果[排序,截取skn]
        RecallSknResult recallSknResult = recallSknResultBuilder.builderRecallResult(recallResponseBatch, param, persionalFactor);
        return recallSknResult;
    }

    /**
     * 批量构造请求
     * @param param
     * @param persionalFactor
     * @return
     */
    public List<RecallRequest> buildBatchRequests(RecallSknParams param, PersionalFactor persionalFactor) {
        //1、构造召回请求
        List<RecallRequest> allRequests = new ArrayList<>();
        //2、构造非个性化的请求
        List<RecallRequest> commonRequests = commonRequestBuilder.buildCommonRecallRequests(param.getParamQueryFilter(), param.getFirstProductSkns());
        allRequests.addAll(commonRequests);
        //4、构建个性化品牌的召回请求
        List<RecallRequest> brandRequests = brandRequestBuilder.buildBrandRecallRequests(param.getParamQueryFilter(), persionalFactor.getBrandIds());
        allRequests.addAll(brandRequests);
        //5、构建个性化品牌的召回请求
        List<RecallRequest> sortPriceRequests = sortPriceRequestBuilder.buildSortPriceRecallRequests(param.getParamQueryFilter(), persionalFactor.getSortPriceAreas());
        allRequests.addAll(sortPriceRequests);
        return allRequests;
    }

}