Authored by hugufei

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

1 -package com.yoho.search.recall.config;  
2 -  
3 -import com.alibaba.fastjson.JSON;  
4 -import com.yoho.search.dal.model.CsRecallConfigCommon;  
5 -import com.yoho.search.service.base.index.CsRecallConfigCommonIndexBaseService;  
6 -import org.apache.commons.lang.StringUtils;  
7 -import org.slf4j.Logger;  
8 -import org.slf4j.LoggerFactory;  
9 -import org.springframework.beans.factory.annotation.Autowired;  
10 -import org.springframework.stereotype.Component;  
11 -  
12 -import javax.annotation.PostConstruct;  
13 -import java.util.HashMap;  
14 -import java.util.List;  
15 -import java.util.Map;  
16 -import java.util.concurrent.Executors;  
17 -import java.util.concurrent.ScheduledExecutorService;  
18 -import java.util.concurrent.TimeUnit;  
19 -  
20 -@Component  
21 -class RecallConfigCommonService {  
22 -  
23 - private static final Logger logger = LoggerFactory.getLogger(RecallConfigCommonService.class);  
24 -  
25 - @Autowired  
26 - private CsRecallConfigCommonIndexBaseService csRecallConfigCommonIndexBaseService;  
27 -  
28 - private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();  
29 -  
30 - private Map<String, Map<Integer, RecallSizeInterval>> typePageConfigCache = new HashMap<>();  
31 -  
32 - @PostConstruct  
33 - void init() {  
34 - schedule.scheduleAtFixedRate(() -> loadConfig(), 0, 1, TimeUnit.MINUTES);  
35 - }  
36 -  
37 - private void loadConfig() {  
38 - try {  
39 - List<CsRecallConfigCommon> configList = csRecallConfigCommonIndexBaseService.queryAll();  
40 - Map<String, Map<Integer, RecallSizeInterval>> temp = new HashMap<>();  
41 - for (CsRecallConfigCommon csRecallConfigCommon : configList) {  
42 - Map<Integer, RecallSizeInterval> pageConfigs = temp.get(csRecallConfigCommon.getConfigType());  
43 - if (pageConfigs == null) {  
44 - pageConfigs = new HashMap<>();  
45 - temp.put(csRecallConfigCommon.getConfigType(), pageConfigs);  
46 - }  
47 - int size = csRecallConfigCommon.getSize();  
48 - int interval = csRecallConfigCommon.getInterval();  
49 - pageConfigs.put(csRecallConfigCommon.getConfigPage(), new RecallSizeInterval(size, interval));  
50 - }  
51 - typePageConfigCache = temp;  
52 - System.out.println(JSON.toJSONString(typePageConfigCache));  
53 - } catch (Exception e) {  
54 - logger.error(e.getMessage(), e);  
55 - }  
56 - }  
57 -  
58 - private RecallSizeInterval queryCommonConfig(String configKey, int configPage) {  
59 - Map<Integer, RecallSizeInterval> pageConfigMap = typePageConfigCache.get(configKey);  
60 - if (pageConfigMap == null) {  
61 - return null;  
62 - }  
63 - RecallSizeInterval pageConfig = pageConfigMap.get(configPage);  
64 - if (pageConfig == null) {  
65 - pageConfig = pageConfigMap.get(RecallConfigConstants.DEFAULT_PAGE_ID);  
66 - }  
67 - if (pageConfigMap == null) {  
68 - return null;  
69 - } else {  
70 - return pageConfig;  
71 - }  
72 - }  
73 -  
74 - /**  
75 - * 获取配置的大小  
76 - *  
77 - * @param pageId  
78 - * @param configKey  
79 - * @param defaultSize  
80 - * @return  
81 - */  
82 - public int queryConfigSize(int pageId, String configKey, int defaultSize) {  
83 - if (StringUtils.isBlank(configKey)) {  
84 - return defaultSize;  
85 - }  
86 - RecallSizeInterval config = queryCommonConfig(configKey, pageId);  
87 - return config == null ? defaultSize : config.getSize();  
88 - }  
89 -  
90 - /**  
91 - * 获取配置的间隔  
92 - *  
93 - * @param pageId  
94 - * @param configKey  
95 - * @param defaultInterval  
96 - * @return  
97 - */  
98 - public int queryConfigInterval(int pageId, String configKey, int defaultInterval) {  
99 - if (StringUtils.isBlank(configKey)) {  
100 - return defaultInterval;  
101 - }  
102 - RecallSizeInterval config = queryCommonConfig(configKey, pageId);  
103 - return config == null ? defaultInterval : config.getInterval();  
104 - }  
105 -  
106 -} 1 +package com.yoho.search.recall.config;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.yoho.search.dal.model.CsRecallConfigCommon;
  5 +import com.yoho.search.service.base.index.CsRecallConfigCommonIndexBaseService;
  6 +import org.apache.commons.lang.StringUtils;
  7 +import org.slf4j.Logger;
  8 +import org.slf4j.LoggerFactory;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.stereotype.Component;
  11 +
  12 +import javax.annotation.PostConstruct;
  13 +import java.util.HashMap;
  14 +import java.util.List;
  15 +import java.util.Map;
  16 +import java.util.concurrent.Executors;
  17 +import java.util.concurrent.ScheduledExecutorService;
  18 +import java.util.concurrent.TimeUnit;
  19 +
  20 +@Component
  21 +class RecallConfigCommonService {
  22 +
  23 + private static final Logger logger = LoggerFactory.getLogger(RecallConfigCommonService.class);
  24 +
  25 + @Autowired
  26 + private CsRecallConfigCommonIndexBaseService csRecallConfigCommonIndexBaseService;
  27 +
  28 + private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
  29 +
  30 + private Map<String, Map<Integer, RecallSizeInterval>> typePageConfigCache = new HashMap<>();
  31 +
  32 + @PostConstruct
  33 + void init() {
  34 + schedule.scheduleAtFixedRate(() -> loadConfig(), 0, 1, TimeUnit.MINUTES);
  35 + }
  36 +
  37 + private void loadConfig() {
  38 + try {
  39 + List<CsRecallConfigCommon> configList = csRecallConfigCommonIndexBaseService.queryAll();
  40 + Map<String, Map<Integer, RecallSizeInterval>> temp = new HashMap<>();
  41 + for (CsRecallConfigCommon csRecallConfigCommon : configList) {
  42 + Map<Integer, RecallSizeInterval> pageConfigs = temp.get(csRecallConfigCommon.getConfigType());
  43 + if (pageConfigs == null) {
  44 + pageConfigs = new HashMap<>();
  45 + temp.put(csRecallConfigCommon.getConfigType(), pageConfigs);
  46 + }
  47 + int size = csRecallConfigCommon.getSize();
  48 + int interval = csRecallConfigCommon.getInterval();
  49 + pageConfigs.put(csRecallConfigCommon.getConfigPage(), new RecallSizeInterval(size, interval));
  50 + }
  51 + typePageConfigCache = temp;
  52 + System.out.println(JSON.toJSONString(typePageConfigCache));
  53 + } catch (Exception e) {
  54 + logger.error(e.getMessage(), e);
  55 + }
  56 + }
  57 +
  58 + private RecallSizeInterval queryCommonConfig(String configKey, int configPage) {
  59 + Map<Integer, RecallSizeInterval> pageConfigMap = typePageConfigCache.get(configKey);
  60 + if (pageConfigMap == null) {
  61 + return null;
  62 + }
  63 + RecallSizeInterval pageConfig = pageConfigMap.get(configPage);
  64 + if (pageConfig == null) {
  65 + pageConfig = pageConfigMap.get(RecallConfigConstants.DEFAULT_PAGE_ID);
  66 + }
  67 + if (pageConfigMap == null) {
  68 + return null;
  69 + } else {
  70 + return pageConfig;
  71 + }
  72 + }
  73 +
  74 + /**
  75 + * 获取配置的大小
  76 + *
  77 + * @param pageId
  78 + * @param configKey
  79 + * @param defaultSize
  80 + * @return
  81 + */
  82 + public int queryConfigSize(int pageId, String configKey, int defaultSize) {
  83 + if (StringUtils.isBlank(configKey)) {
  84 + return defaultSize;
  85 + }
  86 + RecallSizeInterval config = queryCommonConfig(configKey, pageId);
  87 + return config == null ? defaultSize : config.getSize();
  88 + }
  89 +
  90 + /**
  91 + * 获取配置的间隔
  92 + *
  93 + * @param pageId
  94 + * @param configKey
  95 + * @param defaultInterval
  96 + * @return
  97 + */
  98 + public int queryConfigInterval(int pageId, String configKey, int defaultInterval) {
  99 + if (StringUtils.isBlank(configKey)) {
  100 + return defaultInterval;
  101 + }
  102 + RecallSizeInterval config = queryCommonConfig(configKey, pageId);
  103 + return config == null ? defaultInterval : config.getInterval();
  104 + }
  105 +
  106 +}
