新增下单库存预警消息推送
This commit is contained in:
@@ -13,9 +13,11 @@ import com.chaozhanggui.system.cashierservice.redis.RedisUtil;
|
||||
import com.chaozhanggui.system.cashierservice.util.DateUtils;
|
||||
import com.chaozhanggui.system.cashierservice.util.JSONUtil;
|
||||
import com.chaozhanggui.system.cashierservice.util.N;
|
||||
import com.chaozhanggui.system.cashierservice.wxUtil.WechatUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -24,6 +26,7 @@ import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* @author lyf
|
||||
@@ -54,9 +57,19 @@ public class CartService {
|
||||
private TbUserCouponsMapper userCouponsMapper;
|
||||
@Autowired
|
||||
private TbSystemCouponsMapper systemCouponsMapper;
|
||||
private final TbUserShopMsgMapper tbUserShopMsgMapper;
|
||||
private final WechatUtil wechatUtil;
|
||||
|
||||
@Autowired
|
||||
private RabbitProducer producer;
|
||||
@Qualifier("tbShopInfoMapper")
|
||||
@Autowired
|
||||
private TbShopInfoMapper tbShopInfoMapper;
|
||||
|
||||
public CartService(TbUserShopMsgMapper tbUserShopMsgMapper, WechatUtil wechatUtil) {
|
||||
this.tbUserShopMsgMapper = tbUserShopMsgMapper;
|
||||
this.wechatUtil = wechatUtil;
|
||||
}
|
||||
|
||||
public void initCart(JSONObject jsonObject) {
|
||||
String tableId = jsonObject.getString("tableId");
|
||||
@@ -237,13 +250,22 @@ public class CartService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改库存并根据警告线发送消息
|
||||
* @param product 商品
|
||||
* @param productSku sku
|
||||
* @param num 库存数
|
||||
*/
|
||||
private void updateProductStock(TbProduct product, TbProductSkuWithBLOBs productSku, Integer num) {
|
||||
String key = RedisCst.PRODUCT + product.getShopId() + ":product" + product.getId();
|
||||
double stock = num;
|
||||
if (product.getIsDistribute() == 1) {
|
||||
productMapper.updateStockById(product.getId().toString(), num);
|
||||
stock = (double) (product.getStockNumber() - num);
|
||||
} else {
|
||||
key = RedisCst.PRODUCT + product.getShopId() + ":" + productSku.getId();
|
||||
productSkuMapper.updateStockById(productSku.getId().toString(), num);
|
||||
stock = productSku.getStockNumber() - num;
|
||||
}
|
||||
|
||||
if (num > 0) {
|
||||
@@ -251,6 +273,42 @@ public class CartService {
|
||||
} else {
|
||||
redisUtil.getIncrNum(key, "2");
|
||||
}
|
||||
|
||||
CompletableFuture.runAsync(() -> checkWarnLineAndSendMsg(productSku, product));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验商品库存警戒线并通知商户
|
||||
* @param productSku sku
|
||||
*/
|
||||
private void checkWarnLineAndSendMsg(TbProductSku productSku, TbProduct product) {
|
||||
if (productSku.getWarnLine() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
TbShopInfo shopInfo = tbShopInfoMapper.selectByPrimaryKey(Integer.valueOf(product.getShopId()));
|
||||
if (shopInfo == null) {
|
||||
log.info("商品库存预警发送失败,店铺不存在,店铺id:{}", product.getShopId());
|
||||
return;
|
||||
}
|
||||
|
||||
if (productSku.getStockNumber() == null) {
|
||||
productSku.setStockNumber((double) 0);
|
||||
}
|
||||
|
||||
if (product.getStockNumber() == null) {
|
||||
product.setStockNumber(0);
|
||||
}
|
||||
if (
|
||||
(product.getIsDistribute() == 1 && productSku.getStockNumber() <= productSku.getWarnLine())
|
||||
|| (product.getIsDistribute() != 1) && product.getStockNumber() <= productSku.getWarnLine()
|
||||
) {
|
||||
TbUserShopMsg tbUserShopMsg = tbUserShopMsgMapper.selectByShopId(productSku.getShopId());
|
||||
wechatUtil.sendStockWarnMsg(shopInfo.getShopName(), product.getName(),
|
||||
product.getIsDistribute() == 1 ? productSku.getStockNumber().toString() : product.getStockNumber().toString(),
|
||||
"耗材库存不足,请及时补充。", tbUserShopMsg.getOpenId());
|
||||
}
|
||||
}
|
||||
|
||||
private TbCashierCart addCart(String productId, String skuId, Integer userId, Integer num, String tableId, String shopId) throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user