Commit 5c3a2602 by 查志伟

赠品信息存入缓存

parent 9c9a10b4
......@@ -38,6 +38,19 @@ public class RedisKeyConstant {
* 用户购物车总价在redis的key前缀
*/
public final static String SAAS_SHOPPINGCART_AMOUNT_PREFIX = "saas:user:info:cart:amount:goods:";
/**
* 用户购物车赠品在redis中的缓存key
* {0}:商户号
* {1}:门店号
* {2}:用户的userId
*
* 缓存结构为hash
* hashKey为购物车行uid
* value为 CartGoods类型的json字符串
*/
public final static String SAAS_SHOPPING_CART_GIFT_KEY = "saas:user:info:cart:gift:{0}:{1}:{2}";
/**
* 商品的信息前缀
*/
......
......@@ -39,5 +39,7 @@ public interface ActiveService {
ShoppingCartInfoRequestVo shoppingCartInfoRequestVo,
CalculationDiscountResult discountResult,
String partnerId,
String storeId,
String userId,
Integer flag);
}
......@@ -75,6 +75,8 @@ public class CocoActiveServiceImpl implements ActiveService {
ShoppingCartInfoRequestVo shoppingCartInfoRequestVo,
CalculationDiscountResult discountResult,
String partnerId,
String storeId,
String userId,
Integer flag) {
// 点餐页赠品分行显示,赠品显示小料
......
......@@ -22,6 +22,7 @@ import cn.freemud.entities.vo.ShoppingCartInfoRequestVo;
import cn.freemud.enums.ActivityTypeEnum;
import cn.freemud.enums.CalculationGoodsType;
import cn.freemud.service.active.ActiveService;
import cn.freemud.service.cache.GiftCacheManager;
import cn.freemud.service.impl.BuyAndGiftsPromotionService;
import com.freemud.application.sdk.api.productcenter.domain.ProductBeanDTO;
import org.apache.commons.lang.ObjectUtils;
......@@ -39,6 +40,8 @@ public class PlatformActiveServiceImpl implements ActiveService {
@Autowired
private BuyAndGiftsPromotionService buyAndGiftsPromotionService;
@Autowired
private GiftCacheManager giftCacheManager;
/**
* 用户选择的商品和数量都要传给促销
......@@ -78,45 +81,50 @@ public class PlatformActiveServiceImpl implements ActiveService {
ShoppingCartInfoRequestVo shoppingCartInfoRequestVo,
CalculationDiscountResult discountResult,
String partnerId,
String storeId,
String userId,
Integer flag) {
Map<String, Goods> discountForGift = this.getDiscountForGift(discountResult);
for (ProductBeanDTO product : products) {
String k = product.getPid();
Goods goods = discountForGift.get(k);
discountForGift.forEach((pid, goods) -> {
GoodsDiscount discount = goods.getDiscounts()
.stream()
.filter(g -> ActivityTypeEnum.TYPE_63.getCode().equals(g.getType()) ||ActivityTypeEnum.TYPE_62.getCode().equals(g.getType()) || ActivityTypeEnum.TYPE_61.getCode().equals(g.getType()) || ActivityTypeEnum.TYPE_230.getCode().equals(g.getType()))
.findFirst()
.get();
CartGoods cartGood = new CartGoods();
cartGood.setGoodsId(goods.getGoodsId());
cartGood.setGoodsType(1);
cartGood.setSpuId(goods.getGoodsId());
cartGood.setSkuId(goods.getGoodsId());
cartGood.setOriginalPrice(product.getFinalPrice());
cartGood.setPackPrice(product.getPackPrice());
cartGood.setOriginalAmount(goods.getOriginalPrice() * goods.getGoodsQuantity());
cartGood.setAmount(goods.getRealAmount());
cartGood.setName(product.getName());
cartGood.setSpuName(product.getName());
cartGood.setCategoryName(product.getName());
cartGood.setPic(product.getPicture());
cartGood.setSkuName(product.getName());
cartGood.setActivityType(discount.getType());
cartGood.setStockLimit(ObjectUtils.equals(1, product.getStockLimit()));
cartGood.setCustomerCode(product.getCustomerCode());
cartGood.setUnit(product.getUnit());
cartGood.setWeight(product.getWeight());
cartGood.setQty(goods.getGoodsQuantity());
cartGood.setClassificationId(product.getCategory());
cartGood.setClassificationName(product.getCategoryName());
// nodeId标识activeCode,用于计算均摊时每个商品在每个活动上均摊金额
cartGood.setNodeId(discount.getActivityCode());
addcartGoodsList.addAll(buyAndGiftsPromotionService.setGiftExtra(cartGood, product, addcartGoodsList));
}
List<CartGoods> list = new ArrayList<>();
for (ProductBeanDTO product : products) {
CartGoods cartGood = new CartGoods();
cartGood.setGoodsId(goods.getGoodsId());
cartGood.setGoodsType(1);
cartGood.setSpuId(goods.getGoodsId());
cartGood.setSkuId(goods.getGoodsId());
cartGood.setOriginalPrice(product.getFinalPrice());
cartGood.setPackPrice(product.getPackPrice());
cartGood.setOriginalAmount(goods.getOriginalPrice() * goods.getGoodsQuantity());
cartGood.setAmount(goods.getRealAmount());
cartGood.setName(product.getName());
cartGood.setSpuName(product.getName());
cartGood.setCategoryName(product.getName());
cartGood.setPic(product.getPicture());
cartGood.setSkuName(product.getName());
cartGood.setActivityType(discount.getType());
cartGood.setActivityCode(discount.getActivityCode());
cartGood.setStockLimit(ObjectUtils.equals(1, product.getStockLimit()));
cartGood.setCustomerCode(product.getCustomerCode());
cartGood.setUnit(product.getUnit());
cartGood.setWeight(product.getWeight());
cartGood.setQty(goods.getGoodsQuantity());
cartGood.setClassificationId(product.getCategory());
cartGood.setClassificationName(product.getCategoryName());
// nodeId标识activeCode,用于计算均摊时每个商品在每个活动上均摊金额
cartGood.setNodeId(discount.getActivityCode());
list.addAll(buyAndGiftsPromotionService.setGiftExtra(cartGood, product, addcartGoodsList));
}
// 因为有涉及到用户选择的属性,存入redis
addcartGoodsList.addAll(giftCacheManager.resetGiftCache(list, discount.getActivityCode(), partnerId, storeId, userId));
});
}
public Map<String, Goods> getDiscountForGift(CalculationDiscountResult discountResult) {
......
package cn.freemud.service.cache;
import cn.freemud.entities.vo.CartGoods;
import com.alibaba.fastjson.JSON;
import com.freemud.sdk.api.assortment.shoppingcart.constant.RedisKeyConstant;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
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.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* @author Clover.z
* @Date 2021/10/28
* @Desc 购物车对应的赠品缓存
*/
@Component
@RequiredArgsConstructor
public class GiftCacheManager {
private final RedisTemplate<String, String> redisTemplate;
/**
* 购物车中赠品缓存有效期,默认3天
*/
@Value("${saas.shoppingCart.giftCache.expireDay:3}")
private Integer expireDay;
/**
* 重置购物车赠品缓存
* @param giftList 赠品行信息
* @param activityCode 赠品对应的活动号(不传则覆盖缓存所有数据,传则只会重置指定活动的赠品数据)
* @param partnerId 商户号
* @param storeCode 门店号
* @param userId 用户userId
* @return 返回更新后的赠品信息
*/
public List<CartGoods> resetGiftCache(@NotNull List<CartGoods> giftList,
String activityCode,
@NotBlank String partnerId,
@NotBlank String storeCode,
@NotBlank String userId) {
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();
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));
}
// 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)) {
//旧值不为空,清掉
redisTemplate.delete(redisKey);
}
if (!newValues.isEmpty()) {
operations.putAll(newValues);
operations.expire(expireDay, TimeUnit.DAYS);
}
return giftList;
}
}
......@@ -12,6 +12,7 @@ import cn.freemud.enums.ActivityTypeEnum;
import cn.freemud.enums.ResponseResult;
import cn.freemud.interceptor.ServiceException;
import cn.freemud.service.IPromotionService;
import cn.freemud.service.cache.GiftCacheManager;
import com.freemud.application.sdk.api.productcenter.domain.ProductBeanDTO;
import com.freemud.sdk.api.assortment.shoppingcart.service.impl.ShoppingCartBaseServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
......@@ -44,6 +45,8 @@ public class BuyAndGiftsPromotionService implements IPromotionService {
private AssortmentSdkService assortmentSdkService;
@Autowired
private ShoppingCartBaseServiceImpl shoppingCartBaseService;
@Autowired
private GiftCacheManager giftCacheManager;
@Override
public void updateShoppingCartGoodsDiscount(CouponPromotionVO couponPromotionVO, ActivityQueryDto activityQueryDto,
......@@ -68,6 +71,7 @@ public class BuyAndGiftsPromotionService implements IPromotionService {
Long totalGiftAmount = 0L;
for (SendActivity sendActivity : sendActivities) {
List<CartGoods> list = new ArrayList<>();
for (SendGoods sendGoods : sendActivity.getSendGoods()) {
Optional<ProductBeanDTO> firstProduct = productBeanDTOS.stream().filter(productBean -> ObjectUtils.equals(productBean.getPid(), sendGoods.getGoodsId())).findFirst();
if (!firstProduct.isPresent()) {
......@@ -89,6 +93,7 @@ public class BuyAndGiftsPromotionService implements IPromotionService {
cartGoods.setPic(product.getPicture());
cartGoods.setSkuName(product.getName());
cartGoods.setActivityType(sendActivity.getActivityType());
cartGoods.setActivityCode(sendActivity.getActivityCode());
cartGoods.setStockLimit(ObjectUtils.equals(1, product.getStockLimit()));
cartGoods.setCustomerCode(product.getCustomerCode());
cartGoods.setUnit(product.getUnit());
......@@ -100,9 +105,10 @@ public class BuyAndGiftsPromotionService implements IPromotionService {
cartGoods.setNodeId(sendActivity.getActivityCode());
cartGoods.setOriginalGoodsUid(sendGoods.getOriginalGoodsUid());
cartGoods.setIsSendGoods(true);
cartGoodsList.addAll(this.setGiftExtra(cartGoods,product,cartGoodsList));
list.addAll(this.setGiftExtra(cartGoods,product,cartGoodsList));
totalGiftAmount += product.getFinalPrice() * sendGoods.getSendNumber();
}
cartGoodsList.addAll(giftCacheManager.resetGiftCache(list, sendActivity.getActivityCode(), userLoginInfoDto.getPartnerId(), shoppingCartInfoRequestVo.getShopId(), userLoginInfoDto.getMemberId()));
}
......
......@@ -370,7 +370,7 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService {
ActivityQueryDto activityQueryDto = activityAdapter.getActivityQueryDto(partnerId, storeId, userId, appId, addShoppingCartGoodsRequestVo.getOrderType());
activityQueryDto.setBizType(bizType);
if (true) {
if (grayPush(partnerId, storeId, "2")) {
CalculationDiscountResult discountResult = null;
discountResult = sharingDiscountService.getCalculationSharingDiscountResult(menuType, partnerId, storeId, userId
, appId, orderType, assortmentCustomerInfoVo.isMemberPaid(), allCartGoodsList
......@@ -380,7 +380,7 @@ public class ShoppingCartNewServiceImpl implements ShoppingCartNewService {
//更新购物车券状态
newCalculationUpdateCouponStatus(userId, partnerId, storeId, bizType, cartGoods, allCartGoodsList, discountResult);
sharingCartService.distribute(discountResult, allCartGoodsList, shoppingCartGoodsResponseVo, null
, null, null, activityQueryDto, menuType, deliveryAmount
, userLoginInfoDto, null, activityQueryDto, menuType, deliveryAmount
, ShoppingCartConstant.ADD_AND_UPDATE, partnerId, null, userId, storeId);
buildShoppingCartGoodsResponse(shoppingCartGoodsResponseVo, discountResult, null, partnerId);
......
......@@ -95,7 +95,7 @@ public class CalculationSharingCartService {
/**
* 买赠
*/
giftSharingService.giftResponse(discountResult, cartGoodsList, shoppingCartInfoRequestVo, activityQueryDto, partnerId,flag);
giftSharingService.giftResponse(discountResult, cartGoodsList, shoppingCartInfoRequestVo, activityQueryDto, partnerId, storeId, userId, flag);
/**
* 加价购
......
......@@ -54,6 +54,8 @@ public class GiftSharingService {
, ShoppingCartInfoRequestVo shoppingCartInfoRequestVo
, ActivityQueryDto activityQueryDto
, String partnerId
, String storeId
, String userId
, Integer flag) {
if (discountResult == null || CollectionUtils.isEmpty(discountResult.getGoods())) {
......@@ -61,7 +63,7 @@ public class GiftSharingService {
}
String menuType = shoppingCartInfoRequestVo == null ? null : shoppingCartInfoRequestVo.getMenuType();
List<ProductBeanDTO> beanDTOList = this.drawGiftInfo(discountResult, activityQueryDto, menuType);
this.setCartGoods(discountResult, cartGoodsList, beanDTOList,shoppingCartInfoRequestVo, partnerId, flag);
this.setCartGoods(discountResult, cartGoodsList, beanDTOList,shoppingCartInfoRequestVo, partnerId, storeId, userId, flag);
}
/**
......@@ -125,6 +127,8 @@ public class GiftSharingService {
, List<ProductBeanDTO> products
, ShoppingCartInfoRequestVo shoppingCartInfoRequestVo
, String partnerId
, String storeId
, String userId
, Integer flag) {
if (CollectionUtils.isEmpty(products)) return;
......@@ -133,7 +137,7 @@ public class GiftSharingService {
List<CartGoods> cartGoodsList = new ArrayList<>();
ActiveService activeService = activeFactory.getGifAddCartService(partnerId);
activeService.gifAddCart(cartGoodsLists,cartGoodsList, products,shoppingCartInfoRequestVo,discountResult,partnerId,flag);
activeService.gifAddCart(cartGoodsLists,cartGoodsList, products,shoppingCartInfoRequestVo,discountResult,partnerId, storeId, userId, flag);
cartGoodsLists.addAll(cartGoodsList);
}
......
package cn.freemud.service.product;
import cn.freemud.entities.dto.GetMenuResponseDto;
import cn.freemud.entities.dto.GetProductInfoDto;
import cn.freemud.entities.dto.ProductInfosDto;
import cn.freemud.enums.ResponseResult;
import cn.freemud.interceptor.ServiceException;
import cn.freemud.service.thirdparty.ProductClient;
import com.freemud.application.sdk.api.constant.ResponseResultEnum;
import com.freemud.sdk.api.assortment.shoppingcart.constant.CommonsConstant;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author Clover.z
* @Date 2021/10/29
* @Desc 商品服务通用能力下沉
*/
@Component
@RequiredArgsConstructor
public class ProductManager {
private final ProductClient productClient;
List<GetMenuResponseDto.DataBean.RootNodeBean.ChildrenBeanFirst.ChildrenBeanSecond.ProductBean> getProductsInfo(String partnerId, String storeCode, String menuType, List<String> goodsIdList) {
if (CollectionUtils.isEmpty(goodsIdList)) {
return new ArrayList<>();
}
//移除空的goodsId及spq的商品券
goodsIdList = goodsIdList.parallelStream().filter(StringUtils::isNotEmpty).filter(goodsId -> !goodsId.startsWith(CommonsConstant.COUPON_PREFIX)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(goodsIdList)) {
return new ArrayList<>();
}
GetProductInfoDto query = new GetProductInfoDto();
query.setPartnerId(partnerId);
query.setShopId(storeCode);
query.setProductInfoType(2);
query.setProductIds(goodsIdList);
query.setChannel(menuType);
ProductInfosDto resposne = productClient.listProductInfos(query);
if (!Objects.equals(ResponseResultEnum.SUCCESS.getCode(), resposne.getCode()) || null == resposne.getData()) {
throw new ServiceException(ResponseResult.STORE_ITEM_QUERY_ERROR);
}
return CollectionUtils.isEmpty(resposne.getData().getProducts()) ? new ArrayList<>() : resposne.getData().getProducts();
}
}
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