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