Authored by hugufei

fix bug

... ... @@ -75,92 +75,58 @@ public class PromotionIndexBaseService {
return recommendPromotionAggVO;
}
/**
* 传入一个productList,加入促销标签
*/
public List<Map<String, Object>> fillPromotionTag(List<Map<String, Object>> productList) {
try {
if (productList == null || productList.isEmpty()) {
return new ArrayList<>();
}
//1.全量查promotion索引内容,用于和每个skn匹配,1分钟缓存
List<PromotionCond> promotionCondList = promotionCondService.getPromotionCondList();
//2.构建每个skn匹配的促销
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (Map<String, Object> productMap : productList) {
//全球购跳过
if ("Y".equals(MapUtils.getString(productMap, "is_global"))) {
productMap.put("promotion_tag", new JSONArray());
results.add(productMap);
continue;
}
//2.1 获取该skn匹配上的促销
List<PromotionCond> matchedPromotionCondList = promotionCondList.stream()
.filter(promotionCond -> promotionMatchService.matchProduct(promotionCond, productMap))
.collect(Collectors.toList());
//2.2 查询促销类型->促销名映射的索引获取map,用于填充促销标签名称,1分钟缓存
Map<String, String> promotionTypeMap = promotionTypeService.getPromotionTypeMap();
//2.3 生成促销标签对象列表
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(matchedPromotionCondList)) {
List<JSONObject> matchedPromotions = matchedPromotionCondList.stream()
.map(promotionCond -> {
JSONObject matchedPromotion = new JSONObject();
matchedPromotion.put("id", promotionCond.getPromotionId());
matchedPromotion.put("type", promotionCond.getPromotionType());
matchedPromotion.put("startTime", promotionCond.getStartTime());
matchedPromotion.put("endTime", promotionCond.getEndTime());
if (!promotionTypeMap.isEmpty() && promotionCond.getShowStatus() == 1) {
matchedPromotion.put("name", promotionTypeMap.get(promotionCond.getPromotionType()));
}
return matchedPromotion;
}).collect(Collectors.toList());
//2.4 列表中插入促销标签信息
productMap.put("promotion_tag", matchedPromotions);
}
results.add(productMap);
}
return results;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new ArrayList<>();
//1、判断
if (productList == null || productList.isEmpty()) {
return productList;
}
//2、全量查promotion索引内容,用于和每个skn匹配,1分钟缓存
List<PromotionCond> promotionCondList = promotionCondService.getPromotionCondList();
if (promotionCondList == null || promotionCondList.isEmpty()) {
return productList;
}
//3、执行匹配
for (Map<String, Object> productMap : productList) {
List<JSONObject> promotion_tag = this.getProductPromotionTag(productMap, promotionCondList);
productMap.put("promotion_tag", promotion_tag);
}
//4、返回
return productList;
}
/**
* 获取单个product的促销标签-会弃用
*/
public List<JSONObject> getPromotionTag(Map<String, Object> map) {
private List<JSONObject> getProductPromotionTag(Map<String, Object> productMap, List<PromotionCond> promotionCondList) {
try {
//全量查promotion索引内容,用于和每个skn匹配,1分钟缓存
List<PromotionCond> promotionCondList = promotionCondService.getPromotionCondList();
//获取该skn匹配上的促销
List<PromotionCond> matchedPromotionCondList = promotionCondList.stream()
.filter(promotionCond -> promotionMatchService.matchProduct(promotionCond, map))
.collect(Collectors.toList());
//查询促销类型->促销名映射的索引获取map,用于填充促销标签名称,1分钟缓存
//1、promotionCondList判断
if (promotionCondList == null || promotionCondList.isEmpty()) {
return new ArrayList<>();
}
//2、全球购跳过
if ("Y".equals(MapUtils.getString(productMap, "is_global"))) {
return new ArrayList<>();
}
//3、获取该skn匹配上的促销
List<PromotionCond> matchedPromotionCondList = promotionCondList.stream().filter(promotionCond -> promotionMatchService.matchProduct(promotionCond, productMap)).collect(Collectors.toList());
if(matchedPromotionCondList==null || matchedPromotionCondList.isEmpty()){
return new ArrayList<>();
}
//4、查询促销类型->促销名映射的索引获取map,用于填充促销标签名称,1分钟缓存
Map<String, String> promotionTypeMap = promotionTypeService.getPromotionTypeMap();
//生成促销标签对象列表
List<JSONObject> matchedPromotions = new ArrayList<>();
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(matchedPromotionCondList)) {
matchedPromotions = matchedPromotionCondList.stream()
.map(promotionCond -> {
JSONObject matchedPromotion = new JSONObject();
matchedPromotion.put("id", promotionCond.getPromotionId());
matchedPromotion.put("type", promotionCond.getPromotionType());
matchedPromotion.put("startTime", promotionCond.getStartTime());
matchedPromotion.put("endTime", promotionCond.getEndTime());
if (!promotionTypeMap.isEmpty() && promotionCond.getShowStatus() == 1) {
matchedPromotion.put("name", promotionTypeMap.get(promotionCond.getPromotionType()));
}
return matchedPromotion;
}).collect(Collectors.toList());
//5、生成促销标签对象列表
List<JSONObject> results = new ArrayList<>();
for (PromotionCond promotionCond:matchedPromotionCondList){
JSONObject matchedPromotion = new JSONObject();
matchedPromotion.put("id", promotionCond.getPromotionId());
matchedPromotion.put("type", promotionCond.getPromotionType());
matchedPromotion.put("startTime", promotionCond.getStartTime());
matchedPromotion.put("endTime", promotionCond.getEndTime());
if (!promotionTypeMap.isEmpty() && promotionCond.getShowStatus() == 1) {
matchedPromotion.put("name", promotionTypeMap.get(promotionCond.getPromotionType()));
}
results.add(matchedPromotion);
}
return matchedPromotions;
return results;
} catch (Exception e) {
logger.error(e.getMessage(), e);
logger.error(e.getMessage(),e);
return new ArrayList<>();
}
}
... ...
... ... @@ -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();
}
... ...