Commit c482e803 by 查志伟

添加更新赠品属性接口

parent db88828d
...@@ -16,10 +16,7 @@ import org.springframework.stereotype.Component; ...@@ -16,10 +16,7 @@ import org.springframework.stereotype.Component;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.HashMap; import java.util.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -57,34 +54,33 @@ public class GiftCacheManager { ...@@ -57,34 +54,33 @@ public class GiftCacheManager {
String redisKey = MessageFormat.format(RedisKeyConstant.SAAS_SHOPPING_CART_GIFT_KEY, partnerId, storeCode, userId); String redisKey = MessageFormat.format(RedisKeyConstant.SAAS_SHOPPING_CART_GIFT_KEY, partnerId, storeCode, userId);
BoundHashOperations<String, String, String> operations = redisTemplate.boundHashOps(redisKey); BoundHashOperations<String, String, String> operations = redisTemplate.boundHashOps(redisKey);
List<String> values = operations.values(); List<String> values = operations.values();
// 将缓存转为对象
List<CartGoods> oldGiftList = CollectionUtils.isEmpty(values)
? new ArrayList<>()
: values.stream().map(v -> JSON.parseObject(v, CartGoods.class)).collect(Collectors.toList());
// 存放最新的赠品缓存信息
Map<String, String> newValues = new HashMap<>(); Map<String, String> newValues = new HashMap<>();
if (CollectionUtils.isNotEmpty(values)) { for (CartGoods gift : giftList) {
// 缓存里有,筛选出指定活动的赠品数据 // 匹配赠品的商品属性,将原有赠品的属性更新到新计算出的赠品中
List<CartGoods> oldGiftList = values.stream() Iterator<CartGoods> it = oldGiftList.iterator();
.map(v -> JSON.parseObject(v, CartGoods.class)) while (it.hasNext()) {
.collect(Collectors.toList()); CartGoods old = it.next();
for (CartGoods gift : giftList) { boolean isSameType = StringUtils.isBlank(activityCode) || activityCode.equals(old.getActivityCode());
// 匹配赠品的商品属性,将原有赠品的属性更新到新计算出的赠品中 if (! old.getGoodsId().equals(gift.getGoodsId()) || ! isSameType) continue;
Iterator<CartGoods> it = oldGiftList.iterator(); // 赠品匹配上了,copy赠品属性
while (it.hasNext()) { gift.setExtra(old.getExtra());
CartGoods old = it.next(); gift.setCartGoodsUid(old.getCartGoodsUid());
boolean isSameType = StringUtils.isBlank(activityCode) || activityCode.equals(old.getActivityCode()); // 如果赠品为多个同一商品,并且每个商品都有不同的属性,所以这里要移除掉已经匹配上的商品
if (! old.getGoodsId().equals(gift.getGoodsId()) || ! isSameType) continue; it.remove();
// 赠品匹配上了,copy赠品属性 break;
gift.setExtra(old.getExtra());
gift.setCartGoodsUid(old.getCartGoodsUid());
// 如果赠品为多个同一商品,并且每个商品都有不同的属性,所以这里要移除掉已经匹配上的商品
it.remove();
break;
}
newValues.put(gift.getCartGoodsUid(), JSON.toJSONString(gift));
} }
// oldGiftList中剩下的, 属于其他活动的数据,不变,需要原样放入缓存中 newValues.put(gift.getCartGoodsUid(), JSON.toJSONString(gift));
if (StringUtils.isNotBlank(activityCode)) { }
oldGiftList = oldGiftList.stream().filter(old -> !activityCode.equals(old.getActivityCode())).collect(Collectors.toList()); // oldGiftList中剩下的, 属于其他活动的数据,不变,需要原样放入缓存中
for (CartGoods old : oldGiftList) { if (StringUtils.isNotBlank(activityCode)) {
newValues.put(old.getCartGoodsUid(), JSON.toJSONString(old)); oldGiftList = oldGiftList.stream().filter(old -> !activityCode.equals(old.getActivityCode())).collect(Collectors.toList());
} for (CartGoods old : oldGiftList) {
newValues.put(old.getCartGoodsUid(), JSON.toJSONString(old));
} }
} }
if (CollectionUtils.isNotEmpty(values)) { if (CollectionUtils.isNotEmpty(values)) {
...@@ -135,4 +131,18 @@ public class GiftCacheManager { ...@@ -135,4 +131,18 @@ public class GiftCacheManager {
BoundHashOperations<String, String, String> operations = redisTemplate.boundHashOps(redisKey); BoundHashOperations<String, String, String> operations = redisTemplate.boundHashOps(redisKey);
operations.put(goods.getCartGoodsUid(), JSON.toJSONString(goods)); operations.put(goods.getCartGoodsUid(), JSON.toJSONString(goods));
} }
/**
* 清空赠品的购物车信息
* @param partnerId 商户号
* @param storeCode 门店号
* @param userId 用户id
* @return
*/
public void clearGiftCache(@NotBlank String partnerId,
@NotBlank String storeCode,
@NotBlank String userId) {
String redisKey = MessageFormat.format(RedisKeyConstant.SAAS_SHOPPING_CART_GIFT_KEY, partnerId, storeCode, userId);
redisTemplate.delete(redisKey);
}
} }
...@@ -7,6 +7,7 @@ import cn.freemud.service.CartGiftService; ...@@ -7,6 +7,7 @@ import cn.freemud.service.CartGiftService;
import cn.freemud.service.cache.GiftCacheManager; import cn.freemud.service.cache.GiftCacheManager;
import cn.freemud.service.customer.CustomerInfoManager; import cn.freemud.service.customer.CustomerInfoManager;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -39,12 +40,16 @@ public class CartGiftServiceImpl implements CartGiftService { ...@@ -39,12 +40,16 @@ public class CartGiftServiceImpl implements CartGiftService {
// 查询赠品缓存 // 查询赠品缓存
CartGoods goods = giftCacheManager.queryGiftGoods(partnerId, storeCode, user.getMemberId(), requestVo.getUid()); CartGoods goods = giftCacheManager.queryGiftGoods(partnerId, storeCode, user.getMemberId(), requestVo.getUid());
List<CartGoods.CartGoodsExtra> attrList = new ArrayList<>(); List<CartGoods.CartGoodsExtra> attrList = new ArrayList<>();
List<String> nameList = new ArrayList<>();
requestVo.getExtra().forEach(a -> { requestVo.getExtra().forEach(a -> {
CartGoods.CartGoodsExtra e = new CartGoods.CartGoodsExtra(); CartGoods.CartGoodsExtra e = new CartGoods.CartGoodsExtra();
e.setAttributeId(a.getAttributeId()); e.setAttributeId(a.getAttributeId());
e.setAttributeName(a.getAttributeName()); e.setAttributeName(a.getAttributeName());
attrList.add(e);
nameList.add(a.getAttributeName());
}); });
goods.setExtra(attrList); goods.setExtra(attrList);
goods.setSubName(StringUtils.join(nameList, "/"));
// 更新赠品缓存 // 更新赠品缓存
giftCacheManager.updateGiftGoods(partnerId, storeCode, user.getMemberId(), goods); giftCacheManager.updateGiftGoods(partnerId, storeCode, user.getMemberId(), goods);
return goods; return goods;
......
...@@ -47,6 +47,7 @@ import cn.freemud.interceptor.BizServiceException; ...@@ -47,6 +47,7 @@ import cn.freemud.interceptor.BizServiceException;
import cn.freemud.interceptor.ServiceException; import cn.freemud.interceptor.ServiceException;
import cn.freemud.redis.RedisCache; import cn.freemud.redis.RedisCache;
import cn.freemud.service.*; import cn.freemud.service.*;
import cn.freemud.service.cache.GiftCacheManager;
import cn.freemud.service.delivery.DeliveryFactory; import cn.freemud.service.delivery.DeliveryFactory;
import cn.freemud.service.delivery.DeliveryService; import cn.freemud.service.delivery.DeliveryService;
import cn.freemud.service.impl.calculate.*; import cn.freemud.service.impl.calculate.*;
...@@ -212,6 +213,8 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService { ...@@ -212,6 +213,8 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService {
@Autowired @Autowired
private GetActivityMsgHandle getActivityMsgHandle; private GetActivityMsgHandle getActivityMsgHandle;
@Autowired
private GiftCacheManager giftCacheManager;
/** /**
* 从微信卡券向购物车中添加商品 * 从微信卡券向购物车中添加商品
...@@ -1033,6 +1036,8 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService { ...@@ -1033,6 +1036,8 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService {
ShoppingCartRelationService shoppingCartRelationService = shoppingCartRelationFactory.cleanAfter(shoppingCartClearRequestVo.getPartnerId()); ShoppingCartRelationService shoppingCartRelationService = shoppingCartRelationFactory.cleanAfter(shoppingCartClearRequestVo.getPartnerId());
shoppingCartRelationService.cleanAfter(shoppingCartGoodsResponseVo, shoppingCartClearRequestVo); shoppingCartRelationService.cleanAfter(shoppingCartGoodsResponseVo, shoppingCartClearRequestVo);
// 清空购物车赠品的缓存信息
giftCacheManager.clearGiftCache(partnerId, storeId, userId);
return ResponseUtil.success(shoppingCartGoodsResponseVo); return ResponseUtil.success(shoppingCartGoodsResponseVo);
} }
......
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