Authored by hugufei

搜索支持配置的品类品牌召回

package com.yoho.search.recall.config;
import com.alibaba.fastjson.JSON;
import com.yoho.search.dal.model.CsRecallConfigCommon;
import com.yoho.search.service.base.index.CsRecallConfigCommonIndexBaseService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
class RecallConfigCommonService {
private static final Logger logger = LoggerFactory.getLogger(RecallConfigCommonService.class);
@Autowired
private CsRecallConfigCommonIndexBaseService csRecallConfigCommonIndexBaseService;
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
private Map<String, Map<Integer, RecallSizeInterval>> typePageConfigCache = new HashMap<>();
@PostConstruct
void init() {
schedule.scheduleAtFixedRate(() -> loadConfig(), 0, 1, TimeUnit.MINUTES);
}
private void loadConfig() {
try {
List<CsRecallConfigCommon> configList = csRecallConfigCommonIndexBaseService.queryAll();
Map<String, Map<Integer, RecallSizeInterval>> temp = new HashMap<>();
for (CsRecallConfigCommon csRecallConfigCommon : configList) {
Map<Integer, RecallSizeInterval> pageConfigs = temp.get(csRecallConfigCommon.getConfigType());
if (pageConfigs == null) {
pageConfigs = new HashMap<>();
temp.put(csRecallConfigCommon.getConfigType(), pageConfigs);
}
int size = csRecallConfigCommon.getSize();
int interval = csRecallConfigCommon.getInterval();
pageConfigs.put(csRecallConfigCommon.getConfigPage(), new RecallSizeInterval(size, interval));
}
typePageConfigCache = temp;
System.out.println(JSON.toJSONString(typePageConfigCache));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
private RecallSizeInterval queryCommonConfig(String configKey, int configPage) {
Map<Integer, RecallSizeInterval> pageConfigMap = typePageConfigCache.get(configKey);
if (pageConfigMap == null) {
return null;
}
RecallSizeInterval pageConfig = pageConfigMap.get(configPage);
if (pageConfig == null) {
pageConfig = pageConfigMap.get(RecallConfigConstants.DEFAULT_PAGE_ID);
}
if (pageConfigMap == null) {
return null;
} else {
return pageConfig;
}
}
/**
* 获取配置的大小
*
* @param pageId
* @param configKey
* @param defaultSize
* @return
*/
public int queryConfigSize(int pageId, String configKey, int defaultSize) {
if (StringUtils.isBlank(configKey)) {
return defaultSize;
}
RecallSizeInterval config = queryCommonConfig(configKey, pageId);
return config == null ? defaultSize : config.getSize();
}
/**
* 获取配置的间隔
*
* @param pageId
* @param configKey
* @param defaultInterval
* @return
*/
public int queryConfigInterval(int pageId, String configKey, int defaultInterval) {
if (StringUtils.isBlank(configKey)) {
return defaultInterval;
}
RecallSizeInterval config = queryCommonConfig(configKey, pageId);
return config == null ? defaultInterval : config.getInterval();
}
}
package com.yoho.search.recall.config;
import com.alibaba.fastjson.JSON;
import com.yoho.search.dal.model.CsRecallConfigCommon;
import com.yoho.search.service.base.index.CsRecallConfigCommonIndexBaseService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
class RecallConfigCommonService {
private static final Logger logger = LoggerFactory.getLogger(RecallConfigCommonService.class);
@Autowired
private CsRecallConfigCommonIndexBaseService csRecallConfigCommonIndexBaseService;
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
private Map<String, Map<Integer, RecallSizeInterval>> typePageConfigCache = new HashMap<>();
@PostConstruct
void init() {
schedule.scheduleAtFixedRate(() -> loadConfig(), 0, 1, TimeUnit.MINUTES);
}
private void loadConfig() {
try {
List<CsRecallConfigCommon> configList = csRecallConfigCommonIndexBaseService.queryAll();
Map<String, Map<Integer, RecallSizeInterval>> temp = new HashMap<>();
for (CsRecallConfigCommon csRecallConfigCommon : configList) {
Map<Integer, RecallSizeInterval> pageConfigs = temp.get(csRecallConfigCommon.getConfigType());
if (pageConfigs == null) {
pageConfigs = new HashMap<>();
temp.put(csRecallConfigCommon.getConfigType(), pageConfigs);
}
int size = csRecallConfigCommon.getSize();
int interval = csRecallConfigCommon.getInterval();
pageConfigs.put(csRecallConfigCommon.getConfigPage(), new RecallSizeInterval(size, interval));
}
typePageConfigCache = temp;
System.out.println(JSON.toJSONString(typePageConfigCache));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
private RecallSizeInterval queryCommonConfig(String configKey, int configPage) {
Map<Integer, RecallSizeInterval> pageConfigMap = typePageConfigCache.get(configKey);
if (pageConfigMap == null) {
return null;
}
RecallSizeInterval pageConfig = pageConfigMap.get(configPage);
if (pageConfig == null) {
pageConfig = pageConfigMap.get(RecallConfigConstants.DEFAULT_PAGE_ID);
}
if (pageConfigMap == null) {
return null;
} else {
return pageConfig;
}
}
/**
* 获取配置的大小
*
* @param pageId
* @param configKey
* @param defaultSize
* @return
*/
public int queryConfigSize(int pageId, String configKey, int defaultSize) {
if (StringUtils.isBlank(configKey)) {
return defaultSize;
}
RecallSizeInterval config = queryCommonConfig(configKey, pageId);
return config == null ? defaultSize : config.getSize();
}
/**
* 获取配置的间隔
*
* @param pageId
* @param configKey
* @param defaultInterval
* @return
*/
public int queryConfigInterval(int pageId, String configKey, int defaultInterval) {
if (StringUtils.isBlank(configKey)) {
return defaultInterval;
}
RecallSizeInterval config = queryCommonConfig(configKey, pageId);
return config == null ? defaultInterval : config.getInterval();
}
}
... ...
package com.yoho.search.recall.config;
import com.alibaba.fastjson.JSON;
import com.yoho.search.core.personalized.models.SortBrand;
import com.yoho.search.dal.model.CsRecallConfigProduct;
import com.yoho.search.service.base.index.CsRecallConfigProductIndexBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
class RecallConfigProductService {
private static final Logger logger = LoggerFactory.getLogger(RecallConfigProductService.class);
@Autowired
private CsRecallConfigProductIndexBaseService csRecallConfigProductIndexBaseService;
private Map<String, Map<Integer, RecallSknCount>> typePageConfigCache = new HashMap<>();
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
@PostConstruct
void init() {
schedule.scheduleAtFixedRate(() -> loadRecallSknCountConfig(), 0, 1, TimeUnit.MINUTES);
}
private void loadRecallSknCountConfig() {
try {
Map<String, Map<Integer, RecallSknCount>> tempTypePageConfigCache = new HashMap<>();
List<CsRecallConfigProduct> configList = csRecallConfigProductIndexBaseService.queryAll();
for (CsRecallConfigProduct productConfig : configList) {
String configType = productConfig.getConfigType();
int configTypeId = productConfig.getConfigTypeId();
int pageId = productConfig.getConfigPage();
String configKey = this.buildConfiKey(configType,configTypeId);
int configStatus = productConfig.getConfigStatus();
if(configStatus==1){
this.addElement(configKey,pageId,this.genProductCountConfig(productConfig),tempTypePageConfigCache);
}
}
typePageConfigCache = tempTypePageConfigCache;
System.out.println(JSON.toJSONString(typePageConfigCache));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
private String buildConfiKey(String configType, int configTypeId) {
if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_SORT)) {
return this.buildSortCacheKey(configTypeId);
} else if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_BRAND)) {
return this.buildBrandCacheKey(configTypeId);
} else {
return this.buildSortBrandCacheKey(configType, new SortBrand());
}
}
private String buildSortCacheKey(int middleSortId) {
return RecallConfigConstants.RECALL_SKN_COUNT_SORT + "_" + middleSortId;
}
private String buildBrandCacheKey(int brandId) {
return RecallConfigConstants.RECALL_SKN_COUNT_BRAND + "_" + brandId;
}
private String buildSortBrandCacheKey(String sortBrandType, SortBrand sortBrand) {
return sortBrandType + "_" + sortBrand.key();
}
private void addElement(String configKey, int pageId, RecallSknCount recallSknCount, Map<String, Map<Integer, RecallSknCount>> map) {
if (!map.containsKey(configKey)) {
map.put(configKey, new HashMap<>());
}
map.get(configKey).put(pageId, recallSknCount);
}
private RecallSknCount genProductCountConfig(CsRecallConfigProduct configProduct) {
int newShelve = configProduct.getNewShelve();
int promotion = configProduct.getPromotion();
int reducePrice = configProduct.getReducePrice();
int ctrValue = configProduct.getCtrValue();
int heatValue = configProduct.getHeatValue();
int random = configProduct.getRandom();
return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);
}
private RecallSknCount genProductCountConfig(int newShelve, int promotion, int reducePrice, int ctrValue, int heatValue, int random) {
RecallSknCount recallSknCount = new RecallSknCount();
recallSknCount.setNewShelve(newShelve);
recallSknCount.setPromotion(promotion);
recallSknCount.setReducePrice(reducePrice);
recallSknCount.setCtrValue(ctrValue);
recallSknCount.setHeatValue(heatValue);
recallSknCount.setRandom(random);
return recallSknCount;
}
private RecallSknCount genProductCountConfigByCompare(RecallSknCount recallSknCount1, RecallSknCount recallSknCount2) {
int newShelve = Math.min(recallSknCount1.getNewShelve(), recallSknCount2.getNewShelve());
int promotion = Math.min(recallSknCount1.getPromotion(), recallSknCount2.getPromotion());
int reducePrice = Math.min(recallSknCount1.getReducePrice(), recallSknCount2.getReducePrice());
int ctrValue = Math.min(recallSknCount1.getCtrValue(), recallSknCount2.getCtrValue());
int heatValue = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());
int random = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());
return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);
}
/**
* 按页面+品类+品牌获取商品的召回数量
*
* @param pageId
* @param sortBrand
* @return
*/
public RecallSknCount queryRecallSknCount(int pageId, String sortBrandType, SortBrand sortBrand) {
RecallSknCount sortConfig = this.queryConfigBySort(pageId, sortBrand.getMisort());
RecallSknCount brandConfig = this.queryConfigByBrand(pageId, sortBrand.getBrandId());
if (sortConfig != null && sortConfig != null) {
return genProductCountConfigByCompare(sortConfig, brandConfig);
}
if (sortConfig != null) {
return sortConfig;
}
if (brandConfig != null) {
return brandConfig;
}
RecallSknCount pageConfig = this.queryConfigByPage(pageId, sortBrandType, sortBrand);
if (pageConfig != null) {
return pageConfig;
}
pageConfig = this.queryConfigByPage(pageId, sortBrandType, new SortBrand());//使用默认的值替代
if (pageConfig != null) {
return pageConfig;
}
return genProductCountConfig(6, 3, 6, 6, 6, 0);
}
/**
* 查询中分类在页面上的召回配置,没有则取pageId=0的数据
* @param pageId
* @param middleSortId
* @return
*/
private RecallSknCount queryConfigBySort(int pageId, int middleSortId) {
String sortKey = this.buildSortCacheKey(middleSortId);
return this.getRecallSknCount(sortKey, pageId);
}
/**
* 查询品牌在页面上的召回配置,没有则取pageId=0的数据
* @param pageId
* @param brandId
* @return
*/
private RecallSknCount queryConfigByBrand(int pageId, int brandId) {
String brandKey = this.buildBrandCacheKey(brandId);
return this.getRecallSknCount(brandKey, pageId);
}
/**
* 查询【品类*品牌】在页面上的召回配置,没有则取pageId=0的数据
* @param pageId
* @param sortBrandType
* @param sortBrand
* @return
*/
private RecallSknCount queryConfigByPage(int pageId, String sortBrandType, SortBrand sortBrand) {
String sortBrandKey = this.buildSortBrandCacheKey(sortBrandType, sortBrand);
return this.getRecallSknCount(sortBrandKey, pageId);
}
private RecallSknCount getRecallSknCount(String key, int pageId) {
Map<Integer, RecallSknCount> pageConfig = typePageConfigCache.get(key);
if (pageConfig == null) {
return null;
}
RecallSknCount sknCount = pageConfig.get(pageId);
if (sknCount == null) {
sknCount = pageConfig.get(RecallConfigConstants.DEFAULT_PAGE_ID);
}
return sknCount;
}
}
package com.yoho.search.recall.config;
import com.alibaba.fastjson.JSON;
import com.yoho.search.core.personalized.models.SortBrand;
import com.yoho.search.dal.model.CsRecallConfigProduct;
import com.yoho.search.service.base.index.CsRecallConfigProductIndexBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
class RecallConfigProductService {
private static final Logger logger = LoggerFactory.getLogger(RecallConfigProductService.class);
@Autowired
private CsRecallConfigProductIndexBaseService csRecallConfigProductIndexBaseService;
private Map<String, Map<Integer, RecallSknCount>> typePageConfigCache = new HashMap<>();
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
@PostConstruct
void init() {
schedule.scheduleAtFixedRate(() -> loadRecallSknCountConfig(), 0, 1, TimeUnit.MINUTES);
}
private void loadRecallSknCountConfig() {
try {
Map<String, Map<Integer, RecallSknCount>> tempTypePageConfigCache = new HashMap<>();
List<CsRecallConfigProduct> configList = csRecallConfigProductIndexBaseService.queryAll();
for (CsRecallConfigProduct productConfig : configList) {
String configType = productConfig.getConfigType();
int configTypeId = productConfig.getConfigTypeId();
int pageId = productConfig.getConfigPage();
String configKey = this.buildConfiKey(configType,configTypeId);
int configStatus = productConfig.getConfigStatus();
if(configStatus==1){
this.addElement(configKey,pageId,this.genProductCountConfig(productConfig),tempTypePageConfigCache);
}
}
typePageConfigCache = tempTypePageConfigCache;
System.out.println(JSON.toJSONString(typePageConfigCache));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
private String buildConfiKey(String configType, int configTypeId) {
if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_SORT)) {
return this.buildSortCacheKey(configTypeId);
} else if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_BRAND)) {
return this.buildBrandCacheKey(configTypeId);
} else {
return this.buildSortBrandCacheKey(configType, new SortBrand());
}
}
private String buildSortCacheKey(int middleSortId) {
return RecallConfigConstants.RECALL_SKN_COUNT_SORT + "_" + middleSortId;
}
private String buildBrandCacheKey(int brandId) {
return RecallConfigConstants.RECALL_SKN_COUNT_BRAND + "_" + brandId;
}
private String buildSortBrandCacheKey(String sortBrandType, SortBrand sortBrand) {
return sortBrandType + "_" + sortBrand.key();
}
private void addElement(String configKey, int pageId, RecallSknCount recallSknCount, Map<String, Map<Integer, RecallSknCount>> map) {
if (!map.containsKey(configKey)) {
map.put(configKey, new HashMap<>());
}
map.get(configKey).put(pageId, recallSknCount);
}
private RecallSknCount genProductCountConfig(CsRecallConfigProduct configProduct) {
int newShelve = configProduct.getNewShelve();
int promotion = configProduct.getPromotion();
int reducePrice = configProduct.getReducePrice();
int ctrValue = configProduct.getCtrValue();
int heatValue = configProduct.getHeatValue();
int random = configProduct.getRandom();
return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);
}
private RecallSknCount genProductCountConfig(int newShelve, int promotion, int reducePrice, int ctrValue, int heatValue, int random) {
RecallSknCount recallSknCount = new RecallSknCount();
recallSknCount.setNewShelve(newShelve);
recallSknCount.setPromotion(promotion);
recallSknCount.setReducePrice(reducePrice);
recallSknCount.setCtrValue(ctrValue);
recallSknCount.setHeatValue(heatValue);
recallSknCount.setRandom(random);
return recallSknCount;
}
private RecallSknCount genProductCountConfigByCompare(RecallSknCount recallSknCount1, RecallSknCount recallSknCount2) {
int newShelve = Math.min(recallSknCount1.getNewShelve(), recallSknCount2.getNewShelve());
int promotion = Math.min(recallSknCount1.getPromotion(), recallSknCount2.getPromotion());
int reducePrice = Math.min(recallSknCount1.getReducePrice(), recallSknCount2.getReducePrice());
int ctrValue = Math.min(recallSknCount1.getCtrValue(), recallSknCount2.getCtrValue());
int heatValue = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());
int random = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());
return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);
}
/**
* 按页面+品类+品牌获取商品的召回数量
*
* @param pageId
* @param sortBrand
* @return
*/
public RecallSknCount queryRecallSknCount(int pageId, String sortBrandType, SortBrand sortBrand) {
RecallSknCount sortConfig = this.queryConfigBySort(pageId, sortBrand.getMisort());
RecallSknCount brandConfig = this.queryConfigByBrand(pageId, sortBrand.getBrandId());
if (sortConfig != null && sortConfig != null) {
return genProductCountConfigByCompare(sortConfig, brandConfig);
}
if (sortConfig != null) {
return sortConfig;
}
if (brandConfig != null) {
return brandConfig;
}
RecallSknCount pageConfig = this.queryConfigByPage(pageId, sortBrandType, sortBrand);
if (pageConfig != null) {
return pageConfig;
}
pageConfig = this.queryConfigByPage(pageId, sortBrandType, new SortBrand());//使用默认的值替代
if (pageConfig != null) {
return pageConfig;
}
return genProductCountConfig(6, 3, 6, 6, 6, 0);
}
/**
* 查询中分类在页面上的召回配置,没有则取pageId=0的数据
* @param pageId
* @param middleSortId
* @return
*/
private RecallSknCount queryConfigBySort(int pageId, int middleSortId) {
String sortKey = this.buildSortCacheKey(middleSortId);
return this.getRecallSknCount(sortKey, pageId);
}
/**
* 查询品牌在页面上的召回配置,没有则取pageId=0的数据
* @param pageId
* @param brandId
* @return
*/
private RecallSknCount queryConfigByBrand(int pageId, int brandId) {
String brandKey = this.buildBrandCacheKey(brandId);
return this.getRecallSknCount(brandKey, pageId);
}
/**
* 查询【品类*品牌】在页面上的召回配置,没有则取pageId=0的数据
* @param pageId
* @param sortBrandType
* @param sortBrand
* @return
*/
private RecallSknCount queryConfigByPage(int pageId, String sortBrandType, SortBrand sortBrand) {
String sortBrandKey = this.buildSortBrandCacheKey(sortBrandType, sortBrand);
return this.getRecallSknCount(sortBrandKey, pageId);
}
private RecallSknCount getRecallSknCount(String key, int pageId) {
Map<Integer, RecallSknCount> pageConfig = typePageConfigCache.get(key);
if (pageConfig == null) {
return null;
}
RecallSknCount sknCount = pageConfig.get(pageId);
if (sknCount == null) {
sknCount = pageConfig.get(RecallConfigConstants.DEFAULT_PAGE_ID);
}
return sknCount;
}
}
... ...
package com.yoho.search.recall.config;
import com.alibaba.fastjson.JSON;
import com.yoho.search.dal.model.CsRecallConfigCommon;
import com.yoho.search.service.base.index.CsRecallConfigCommonIndexBaseService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
class RecallConfigServiceCommon {
private static final Logger logger = LoggerFactory.getLogger(RecallConfigServiceCommon.class);
@Autowired
private CsRecallConfigCommonIndexBaseService csRecallConfigCommonIndexBaseService;
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
private Map<String, Map<Integer, RecallSizeInterval>> typePageConfigCache = new HashMap<>();
@PostConstruct
void init() {
schedule.scheduleAtFixedRate(() -> loadConfig(), 0, 1, TimeUnit.MINUTES);
}
private void loadConfig() {
try {
List<CsRecallConfigCommon> configList = csRecallConfigCommonIndexBaseService.queryAll();
Map<String, Map<Integer, RecallSizeInterval>> temp = new HashMap<>();
for (CsRecallConfigCommon csRecallConfigCommon : configList) {
Map<Integer, RecallSizeInterval> pageConfigs = temp.get(csRecallConfigCommon.getConfigType());
if (pageConfigs == null) {
pageConfigs = new HashMap<>();
temp.put(csRecallConfigCommon.getConfigType(), pageConfigs);
}
int size = csRecallConfigCommon.getSize();
int interval = csRecallConfigCommon.getInterval();
pageConfigs.put(csRecallConfigCommon.getConfigPage(), new RecallSizeInterval(size, interval));
}
typePageConfigCache = temp;
System.out.println(JSON.toJSONString(typePageConfigCache));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
private RecallSizeInterval queryCommonConfig(String configKey, int configPage) {
Map<Integer, RecallSizeInterval> pageConfigMap = typePageConfigCache.get(configKey);
if (pageConfigMap == null) {
return null;
}
RecallSizeInterval pageConfig = pageConfigMap.get(configPage);
if (pageConfig == null) {
pageConfig = pageConfigMap.get(RecallConfigConstants.DEFAULT_PAGE_ID);
}
if (pageConfigMap == null) {
return null;
} else {
return pageConfig;
}
}
/**
* 获取配置的大小
*
* @param pageId
* @param configKey
* @param defaultSize
* @return
*/
public int queryConfigSize(int pageId, String configKey, int defaultSize) {
if (StringUtils.isBlank(configKey)) {
return defaultSize;
}
RecallSizeInterval config = queryCommonConfig(configKey, pageId);
return config == null ? defaultSize : config.getSize();
}
/**
* 获取配置的间隔
*
* @param pageId
* @param configKey
* @param defaultInterval
* @return
*/
public int queryConfigInterval(int pageId, String configKey, int defaultInterval) {
if (StringUtils.isBlank(configKey)) {
return defaultInterval;
}
RecallSizeInterval config = queryCommonConfig(configKey, pageId);
return config == null ? defaultInterval : config.getInterval();
}
}
package com.yoho.search.recall.config;
import com.alibaba.fastjson.JSON;
import com.yoho.search.core.personalized.models.SortBrand;
import com.yoho.search.dal.model.CsRecallConfigProduct;
import com.yoho.search.recall.beans.strategy.SortBrandType;
import com.yoho.search.service.base.index.CsRecallConfigProductIndexBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
class RecallConfigServiceProduct {
private static final Logger logger = LoggerFactory.getLogger(RecallConfigServiceProduct.class);
@Autowired
private CsRecallConfigProductIndexBaseService csRecallConfigProductIndexBaseService;
private Map<String, Map<Integer, RecallSknCount>> typePageConfigCache = new HashMap<>();
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
@PostConstruct
void init() {
schedule.scheduleAtFixedRate(() -> loadRecallSknCountConfig(), 0, 1, TimeUnit.MINUTES);
}
private void loadRecallSknCountConfig() {
try {
Map<String, Map<Integer, RecallSknCount>> tempTypePageConfigCache = new HashMap<>();
List<CsRecallConfigProduct> configList = csRecallConfigProductIndexBaseService.queryAll();
for (CsRecallConfigProduct productConfig : configList) {
String configType = productConfig.getConfigType();
int configTypeId = productConfig.getConfigTypeId();
int pageId = productConfig.getConfigPage();
String configKey = this.buildConfiKey(configType,configTypeId);
int configStatus = productConfig.getConfigStatus();
if(configStatus==1){
this.addElement(configKey,pageId,this.genProductCountConfig(productConfig),tempTypePageConfigCache);
}
}
typePageConfigCache = tempTypePageConfigCache;
System.out.println(JSON.toJSONString(typePageConfigCache));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
private String buildConfiKey(String configType, int configTypeId) {
if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_SORT)) {
return this.buildSortCacheKey(configTypeId);
} else if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_BRAND)) {
return this.buildBrandCacheKey(configTypeId);
} else {
return this.buildSortBrandCacheKey(configType, new SortBrand());
}
}
private String buildSortCacheKey(int middleSortId) {
return RecallConfigConstants.RECALL_SKN_COUNT_SORT + "_" + middleSortId;
}
private String buildBrandCacheKey(int brandId) {
return RecallConfigConstants.RECALL_SKN_COUNT_BRAND + "_" + brandId;
}
private String buildSortBrandCacheKey(String sortBrandType, SortBrand sortBrand) {
return sortBrandType + "_" + sortBrand.key();
}
private void addElement(String configKey, int pageId, RecallSknCount recallSknCount, Map<String, Map<Integer, RecallSknCount>> map) {
if (!map.containsKey(configKey)) {
map.put(configKey, new HashMap<>());
}
map.get(configKey).put(pageId, recallSknCount);
}
private RecallSknCount genProductCountConfig(CsRecallConfigProduct configProduct) {
int newShelve = configProduct.getNewShelve();
int promotion = configProduct.getPromotion();
int reducePrice = configProduct.getReducePrice();
int ctrValue = configProduct.getCtrValue();
int heatValue = configProduct.getHeatValue();
int random = configProduct.getRandom();
return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);
}
private RecallSknCount genProductCountConfig(int newShelve, int promotion, int reducePrice, int ctrValue, int heatValue, int random) {
RecallSknCount recallSknCount = new RecallSknCount();
recallSknCount.setNewShelve(newShelve);
recallSknCount.setPromotion(promotion);
recallSknCount.setReducePrice(reducePrice);
recallSknCount.setCtrValue(ctrValue);
recallSknCount.setHeatValue(heatValue);
recallSknCount.setRandom(random);
return recallSknCount;
}
private RecallSknCount genProductCountConfigByCompare(RecallSknCount recallSknCount1, RecallSknCount recallSknCount2) {
int newShelve = Math.min(recallSknCount1.getNewShelve(), recallSknCount2.getNewShelve());
int promotion = Math.min(recallSknCount1.getPromotion(), recallSknCount2.getPromotion());
int reducePrice = Math.min(recallSknCount1.getReducePrice(), recallSknCount2.getReducePrice());
int ctrValue = Math.min(recallSknCount1.getCtrValue(), recallSknCount2.getCtrValue());
int heatValue = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());
int random = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());
return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);
}
/**
* 按页面+品类+品牌获取商品的召回数量
*
* @param pageId
* @param sortBrand
* @return
*/
public RecallSknCount queryRecallSknCount(int pageId, String sortBrandType, SortBrand sortBrand) {
RecallSknCount sortConfig = this.queryConfigBySort(pageId, sortBrand.getMisort());
RecallSknCount brandConfig = this.queryConfigByBrand(pageId, sortBrand.getBrandId());
if (sortConfig != null && sortConfig != null) {
return genProductCountConfigByCompare(sortConfig, brandConfig);
}
if (sortConfig != null) {
return sortConfig;
}
if (brandConfig != null) {
return brandConfig;
}
RecallSknCount pageConfig = this.queryConfigByPage(pageId, sortBrandType, sortBrand);
if (pageConfig != null) {
return pageConfig;
}
pageConfig = this.queryConfigByPage(pageId, sortBrandType, new SortBrand());//使用默认的值替代
if (pageConfig != null) {
return pageConfig;
}
return genProductCountConfig(6, 3, 6, 6, 6, 0);
}
/**
* 查询中分类在页面上的召回配置,没有则取pageId=0的数据
* @param pageId
* @param middleSortId
* @return
*/
private RecallSknCount queryConfigBySort(int pageId, int middleSortId) {
String sortKey = this.buildSortCacheKey(middleSortId);
return this.getRecallSknCount(sortKey, pageId);
}
/**
* 查询品牌在页面上的召回配置,没有则取pageId=0的数据
* @param pageId
* @param brandId
* @return
*/
private RecallSknCount queryConfigByBrand(int pageId, int brandId) {
String brandKey = this.buildBrandCacheKey(brandId);
return this.getRecallSknCount(brandKey, pageId);
}
/**
* 查询【品类*品牌】在页面上的召回配置,没有则取pageId=0的数据
* @param pageId
* @param sortBrandType
* @param sortBrand
* @return
*/
private RecallSknCount queryConfigByPage(int pageId, String sortBrandType, SortBrand sortBrand) {
String sortBrandKey = this.buildSortBrandCacheKey(sortBrandType, sortBrand);
return this.getRecallSknCount(sortBrandKey, pageId);
}
private RecallSknCount getRecallSknCount(String key, int pageId) {
Map<Integer, RecallSknCount> pageConfig = typePageConfigCache.get(key);
if (pageConfig == null) {
return null;
}
RecallSknCount sknCount = pageConfig.get(pageId);
if (sknCount == null) {
sknCount = pageConfig.get(RecallConfigConstants.DEFAULT_PAGE_ID);
}
return sknCount;
}
}
package com.yoho.search.recall.config;
import com.yoho.search.core.personalized.models.SortBrand;
import com.yoho.search.recall.models.personal.PagePersonalFactor;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
class RecallConfigSortBrandService {
private Map<String,SortBrand> allConfigSortBrand = new HashMap<>();
public List<SortBrand> queryConfigSortBrand(PagePersonalFactor pageFactor, Set<String> filterSortBrandKeys) {
return new ArrayList<>();
//pageFactor.getSortBrandList();
}
}
package com.yoho.search.recall.config;
import com.yoho.search.core.personalized.models.SortBrand;
import com.yoho.search.recall.models.personal.PagePersonalFactor;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
class RecallConfigSortBrandService {
private Map<String,SortBrand> allConfigSortBrand = new HashMap<>();
public List<SortBrand> queryConfigSortBrand(PagePersonalFactor pageFactor, Set<String> filterSortBrandKeys) {
return new ArrayList<>();
//pageFactor.getSortBrandList();
}
}
... ...