1 -package com.yoho.search.recall.config;  
2 -  
3 -import com.alibaba.fastjson.JSON;  
4 -import com.yoho.search.core.personalized.models.SortBrand;  
5 -import com.yoho.search.dal.model.CsRecallConfigProduct;  
6 -import com.yoho.search.service.base.index.CsRecallConfigProductIndexBaseService;  
7 -import org.slf4j.Logger;  
8 -import org.slf4j.LoggerFactory;  
9 -import org.springframework.beans.factory.annotation.Autowired;  
10 -import org.springframework.stereotype.Component;  
11 -  
12 -import javax.annotation.PostConstruct;  
13 -import java.util.HashMap;  
14 -import java.util.List;  
15 -import java.util.Map;  
16 -import java.util.concurrent.Executors;  
17 -import java.util.concurrent.ScheduledExecutorService;  
18 -import java.util.concurrent.TimeUnit;  
19 -  
20 -@Component  
21 -class RecallConfigProductService {  
22 -  
23 - private static final Logger logger = LoggerFactory.getLogger(RecallConfigProductService.class);  
24 -  
25 - @Autowired  
26 - private CsRecallConfigProductIndexBaseService csRecallConfigProductIndexBaseService;  
27 -  
28 - private Map<String, Map<Integer, RecallSknCount>> typePageConfigCache = new HashMap<>();  
29 -  
30 - private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();  
31 -  
32 - @PostConstruct  
33 - void init() {  
34 - schedule.scheduleAtFixedRate(() -> loadRecallSknCountConfig(), 0, 1, TimeUnit.MINUTES);  
35 - }  
36 -  
37 - private void loadRecallSknCountConfig() {  
38 - try {  
39 - Map<String, Map<Integer, RecallSknCount>> tempTypePageConfigCache = new HashMap<>();  
40 - List<CsRecallConfigProduct> configList = csRecallConfigProductIndexBaseService.queryAll();  
41 - for (CsRecallConfigProduct productConfig : configList) {  
42 - String configType = productConfig.getConfigType();  
43 - int configTypeId = productConfig.getConfigTypeId();  
44 - int pageId = productConfig.getConfigPage();  
45 - String configKey = this.buildConfiKey(configType,configTypeId);  
46 - int configStatus = productConfig.getConfigStatus();  
47 - if(configStatus==1){  
48 - this.addElement(configKey,pageId,this.genProductCountConfig(productConfig),tempTypePageConfigCache);  
49 - }  
50 - }  
51 - typePageConfigCache = tempTypePageConfigCache;  
52 - System.out.println(JSON.toJSONString(typePageConfigCache));  
53 - } catch (Exception e) {  
54 - logger.error(e.getMessage(), e);  
55 - }  
56 - }  
57 -  
58 - private String buildConfiKey(String configType, int configTypeId) {  
59 - if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_SORT)) {  
60 - return this.buildSortCacheKey(configTypeId);  
61 - } else if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_BRAND)) {  
62 - return this.buildBrandCacheKey(configTypeId);  
63 - } else {  
64 - return this.buildSortBrandCacheKey(configType, new SortBrand());  
65 - }  
66 - }  
67 -  
68 - private String buildSortCacheKey(int middleSortId) {  
69 - return RecallConfigConstants.RECALL_SKN_COUNT_SORT + "_" + middleSortId;  
70 - }  
71 -  
72 - private String buildBrandCacheKey(int brandId) {  
73 - return RecallConfigConstants.RECALL_SKN_COUNT_BRAND + "_" + brandId;  
74 - }  
75 -  
76 - private String buildSortBrandCacheKey(String sortBrandType, SortBrand sortBrand) {  
77 - return sortBrandType + "_" + sortBrand.key();  
78 - }  
79 -  
80 - private void addElement(String configKey, int pageId, RecallSknCount recallSknCount, Map<String, Map<Integer, RecallSknCount>> map) {  
81 - if (!map.containsKey(configKey)) {  
82 - map.put(configKey, new HashMap<>());  
83 - }  
84 - map.get(configKey).put(pageId, recallSknCount);  
85 - }  
86 -  
87 - private RecallSknCount genProductCountConfig(CsRecallConfigProduct configProduct) {  
88 - int newShelve = configProduct.getNewShelve();  
89 - int promotion = configProduct.getPromotion();  
90 - int reducePrice = configProduct.getReducePrice();  
91 - int ctrValue = configProduct.getCtrValue();  
92 - int heatValue = configProduct.getHeatValue();  
93 - int random = configProduct.getRandom();  
94 - return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);  
95 - }  
96 -  
97 - private RecallSknCount genProductCountConfig(int newShelve, int promotion, int reducePrice, int ctrValue, int heatValue, int random) {  
98 - RecallSknCount recallSknCount = new RecallSknCount();  
99 - recallSknCount.setNewShelve(newShelve);  
100 - recallSknCount.setPromotion(promotion);  
101 - recallSknCount.setReducePrice(reducePrice);  
102 - recallSknCount.setCtrValue(ctrValue);  
103 - recallSknCount.setHeatValue(heatValue);  
104 - recallSknCount.setRandom(random);  
105 - return recallSknCount;  
106 - }  
107 -  
108 - private RecallSknCount genProductCountConfigByCompare(RecallSknCount recallSknCount1, RecallSknCount recallSknCount2) {  
109 - int newShelve = Math.min(recallSknCount1.getNewShelve(), recallSknCount2.getNewShelve());  
110 - int promotion = Math.min(recallSknCount1.getPromotion(), recallSknCount2.getPromotion());  
111 - int reducePrice = Math.min(recallSknCount1.getReducePrice(), recallSknCount2.getReducePrice());  
112 - int ctrValue = Math.min(recallSknCount1.getCtrValue(), recallSknCount2.getCtrValue());  
113 - int heatValue = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());  
114 - int random = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());  
115 - return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);  
116 - }  
117 -  
118 - /**  
119 - * 按页面+品类+品牌获取商品的召回数量  
120 - *  
121 - * @param pageId  
122 - * @param sortBrand  
123 - * @return  
124 - */  
125 - public RecallSknCount queryRecallSknCount(int pageId, String sortBrandType, SortBrand sortBrand) {  
126 - RecallSknCount sortConfig = this.queryConfigBySort(pageId, sortBrand.getMisort());  
127 - RecallSknCount brandConfig = this.queryConfigByBrand(pageId, sortBrand.getBrandId());  
128 - if (sortConfig != null && sortConfig != null) {  
129 - return genProductCountConfigByCompare(sortConfig, brandConfig);  
130 - }  
131 - if (sortConfig != null) {  
132 - return sortConfig;  
133 - }  
134 - if (brandConfig != null) {  
135 - return brandConfig;  
136 - }  
137 - RecallSknCount pageConfig = this.queryConfigByPage(pageId, sortBrandType, sortBrand);  
138 - if (pageConfig != null) {  
139 - return pageConfig;  
140 - }  
141 - pageConfig = this.queryConfigByPage(pageId, sortBrandType, new SortBrand());//使用默认的值替代  
142 - if (pageConfig != null) {  
143 - return pageConfig;  
144 - }  
145 - return genProductCountConfig(6, 3, 6, 6, 6, 0);  
146 - }  
147 -  
148 - /**  
149 - * 查询中分类在页面上的召回配置,没有则取pageId=0的数据  
150 - * @param pageId  
151 - * @param middleSortId  
152 - * @return  
153 - */  
154 - private RecallSknCount queryConfigBySort(int pageId, int middleSortId) {  
155 - String sortKey = this.buildSortCacheKey(middleSortId);  
156 - return this.getRecallSknCount(sortKey, pageId);  
157 - }  
158 -  
159 - /**  
160 - * 查询品牌在页面上的召回配置,没有则取pageId=0的数据  
161 - * @param pageId  
162 - * @param brandId  
163 - * @return  
164 - */  
165 - private RecallSknCount queryConfigByBrand(int pageId, int brandId) {  
166 - String brandKey = this.buildBrandCacheKey(brandId);  
167 - return this.getRecallSknCount(brandKey, pageId);  
168 - }  
169 -  
170 - /**  
171 - * 查询【品类*品牌】在页面上的召回配置,没有则取pageId=0的数据  
172 - * @param pageId  
173 - * @param sortBrandType  
174 - * @param sortBrand  
175 - * @return  
176 - */  
177 - private RecallSknCount queryConfigByPage(int pageId, String sortBrandType, SortBrand sortBrand) {  
178 - String sortBrandKey = this.buildSortBrandCacheKey(sortBrandType, sortBrand);  
179 - return this.getRecallSknCount(sortBrandKey, pageId);  
180 - }  
181 -  
182 - private RecallSknCount getRecallSknCount(String key, int pageId) {  
183 - Map<Integer, RecallSknCount> pageConfig = typePageConfigCache.get(key);  
184 - if (pageConfig == null) {  
185 - return null;  
186 - }  
187 - RecallSknCount sknCount = pageConfig.get(pageId);  
188 - if (sknCount == null) {  
189 - sknCount = pageConfig.get(RecallConfigConstants.DEFAULT_PAGE_ID);  
190 - }  
191 - return sknCount;  
192 - }  
193 -  
194 -} 1 +package com.yoho.search.recall.config;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.yoho.search.core.personalized.models.SortBrand;
  5 +import com.yoho.search.dal.model.CsRecallConfigProduct;
  6 +import com.yoho.search.service.base.index.CsRecallConfigProductIndexBaseService;
  7 +import org.slf4j.Logger;
  8 +import org.slf4j.LoggerFactory;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.stereotype.Component;
  11 +
  12 +import javax.annotation.PostConstruct;
  13 +import java.util.HashMap;
  14 +import java.util.List;
  15 +import java.util.Map;
  16 +import java.util.concurrent.Executors;
  17 +import java.util.concurrent.ScheduledExecutorService;
  18 +import java.util.concurrent.TimeUnit;
  19 +
  20 +@Component
  21 +class RecallConfigProductService {
  22 +
  23 + private static final Logger logger = LoggerFactory.getLogger(RecallConfigProductService.class);
  24 +
  25 + @Autowired
  26 + private CsRecallConfigProductIndexBaseService csRecallConfigProductIndexBaseService;
  27 +
  28 + private Map<String, Map<Integer, RecallSknCount>> typePageConfigCache = new HashMap<>();
  29 +
  30 + private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
  31 +
  32 + @PostConstruct
  33 + void init() {
  34 + schedule.scheduleAtFixedRate(() -> loadRecallSknCountConfig(), 0, 1, TimeUnit.MINUTES);
  35 + }
  36 +
  37 + private void loadRecallSknCountConfig() {
  38 + try {
  39 + Map<String, Map<Integer, RecallSknCount>> tempTypePageConfigCache = new HashMap<>();
  40 + List<CsRecallConfigProduct> configList = csRecallConfigProductIndexBaseService.queryAll();
  41 + for (CsRecallConfigProduct productConfig : configList) {
  42 + String configType = productConfig.getConfigType();
  43 + int configTypeId = productConfig.getConfigTypeId();
  44 + int pageId = productConfig.getConfigPage();
  45 + String configKey = this.buildConfiKey(configType,configTypeId);
  46 + int configStatus = productConfig.getConfigStatus();
  47 + if(configStatus==1){
  48 + this.addElement(configKey,pageId,this.genProductCountConfig(productConfig),tempTypePageConfigCache);
  49 + }
  50 + }
  51 + typePageConfigCache = tempTypePageConfigCache;
  52 + System.out.println(JSON.toJSONString(typePageConfigCache));
  53 + } catch (Exception e) {
  54 + logger.error(e.getMessage(), e);
  55 + }
  56 + }
  57 +
  58 + private String buildConfiKey(String configType, int configTypeId) {
  59 + if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_SORT)) {
  60 + return this.buildSortCacheKey(configTypeId);
  61 + } else if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_BRAND)) {
  62 + return this.buildBrandCacheKey(configTypeId);
  63 + } else {
  64 + return this.buildSortBrandCacheKey(configType, new SortBrand());
  65 + }
  66 + }
  67 +
  68 + private String buildSortCacheKey(int middleSortId) {
  69 + return RecallConfigConstants.RECALL_SKN_COUNT_SORT + "_" + middleSortId;
  70 + }
  71 +
  72 + private String buildBrandCacheKey(int brandId) {
  73 + return RecallConfigConstants.RECALL_SKN_COUNT_BRAND + "_" + brandId;
  74 + }
  75 +
  76 + private String buildSortBrandCacheKey(String sortBrandType, SortBrand sortBrand) {
  77 + return sortBrandType + "_" + sortBrand.key();
  78 + }
  79 +
  80 + private void addElement(String configKey, int pageId, RecallSknCount recallSknCount, Map<String, Map<Integer, RecallSknCount>> map) {
  81 + if (!map.containsKey(configKey)) {
  82 + map.put(configKey, new HashMap<>());
  83 + }
  84 + map.get(configKey).put(pageId, recallSknCount);
  85 + }
  86 +
  87 + private RecallSknCount genProductCountConfig(CsRecallConfigProduct configProduct) {
  88 + int newShelve = configProduct.getNewShelve();
  89 + int promotion = configProduct.getPromotion();
  90 + int reducePrice = configProduct.getReducePrice();
  91 + int ctrValue = configProduct.getCtrValue();
  92 + int heatValue = configProduct.getHeatValue();
  93 + int random = configProduct.getRandom();
  94 + return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);
  95 + }
  96 +
  97 + private RecallSknCount genProductCountConfig(int newShelve, int promotion, int reducePrice, int ctrValue, int heatValue, int random) {
  98 + RecallSknCount recallSknCount = new RecallSknCount();
  99 + recallSknCount.setNewShelve(newShelve);
  100 + recallSknCount.setPromotion(promotion);
  101 + recallSknCount.setReducePrice(reducePrice);
  102 + recallSknCount.setCtrValue(ctrValue);
  103 + recallSknCount.setHeatValue(heatValue);
  104 + recallSknCount.setRandom(random);
  105 + return recallSknCount;
  106 + }
  107 +
  108 + private RecallSknCount genProductCountConfigByCompare(RecallSknCount recallSknCount1, RecallSknCount recallSknCount2) {
  109 + int newShelve = Math.min(recallSknCount1.getNewShelve(), recallSknCount2.getNewShelve());
  110 + int promotion = Math.min(recallSknCount1.getPromotion(), recallSknCount2.getPromotion());
  111 + int reducePrice = Math.min(recallSknCount1.getReducePrice(), recallSknCount2.getReducePrice());
  112 + int ctrValue = Math.min(recallSknCount1.getCtrValue(), recallSknCount2.getCtrValue());
  113 + int heatValue = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());
  114 + int random = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());
  115 + return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);
  116 + }
  117 +
  118 + /**
  119 + * 按页面+品类+品牌获取商品的召回数量
  120 + *
  121 + * @param pageId
  122 + * @param sortBrand
  123 + * @return
  124 + */
  125 + public RecallSknCount queryRecallSknCount(int pageId, String sortBrandType, SortBrand sortBrand) {
  126 + RecallSknCount sortConfig = this.queryConfigBySort(pageId, sortBrand.getMisort());
  127 + RecallSknCount brandConfig = this.queryConfigByBrand(pageId, sortBrand.getBrandId());
  128 + if (sortConfig != null && sortConfig != null) {
  129 + return genProductCountConfigByCompare(sortConfig, brandConfig);
  130 + }
  131 + if (sortConfig != null) {
  132 + return sortConfig;
  133 + }
  134 + if (brandConfig != null) {
  135 + return brandConfig;
  136 + }
  137 + RecallSknCount pageConfig = this.queryConfigByPage(pageId, sortBrandType, sortBrand);
  138 + if (pageConfig != null) {
  139 + return pageConfig;
  140 + }
  141 + pageConfig = this.queryConfigByPage(pageId, sortBrandType, new SortBrand());//使用默认的值替代
  142 + if (pageConfig != null) {
  143 + return pageConfig;
  144 + }
  145 + return genProductCountConfig(6, 3, 6, 6, 6, 0);
  146 + }
  147 +
  148 + /**
  149 + * 查询中分类在页面上的召回配置,没有则取pageId=0的数据
  150 + * @param pageId
  151 + * @param middleSortId
  152 + * @return
  153 + */
  154 + private RecallSknCount queryConfigBySort(int pageId, int middleSortId) {
  155 + String sortKey = this.buildSortCacheKey(middleSortId);
  156 + return this.getRecallSknCount(sortKey, pageId);
  157 + }
  158 +
  159 + /**
  160 + * 查询品牌在页面上的召回配置,没有则取pageId=0的数据
  161 + * @param pageId
  162 + * @param brandId
  163 + * @return
  164 + */
  165 + private RecallSknCount queryConfigByBrand(int pageId, int brandId) {
  166 + String brandKey = this.buildBrandCacheKey(brandId);
  167 + return this.getRecallSknCount(brandKey, pageId);
  168 + }
  169 +
  170 + /**
  171 + * 查询【品类*品牌】在页面上的召回配置,没有则取pageId=0的数据
  172 + * @param pageId
  173 + * @param sortBrandType
  174 + * @param sortBrand
  175 + * @return
  176 + */
  177 + private RecallSknCount queryConfigByPage(int pageId, String sortBrandType, SortBrand sortBrand) {
  178 + String sortBrandKey = this.buildSortBrandCacheKey(sortBrandType, sortBrand);
  179 + return this.getRecallSknCount(sortBrandKey, pageId);
  180 + }
  181 +
  182 + private RecallSknCount getRecallSknCount(String key, int pageId) {
  183 + Map<Integer, RecallSknCount> pageConfig = typePageConfigCache.get(key);
  184 + if (pageConfig == null) {
  185 + return null;
  186 + }
  187 + RecallSknCount sknCount = pageConfig.get(pageId);
  188 + if (sknCount == null) {
  189 + sknCount = pageConfig.get(RecallConfigConstants.DEFAULT_PAGE_ID);
  190 + }
  191 + return sknCount;
  192 + }
  193 +
  194 +}
