清空购物车同时返还库存及销量

This commit is contained in:
SongZhang 2024-06-25 11:06:48 +08:00
parent f51fcece8e
commit 98439a14b8
8 changed files with 77 additions and 5 deletions

View File

@ -67,4 +67,8 @@ public interface CacheKey {
* 库存 PRODUCT:shopid:skuid
*/
String PRODUCT_SKU = "PRODUCT:";
/**
* 商品库存
*/
String PRODUCT = "PRODUCT:";
}

View File

@ -716,6 +716,10 @@ public class RedisUtils {
redisTemplate.opsForValue().increment(key, delta);
}
public void decrBy(String key, long delta) {
redisTemplate.opsForValue().decrement(key, delta);
}
/**
* @param prefix 前缀
* @param ids id

View File

@ -3,12 +3,14 @@ package cn.ysk.cashier.quartz.task;
import cn.ysk.cashier.pojo.order.TbCashierCart;
import cn.ysk.cashier.repository.order.TbCashierCartRepository;
import cn.ysk.cashier.repository.product.TbProductSkuRepository;
import cn.ysk.cashier.service.order.TbCashierCartService;
import cn.ysk.cashier.service.shop.TbShopStorageService;
import cn.ysk.cashier.utils.CacheKey;
import cn.ysk.cashier.utils.QueryHelp;
import cn.ysk.cashier.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -36,6 +38,12 @@ public class TestTask {
@Autowired
private RedisUtils redisUtils;
private final TbCashierCartService tbCashierCartService;
public TestTask(TbCashierCartService tbCashierCartService) {
this.tbCashierCartService = tbCashierCartService;
}
public void run(){
log.info("run 执行成功");
@ -70,7 +78,7 @@ public class TestTask {
});
Set<String> keys = new HashSet<>();
for (TbCashierCart cart : all) {
skuRepository.updateStockNumber(Integer.valueOf(cart.getSkuId()),cart.getNumber().doubleValue());
tbCashierCartService.clearExpireOrder(cart);
cart.setStatus("cancelled");
cartService.save(cart);
keys.add(CacheKey.PRODUCT_SKU + cart.getShopId() + ":" + cart.getSkuId());

View File

@ -39,4 +39,8 @@ public interface TbProductRepository extends JpaRepository<TbProduct, Integer>,
@Modifying
@Query("update FROM TbProduct pro set pro.stockNumber=:stockNumber WHERE pro.id =:productId")
void updateProductStockNumber(@Param("productId") Integer productId,@Param("stockNumber") Integer stockNumber);
@Modifying
@Query("update FROM TbProduct pro set pro.stockNumber=pro.stockNumber+:number WHERE pro.id =:productId")
void incrProductStockNumber(Integer productId, Integer number);
}

View File

@ -15,24 +15,31 @@
*/
package cn.ysk.cashier.service.impl.order;
import cn.ysk.cashier.dto.product.TbProductDto;
import cn.ysk.cashier.pojo.order.TbCashierCart;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import cn.ysk.cashier.pojo.product.TbProduct;
import cn.ysk.cashier.repository.product.TbProductRepository;
import cn.ysk.cashier.repository.product.TbProductSkuRepository;
import cn.ysk.cashier.service.product.TbProductService;
import cn.ysk.cashier.utils.*;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.order.TbCashierCartRepository;
import cn.ysk.cashier.service.order.TbCashierCartService;
import cn.ysk.cashier.dto.order.TbCashierCartDto;
import cn.ysk.cashier.dto.order.TbCashierCartQueryCriteria;
import cn.ysk.cashier.mapper.order.TbCashierCartMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.persistence.criteria.*;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@ -47,8 +54,14 @@ import java.util.LinkedHashMap;
@RequiredArgsConstructor
public class TbCashierCartServiceImpl implements TbCashierCartService {
private static final Logger log = LoggerFactory.getLogger(TbCashierCartServiceImpl.class);
private final TbCashierCartRepository tbCashierCartRepository;
private final TbCashierCartMapper tbCashierCartMapper;
private final TbProductService tbproductService;
private final TbProductSkuRepository skuRepository;
private final RedisUtils redisUtils;
private final TbProductRepository tbProductRepository;
@Override
public Map<String,Object> queryAll(TbCashierCartQueryCriteria criteria, Pageable pageable){
@ -127,4 +140,25 @@ public class TbCashierCartServiceImpl implements TbCashierCartService {
}
FileUtil.downloadExcel(list, response);
}
@Override
public void clearExpireOrder(TbCashierCart cart) {
String key = CacheKey.PRODUCT + cart.getShopId() + ":product" + cart.getProductId();
TbProduct tbProduct = tbProductRepository.findById(Integer.valueOf(cart.getProductId())).orElse(null);
if (tbProduct == null) {
log.warn("清空购物车查询订单失败product id {}", cart.getProductId());
return;
}
if (tbProduct.getIsDistribute() == 1) {
tbproductService.incrStockNumber(cart.getProductId(), cart.getNumber());
} else {
key = CacheKey.PRODUCT + cart.getShopId() + ":" + cart.getId();
skuRepository.updateStockNumber(Integer.valueOf(cart.getSkuId()),cart.getNumber().doubleValue());
}
// 减去销量
if (cart.getNumber() > 0) {
redisUtils.decrBy(key, cart.getNumber());
}
}
}

View File

@ -613,4 +613,9 @@ public class TbProductServiceImpl implements TbProductService {
}
FileUtil.downloadExcel(list, response);
}
@Override
public void incrStockNumber(String productId, Integer number) {
tbProductRepository.incrProductStockNumber(Integer.valueOf(productId), number);
}
}

View File

@ -80,4 +80,10 @@ public interface TbCashierCartService {
* @throws IOException /
*/
void download(List<TbCashierCartDto> all, HttpServletResponse response) throws IOException;
/**
* 清除过期订单
* @param cart 购物车订单
*/
void clearExpireOrder(TbCashierCart cart);
}

View File

@ -77,4 +77,11 @@ public interface TbProductService {
void updateIsHot(Integer id, String shopId);
void updateIsStock(Integer proId, String shopId,Integer isStock);
/**
* 增加库存
* @param productId 商品id
* @param number 增加数量
*/
void incrStockNumber(String productId, Integer number);
}