RecallConfigService.java 6.88 KB
package com.yoho.search.recall.config;

import com.alibaba.fastjson.JSON;
import com.yoho.search.core.personalized.models.SortBrand;
import com.yoho.search.recall.beans.strategy.SortBrandType;
import com.yoho.search.recall.beans.strategy.StrategyEnum;
import com.yoho.search.recall.models.personal.PagePersonalFactor;
import com.yoho.search.recall.models.req.UserRecallRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class RecallConfigService {

    private static final Logger RECALL_LOGGER = LoggerFactory.getLogger("RECALL");

    @Autowired
    private RecallConfigProductService recallConfigProductService;
    @Autowired
    private RecallConfigCommonService recallConfigCommonService;
    @Autowired
    private RecallConfigSortBrandService recallConfigSortBrandService;

    private static int legallValue(int value, int min, int max) {
        value = Math.min(value, max);
        value = Math.max(value, min);
        return value;
    }

    /**
     * 获取某些策略的召回数量
     *
     * @param userRecallRequest
     * @param strategyEnum
     * @return
     */
    public int queryStrategyConfigSize(UserRecallRequest userRecallRequest, StrategyEnum strategyEnum) {
        String configKey = getStrategyConfigKey(strategyEnum);
        int size = recallConfigCommonService.queryConfigSize(userRecallRequest.getPageId(), configKey, 0);
        size = this.legallValue(size, 0, 100);
        if (userRecallRequest.openDetailLog()) {
            RECALL_LOGGER.info("queryStrategyConfigSize,pageId is [{}],strategyEnum is [{}],size is[{}]", userRecallRequest.getPageId(), strategyEnum.name(), size);
        }
        return size;
    }

    /**
     * 获取某些策略的间隔
     *
     * @param userRecallRequest
     * @param strategyEnum
     * @return
     */
    public int queryStrategyConfigInterval(UserRecallRequest userRecallRequest, StrategyEnum strategyEnum) {
        String configKey = getStrategyConfigKey(strategyEnum);
        int interval = recallConfigCommonService.queryConfigInterval(userRecallRequest.getPageId(), configKey, 10);
        interval = this.legallValue(interval, 2, 40);
        if (userRecallRequest.openDetailLog()) {
            RECALL_LOGGER.info("queryStrategyConfigInterval,pageId is [{}],strategyEnum is [{}],interval is[{}]", userRecallRequest.getPageId(), strategyEnum.name(), interval);
        }
        return interval;
    }

    private String getStrategyConfigKey(StrategyEnum strategyEnum) {
        if (strategyEnum.equals(StrategyEnum.REC_SKN)) {
            return RecallConfigConstants.REC_SKN;
        }
        if (strategyEnum.equals(StrategyEnum.RT_SIM_SKN)) {
            return RecallConfigConstants.RT_SIM_SKN;
        }
        if (strategyEnum.equals(StrategyEnum.DIRECT_TRAIN)) {
            return RecallConfigConstants.DIRECT_TRAIN;
        }
        if (strategyEnum.equals(StrategyEnum.NEW_SHOP)) {
            return RecallConfigConstants.NEW_SHOP;
        }
        if (strategyEnum.equals(StrategyEnum.ADD_FLOW)) {
            return RecallConfigConstants.ADD_FLOW;
        }
        return null;
    }

    /**
     * 获取推荐的【品类*品牌】的数量
     *
     * @param pageId
     * @return
     */
    public int querySortBrandConfigCount(int pageId, SortBrandType sortBrandType, int defaultSize) {
        String sortBrandConfigKey = this.getSortBrandConfigKey(sortBrandType);
        int size = recallConfigCommonService.queryConfigSize(pageId, sortBrandConfigKey, defaultSize);
        size = this.legallValue(size, 0, 30);
        RECALL_LOGGER.info("querySortBrandConfigCount,pageId is [{}],sortBrandType is [{}],size is[{}]", pageId, sortBrandType.name(), size);
        return size;
    }

    /**
     * 获取【品类*品牌】下的的商品的召回数量
     *
     * @param userRecallRequest
     * @param sortBrandType
     * @param sortBrand
     * @return
     */
    public ConfigSknCount queryRecallSknCount(UserRecallRequest userRecallRequest, SortBrandType sortBrandType, SortBrand sortBrand) {
        String sortBrandTypeKey = this.getSortBrandConfigKey(sortBrandType);
        ConfigSknCount configSknCount = recallConfigProductService.queryRecallSknCount(userRecallRequest.getPageId(), sortBrandTypeKey, sortBrand);
        if (configSknCount == null) {
            RECALL_LOGGER.info("queryRecallSknCount,pageId is [{}],sortBrandType is [{}],sortBrand is[{}], RecallSknCount is[{}]", userRecallRequest.getPageId(), sortBrand.key(), sortBrandType.name(), "null");
            return null;
        }
        configSknCount.setNewShelve(this.legallValue(configSknCount.getNewShelve(), 0, 20));
        configSknCount.setPromotion(this.legallValue(configSknCount.getPromotion(), 0, 20));
        configSknCount.setCtrValue(this.legallValue(configSknCount.getCtrValue(), 0, 20));
        configSknCount.setHeatValue(this.legallValue(configSknCount.getHeatValue(), 0, 20));
        configSknCount.setReducePrice(this.legallValue(configSknCount.getReducePrice(), 0, 20));
        configSknCount.setRandom(this.legallValue(configSknCount.getRandom(), 0, 20));
        if (userRecallRequest.openDetailLog()) {
            RECALL_LOGGER.info("queryRecallSknCount,pageId is [{}],sortBrandType is [{}],sortBrand is[{}], RecallSknCount is[{}]", userRecallRequest.getPageId(), sortBrand.key(), sortBrandType.name(), JSON.toJSONString(configSknCount));
        }
        return configSknCount;
    }

    private String getSortBrandConfigKey(SortBrandType sortBrandType) {
        if (sortBrandType.equals(SortBrandType.REC_SORT_BRAND)) {
            return RecallConfigConstants.REC_SORT_BRAND;
        }
        if (sortBrandType.equals(SortBrandType.VEC_RNN_SORT_BRAND)) {
            return RecallConfigConstants.VEC_RNN_SORT_BRAND;
        }
        if (sortBrandType.equals(SortBrandType.VEC_W2V_SORT_BRAND)) {
            return RecallConfigConstants.VEC_W2V_SORT_BRAND;
        }
        if (sortBrandType.equals(SortBrandType.CONFIG_SORT_BRAND)) {
            return RecallConfigConstants.CONFIG_SORT_BRAND;
        }
        return null;
    }

    /**
     * 随机获取configSortBrandCount数量
     *
     * @param pageFactor
     * @param filterSortBrandKeys
     * @param configSortBrandCount
     * @return
     */
    public List<SortBrand> queryConfigSortBrandList(PagePersonalFactor pageFactor, Set<String> pageSortBrandKeys, Set<String> filterSortBrandKeys, int configSortBrandCount) {
        return recallConfigSortBrandService.queryConfigSortBrand(pageFactor, pageSortBrandKeys, filterSortBrandKeys, configSortBrandCount);
    }

    public static void main(String[] args) {
        System.out.println(legallValue(-1, 0, 30));
    }


}