...
|
...
|
@@ -30,139 +30,141 @@ public class PromotionPriceService { |
|
|
private PromotionMatchService promotionMatchService;
|
|
|
|
|
|
public List<Map<String, Object>> fillPromotionPrice(List<Map<String, Object>> productList) {
|
|
|
//1、参数判断
|
|
|
if (productList == null || productList.isEmpty()) {
|
|
|
return productList;
|
|
|
}
|
|
|
//2、获取所有促销信息,制作一个以promotionId为key的Map
|
|
|
List<PromotionIndexBO> promotionIndexBOList = promotionCondService.getPromotionList();
|
|
|
if (CollectionUtils.isEmpty(promotionIndexBOList)) {
|
|
|
return productList;
|
|
|
}
|
|
|
Map<Integer, PromotionIndexBO> promotionIndexBOMap = promotionIndexBOList.stream().collect(Collectors.toMap(PromotionIndexBO::getId, p -> p));
|
|
|
//3、循环匹配promotionPrice
|
|
|
for (Map<String, Object> product : productList) {
|
|
|
Integer promotionPrice = this.getPromotionPrice(product, promotionIndexBOMap);
|
|
|
product.put("promotion_price", promotionPrice);
|
|
|
}
|
|
|
//4、返回结果
|
|
|
return productList;
|
|
|
}
|
|
|
|
|
|
private Integer getPromotionPrice(Map<String, Object> product, Map<Integer, PromotionIndexBO> promotionIndexBOMap) {
|
|
|
try {
|
|
|
List<Map<String, Object>> results = new ArrayList<>();
|
|
|
if (CollectionUtils.isEmpty(productList)) {
|
|
|
return new ArrayList<>();
|
|
|
//全球购skn,promotion_price为空
|
|
|
if ("Y".equals(MapUtils.getString(product, "is_global"))) {
|
|
|
return null;
|
|
|
}
|
|
|
//获取所有促销信息,制作一个以promotionId为key的Map
|
|
|
List<PromotionIndexBO> promotionIndexBOList = promotionCondService.getPromotionList();
|
|
|
if (CollectionUtils.isEmpty(promotionIndexBOList)) {
|
|
|
return productList;
|
|
|
//获取当前变价计划里的salesPrice,默认salesPrice为skn的sales_price
|
|
|
Double salesPrice = this.getProductPricePlanSalesPrice(product);
|
|
|
if (salesPrice == null) {
|
|
|
return null;
|
|
|
}
|
|
|
//获取单个skn的促销列表
|
|
|
List<Map<String, String>> promotionTagList = (List<Map<String, String>>) product.get("promotion_tag");
|
|
|
if (promotionTagList == null || promotionTagList.isEmpty()) {
|
|
|
return null;
|
|
|
}
|
|
|
List<PromotionIndexBO> promotionIndexBOS = new ArrayList<>();
|
|
|
Map<Integer, Map<String, String>> promotionTagMap = new HashMap<>();
|
|
|
for (Map<String, String> promotionTag : promotionTagList) {
|
|
|
Integer promotionId = MapUtils.getInteger(promotionTag, "id");
|
|
|
promotionTagMap.put(promotionId, promotionTag);
|
|
|
PromotionIndexBO promotionIndexBO = promotionIndexBOMap.get(promotionId);
|
|
|
promotionIndexBOS.add(promotionIndexBO);
|
|
|
}
|
|
|
//生成安装权重排序后的促销列表
|
|
|
List<PromotionIndexBO> tempPromotionIndexBos = promotionIndexBOS.stream().sorted(Comparator.comparing(PromotionIndexBO::getPriority).reversed()).collect(Collectors.toList());
|
|
|
List<PromotionIndexBO> sortedPromotionIndexBOs = new ArrayList<>();
|
|
|
//去掉不满足的促销
|
|
|
for (PromotionIndexBO promotionIndexBO : tempPromotionIndexBos) {
|
|
|
//判断是否满足金额和件数条件
|
|
|
String conditionParam = promotionIndexBOMap.get(promotionIndexBO.getId()).getConditionParam();
|
|
|
PromotionCond promotionCond = promotionCondService.buildPromotionCondWithJsonString(conditionParam);
|
|
|
boolean result = promotionMatchService.matchProductForPromotionPrice(promotionCond, product);
|
|
|
if (result) {
|
|
|
sortedPromotionIndexBOs.add(promotionIndexBO);
|
|
|
}
|
|
|
}
|
|
|
Map<Integer, PromotionIndexBO> promotionIndexBOMap = promotionIndexBOList.stream().collect(Collectors.toMap(PromotionIndexBO::getId, p -> p));
|
|
|
|
|
|
//遍历每个skn,计算promotion_price字段
|
|
|
for (Map<String, Object> productMap : productList) {
|
|
|
//生成按照权重排序后的促销标签列表
|
|
|
List<Integer> promotionIdList = sortedPromotionIndexBOs.stream().map(PromotionIndexBO::getId).collect(Collectors.toList());
|
|
|
List<Map<String, String>> promotionTagListSorted = new ArrayList<>();
|
|
|
for (Integer id : promotionIdList) {
|
|
|
promotionTagListSorted.add(promotionTagMap.get(id));
|
|
|
}
|
|
|
|
|
|
//记录因为互斥需要跳过的促销的位置列表
|
|
|
Set<Integer> markForDeletePositionSet = new HashSet<>();
|
|
|
this.buildMarkForDeletePositionSet(markForDeletePositionSet, sortedPromotionIndexBOs, promotionIdList);
|
|
|
|
|
|
//把需要跳过的促销id从列表中删除
|
|
|
for (Integer position : markForDeletePositionSet) {
|
|
|
promotionIdList.remove(sortedPromotionIndexBOs.get(position).getId());
|
|
|
}
|
|
|
|
|
|
//全球购skn,promotion_price为空
|
|
|
if ("Y".equals(MapUtils.getString(productMap, "is_global"))) {
|
|
|
productMap.put("promotion_price", null);
|
|
|
results.add(productMap);
|
|
|
boolean isPromotionPriceActive = false;
|
|
|
|
|
|
//遍历每个促销,计算促销价
|
|
|
Double currentSalesPrice = salesPrice;
|
|
|
for (Map<String, String> promotionTag : promotionTagListSorted) {
|
|
|
|
|
|
//被标记删除的促销跳过
|
|
|
Integer promotionId = MapUtils.getInteger(promotionTag, "id");
|
|
|
if (!promotionIdList.contains(promotionId)) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//获取当前变价计划里的salesPrice,默认salesPrice为skn的sales_price
|
|
|
Double salesPrice = this.getProductPricePlanSalesPrice(productMap);
|
|
|
|
|
|
//获取单个skn的促销列表
|
|
|
productMap.put("promotion_price", null);
|
|
|
List<Map<String, String>> promotionTagList = (List<Map<String, String>>) productMap.get("promotion_tag");
|
|
|
|
|
|
if (CollectionUtils.isNotEmpty(promotionTagList)) {
|
|
|
boolean isPromotionPriceActive = false;
|
|
|
List<PromotionIndexBO> promotionIndexBOS = new ArrayList<>();
|
|
|
Map<Integer, Map<String, String>> promotionTagMap = new HashMap<>();
|
|
|
for (Map<String, String> promotionTag : promotionTagList) {
|
|
|
Integer promotionId = MapUtils.getInteger(promotionTag, "id");
|
|
|
promotionTagMap.put(promotionId, promotionTag);
|
|
|
PromotionIndexBO promotionIndexBO = promotionIndexBOMap.get(promotionId);
|
|
|
promotionIndexBOS.add(promotionIndexBO);
|
|
|
//满足条件才可以继续算价格
|
|
|
String actionParam = promotionIndexBOMap.get(promotionId).getActionParam();
|
|
|
JSONObject actionParamJson = JSONObject.parseObject(actionParam);
|
|
|
if (actionParamJson == null) {
|
|
|
continue;
|
|
|
}
|
|
|
if ("Discount".equals(promotionTag.get("type"))) {
|
|
|
Double discount = Double.valueOf(actionParamJson.get("discount").toString());
|
|
|
if (discount == null || currentSalesPrice == null) {
|
|
|
continue;
|
|
|
}
|
|
|
//生成安装权重排序后的促销列表
|
|
|
List<PromotionIndexBO> tempPromotionIndexBos = promotionIndexBOS.stream().sorted(Comparator.comparing(PromotionIndexBO::getPriority).reversed()).collect(Collectors.toList());
|
|
|
List<PromotionIndexBO> SortedPromotionIndexBOs = new ArrayList<>();
|
|
|
//去掉不满足的促销
|
|
|
for (PromotionIndexBO promotionIndexBO : tempPromotionIndexBos) {
|
|
|
//判断是否满足金额和件数条件
|
|
|
String conditionParam = promotionIndexBOMap.get(promotionIndexBO.getId()).getConditionParam();
|
|
|
PromotionCond promotionCond = promotionCondService.buildPromotionCondWithJsonString(conditionParam);
|
|
|
boolean result = promotionMatchService.matchProductForPromotionPrice(promotionCond, productMap);
|
|
|
if (result) {
|
|
|
SortedPromotionIndexBOs.add(promotionIndexBO);
|
|
|
}
|
|
|
currentSalesPrice = currentSalesPrice * discount;
|
|
|
isPromotionPriceActive = true;
|
|
|
}
|
|
|
if ("Cashreduce".equals(promotionTag.get("type"))) {
|
|
|
Double reduce = Double.valueOf(actionParamJson.get("reduce").toString());
|
|
|
if (reduce == null || currentSalesPrice == null) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//生成按照权重排序后的促销标签列表
|
|
|
List<Integer> promotionIdList = SortedPromotionIndexBOs.stream().map(PromotionIndexBO::getId).collect(Collectors.toList());
|
|
|
List<Map<String, String>> promotionTagListSorted = new ArrayList<>();
|
|
|
for (Integer id : promotionIdList) {
|
|
|
promotionTagListSorted.add(promotionTagMap.get(id));
|
|
|
currentSalesPrice = currentSalesPrice - reduce;
|
|
|
isPromotionPriceActive = true;
|
|
|
}
|
|
|
if ("Degressdiscount".equals(promotionTag.get("type"))) {
|
|
|
if (actionParamJson.get("degress_discount_list") == null || currentSalesPrice == null) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//记录因为互斥需要跳过的促销的位置列表
|
|
|
Set<Integer> markForDeletePositionSet = new HashSet<>();
|
|
|
this.buildMarkForDeletePositionSet(markForDeletePositionSet, SortedPromotionIndexBOs, promotionIdList);
|
|
|
|
|
|
//把需要跳过的促销id从列表中删除
|
|
|
for (Integer position : markForDeletePositionSet) {
|
|
|
promotionIdList.remove(SortedPromotionIndexBOs.get(position).getId());
|
|
|
String degressDiscountList = actionParamJson.get("degress_discount_list").toString();
|
|
|
String[] degress = degressDiscountList.split(";");
|
|
|
if (degress[0] == null) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//遍历每个促销,计算促销价
|
|
|
Double currentSalesPrice = salesPrice;
|
|
|
for (Map<String, String> promotionTag : promotionTagListSorted) {
|
|
|
|
|
|
//被标记删除的促销跳过
|
|
|
Integer promotionId = MapUtils.getInteger(promotionTag, "id");
|
|
|
if (!promotionIdList.contains(promotionId)) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//满足条件才可以继续算价格
|
|
|
String actionParam = promotionIndexBOMap.get(promotionId).getActionParam();
|
|
|
JSONObject actionParamJson = JSONObject.parseObject(actionParam);
|
|
|
if (actionParamJson == null) {
|
|
|
continue;
|
|
|
}
|
|
|
if ("Discount".equals(promotionTag.get("type"))) {
|
|
|
Double discount = Double.valueOf(actionParamJson.get("discount").toString());
|
|
|
if (discount == null || currentSalesPrice == null) {
|
|
|
continue;
|
|
|
}
|
|
|
currentSalesPrice = currentSalesPrice * discount;
|
|
|
isPromotionPriceActive = true;
|
|
|
}
|
|
|
if ("Cashreduce".equals(promotionTag.get("type"))) {
|
|
|
Double reduce = Double.valueOf(actionParamJson.get("reduce").toString());
|
|
|
if (reduce == null || currentSalesPrice == null) {
|
|
|
continue;
|
|
|
}
|
|
|
currentSalesPrice = currentSalesPrice - reduce;
|
|
|
isPromotionPriceActive = true;
|
|
|
}
|
|
|
if ("Degressdiscount".equals(promotionTag.get("type"))) {
|
|
|
if (actionParamJson.get("degress_discount_list") == null || currentSalesPrice == null) {
|
|
|
continue;
|
|
|
}
|
|
|
String degressDiscountList = actionParamJson.get("degress_discount_list").toString();
|
|
|
String[] degress = degressDiscountList.split(";");
|
|
|
if (degress[0] == null) {
|
|
|
continue;
|
|
|
}
|
|
|
String[] one = degress[0].split(":");
|
|
|
if (one[1] == null || Double.valueOf(one[1]) == 1) {
|
|
|
continue;
|
|
|
}
|
|
|
Double discount = Double.valueOf(one[1]);
|
|
|
currentSalesPrice = currentSalesPrice * discount;
|
|
|
if (currentSalesPrice != null) {
|
|
|
isPromotionPriceActive = true;
|
|
|
}
|
|
|
}
|
|
|
String[] one = degress[0].split(":");
|
|
|
if (one[1] == null || Double.valueOf(one[1]) == 1) {
|
|
|
continue;
|
|
|
}
|
|
|
if (isPromotionPriceActive) {
|
|
|
Integer finalPrice = getIntCeilPrice(currentSalesPrice);
|
|
|
productMap.put("promotion_price", finalPrice.toString());
|
|
|
Double discount = Double.valueOf(one[1]);
|
|
|
currentSalesPrice = currentSalesPrice * discount;
|
|
|
if (currentSalesPrice != null) {
|
|
|
isPromotionPriceActive = true;
|
|
|
}
|
|
|
}
|
|
|
//插入字段
|
|
|
results.add(productMap);
|
|
|
}
|
|
|
return results;
|
|
|
if (isPromotionPriceActive) {
|
|
|
return getIntCeilPrice(currentSalesPrice);
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return new ArrayList<>();
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -190,28 +192,29 @@ public class PromotionPriceService { |
|
|
}
|
|
|
|
|
|
private Double getProductPricePlanSalesPrice(Map<String, Object> productMap) {
|
|
|
Double salesPrice = Double.valueOf(productMap.get("sales_price").toString());
|
|
|
//1、取当前销售价
|
|
|
Double salesPrice = MapUtils.getDoubleValue(productMap, "sales_price", 0);
|
|
|
List<Map<String, String>> pricePlanList = (List<Map<String, String>>) productMap.get("product_price_plan_list");
|
|
|
List<Map<String, String>> pricePlanListSorted;
|
|
|
pricePlanListSorted = pricePlanList.stream().sorted(Comparator.comparingInt(map -> MapUtils.getInteger((Map) map, "effect_time")).reversed()).collect(Collectors.toList());
|
|
|
if (CollectionUtils.isNotEmpty(pricePlanListSorted)) {
|
|
|
long currentTime = DateUtil.getCurrentTimeSecond();
|
|
|
for (Map<String, String> pricePlanMap : pricePlanListSorted) {
|
|
|
Integer effectTime = MapUtils.getInteger(pricePlanMap, "effect_time");
|
|
|
Integer endTime = MapUtils.getInteger(pricePlanMap, "end_time");
|
|
|
if (effectTime <= currentTime && (endTime >= currentTime || endTime == 0)) {
|
|
|
if (MapUtils.getDouble(pricePlanMap, "current_saleprice") == null) {
|
|
|
continue;
|
|
|
}
|
|
|
salesPrice = MapUtils.getDouble(pricePlanMap, "current_saleprice");
|
|
|
return salesPrice;
|
|
|
if (pricePlanList == null || pricePlanList.isEmpty()) {
|
|
|
return salesPrice;
|
|
|
}
|
|
|
//2、截取当前销售价格-取满足时间的第一个变价计算,获取current_saleprice
|
|
|
List<Map<String, String>> pricePlanListSorted = pricePlanList.stream().sorted(Comparator.comparingInt(map -> MapUtils.getInteger((Map) map, "effect_time")).reversed()).collect(Collectors.toList());
|
|
|
long currentTime = DateUtil.getCurrentTimeSecond();
|
|
|
for (Map<String, String> pricePlanMap : pricePlanListSorted) {
|
|
|
Integer effectTime = MapUtils.getInteger(pricePlanMap, "effect_time");
|
|
|
Integer endTime = MapUtils.getInteger(pricePlanMap, "end_time");
|
|
|
if (effectTime <= currentTime && (endTime >= currentTime || endTime == 0)) {
|
|
|
if (MapUtils.getDouble(pricePlanMap, "current_saleprice") == null) {
|
|
|
continue;
|
|
|
}
|
|
|
salesPrice = MapUtils.getDouble(pricePlanMap, "current_saleprice");
|
|
|
return salesPrice;
|
|
|
}
|
|
|
}
|
|
|
return salesPrice;
|
|
|
}
|
|
|
|
|
|
|
|
|
private static Integer getIntCeilPrice(Double price) {
|
|
|
return Double.valueOf(Math.ceil(price)).intValue();
|
|
|
}
|
...
|
...
|
|