fix: 优化购物车对于更新商品的检查验证

This commit is contained in:
misworga831 2023-04-10 14:43:47 +08:00
parent e18b62da6e
commit a9c9886210
2 changed files with 70 additions and 28 deletions

View File

@ -12,6 +12,7 @@ import cn.lili.common.enums.ResultCode;
import cn.lili.common.event.TransactionCommitSendMQEvent; import cn.lili.common.event.TransactionCommitSendMQEvent;
import cn.lili.common.exception.ServiceException; import cn.lili.common.exception.ServiceException;
import cn.lili.common.properties.RocketmqCustomProperties; import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext; import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.SnowFlake; import cn.lili.common.utils.SnowFlake;
import cn.lili.modules.goods.entity.dos.Goods; import cn.lili.modules.goods.entity.dos.Goods;
@ -29,16 +30,22 @@ import cn.lili.modules.goods.entity.vos.GoodsSkuVO;
import cn.lili.modules.goods.entity.vos.GoodsVO; import cn.lili.modules.goods.entity.vos.GoodsVO;
import cn.lili.modules.goods.entity.vos.SpecValueVO; import cn.lili.modules.goods.entity.vos.SpecValueVO;
import cn.lili.modules.goods.mapper.GoodsSkuMapper; import cn.lili.modules.goods.mapper.GoodsSkuMapper;
import cn.lili.modules.goods.service.*; import cn.lili.modules.goods.service.GoodsGalleryService;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.modules.goods.service.GoodsSkuService;
import cn.lili.modules.goods.service.WholesaleService;
import cn.lili.modules.goods.sku.GoodsSkuBuilder; import cn.lili.modules.goods.sku.GoodsSkuBuilder;
import cn.lili.modules.goods.sku.render.SalesModelRender; import cn.lili.modules.goods.sku.render.SalesModelRender;
import cn.lili.modules.member.entity.dos.FootPrint; import cn.lili.modules.member.entity.dos.FootPrint;
import cn.lili.modules.member.entity.dto.EvaluationQueryParams; import cn.lili.modules.member.entity.dto.EvaluationQueryParams;
import cn.lili.modules.member.entity.enums.EvaluationGradeEnum; import cn.lili.modules.member.entity.enums.EvaluationGradeEnum;
import cn.lili.modules.member.service.MemberEvaluationService; import cn.lili.modules.member.service.MemberEvaluationService;
import cn.lili.modules.promotion.entity.dos.Coupon;
import cn.lili.modules.promotion.entity.dos.PromotionGoods; import cn.lili.modules.promotion.entity.dos.PromotionGoods;
import cn.lili.modules.promotion.entity.dto.search.PromotionGoodsSearchParams; import cn.lili.modules.promotion.entity.dto.search.PromotionGoodsSearchParams;
import cn.lili.modules.promotion.entity.enums.CouponGetEnum; import cn.lili.modules.promotion.entity.enums.CouponGetEnum;
import cn.lili.modules.promotion.service.CouponService;
import cn.lili.modules.promotion.service.MemberCouponService;
import cn.lili.modules.promotion.service.PromotionGoodsService; import cn.lili.modules.promotion.service.PromotionGoodsService;
import cn.lili.modules.search.entity.dos.EsGoodsIndex; import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.search.service.EsGoodsIndexService; import cn.lili.modules.search.service.EsGoodsIndexService;
@ -80,7 +87,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
* 分类 * 分类
*/ */
@Autowired @Autowired
private CategoryService categoryService; private MemberCouponService memberCouponService;
/** /**
* 商品相册 * 商品相册
*/ */
@ -121,6 +128,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
@Autowired @Autowired
private WholesaleService wholesaleService; private WholesaleService wholesaleService;
@Autowired
private CouponService couponService;
@Autowired @Autowired
private List<SalesModelRender> salesModelRenders; private List<SalesModelRender> salesModelRenders;
@ -166,7 +176,8 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
//发送mq消息 //发送mq消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.SKU_DELETE.name(); String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.SKU_DELETE.name();
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(oldSkuIds), RocketmqSendCallbackBuilder.commonCallback()); rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(oldSkuIds),
RocketmqSendCallbackBuilder.commonCallback());
} else { } else {
skuList = new ArrayList<>(); skuList = new ArrayList<>();
for (Map<String, Object> map : goodsOperationDTO.getSkuList()) { for (Map<String, Object> map : goodsOperationDTO.getSkuList()) {
@ -183,7 +194,8 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
if (!skuList.isEmpty()) { if (!skuList.isEmpty()) {
LambdaQueryWrapper<GoodsSku> unnecessarySkuIdsQuery = new LambdaQueryWrapper<>(); LambdaQueryWrapper<GoodsSku> unnecessarySkuIdsQuery = new LambdaQueryWrapper<>();
unnecessarySkuIdsQuery.eq(GoodsSku::getGoodsId, goods.getId()); unnecessarySkuIdsQuery.eq(GoodsSku::getGoodsId, goods.getId());
unnecessarySkuIdsQuery.notIn(GoodsSku::getId, skuList.stream().map(BaseEntity::getId).collect(Collectors.toList())); unnecessarySkuIdsQuery.notIn(GoodsSku::getId,
skuList.stream().map(BaseEntity::getId).collect(Collectors.toList()));
this.remove(unnecessarySkuIdsQuery); this.remove(unnecessarySkuIdsQuery);
this.saveOrUpdateBatch(skuList); this.saveOrUpdateBatch(skuList);
this.updateStock(skuList); this.updateStock(skuList);
@ -263,14 +275,16 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
if (goodsVO == null || goodsSku == null) { if (goodsVO == null || goodsSku == null) {
//发送mq消息 //发送mq消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GOODS_DELETE.name(); String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GOODS_DELETE.name();
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(Collections.singletonList(goodsId)), RocketmqSendCallbackBuilder.commonCallback()); rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(Collections.singletonList(goodsId)),
RocketmqSendCallbackBuilder.commonCallback());
throw new ServiceException(ResultCode.GOODS_NOT_EXIST); throw new ServiceException(ResultCode.GOODS_NOT_EXIST);
} }
//商品下架||商品未审核通过||商品删除则提示商品已下架 //商品下架||商品未审核通过||商品删除则提示商品已下架
if (GoodsStatusEnum.DOWN.name().equals(goodsVO.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goodsVO.getAuthFlag()) || Boolean.TRUE.equals(goodsVO.getDeleteFlag())) { if (GoodsStatusEnum.DOWN.name().equals(goodsVO.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goodsVO.getAuthFlag()) || Boolean.TRUE.equals(goodsVO.getDeleteFlag())) {
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GOODS_DELETE.name(); String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GOODS_DELETE.name();
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(Collections.singletonList(goodsId)), RocketmqSendCallbackBuilder.commonCallback()); rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(Collections.singletonList(goodsId)),
RocketmqSendCallbackBuilder.commonCallback());
throw new ServiceException(ResultCode.GOODS_NOT_EXIST); throw new ServiceException(ResultCode.GOODS_NOT_EXIST);
} }
@ -284,15 +298,31 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
GoodsSkuVO goodsSkuDetail = this.getGoodsSkuVO(goodsSku); GoodsSkuVO goodsSkuDetail = this.getGoodsSkuVO(goodsSku);
Map<String, Object> promotionMap = goodsIndex.getPromotionMap(); Map<String, Object> promotionMap = goodsIndex.getPromotionMap();
AuthUser currentUser = UserContext.getCurrentUser();
//设置当前商品的促销价格 //设置当前商品的促销价格
if (promotionMap != null && !promotionMap.isEmpty()) { if (promotionMap != null && !promotionMap.isEmpty()) {
promotionMap = promotionMap.entrySet().stream().parallel().filter(i -> { promotionMap = promotionMap.entrySet().stream().parallel().filter(i -> {
JSONObject jsonObject = JSONUtil.parseObj(i.getValue()); JSONObject jsonObject = JSONUtil.parseObj(i.getValue());
if (i.getKey().contains(PromotionTypeEnum.COUPON.name()) && currentUser != null) {
Integer couponLimitNum = jsonObject.getInt("couponLimitNum");
Coupon coupon = couponService.getById(jsonObject.getStr("id"));
if (coupon == null || (coupon.getPublishNum() != 0 && coupon.getReceivedNum() >= coupon.getPublishNum())) {
return false;
}
if (couponLimitNum > 0) {
Long count = memberCouponService.getMemberCouponNum(currentUser.getId(), jsonObject.getStr(
"id"));
if (count >= couponLimitNum) {
return false;
}
}
}
// 过滤活动赠送优惠券和无效时间的活动 // 过滤活动赠送优惠券和无效时间的活动
return (jsonObject.get("getType") == null || jsonObject.get("getType", String.class).equals(CouponGetEnum.FREE.name())) && (jsonObject.get("startTime") != null && jsonObject.get("startTime", Date.class).getTime() <= System.currentTimeMillis()) && (jsonObject.get("endTime") == null || jsonObject.get("endTime", Date.class).getTime() >= System.currentTimeMillis()); return (jsonObject.get("getType") == null || jsonObject.get("getType", String.class).equals(CouponGetEnum.FREE.name())) && (jsonObject.get("startTime") != null && jsonObject.get("startTime", Date.class).getTime() <= System.currentTimeMillis()) && (jsonObject.get("endTime") == null || jsonObject.get("endTime", Date.class).getTime() >= System.currentTimeMillis());
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Optional<Map.Entry<String, Object>> containsPromotion = promotionMap.entrySet().stream().filter(i -> i.getKey().contains(PromotionTypeEnum.SECKILL.name()) || i.getKey().contains(PromotionTypeEnum.PINTUAN.name())).findFirst(); Optional<Map.Entry<String, Object>> containsPromotion =
promotionMap.entrySet().stream().filter(i -> i.getKey().contains(PromotionTypeEnum.SECKILL.name()) || i.getKey().contains(PromotionTypeEnum.PINTUAN.name())).findFirst();
if (containsPromotion.isPresent()) { if (containsPromotion.isPresent()) {
JSONObject jsonObject = JSONUtil.parseObj(containsPromotion.get().getValue()); JSONObject jsonObject = JSONUtil.parseObj(containsPromotion.get().getValue());
PromotionGoodsSearchParams searchParams = new PromotionGoodsSearchParams(); PromotionGoodsSearchParams searchParams = new PromotionGoodsSearchParams();
@ -312,8 +342,10 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
map.put("data", goodsSkuDetail); map.put("data", goodsSkuDetail);
//获取分类 //获取分类
map.put("wholesaleList", GoodsSalesModeEnum.WHOLESALE.name().equals(goodsVO.getSalesModel()) ? wholesaleService.findByGoodsId(goodsSkuDetail.getGoodsId()) : Collections.emptyList()); map.put("wholesaleList", GoodsSalesModeEnum.WHOLESALE.name().equals(goodsVO.getSalesModel()) ?
map.put("categoryName", CharSequenceUtil.isNotEmpty(goodsIndex.getCategoryNamePath()) ? goodsIndex.getCategoryNamePath().split(",") : null); wholesaleService.findByGoodsId(goodsSkuDetail.getGoodsId()) : Collections.emptyList());
map.put("categoryName", CharSequenceUtil.isNotEmpty(goodsIndex.getCategoryNamePath()) ?
goodsIndex.getCategoryNamePath().split(",") : null);
//获取规格信息 //获取规格信息
map.put("specs", this.groupBySkuAndSpec(goodsVO.getSkuList())); map.put("specs", this.groupBySkuAndSpec(goodsVO.getSkuList()));
@ -325,8 +357,8 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
} }
//记录用户足迹 //记录用户足迹
if (UserContext.getCurrentUser() != null) { if (currentUser != null) {
FootPrint footPrint = new FootPrint(UserContext.getCurrentUser().getId(), goodsIndex.getStoreId(), goodsId, skuId); FootPrint footPrint = new FootPrint(currentUser.getId(), goodsIndex.getStoreId(), goodsId, skuId);
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.VIEW_GOODS.name(); String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.VIEW_GOODS.name();
rocketMQTemplate.asyncSend(destination, footPrint, RocketmqSendCallbackBuilder.commonCallback()); rocketMQTemplate.asyncSend(destination, footPrint, RocketmqSendCallbackBuilder.commonCallback());
} }
@ -374,10 +406,13 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
boolean update = this.update(updateWrapper); boolean update = this.update(updateWrapper);
if (Boolean.TRUE.equals(update)) { if (Boolean.TRUE.equals(update)) {
if (GoodsStatusEnum.UPPER.name().equals(marketEnable)) { if (GoodsStatusEnum.UPPER.name().equals(marketEnable)) {
applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("生成店铺商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.GENERATOR_STORE_GOODS_INDEX.name(), storeId)); applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("生成店铺商品",
rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.GENERATOR_STORE_GOODS_INDEX.name(),
storeId));
} else if (GoodsStatusEnum.DOWN.name().equals(marketEnable)) { } else if (GoodsStatusEnum.DOWN.name().equals(marketEnable)) {
cache.vagueDel(CachePrefix.GOODS_SKU.getPrefix()); cache.vagueDel(CachePrefix.GOODS_SKU.getPrefix());
applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("删除店铺商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.STORE_GOODS_DELETE.name(), storeId)); applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("删除店铺商品",
rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.STORE_GOODS_DELETE.name(), storeId));
} }
} }
} }
@ -443,9 +478,11 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
if ("images".equals(entry.getKey())) { if ("images".equals(entry.getKey())) {
specValueVO.setSpecName(entry.getKey()); specValueVO.setSpecName(entry.getKey());
if (entry.getValue().toString().contains("url")) { if (entry.getValue().toString().contains("url")) {
List<SpecValueVO.SpecImages> specImages = JSONUtil.toList(JSONUtil.parseArray(entry.getValue()), SpecValueVO.SpecImages.class); List<SpecValueVO.SpecImages> specImages = JSONUtil.toList(JSONUtil.parseArray(entry.getValue()),
SpecValueVO.SpecImages.class);
specValueVO.setSpecImage(specImages); specValueVO.setSpecImage(specImages);
goodsGalleryList = specImages.stream().map(SpecValueVO.SpecImages::getUrl).collect(Collectors.toList()); goodsGalleryList =
specImages.stream().map(SpecValueVO.SpecImages::getUrl).collect(Collectors.toList());
} }
} else { } else {
specValueVO.setSpecName(entry.getKey()); specValueVO.setSpecName(entry.getKey());
@ -496,7 +533,8 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
goodsIndexService.deleteIndexById(goodsSku.getId()); goodsIndexService.deleteIndexById(goodsSku.getId());
} }
goodsSku.setQuantity(quantity); goodsSku.setQuantity(quantity);
boolean update = this.update(new LambdaUpdateWrapper<GoodsSku>().eq(GoodsSku::getId, skuId).set(GoodsSku::getQuantity, quantity)); boolean update =
this.update(new LambdaUpdateWrapper<GoodsSku>().eq(GoodsSku::getId, skuId).set(GoodsSku::getQuantity, quantity));
if (update) { if (update) {
cache.remove(CachePrefix.GOODS.getPrefix() + goodsSku.getGoodsId()); cache.remove(CachePrefix.GOODS.getPrefix() + goodsSku.getGoodsId());
} }
@ -527,7 +565,8 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updateGoodsStuck(List<GoodsSku> goodsSkus) { public void updateGoodsStuck(List<GoodsSku> goodsSkus) {
Map<String, List<GoodsSku>> groupByGoodsIds = goodsSkus.stream().collect(Collectors.groupingBy(GoodsSku::getGoodsId)); Map<String, List<GoodsSku>> groupByGoodsIds =
goodsSkus.stream().collect(Collectors.groupingBy(GoodsSku::getGoodsId));
//获取相关的sku集合 //获取相关的sku集合
LambdaQueryWrapper<GoodsSku> lambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<GoodsSku> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(GoodsSku::getGoodsId, groupByGoodsIds.keySet()); lambdaQueryWrapper.in(GoodsSku::getGoodsId, groupByGoodsIds.keySet());
@ -572,9 +611,14 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
//修改规格索引,发送mq消息 //修改规格索引,发送mq消息
Map<String, Object> updateIndexFieldsMap = EsIndexUtil.getUpdateIndexFieldsMap(MapUtil.builder(new HashMap<String, Object>()).put("id", goodsSku.getId()).build(), MapUtil.builder(new HashMap<String, Object>()).put("commentNum", goodsSku.getCommentNum()).put("highPraiseNum", highPraiseNum).put("grade", grade).build()); Map<String, Object> updateIndexFieldsMap =
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_FIELD.name(); EsIndexUtil.getUpdateIndexFieldsMap(MapUtil.builder(new HashMap<String, Object>()).put("id",
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(updateIndexFieldsMap), RocketmqSendCallbackBuilder.commonCallback()); goodsSku.getId()).build(), MapUtil.builder(new HashMap<String, Object>()).put("commentNum",
goodsSku.getCommentNum()).put("highPraiseNum", highPraiseNum).put("grade", grade).build());
String destination =
rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_FIELD.name();
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(updateIndexFieldsMap),
RocketmqSendCallbackBuilder.commonCallback());
//修改商品的评价数量 //修改商品的评价数量
goodsService.updateGoodsCommentNum(goodsSku.getGoodsId()); goodsService.updateGoodsCommentNum(goodsSku.getGoodsId());
@ -608,11 +652,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
public Long countSkuNum(String storeId) { public Long countSkuNum(String storeId) {
LambdaQueryWrapper<GoodsSku> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<GoodsSku> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper queryWrapper.eq(GoodsSku::getStoreId, storeId).eq(GoodsSku::getDeleteFlag, Boolean.FALSE).eq(GoodsSku::getAuthFlag, GoodsAuthEnum.PASS.name()).eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name());
.eq(GoodsSku::getStoreId, storeId)
.eq(GoodsSku::getDeleteFlag, Boolean.FALSE)
.eq(GoodsSku::getAuthFlag, GoodsAuthEnum.PASS.name())
.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name());
return this.count(queryWrapper); return this.count(queryWrapper);
} }

View File

@ -120,6 +120,9 @@ public class CheckDataRender implements CartRenderStep {
if (checkGoodsStatus || checkGoodsValid) { if (checkGoodsStatus || checkGoodsValid) {
if (checkGoodsValid) {
cartSkuVO.rebuildBySku(dataSku);
}
if (checkGoodsStatus) { if (checkGoodsStatus) {
//设置购物车未选中 //设置购物车未选中
cartSkuVO.setChecked(false); cartSkuVO.setChecked(false);
@ -127,10 +130,9 @@ public class CheckDataRender implements CartRenderStep {
cartSkuVO.setInvalid(true); cartSkuVO.setInvalid(true);
//设置失效消息 //设置失效消息
cartSkuVO.setErrorMessage("商品已下架"); cartSkuVO.setErrorMessage("商品已下架");
continue;
} }
if (checkGoodsValid) {
cartSkuVO.rebuildBySku(dataSku);
}
} }
//商品库存判定 //商品库存判定