Commit 1fa7810e by ping.wu

购物车限购数量优化

parent 1b93a61a
...@@ -35,10 +35,7 @@ import cn.freemud.interceptor.ServiceException; ...@@ -35,10 +35,7 @@ import cn.freemud.interceptor.ServiceException;
import cn.freemud.redis.RedisCache; import cn.freemud.redis.RedisCache;
import cn.freemud.service.impl.AssortmentSdkService; import cn.freemud.service.impl.AssortmentSdkService;
import cn.freemud.service.impl.calculate.CalculationCommonService; import cn.freemud.service.impl.calculate.CalculationCommonService;
import cn.freemud.utils.BeanUtil; import cn.freemud.utils.*;
import cn.freemud.utils.LogUtil;
import cn.freemud.utils.ResponseUtil;
import cn.freemud.utils.WebUtil;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.freemud.sdk.api.assortment.shoppingcart.constant.CommonsConstant; import com.freemud.sdk.api.assortment.shoppingcart.constant.CommonsConstant;
...@@ -52,6 +49,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -52,6 +49,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -686,44 +684,64 @@ public abstract class AbstractAddGoodsService implements AddGoodsService { ...@@ -686,44 +684,64 @@ public abstract class AbstractAddGoodsService implements AddGoodsService {
/** /**
* 查询生态配置限制加购开关与限制数量 * 查询生态配置限制加购开关与限制数量
*/ */
private Integer limitGoodsQty(List<CartGoods> oldCartGoodsList, CartGoods addGoods,String appid){ private Integer limitGoodsQty(List<CartGoods> oldCartGoodsList, CartGoods addGoods, String appid) {
Integer qty = 0; Integer qty = 0;
if(CollectionUtils.isNotEmpty(oldCartGoodsList)){ if (CollectionUtils.isNotEmpty(oldCartGoodsList)) {
for (CartGoods goods : oldCartGoodsList) { for (CartGoods goods : oldCartGoodsList) {
qty += goods.getQty(); qty += goods.getQty();
} }
} }
qty += addGoods.getQty(); qty += addGoods.getQty();
String redisKey = limitCartKey + appid;
Long expire = 5L;
//购物车内部缓存
Integer limitCount = redisCache.getValue(RedisUtil.limitCart + appid);
if (limitCount != null) {
if (qty > limitCount) {
throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD);
}
return qty;
}
String redisKey = limitCartKey + appid;
//生态缓存获取慢
Object value = redisCache.hashGet(redisKey, "2"); Object value = redisCache.hashGet(redisKey, "2");
if (value == null) { if (value == null) {
redisCache.save(RedisUtil.limitCart + appid, 0, expire, TimeUnit.MINUTES);
return qty; return qty;
} else { } else {
boolean bool = value.getClass().getName().equals("java.lang.String"); boolean bool = value.getClass().getName().equals("java.lang.String");
if(bool){ if (bool) {
redisCache.save(RedisUtil.limitCart + appid, 0, expire, TimeUnit.MINUTES);
return qty; return qty;
} }
JSONArray jsonArray = (JSONArray)value; JSONArray jsonArray = (JSONArray) value;
int cartLimitFlag = 0; int cartLimitFlag = 0;
int cartLimitCount = 0; int cartLimitCount = 0;
for (Object object : jsonArray){ for (Object object : jsonArray) {
JSONObject jsonObject = (JSONObject)object; JSONObject jsonObject = (JSONObject) object;
HashMap hashMap = JSONObject.parseObject(jsonObject.toJSONString(),HashMap.class); HashMap hashMap = JSONObject.parseObject(jsonObject.toJSONString(), HashMap.class);
if("cartLimitFlag".equals(hashMap.get("appKey"))){ if ("cartLimitFlag".equals(hashMap.get("appKey"))) {
if (StringUtils.isBlank((String)hashMap.get("appValue"))) { if (StringUtils.isBlank((String) hashMap.get("appValue"))) {
redisCache.save(RedisUtil.limitCart + appid, 0, expire, TimeUnit.MINUTES);
return qty;
}
cartLimitFlag = Integer.parseInt((String) hashMap.get("appValue"));
if (cartLimitFlag != 1) {
redisCache.save(RedisUtil.limitCart + appid, 0, expire, TimeUnit.MINUTES);
return qty; return qty;
} }
cartLimitFlag = Integer.parseInt((String)hashMap.get("appValue"));
} }
if("cartLimitCount".equals(hashMap.get("appKey"))){ if ("cartLimitCount".equals(hashMap.get("appKey"))) {
if (StringUtils.isBlank((String)hashMap.get("appValue"))) { if (StringUtils.isBlank((String) hashMap.get("appValue"))) {
redisCache.save(RedisUtil.limitCart + appid, 0, expire, TimeUnit.MINUTES);
return qty; return qty;
} }
cartLimitCount = Integer.parseInt((String)hashMap.get("appValue")); cartLimitCount = Integer.parseInt((String) hashMap.get("appValue"));
redisCache.save(RedisUtil.limitCart + appid, cartLimitCount, expire, TimeUnit.MINUTES);
} }
} }
if(cartLimitFlag == 1 && cartLimitCount != 0 && qty > cartLimitCount){ if (cartLimitFlag == 1 && cartLimitCount != 0 && qty > cartLimitCount) {
throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD); throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD);
} }
} }
......
...@@ -1474,53 +1474,6 @@ public class AbstractApportionService implements GetShoppingCartGoodsApportionSe ...@@ -1474,53 +1474,6 @@ public class AbstractApportionService implements GetShoppingCartGoodsApportionSe
} }
/** /**
* 查询生态配置限制加购开关与限制数量
*/
private Integer limitGoodsQty(List<CartGoods> oldCartGoodsList, CartGoods addGoods,String appid){
Integer qty = 0;
if(CollectionUtils.isNotEmpty(oldCartGoodsList)){
for (CartGoods goods : oldCartGoodsList) {
qty += goods.getQty();
}
}
qty += addGoods.getQty();
String redisKey = limitCartKey + appid;
Object value = redisCache.hashGet(redisKey, "2");
if (value == null) {
return qty;
} else {
boolean bool = value.getClass().getName().equals("java.lang.String");
if(bool){
return qty;
}
JSONArray jsonArray = (JSONArray)value;
int cartLimitFlag = 0;
int cartLimitCount = 0;
for (Object object : jsonArray){
JSONObject jsonObject = (JSONObject)object;
HashMap hashMap = JSONObject.parseObject(jsonObject.toJSONString(),HashMap.class);
if("cartLimitFlag".equals(hashMap.get("appKey"))){
if (StringUtils.isBlank((String)hashMap.get("appValue"))) {
return qty;
}
cartLimitFlag = Integer.parseInt((String)hashMap.get("appValue"));
}
if("cartLimitCount".equals(hashMap.get("appKey"))){
if (StringUtils.isBlank((String)hashMap.get("appValue"))) {
return qty;
}
cartLimitCount = Integer.parseInt((String)hashMap.get("appValue"));
}
}
if(cartLimitFlag == 1 && cartLimitCount != 0 && qty > cartLimitCount){
throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD);
}
}
return qty;
}
/**
* 初始化价格 * 初始化价格
*/ */
public ShoppingGoodsAmountBO initShoppingCart(CalculationDiscountBO calculationDiscountBO public ShoppingGoodsAmountBO initShoppingCart(CalculationDiscountBO calculationDiscountBO
......
...@@ -1326,53 +1326,6 @@ public class AbstractListCartGoodsService implements ListCartGoodsService { ...@@ -1326,53 +1326,6 @@ public class AbstractListCartGoodsService implements ListCartGoodsService {
return cartContext; return cartContext;
} }
/**
* 查询生态配置限制加购开关与限制数量
*/
private Integer limitGoodsQty(List<CartGoods> oldCartGoodsList, CartGoods addGoods,String appid){
Integer qty = 0;
if(CollectionUtils.isNotEmpty(oldCartGoodsList)){
for (CartGoods goods : oldCartGoodsList) {
qty += goods.getQty();
}
}
qty += addGoods.getQty();
String redisKey = limitCartKey + appid;
Object value = redisCache.hashGet(redisKey, "2");
if (value == null) {
return qty;
} else {
boolean bool = value.getClass().getName().equals("java.lang.String");
if(bool){
return qty;
}
JSONArray jsonArray = (JSONArray)value;
int cartLimitFlag = 0;
int cartLimitCount = 0;
for (Object object : jsonArray){
JSONObject jsonObject = (JSONObject)object;
HashMap hashMap = JSONObject.parseObject(jsonObject.toJSONString(),HashMap.class);
if("cartLimitFlag".equals(hashMap.get("appKey"))){
if (StringUtils.isBlank((String)hashMap.get("appValue"))) {
return qty;
}
cartLimitFlag = Integer.parseInt((String)hashMap.get("appValue"));
}
if("cartLimitCount".equals(hashMap.get("appKey"))){
if (StringUtils.isBlank((String)hashMap.get("appValue"))) {
return qty;
}
cartLimitCount = Integer.parseInt((String)hashMap.get("appValue"));
}
}
if(cartLimitFlag == 1 && cartLimitCount != 0 && qty > cartLimitCount){
throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD);
}
}
return qty;
}
private void checkStocks(ListCartGoodsBO baseRequestDTO, CheckBussinessRulesBO checkBussinessRulesBO){ private void checkStocks(ListCartGoodsBO baseRequestDTO, CheckBussinessRulesBO checkBussinessRulesBO){
......
...@@ -95,6 +95,7 @@ import java.awt.geom.Point2D; ...@@ -95,6 +95,7 @@ import java.awt.geom.Point2D;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -2619,14 +2620,38 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService { ...@@ -2619,14 +2620,38 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService {
if (Objects.nonNull(cartGoods)) { if (Objects.nonNull(cartGoods)) {
qty += cartGoods.getQty(); qty += cartGoods.getQty();
} }
String redisKey = limitCartKey + appid; int limitCount = queryLimitCount(appid);
if (qty > limitCount) {
throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD);
}
return qty;
}
/**
* 查询购物车限购数量
*/
public int queryLimitCount(String appid) {
int qty = 0;
Long expire = 1L;
//购物车内部缓存
Integer limitCount = redisCache.getValue(RedisUtil.limitCart + appid);
if (limitCount != null) {
if (qty > limitCount) {
throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD);
}
return qty;
}
String redisKey = limitCartKey + appid;
//生态缓存获取慢
Object value = redisCache.hashGet(redisKey, "2"); Object value = redisCache.hashGet(redisKey, "2");
if (value == null) { if (value == null) {
redisCache.save(RedisUtil.limitCart + appid, 0, expire, TimeUnit.MINUTES);
return qty; return qty;
} else { } else {
boolean bool = value.getClass().getName().equals("java.lang.String"); boolean bool = value.getClass().getName().equals("java.lang.String");
if (bool) { if (bool) {
redisCache.save(RedisUtil.limitCart + appid, 0, expire, TimeUnit.MINUTES);
return qty; return qty;
} }
JSONArray jsonArray = (JSONArray) value; JSONArray jsonArray = (JSONArray) value;
...@@ -2637,20 +2662,25 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService { ...@@ -2637,20 +2662,25 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService {
HashMap hashMap = JSONObject.parseObject(jsonObject.toJSONString(), HashMap.class); HashMap hashMap = JSONObject.parseObject(jsonObject.toJSONString(), HashMap.class);
if ("cartLimitFlag".equals(hashMap.get("appKey"))) { if ("cartLimitFlag".equals(hashMap.get("appKey"))) {
if (StringUtils.isBlank((String) hashMap.get("appValue"))) { if (StringUtils.isBlank((String) hashMap.get("appValue"))) {
redisCache.save(RedisUtil.limitCart + appid, 0, expire, TimeUnit.MINUTES);
return qty; return qty;
} }
cartLimitFlag = Integer.parseInt((String) hashMap.get("appValue")); cartLimitFlag = Integer.parseInt((String) hashMap.get("appValue"));
if (cartLimitFlag != 1) {
redisCache.save(RedisUtil.limitCart + appid, 0, expire, TimeUnit.MINUTES);
return qty;
}
} }
if ("cartLimitCount".equals(hashMap.get("appKey"))) { if ("cartLimitCount".equals(hashMap.get("appKey"))) {
if (StringUtils.isBlank((String) hashMap.get("appValue"))) { if (StringUtils.isBlank((String) hashMap.get("appValue"))) {
redisCache.save(RedisUtil.limitCart + appid, 0, expire, TimeUnit.MINUTES);
return qty; return qty;
} }
cartLimitCount = Integer.parseInt((String) hashMap.get("appValue")); cartLimitCount = Integer.parseInt((String) hashMap.get("appValue"));
redisCache.save(RedisUtil.limitCart + appid, cartLimitCount, expire, TimeUnit.MINUTES);
return cartLimitCount;
} }
} }
if (cartLimitFlag == 1 && cartLimitCount != 0 && qty > cartLimitCount) {
throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD);
}
} }
return qty; return qty;
} }
...@@ -2669,43 +2699,13 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService { ...@@ -2669,43 +2699,13 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService {
sendQty += sendGoods.getSendNumber(); sendQty += sendGoods.getSendNumber();
} }
} }
String redisKey = limitCartKey + appid; int cartLimitCount = queryLimitCount(appid);
if (checkQty + sendQty > cartLimitCount) {
Object value = redisCache.hashGet(redisKey, "2");
if (value == null) {
return;
} else {
boolean bool = value.getClass().getName().equals("java.lang.String");
if (bool) {
return;
}
JSONArray jsonArray = (JSONArray) value;
int cartLimitFlag = 0;
int cartLimitCount = 0;
for (Object object : jsonArray) {
JSONObject jsonObject = (JSONObject) object;
HashMap hashMap = JSONObject.parseObject(jsonObject.toJSONString(), HashMap.class);
if ("cartLimitFlag".equals(hashMap.get("appKey"))) {
if (StringUtils.isBlank((String) hashMap.get("appValue"))) {
return;
}
cartLimitFlag = Integer.parseInt((String) hashMap.get("appValue"));
}
if ("cartLimitCount".equals(hashMap.get("appKey"))) {
if (StringUtils.isBlank((String) hashMap.get("appValue"))) {
return;
}
cartLimitCount = Integer.parseInt((String) hashMap.get("appValue"));
}
}
if (cartLimitFlag == 1 && cartLimitCount != 0 && checkQty + sendQty > cartLimitCount) {
assortmentSdkService.setShoppingCart(partnerId, storeId, userId, oldCartGoodsList, null, tableNumber, shoppingCartService); assortmentSdkService.setShoppingCart(partnerId, storeId, userId, oldCartGoodsList, null, tableNumber, shoppingCartService);
throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD); throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD);
} }
} }
}
/** /**
* 购物车添加完成后促销返回赠送商品总数量校验 * 购物车添加完成后促销返回赠送商品总数量校验
*/ */
...@@ -2717,36 +2717,7 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService { ...@@ -2717,36 +2717,7 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService {
} }
} }
String redisKey = limitCartKey + appid; int cartLimitCount = queryLimitCount(appid);
Object value = redisCache.hashGet(redisKey, "2");
if (value == null) {
return;
} else {
boolean bool = value.getClass().getName().equals("java.lang.String");
if (bool) {
return;
}
JSONArray jsonArray = (JSONArray) value;
int cartLimitFlag = 0;
int cartLimitCount = 0;
for (Object object : jsonArray) {
JSONObject jsonObject = (JSONObject) object;
HashMap hashMap = JSONObject.parseObject(jsonObject.toJSONString(), HashMap.class);
if ("cartLimitFlag".equals(hashMap.get("appKey"))) {
if (StringUtils.isBlank((String) hashMap.get("appValue"))) {
return;
}
cartLimitFlag = Integer.parseInt((String) hashMap.get("appValue"));
}
if ("cartLimitCount".equals(hashMap.get("appKey"))) {
if (StringUtils.isBlank((String) hashMap.get("appValue"))) {
return;
}
cartLimitCount = Integer.parseInt((String) hashMap.get("appValue"));
}
}
int sendQty = 0; int sendQty = 0;
if (CollectionUtils.isNotEmpty(sendActivitys)) { if (CollectionUtils.isNotEmpty(sendActivitys)) {
for (ActivityCalculationDiscountResponseDto.CalculationDiscountResult.SendActivity sendActivity : sendActivitys) { for (ActivityCalculationDiscountResponseDto.CalculationDiscountResult.SendActivity sendActivity : sendActivitys) {
...@@ -2755,14 +2726,12 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService { ...@@ -2755,14 +2726,12 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService {
} }
} }
} }
if (cartLimitFlag == 1 && cartLimitCount != 0 && updateQty + sendQty > cartLimitCount) { if (updateQty + sendQty > cartLimitCount) {
assortmentSdkService.updateGoodsQtyBySdk(partnerId, userId, storeId, cartGoodsUid, goodsQty, "", shoppingCartBaseService); assortmentSdkService.updateGoodsQtyBySdk(partnerId, userId, storeId, cartGoodsUid, goodsQty, "", shoppingCartBaseService);
throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD); throw new ServiceException(ResponseResult.SHOPPING_CART_LIMIT_ADD);
} }
} }
}
/** /**
* 获取混合支付是否开启 * 获取混合支付是否开启
* *
......
...@@ -5,6 +5,7 @@ public class RedisUtil { ...@@ -5,6 +5,7 @@ public class RedisUtil {
private final static String ENTER_SHOP_KEY = BASE_URL + "ENTER_SHOP_KEY_"; private final static String ENTER_SHOP_KEY = BASE_URL + "ENTER_SHOP_KEY_";
private final static String JOIN_FISSION_ACTIVITIES_KEY = BASE_URL + "JOIN_FISSION_ACTIVITIES_KEY_"; private final static String JOIN_FISSION_ACTIVITIES_KEY = BASE_URL + "JOIN_FISSION_ACTIVITIES_KEY_";
private final static String COUPON_APP_SECRET = "coupon:app_secret_"; private final static String COUPON_APP_SECRET = "coupon:app_secret_";
public static String limitCart = "kgd:shoppingcart:cartLimitCount:";
public static String getEnterShopKey(String userId) { public static String getEnterShopKey(String userId) {
return ENTER_SHOP_KEY + userId; return ENTER_SHOP_KEY + userId;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment