Commit c482e803 by 查志伟

添加更新赠品属性接口

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