Authored by hugufei

fix bug

@@ -75,78 +75,45 @@ public class PromotionIndexBaseService { @@ -75,78 +75,45 @@ public class PromotionIndexBaseService {
75 return recommendPromotionAggVO; 75 return recommendPromotionAggVO;
76 } 76 }
77 77
78 -  
79 - /**  
80 - * 传入一个productList,加入促销标签  
81 - */  
82 public List<Map<String, Object>> fillPromotionTag(List<Map<String, Object>> productList) { 78 public List<Map<String, Object>> fillPromotionTag(List<Map<String, Object>> productList) {
83 - try { 79 + //1、判断
84 if (productList == null || productList.isEmpty()) { 80 if (productList == null || productList.isEmpty()) {
85 - return new ArrayList<>(); 81 + return productList;
86 } 82 }
87 - //1.全量查promotion索引内容,用于和每个skn匹配,1分钟缓存 83 + //2、全量查promotion索引内容,用于和每个skn匹配,1分钟缓存
88 List<PromotionCond> promotionCondList = promotionCondService.getPromotionCondList(); 84 List<PromotionCond> promotionCondList = promotionCondService.getPromotionCondList();
89 - //2.构建每个skn匹配的促销  
90 - List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); 85 + if (promotionCondList == null || promotionCondList.isEmpty()) {
  86 + return productList;
  87 + }
  88 + //3、执行匹配
91 for (Map<String, Object> productMap : productList) { 89 for (Map<String, Object> productMap : productList) {
92 - //全球购跳过  
93 - if ("Y".equals(MapUtils.getString(productMap, "is_global"))) {  
94 - productMap.put("promotion_tag", new JSONArray());  
95 - results.add(productMap);  
96 - continue;  
97 - }  
98 - //2.1 获取该skn匹配上的促销  
99 - List<PromotionCond> matchedPromotionCondList = promotionCondList.stream()  
100 - .filter(promotionCond -> promotionMatchService.matchProduct(promotionCond, productMap))  
101 - .collect(Collectors.toList());  
102 - //2.2 查询促销类型->促销名映射的索引获取map,用于填充促销标签名称,1分钟缓存  
103 - Map<String, String> promotionTypeMap = promotionTypeService.getPromotionTypeMap();  
104 - //2.3 生成促销标签对象列表  
105 - if (org.apache.commons.collections.CollectionUtils.isNotEmpty(matchedPromotionCondList)) {  
106 - List<JSONObject> matchedPromotions = matchedPromotionCondList.stream()  
107 - .map(promotionCond -> {  
108 - JSONObject matchedPromotion = new JSONObject();  
109 - matchedPromotion.put("id", promotionCond.getPromotionId());  
110 - matchedPromotion.put("type", promotionCond.getPromotionType());  
111 - matchedPromotion.put("startTime", promotionCond.getStartTime());  
112 - matchedPromotion.put("endTime", promotionCond.getEndTime());  
113 - if (!promotionTypeMap.isEmpty() && promotionCond.getShowStatus() == 1) {  
114 - matchedPromotion.put("name", promotionTypeMap.get(promotionCond.getPromotionType())); 90 + List<JSONObject> promotion_tag = this.getProductPromotionTag(productMap, promotionCondList);
  91 + productMap.put("promotion_tag", promotion_tag);
115 } 92 }
116 - return matchedPromotion;  
117 - }).collect(Collectors.toList());  
118 - //2.4 列表中插入促销标签信息  
119 - productMap.put("promotion_tag", matchedPromotions); 93 + //4、返回
  94 + return productList;
120 } 95 }
121 - results.add(productMap); 96 +
  97 + private List<JSONObject> getProductPromotionTag(Map<String, Object> productMap, List<PromotionCond> promotionCondList) {
  98 + try {
  99 + //1、promotionCondList判断
  100 + if (promotionCondList == null || promotionCondList.isEmpty()) {
  101 + return new ArrayList<>();
122 } 102 }
123 - return results;  
124 - } catch (Exception e) {  
125 - logger.error(e.getMessage(), e); 103 + //2、全球购跳过
  104 + if ("Y".equals(MapUtils.getString(productMap, "is_global"))) {
126 return new ArrayList<>(); 105 return new ArrayList<>();
127 } 106 }
  107 + //3、获取该skn匹配上的促销
  108 + List<PromotionCond> matchedPromotionCondList = promotionCondList.stream().filter(promotionCond -> promotionMatchService.matchProduct(promotionCond, productMap)).collect(Collectors.toList());
  109 + if(matchedPromotionCondList==null || matchedPromotionCondList.isEmpty()){
  110 + return new ArrayList<>();
128 } 111 }
129 -  
130 -  
131 - /**  
132 - * 获取单个product的促销标签-会弃用  
133 - */  
134 - public List<JSONObject> getPromotionTag(Map<String, Object> map) {  
135 - try {  
136 - //全量查promotion索引内容,用于和每个skn匹配,1分钟缓存  
137 - List<PromotionCond> promotionCondList = promotionCondService.getPromotionCondList();  
138 -  
139 - //获取该skn匹配上的促销  
140 - List<PromotionCond> matchedPromotionCondList = promotionCondList.stream()  
141 - .filter(promotionCond -> promotionMatchService.matchProduct(promotionCond, map))  
142 - .collect(Collectors.toList());  
143 - //查询促销类型->促销名映射的索引获取map,用于填充促销标签名称,1分钟缓存 112 + //4、查询促销类型->促销名映射的索引获取map,用于填充促销标签名称,1分钟缓存
144 Map<String, String> promotionTypeMap = promotionTypeService.getPromotionTypeMap(); 113 Map<String, String> promotionTypeMap = promotionTypeService.getPromotionTypeMap();
145 - //生成促销标签对象列表  
146 - List<JSONObject> matchedPromotions = new ArrayList<>();  
147 - if (org.apache.commons.collections.CollectionUtils.isNotEmpty(matchedPromotionCondList)) {  
148 - matchedPromotions = matchedPromotionCondList.stream()  
149 - .map(promotionCond -> { 114 + //5、生成促销标签对象列表
  115 + List<JSONObject> results = new ArrayList<>();
  116 + for (PromotionCond promotionCond:matchedPromotionCondList){
150 JSONObject matchedPromotion = new JSONObject(); 117 JSONObject matchedPromotion = new JSONObject();
151 matchedPromotion.put("id", promotionCond.getPromotionId()); 118 matchedPromotion.put("id", promotionCond.getPromotionId());
152 matchedPromotion.put("type", promotionCond.getPromotionType()); 119 matchedPromotion.put("type", promotionCond.getPromotionType());
@@ -155,12 +122,11 @@ public class PromotionIndexBaseService { @@ -155,12 +122,11 @@ public class PromotionIndexBaseService {
155 if (!promotionTypeMap.isEmpty() && promotionCond.getShowStatus() == 1) { 122 if (!promotionTypeMap.isEmpty() && promotionCond.getShowStatus() == 1) {
156 matchedPromotion.put("name", promotionTypeMap.get(promotionCond.getPromotionType())); 123 matchedPromotion.put("name", promotionTypeMap.get(promotionCond.getPromotionType()));
157 } 124 }
158 - return matchedPromotion;  
159 - }).collect(Collectors.toList()); 125 + results.add(matchedPromotion);
160 } 126 }
161 - return matchedPromotions; 127 + return results;
162 } catch (Exception e) { 128 } catch (Exception e) {
163 - logger.error(e.getMessage(), e); 129 + logger.error(e.getMessage(),e);
164 return new ArrayList<>(); 130 return new ArrayList<>();
165 } 131 }
166 } 132 }
@@ -30,37 +30,41 @@ public class PromotionPriceService { @@ -30,37 +30,41 @@ public class PromotionPriceService {
30 private PromotionMatchService promotionMatchService; 30 private PromotionMatchService promotionMatchService;
31 31
32 public List<Map<String, Object>> fillPromotionPrice(List<Map<String, Object>> productList) { 32 public List<Map<String, Object>> fillPromotionPrice(List<Map<String, Object>> productList) {
33 - try {  
34 - List<Map<String, Object>> results = new ArrayList<>();  
35 - if (CollectionUtils.isEmpty(productList)) {  
36 - return new ArrayList<>(); 33 + //1、参数判断
  34 + if (productList == null || productList.isEmpty()) {
  35 + return productList;
37 } 36 }
38 - //获取所有促销信息,制作一个以promotionId为key的Map 37 + //2、获取所有促销信息,制作一个以promotionId为key的Map
39 List<PromotionIndexBO> promotionIndexBOList = promotionCondService.getPromotionList(); 38 List<PromotionIndexBO> promotionIndexBOList = promotionCondService.getPromotionList();
40 if (CollectionUtils.isEmpty(promotionIndexBOList)) { 39 if (CollectionUtils.isEmpty(promotionIndexBOList)) {
41 return productList; 40 return productList;
42 } 41 }
43 Map<Integer, PromotionIndexBO> promotionIndexBOMap = promotionIndexBOList.stream().collect(Collectors.toMap(PromotionIndexBO::getId, p -> p)); 42 Map<Integer, PromotionIndexBO> promotionIndexBOMap = promotionIndexBOList.stream().collect(Collectors.toMap(PromotionIndexBO::getId, p -> p));
  43 + //3、循环匹配promotionPrice
  44 + for (Map<String, Object> product : productList) {
  45 + Integer promotionPrice = this.getPromotionPrice(product, promotionIndexBOMap);
  46 + product.put("promotion_price", promotionPrice);
  47 + }
  48 + //4、返回结果
  49 + return productList;
  50 + }
44 51
45 - //遍历每个skn,计算promotion_price字段  
46 - for (Map<String, Object> productMap : productList) {  
47 - 52 + private Integer getPromotionPrice(Map<String, Object> product, Map<Integer, PromotionIndexBO> promotionIndexBOMap) {
  53 + try {
48 //全球购skn,promotion_price为空 54 //全球购skn,promotion_price为空
49 - if ("Y".equals(MapUtils.getString(productMap, "is_global"))) {  
50 - productMap.put("promotion_price", null);  
51 - results.add(productMap);  
52 - continue; 55 + if ("Y".equals(MapUtils.getString(product, "is_global"))) {
  56 + return null;
53 } 57 }
54 -  
55 //获取当前变价计划里的salesPrice,默认salesPrice为skn的sales_price 58 //获取当前变价计划里的salesPrice,默认salesPrice为skn的sales_price
56 - Double salesPrice = this.getProductPricePlanSalesPrice(productMap);  
57 - 59 + Double salesPrice = this.getProductPricePlanSalesPrice(product);
  60 + if (salesPrice == null) {
  61 + return null;
  62 + }
58 //获取单个skn的促销列表 63 //获取单个skn的促销列表
59 - productMap.put("promotion_price", null);  
60 - List<Map<String, String>> promotionTagList = (List<Map<String, String>>) productMap.get("promotion_tag");  
61 -  
62 - if (CollectionUtils.isNotEmpty(promotionTagList)) {  
63 - boolean isPromotionPriceActive = false; 64 + List<Map<String, String>> promotionTagList = (List<Map<String, String>>) product.get("promotion_tag");
  65 + if (promotionTagList == null || promotionTagList.isEmpty()) {
  66 + return null;
  67 + }
64 List<PromotionIndexBO> promotionIndexBOS = new ArrayList<>(); 68 List<PromotionIndexBO> promotionIndexBOS = new ArrayList<>();
65 Map<Integer, Map<String, String>> promotionTagMap = new HashMap<>(); 69 Map<Integer, Map<String, String>> promotionTagMap = new HashMap<>();
66 for (Map<String, String> promotionTag : promotionTagList) { 70 for (Map<String, String> promotionTag : promotionTagList) {
@@ -71,20 +75,20 @@ public class PromotionPriceService { @@ -71,20 +75,20 @@ public class PromotionPriceService {
71 } 75 }
72 //生成安装权重排序后的促销列表 76 //生成安装权重排序后的促销列表
73 List<PromotionIndexBO> tempPromotionIndexBos = promotionIndexBOS.stream().sorted(Comparator.comparing(PromotionIndexBO::getPriority).reversed()).collect(Collectors.toList()); 77 List<PromotionIndexBO> tempPromotionIndexBos = promotionIndexBOS.stream().sorted(Comparator.comparing(PromotionIndexBO::getPriority).reversed()).collect(Collectors.toList());
74 - List<PromotionIndexBO> SortedPromotionIndexBOs = new ArrayList<>(); 78 + List<PromotionIndexBO> sortedPromotionIndexBOs = new ArrayList<>();
75 //去掉不满足的促销 79 //去掉不满足的促销
76 for (PromotionIndexBO promotionIndexBO : tempPromotionIndexBos) { 80 for (PromotionIndexBO promotionIndexBO : tempPromotionIndexBos) {
77 //判断是否满足金额和件数条件 81 //判断是否满足金额和件数条件
78 String conditionParam = promotionIndexBOMap.get(promotionIndexBO.getId()).getConditionParam(); 82 String conditionParam = promotionIndexBOMap.get(promotionIndexBO.getId()).getConditionParam();
79 PromotionCond promotionCond = promotionCondService.buildPromotionCondWithJsonString(conditionParam); 83 PromotionCond promotionCond = promotionCondService.buildPromotionCondWithJsonString(conditionParam);
80 - boolean result = promotionMatchService.matchProductForPromotionPrice(promotionCond, productMap); 84 + boolean result = promotionMatchService.matchProductForPromotionPrice(promotionCond, product);
81 if (result) { 85 if (result) {
82 - SortedPromotionIndexBOs.add(promotionIndexBO); 86 + sortedPromotionIndexBOs.add(promotionIndexBO);
83 } 87 }
84 } 88 }
85 89
86 //生成按照权重排序后的促销标签列表 90 //生成按照权重排序后的促销标签列表
87 - List<Integer> promotionIdList = SortedPromotionIndexBOs.stream().map(PromotionIndexBO::getId).collect(Collectors.toList()); 91 + List<Integer> promotionIdList = sortedPromotionIndexBOs.stream().map(PromotionIndexBO::getId).collect(Collectors.toList());
88 List<Map<String, String>> promotionTagListSorted = new ArrayList<>(); 92 List<Map<String, String>> promotionTagListSorted = new ArrayList<>();
89 for (Integer id : promotionIdList) { 93 for (Integer id : promotionIdList) {
90 promotionTagListSorted.add(promotionTagMap.get(id)); 94 promotionTagListSorted.add(promotionTagMap.get(id));
@@ -92,13 +96,15 @@ public class PromotionPriceService { @@ -92,13 +96,15 @@ public class PromotionPriceService {
92 96
93 //记录因为互斥需要跳过的促销的位置列表 97 //记录因为互斥需要跳过的促销的位置列表
94 Set<Integer> markForDeletePositionSet = new HashSet<>(); 98 Set<Integer> markForDeletePositionSet = new HashSet<>();
95 - this.buildMarkForDeletePositionSet(markForDeletePositionSet, SortedPromotionIndexBOs, promotionIdList); 99 + this.buildMarkForDeletePositionSet(markForDeletePositionSet, sortedPromotionIndexBOs, promotionIdList);
96 100
97 //把需要跳过的促销id从列表中删除 101 //把需要跳过的促销id从列表中删除
98 for (Integer position : markForDeletePositionSet) { 102 for (Integer position : markForDeletePositionSet) {
99 - promotionIdList.remove(SortedPromotionIndexBOs.get(position).getId()); 103 + promotionIdList.remove(sortedPromotionIndexBOs.get(position).getId());
100 } 104 }
101 105
  106 + boolean isPromotionPriceActive = false;
  107 +
102 //遍历每个促销,计算促销价 108 //遍历每个促销,计算促销价
103 Double currentSalesPrice = salesPrice; 109 Double currentSalesPrice = salesPrice;
104 for (Map<String, String> promotionTag : promotionTagListSorted) { 110 for (Map<String, String> promotionTag : promotionTagListSorted) {
@@ -152,17 +158,13 @@ public class PromotionPriceService { @@ -152,17 +158,13 @@ public class PromotionPriceService {
152 } 158 }
153 } 159 }
154 if (isPromotionPriceActive) { 160 if (isPromotionPriceActive) {
155 - Integer finalPrice = getIntCeilPrice(currentSalesPrice);  
156 - productMap.put("promotion_price", finalPrice.toString());  
157 - } 161 + return getIntCeilPrice(currentSalesPrice);
  162 + } else {
  163 + return null;
158 } 164 }
159 - //插入字段  
160 - results.add(productMap);  
161 - }  
162 - return results;  
163 } catch (Exception e) { 165 } catch (Exception e) {
164 logger.error(e.getMessage(), e); 166 logger.error(e.getMessage(), e);
165 - return new ArrayList<>(); 167 + return null;
166 } 168 }
167 } 169 }
168 170
@@ -190,11 +192,14 @@ public class PromotionPriceService { @@ -190,11 +192,14 @@ public class PromotionPriceService {
190 } 192 }
191 193
192 private Double getProductPricePlanSalesPrice(Map<String, Object> productMap) { 194 private Double getProductPricePlanSalesPrice(Map<String, Object> productMap) {
193 - Double salesPrice = Double.valueOf(productMap.get("sales_price").toString()); 195 + //1、取当前销售价
  196 + Double salesPrice = MapUtils.getDoubleValue(productMap, "sales_price", 0);
194 List<Map<String, String>> pricePlanList = (List<Map<String, String>>) productMap.get("product_price_plan_list"); 197 List<Map<String, String>> pricePlanList = (List<Map<String, String>>) productMap.get("product_price_plan_list");
195 - List<Map<String, String>> pricePlanListSorted;  
196 - pricePlanListSorted = pricePlanList.stream().sorted(Comparator.comparingInt(map -> MapUtils.getInteger((Map) map, "effect_time")).reversed()).collect(Collectors.toList());  
197 - if (CollectionUtils.isNotEmpty(pricePlanListSorted)) { 198 + if (pricePlanList == null || pricePlanList.isEmpty()) {
  199 + return salesPrice;
  200 + }
  201 + //2、截取当前销售价格-取满足时间的第一个变价计算,获取current_saleprice
  202 + List<Map<String, String>> pricePlanListSorted = pricePlanList.stream().sorted(Comparator.comparingInt(map -> MapUtils.getInteger((Map) map, "effect_time")).reversed()).collect(Collectors.toList());
198 long currentTime = DateUtil.getCurrentTimeSecond(); 203 long currentTime = DateUtil.getCurrentTimeSecond();
199 for (Map<String, String> pricePlanMap : pricePlanListSorted) { 204 for (Map<String, String> pricePlanMap : pricePlanListSorted) {
200 Integer effectTime = MapUtils.getInteger(pricePlanMap, "effect_time"); 205 Integer effectTime = MapUtils.getInteger(pricePlanMap, "effect_time");
@@ -207,11 +212,9 @@ public class PromotionPriceService { @@ -207,11 +212,9 @@ public class PromotionPriceService {
207 return salesPrice; 212 return salesPrice;
208 } 213 }
209 } 214 }
210 - }  
211 return salesPrice; 215 return salesPrice;
212 } 216 }
213 217
214 -  
215 private static Integer getIntCeilPrice(Double price) { 218 private static Integer getIntCeilPrice(Double price) {
216 return Double.valueOf(Math.ceil(price)).intValue(); 219 return Double.valueOf(Math.ceil(price)).intValue();
217 } 220 }