1 -package com.yoho.search.recall.config;  
2 -  
3 -import com.alibaba.fastjson.JSON;  
4 -import com.yoho.search.dal.model.CsRecallConfigCommon;  
5 -import com.yoho.search.service.base.index.CsRecallConfigCommonIndexBaseService;  
6 -import org.apache.commons.lang.StringUtils;  
7 -import org.slf4j.Logger;  
8 -import org.slf4j.LoggerFactory;  
9 -import org.springframework.beans.factory.annotation.Autowired;  
10 -import org.springframework.stereotype.Component;  
11 -  
12 -import javax.annotation.PostConstruct;  
13 -import java.util.HashMap;  
14 -import java.util.List;  
15 -import java.util.Map;  
16 -import java.util.concurrent.Executors;  
17 -import java.util.concurrent.ScheduledExecutorService;  
18 -import java.util.concurrent.TimeUnit;  
19 -  
20 -@Component  
21 -class RecallConfigServiceCommon {  
22 -  
23 - private static final Logger logger = LoggerFactory.getLogger(RecallConfigServiceCommon.class);  
24 -  
25 - @Autowired  
26 - private CsRecallConfigCommonIndexBaseService csRecallConfigCommonIndexBaseService;  
27 -  
28 - private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();  
29 -  
30 - private Map<String, Map<Integer, RecallSizeInterval>> typePageConfigCache = new HashMap<>();  
31 -  
32 - @PostConstruct  
33 - void init() {  
34 - schedule.scheduleAtFixedRate(() -> loadConfig(), 0, 1, TimeUnit.MINUTES);  
35 - }  
36 -  
37 - private void loadConfig() {  
38 - try {  
39 - List<CsRecallConfigCommon> configList = csRecallConfigCommonIndexBaseService.queryAll();  
40 - Map<String, Map<Integer, RecallSizeInterval>> temp = new HashMap<>();  
41 - for (CsRecallConfigCommon csRecallConfigCommon : configList) {  
42 - Map<Integer, RecallSizeInterval> pageConfigs = temp.get(csRecallConfigCommon.getConfigType());  
43 - if (pageConfigs == null) {  
44 - pageConfigs = new HashMap<>();  
45 - temp.put(csRecallConfigCommon.getConfigType(), pageConfigs);  
46 - }  
47 - int size = csRecallConfigCommon.getSize();  
48 - int interval = csRecallConfigCommon.getInterval();  
49 - pageConfigs.put(csRecallConfigCommon.getConfigPage(), new RecallSizeInterval(size, interval));  
50 - }  
51 - typePageConfigCache = temp;  
52 - System.out.println(JSON.toJSONString(typePageConfigCache));  
53 - } catch (Exception e) {  
54 - logger.error(e.getMessage(), e);  
55 - }  
56 - }  
57 -  
58 - private RecallSizeInterval queryCommonConfig(String configKey, int configPage) {  
59 - Map<Integer, RecallSizeInterval> pageConfigMap = typePageConfigCache.get(configKey);  
60 - if (pageConfigMap == null) {  
61 - return null;  
62 - }  
63 - RecallSizeInterval pageConfig = pageConfigMap.get(configPage);  
64 - if (pageConfig == null) {  
65 - pageConfig = pageConfigMap.get(RecallConfigConstants.DEFAULT_PAGE_ID);  
66 - }  
67 - if (pageConfigMap == null) {  
68 - return null;  
69 - } else {  
70 - return pageConfig;  
71 - }  
72 - }  
73 -  
74 - /**  
75 - * 获取配置的大小  
76 - *  
77 - * @param pageId  
78 - * @param configKey  
79 - * @param defaultSize  
80 - * @return  
81 - */  
82 - public int queryConfigSize(int pageId, String configKey, int defaultSize) {  
83 - if (StringUtils.isBlank(configKey)) {  
84 - return defaultSize;  
85 - }  
86 - RecallSizeInterval config = queryCommonConfig(configKey, pageId);  
87 - return config == null ? defaultSize : config.getSize();  
88 - }  
89 -  
90 - /**  
91 - * 获取配置的间隔  
92 - *  
93 - * @param pageId  
94 - * @param configKey  
95 - * @param defaultInterval  
96 - * @return  
97 - */  
98 - public int queryConfigInterval(int pageId, String configKey, int defaultInterval) {  
99 - if (StringUtils.isBlank(configKey)) {  
100 - return defaultInterval;  
101 - }  
102 - RecallSizeInterval config = queryCommonConfig(configKey, pageId);  
103 - return config == null ? defaultInterval : config.getInterval();  
104 - }  
105 -  
106 -}  
1 -package com.yoho.search.recall.config;  
2 -  
3 -import com.alibaba.fastjson.JSON;  
4 -import com.yoho.search.core.personalized.models.SortBrand;  
5 -import com.yoho.search.dal.model.CsRecallConfigProduct;  
6 -import com.yoho.search.recall.beans.strategy.SortBrandType;  
7 -import com.yoho.search.service.base.index.CsRecallConfigProductIndexBaseService;  
8 -import org.slf4j.Logger;  
9 -import org.slf4j.LoggerFactory;  
10 -import org.springframework.beans.factory.annotation.Autowired;  
11 -import org.springframework.stereotype.Component;  
12 -  
13 -import javax.annotation.PostConstruct;  
14 -import java.util.HashMap;  
15 -import java.util.List;  
16 -import java.util.Map;  
17 -import java.util.concurrent.Executors;  
18 -import java.util.concurrent.ScheduledExecutorService;  
19 -import java.util.concurrent.TimeUnit;  
20 -  
21 -@Component  
22 -class RecallConfigServiceProduct {  
23 -  
24 - private static final Logger logger = LoggerFactory.getLogger(RecallConfigServiceProduct.class);  
25 -  
26 - @Autowired  
27 - private CsRecallConfigProductIndexBaseService csRecallConfigProductIndexBaseService;  
28 -  
29 - private Map<String, Map<Integer, RecallSknCount>> typePageConfigCache = new HashMap<>();  
30 -  
31 - private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();  
32 -  
33 - @PostConstruct  
34 - void init() {  
35 - schedule.scheduleAtFixedRate(() -> loadRecallSknCountConfig(), 0, 1, TimeUnit.MINUTES);  
36 - }  
37 -  
38 - private void loadRecallSknCountConfig() {  
39 - try {  
40 - Map<String, Map<Integer, RecallSknCount>> tempTypePageConfigCache = new HashMap<>();  
41 - List<CsRecallConfigProduct> configList = csRecallConfigProductIndexBaseService.queryAll();  
42 - for (CsRecallConfigProduct productConfig : configList) {  
43 - String configType = productConfig.getConfigType();  
44 - int configTypeId = productConfig.getConfigTypeId();  
45 - int pageId = productConfig.getConfigPage();  
46 - String configKey = this.buildConfiKey(configType,configTypeId);  
47 - int configStatus = productConfig.getConfigStatus();  
48 - if(configStatus==1){  
49 - this.addElement(configKey,pageId,this.genProductCountConfig(productConfig),tempTypePageConfigCache);  
50 - }  
51 - }  
52 - typePageConfigCache = tempTypePageConfigCache;  
53 - System.out.println(JSON.toJSONString(typePageConfigCache));  
54 - } catch (Exception e) {  
55 - logger.error(e.getMessage(), e);  
56 - }  
57 - }  
58 -  
59 - private String buildConfiKey(String configType, int configTypeId) {  
60 - if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_SORT)) {  
61 - return this.buildSortCacheKey(configTypeId);  
62 - } else if (configType.equalsIgnoreCase(RecallConfigConstants.RECALL_SKN_COUNT_BRAND)) {  
63 - return this.buildBrandCacheKey(configTypeId);  
64 - } else {  
65 - return this.buildSortBrandCacheKey(configType, new SortBrand());  
66 - }  
67 - }  
68 -  
69 - private String buildSortCacheKey(int middleSortId) {  
70 - return RecallConfigConstants.RECALL_SKN_COUNT_SORT + "_" + middleSortId;  
71 - }  
72 -  
73 - private String buildBrandCacheKey(int brandId) {  
74 - return RecallConfigConstants.RECALL_SKN_COUNT_BRAND + "_" + brandId;  
75 - }  
76 -  
77 - private String buildSortBrandCacheKey(String sortBrandType, SortBrand sortBrand) {  
78 - return sortBrandType + "_" + sortBrand.key();  
79 - }  
80 -  
81 - private void addElement(String configKey, int pageId, RecallSknCount recallSknCount, Map<String, Map<Integer, RecallSknCount>> map) {  
82 - if (!map.containsKey(configKey)) {  
83 - map.put(configKey, new HashMap<>());  
84 - }  
85 - map.get(configKey).put(pageId, recallSknCount);  
86 - }  
87 -  
88 - private RecallSknCount genProductCountConfig(CsRecallConfigProduct configProduct) {  
89 - int newShelve = configProduct.getNewShelve();  
90 - int promotion = configProduct.getPromotion();  
91 - int reducePrice = configProduct.getReducePrice();  
92 - int ctrValue = configProduct.getCtrValue();  
93 - int heatValue = configProduct.getHeatValue();  
94 - int random = configProduct.getRandom();  
95 - return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);  
96 - }  
97 -  
98 - private RecallSknCount genProductCountConfig(int newShelve, int promotion, int reducePrice, int ctrValue, int heatValue, int random) {  
99 - RecallSknCount recallSknCount = new RecallSknCount();  
100 - recallSknCount.setNewShelve(newShelve);  
101 - recallSknCount.setPromotion(promotion);  
102 - recallSknCount.setReducePrice(reducePrice);  
103 - recallSknCount.setCtrValue(ctrValue);  
104 - recallSknCount.setHeatValue(heatValue);  
105 - recallSknCount.setRandom(random);  
106 - return recallSknCount;  
107 - }  
108 -  
109 - private RecallSknCount genProductCountConfigByCompare(RecallSknCount recallSknCount1, RecallSknCount recallSknCount2) {  
110 - int newShelve = Math.min(recallSknCount1.getNewShelve(), recallSknCount2.getNewShelve());  
111 - int promotion = Math.min(recallSknCount1.getPromotion(), recallSknCount2.getPromotion());  
112 - int reducePrice = Math.min(recallSknCount1.getReducePrice(), recallSknCount2.getReducePrice());  
113 - int ctrValue = Math.min(recallSknCount1.getCtrValue(), recallSknCount2.getCtrValue());  
114 - int heatValue = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());  
115 - int random = Math.min(recallSknCount1.getHeatValue(), recallSknCount2.getHeatValue());  
116 - return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);  
117 - }  
118 -  
119 - /**  
120 - * 按页面+品类+品牌获取商品的召回数量  
121 - *  
122 - * @param pageId  
123 - * @param sortBrand  
124 - * @return  
125 - */  
126 - public RecallSknCount queryRecallSknCount(int pageId, String sortBrandType, SortBrand sortBrand) {  
127 - RecallSknCount sortConfig = this.queryConfigBySort(pageId, sortBrand.getMisort());  
128 - RecallSknCount brandConfig = this.queryConfigByBrand(pageId, sortBrand.getBrandId());  
129 - if (sortConfig != null && sortConfig != null) {  
130 - return genProductCountConfigByCompare(sortConfig, brandConfig);  
131 - }  
132 - if (sortConfig != null) {  
133 - return sortConfig;  
134 - }  
135 - if (brandConfig != null) {  
136 - return brandConfig;  
137 - }  
138 - RecallSknCount pageConfig = this.queryConfigByPage(pageId, sortBrandType, sortBrand);  
139 - if (pageConfig != null) {  
140 - return pageConfig;  
141 - }  
142 - pageConfig = this.queryConfigByPage(pageId, sortBrandType, new SortBrand());//使用默认的值替代  
143 - if (pageConfig != null) {  
144 - return pageConfig;  
145 - }  
146 - return genProductCountConfig(6, 3, 6, 6, 6, 0);  
147 - }  
148 -  
149 - /**  
150 - * 查询中分类在页面上的召回配置,没有则取pageId=0的数据  
151 - * @param pageId  
152 - * @param middleSortId  
153 - * @return  
154 - */  
155 - private RecallSknCount queryConfigBySort(int pageId, int middleSortId) {  
156 - String sortKey = this.buildSortCacheKey(middleSortId);  
157 - return this.getRecallSknCount(sortKey, pageId);  
158 - }  
159 -  
160 - /**  
161 - * 查询品牌在页面上的召回配置,没有则取pageId=0的数据  
162 - * @param pageId  
163 - * @param brandId  
164 - * @return  
165 - */  
166 - private RecallSknCount queryConfigByBrand(int pageId, int brandId) {  
167 - String brandKey = this.buildBrandCacheKey(brandId);  
168 - return this.getRecallSknCount(brandKey, pageId);  
169 - }  
170 -  
171 - /**  
172 - * 查询【品类*品牌】在页面上的召回配置,没有则取pageId=0的数据  
173 - * @param pageId  
174 - * @param sortBrandType  
175 - * @param sortBrand  
176 - * @return  
177 - */  
178 - private RecallSknCount queryConfigByPage(int pageId, String sortBrandType, SortBrand sortBrand) {  
179 - String sortBrandKey = this.buildSortBrandCacheKey(sortBrandType, sortBrand);  
180 - return this.getRecallSknCount(sortBrandKey, pageId);  
181 - }  
182 -  
183 - private RecallSknCount getRecallSknCount(String key, int pageId) {  
184 - Map<Integer, RecallSknCount> pageConfig = typePageConfigCache.get(key);  
185 - if (pageConfig == null) {  
186 - return null;  
187 - }  
188 - RecallSknCount sknCount = pageConfig.get(pageId);  
189 - if (sknCount == null) {  
190 - sknCount = pageConfig.get(RecallConfigConstants.DEFAULT_PAGE_ID);  
191 - }  
192 - return sknCount;  
193 - }  
194 -  
195 -}  
1 -package com.yoho.search.recall.config;  
2 -  
3 -import com.yoho.search.core.personalized.models.SortBrand;  
4 -import com.yoho.search.recall.models.personal.PagePersonalFactor;  
5 -import org.springframework.stereotype.Component;  
6 -  
7 -import java.util.*;  
8 -  
9 -@Component  
10 -class RecallConfigSortBrandService {  
11 -  
12 - private Map<String,SortBrand> allConfigSortBrand = new HashMap<>();  
13 -  
14 - public List<SortBrand> queryConfigSortBrand(PagePersonalFactor pageFactor, Set<String> filterSortBrandKeys) {  
15 - return new ArrayList<>();  
16 - //pageFactor.getSortBrandList();  
17 - }  
18 -} 1 +package com.yoho.search.recall.config;
  2 +
  3 +import com.yoho.search.core.personalized.models.SortBrand;
  4 +import com.yoho.search.recall.models.personal.PagePersonalFactor;
  5 +import org.springframework.stereotype.Component;
  6 +
  7 +import java.util.*;
  8 +
  9 +@Component
  10 +class RecallConfigSortBrandService {
  11 +
  12 + private Map<String,SortBrand> allConfigSortBrand = new HashMap<>();
  13 +
  14 + public List<SortBrand> queryConfigSortBrand(PagePersonalFactor pageFactor, Set<String> filterSortBrandKeys) {
  15 + return new ArrayList<>();
  16 + //pageFactor.getSortBrandList();
  17 + }
  18 +}