RecallConfigService.java 6.34 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 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 pageId
     * @param strategyEnum
     * @return
     */
    public int queryStrategyConfigSize(int pageId, StrategyEnum strategyEnum) {
        String configKey = getStrategyConfigKey(strategyEnum);
        int size = recallConfigCommonService.queryConfigSize(pageId, configKey, 0);
        size = this.legallValue(size, 0, 100);
        RECALL_LOGGER.info("queryStrategyConfigSize,pageId is [{}],strategyEnum is [{}],size is[{}]", pageId, strategyEnum.name(), size);
        return size;
    }

    /**
     * 获取某些策略的间隔
     *
     * @param pageId
     * @param strategyEnum
     * @return
     */
    public int queryStrategyConfigInterval(int pageId, StrategyEnum strategyEnum) {
        String configKey = getStrategyConfigKey(strategyEnum);
        int interval = recallConfigCommonService.queryConfigInterval(pageId, configKey, 10);
        interval = this.legallValue(interval, 2, 40);
        RECALL_LOGGER.info("queryStrategyConfigInterval,pageId is [{}],strategyEnum is [{}],interval is[{}]", pageId, 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 pageId
     * @param sortBrand
     * @return
     */
    public RecallSknCount queryRecallSknCount(int pageId, SortBrandType sortBrandType, SortBrand sortBrand) {
        String sortBrandTypeKey = this.getSortBrandConfigKey(sortBrandType);
        RecallSknCount recallSknCount = recallConfigProductService.queryRecallSknCount(pageId, sortBrandTypeKey, sortBrand);
        if (recallSknCount == null) {
            RECALL_LOGGER.info("queryRecallSknCount,pageId is [{}],sortBrandType is [{}],sortBrand is[{}], RecallSknCount is[{}]", pageId, sortBrand.key(), sortBrandType.name(), "null");
            return null;
        }
        recallSknCount.setNewShelve(this.legallValue(recallSknCount.getNewShelve(), 0, 20));
        recallSknCount.setPromotion(this.legallValue(recallSknCount.getPromotion(), 0, 20));
        recallSknCount.setCtrValue(this.legallValue(recallSknCount.getCtrValue(), 0, 20));
        recallSknCount.setHeatValue(this.legallValue(recallSknCount.getHeatValue(), 0, 20));
        recallSknCount.setReducePrice(this.legallValue(recallSknCount.getReducePrice(), 0, 20));
        recallSknCount.setRandom(this.legallValue(recallSknCount.getRandom(), 0, 20));
        RECALL_LOGGER.info("queryRecallSknCount,pageId is [{}],sortBrandType is [{}],sortBrand is[{}], RecallSknCount is[{}]", pageId,  sortBrand.key(), sortBrandType.name(), JSON.toJSONString(recallSknCount));
        return recallSknCount;
    }

    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));
    }


}