Authored by wangnan

Merge branch 'wn_promotion' into gray

... ... @@ -392,6 +392,7 @@ public class ProductIndexBaseService {
/**
* 获取skn促销标签-用于在待返回的列表返回之前加上促销标签这个节点
* add by wangnan in 2018/1/29
*/
public List<Map<String, Object>> getProductListWithPromotion(List<Map<String, Object>> productList) {
try {
... ... @@ -435,9 +436,6 @@ public class ProductIndexBaseService {
}
}
/**
* 给列表添加到手价字段
*/
public List<Map<String, Object>> getProductListWithPriceInHand(List<Map<String, Object>> productList) {
try {
List<Map<String, Object>> results = new ArrayList<>();
... ... @@ -449,17 +447,20 @@ public class ProductIndexBaseService {
if (CollectionUtils.isEmpty(promotionIndexBOList)) {
return productList;
}
Map<Integer, String> promotionActionParamMap = promotionIndexBOList.stream().collect(Collectors.toMap(PromotionIndexBO::getId, PromotionIndexBO::getActionParam));
Map<Integer, PromotionIndexBO> promotionActionParamMap = promotionIndexBOList.stream().collect(Collectors.toMap(PromotionIndexBO::getId,p->p));
//遍历每个skn,计算promotion_price字段
for (Map<String, Object> productMap : productList) {
//全球购跳过
if ("Y".equals(MapUtils.getString(productMap, "is_global"))) {
productMap.put("promotion_price", null);
results.add(productMap);
continue;
}
//默认salesPrice为skn的sales_price
Double salesPrice = Double.valueOf(productMap.get("sales_price").toString());
//获取当前变价计划里的current_saleprice为salesPrice
List<Map<String, String>> pricePlanList = (List<Map<String, String>>) productMap.get("product_price_plan_list");
if (CollectionUtils.isNotEmpty(pricePlanList)) {
... ... @@ -472,59 +473,56 @@ public class ProductIndexBaseService {
}
}
}
//获取促销列表,得到每个促销的actionParam
Double minSalesPrice = salesPrice;
List<Map<String, String>> promotionTagList = (List<Map<String, String>>) productMap.get("promotion_tag");
productMap.put("promotion_price", null);
//计算到手价
this.countPromotionPrice(promotionTagList, salesPrice, promotionActionParamMap, productMap);
//插入字段
results.add(productMap);
}
return results;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new ArrayList<>();
}
}
/**
* 计算到手价
*/
private void countPromotionPrice(List<Map<String, String>> promotionTagList, Double salesPrice, Map<Integer, String> promotionActionParamMap, Map<String, Object> productMap) {
try {
if (CollectionUtils.isNotEmpty(promotionTagList)) {
Double minSalesPrice = salesPrice;
boolean isPromotionPriceActive = false;
for (Map<String, String> promotionTag : promotionTagList) {
Double currentSalesPrice = salesPrice;
Integer promotionId = MapUtils.getInteger(promotionTag, "id");
String actionParam = promotionActionParamMap.get(promotionId);
//判断是否满足金额和件数条件
String conditionParam = promotionActionParamMap.get(promotionId).getConditionParam();
PromotionCond promotionCond = promotionCondService.buildPromotionCondWithJsonString(conditionParam);
boolean result = promotionMatchService.matchProductForPromotionPrice(promotionCond,productMap);
if(!result){
continue;
}
String actionParam = promotionActionParamMap.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){
continue;
}
currentSalesPrice = salesPrice * discount;
isPromotionPriceActive = true;
}
if ("Cashreduce".equals(promotionTag.get("type"))) {
Double reduce = Double.valueOf(actionParamJson.get("reduce").toString());
if (reduce == null) {
if(reduce==null){
continue;
}
currentSalesPrice = salesPrice - reduce;
isPromotionPriceActive = true;
}
if ("Degressdiscount".equals(promotionTag.get("type"))) {
if (actionParamJson.get("degress_discount_list") == null) {
if(actionParamJson.get("degress_discount_list")==null){
continue;
}
String degressDiscountList = actionParamJson.get("degress_discount_list").toString();
String[] degress = degressDiscountList.split(";");
if (degress[0] == null) {
if(degress[0]==null){
continue;
}
String[] one = degress[0].split(":");
if (one[1] == null) {
if(one[1]==null){
continue;
}
Double discount = Double.valueOf(one[1]);
... ... @@ -540,12 +538,16 @@ public class ProductIndexBaseService {
productMap.put("promotion_price", finalPrice.toString());
}
}
//插入字段
results.add(productMap);
}
return results;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new ArrayList<>();
}
}
private static Integer getIntCeilPrice(Double price) {
return Double.valueOf(Math.ceil(price)).intValue();
}
... ...
... ... @@ -14,8 +14,6 @@ import java.util.Map;
/**
* 判断促销和skn是否匹配逻辑
* @Author: wangnan
* @Date: Created in 2018/1/29
*/
@Service
public class PromotionMatchService {
... ... @@ -69,15 +67,83 @@ public class PromotionMatchService {
*/
private boolean matchProduct(PromotionCond.CondItem condItem, Map<String, Object> productMap) {
PromotionMatchFactor promotionMatchFactor = new PromotionMatchFactor();
promotionMatchFactor.setBrandId(MapUtils.getIntValue(productMap, "brand_id")==0?MapUtils.getIntValue(productMap, "brandId"):MapUtils.getIntValue(productMap, "brand_id"));
promotionMatchFactor.setSmallSortId(MapUtils.getIntValue(productMap, "small_sort_id")==0?MapUtils.getIntValue(productMap, "smallSortId"):MapUtils.getIntValue(productMap, "small_sort_id"));
promotionMatchFactor.setMiddleSortId(MapUtils.getIntValue(productMap, "middle_sort_id")==0?MapUtils.getIntValue(productMap, "middleSortId"):MapUtils.getIntValue(productMap, "middle_sort_id"));
promotionMatchFactor.setMaxSortId(MapUtils.getIntValue(productMap, "max_sort_id")==0?MapUtils.getIntValue(productMap, "maxSortId"):MapUtils.getIntValue(productMap, "max_sort_id"));
promotionMatchFactor.setIspromotion(MapUtils.getIntValue(productMap, "is_promotion")==0?MapUtils.getIntValue(productMap, "isPromotion"):MapUtils.getIntValue(productMap, "is_promotion"));
promotionMatchFactor.setProductskn(MapUtils.getIntValue(productMap, "product_skn")==0?MapUtils.getIntValue(productMap, "productSkn"):MapUtils.getIntValue(productMap, "product_skn"));
promotionMatchFactor.setBrandId(MapUtils.getIntValue(productMap, "brand_id") == 0 ? MapUtils.getIntValue(productMap, "brandId") : MapUtils.getIntValue(productMap, "brand_id"));
promotionMatchFactor.setSmallSortId(MapUtils.getIntValue(productMap, "small_sort_id") == 0 ? MapUtils.getIntValue(productMap, "smallSortId") : MapUtils.getIntValue(productMap, "small_sort_id"));
promotionMatchFactor.setMiddleSortId(MapUtils.getIntValue(productMap, "middle_sort_id") == 0 ? MapUtils.getIntValue(productMap, "middleSortId") : MapUtils.getIntValue(productMap, "middle_sort_id"));
promotionMatchFactor.setMaxSortId(MapUtils.getIntValue(productMap, "max_sort_id") == 0 ? MapUtils.getIntValue(productMap, "maxSortId") : MapUtils.getIntValue(productMap, "max_sort_id"));
promotionMatchFactor.setIspromotion(MapUtils.getIntValue(productMap, "is_promotion") == 0 ? MapUtils.getIntValue(productMap, "isPromotion") : MapUtils.getIntValue(productMap, "is_promotion"));
promotionMatchFactor.setProductskn(MapUtils.getIntValue(productMap, "product_skn") == 0 ? MapUtils.getIntValue(productMap, "productSkn") : MapUtils.getIntValue(productMap, "product_skn"));
return promotionHelper.matchProduct(condItem, promotionMatchFactor);
}
public boolean matchProductForPromotionPrice(PromotionCond promotionCond, Map<String, Object> productMap) {
try {
boolean matchResult;
if (promotionCond == null) {
return false;
}
if (promotionCond.getAggregator() == PromotionCond.Aggregator.ALL) {
matchResult = promotionCond.getCondDetailList().stream().allMatch(condDetail -> matchCondDetailForPromotionPrice(condDetail, productMap));
} else {
matchResult = promotionCond.getCondDetailList().stream().anyMatch(condDetail -> matchCondDetailForPromotionPrice(condDetail, productMap));
}
if (matchResult) {
logger.debug("[func=PromotionCond.matchResult][skn={}][promotionId={}][promotionTitle={}]",
MapUtils.getIntValue(productMap, "id"), promotionCond.getPromotionId(), promotionCond.getPromotionTitle());
}
return matchResult;
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return false;
}
private boolean matchCondDetailForPromotionPrice(PromotionCond.CondDetail condDetail, Map<String, Object> productMap) {
Double salesPrice = Double.valueOf(productMap.get("sales_price").toString());
if ("amount".equals(condDetail.getOperatobj())) {
if (">".equals(condDetail.getOperator())) {
if (salesPrice > Double.valueOf(condDetail.getValue())) {
return true;
}
}
if (">=".equals(condDetail.getOperator())) {
if (salesPrice >= Double.valueOf(condDetail.getValue())) {
return true;
}
}
if ("<".equals(condDetail.getOperator())) {
if (salesPrice < Double.valueOf(condDetail.getValue())) {
return true;
}
}
if ("<=".equals(condDetail.getOperator())) {
if (salesPrice <= Double.valueOf(condDetail.getValue())) {
return true;
}
}
if ("=".equals(condDetail.getOperator())) {
if (salesPrice == Double.valueOf(condDetail.getValue())) {
return true;
}
}
}
if ("num".equals(condDetail.getOperatobj())) {
if (">".equals(condDetail.getOperator())) {
if (Double.valueOf(condDetail.getValue())>0) {
return false;
}
}
if (">=".equals(condDetail.getOperator())) {
if (Double.valueOf(condDetail.getValue())==1) {
return true;
}
}
}
return false;
}
}
... ...