客户端增加扣减库存

This commit is contained in:
19991905653
2024-04-09 17:24:44 +08:00
parent bc4c3e6975
commit 49ed813f63
5 changed files with 86 additions and 19 deletions

View File

@@ -29,4 +29,8 @@ public interface TbProductSkuMapper {
TbProductSkuWithBLOBs selectByProduct(@Param("productId") Integer productId); TbProductSkuWithBLOBs selectByProduct(@Param("productId") Integer productId);
void updateByskuId(@Param("productSkuId") Integer productSkuId,@Param("num") Integer num); void updateByskuId(@Param("productSkuId") Integer productSkuId,@Param("num") Integer num);
void updateByskuIdSub(@Param("skuId")Integer skuId, @Param("num")Integer num);
void updateStockNum(@Param("skuId") Integer skuId, @Param("num") Integer num);
} }

View File

@@ -13,10 +13,12 @@ import com.chaozhanggui.system.cashierservice.sign.CodeEnum;
import com.chaozhanggui.system.cashierservice.sign.Result; import com.chaozhanggui.system.cashierservice.sign.Result;
import com.chaozhanggui.system.cashierservice.util.*; import com.chaozhanggui.system.cashierservice.util.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
@@ -44,10 +46,13 @@ public class DutyService {
private TbProductSkuMapper productSkuMapper; private TbProductSkuMapper productSkuMapper;
@Autowired @Autowired
private CloudPrinterService cloudPrinterService; private CloudPrinterService cloudPrinterService;
@Autowired
private RedisUtil redisUtil;
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void exect(String message) { public void exect(String message) {
try { try {
System.out.println("数据落地开始:"+message); System.out.println("数据落地开始:" + message);
JSONObject jsonObject = JSON.parseObject(message); JSONObject jsonObject = JSON.parseObject(message);
String token = jsonObject.getString("token"); String token = jsonObject.getString("token");
String type = jsonObject.getString("type"); String type = jsonObject.getString("type");
@@ -69,7 +74,7 @@ public class DutyService {
} }
List<TbOrderDetail> list = orderDetailMapper.selectAllByOrderId(orderId); List<TbOrderDetail> list = orderDetailMapper.selectAllByOrderId(orderId);
// ShopUserDuty shopUserDuty = shopUserDutyMapper.selectByTokenId(tokenId); // ShopUserDuty shopUserDuty = shopUserDutyMapper.selectByTokenId(tokenId);
ShopUserDuty shopUserDuty = shopUserDutyMapper.selectByShopIdAndTrade(shopId,day); ShopUserDuty shopUserDuty = shopUserDutyMapper.selectByShopIdAndTrade(shopId, day);
// ShopUserDuty shopUserDuty = shopUserDutyMapper.selectByTokenIdAndTradeDay(tokenId, day, orderInfo.getShopId()); // ShopUserDuty shopUserDuty = shopUserDutyMapper.selectByTokenIdAndTradeDay(tokenId, day, orderInfo.getShopId());
BigDecimal cashAmount = BigDecimal.ZERO; BigDecimal cashAmount = BigDecimal.ZERO;
@@ -95,7 +100,8 @@ public class DutyService {
shopUserDutyDetail.setProductId(orderDetail.getProductId()); shopUserDutyDetail.setProductId(orderDetail.getProductId());
shopUserDutyDetail.setProductName(orderDetail.getProductName()); shopUserDutyDetail.setProductName(orderDetail.getProductName());
detaiList.add(shopUserDutyDetail); detaiList.add(shopUserDutyDetail);
productSkuMapper.updateByskuId(orderDetail.getProductSkuId(),orderDetail.getNum()); // productSkuMapper.updateByskuId(orderDetail.getProductSkuId(), orderDetail.getNum());
subInventory(shopId,orderDetail.getProductSkuId(),orderDetail.getNum());
} }
if (detaiList.size() > 0) { if (detaiList.size() > 0) {
shopUserDutyDetailMapper.batchInsert(detaiList); shopUserDutyDetailMapper.batchInsert(detaiList);
@@ -135,18 +141,19 @@ public class DutyService {
detaiList.add(shopUserDutyDetail); detaiList.add(shopUserDutyDetail);
} }
productSkuMapper.updateByskuId(orderDetail.getProductSkuId(),orderDetail.getNum()); // productSkuMapper.updateByskuId(orderDetail.getProductSkuId(), orderDetail.getNum());
subInventory(shopId,orderDetail.getProductSkuId(),orderDetail.getNum());
} }
if (detaiList.size() > 0) { if (detaiList.size() > 0) {
shopUserDutyDetailMapper.batchInsert(detaiList); shopUserDutyDetailMapper.batchInsert(detaiList);
} }
} }
ShopUserDutyPay shopUserDutyPay = shopUserDutyPayMapper.selectByDuctIdAndType(shopUserDuty.getId(),orderInfo.getPayType()); ShopUserDutyPay shopUserDutyPay = shopUserDutyPayMapper.selectByDuctIdAndType(shopUserDuty.getId(), orderInfo.getPayType());
if (Objects.nonNull(shopUserDutyPay)){ if (Objects.nonNull(shopUserDutyPay)) {
shopUserDutyPay.setAmount(orderInfo.getOrderAmount().add(shopUserDutyPay.getAmount())); shopUserDutyPay.setAmount(orderInfo.getOrderAmount().add(shopUserDutyPay.getAmount()));
shopUserDutyPayMapper.updateByPrimaryKeySelective(shopUserDutyPay); shopUserDutyPayMapper.updateByPrimaryKeySelective(shopUserDutyPay);
}else { } else {
shopUserDutyPay=new ShopUserDutyPay(); shopUserDutyPay = new ShopUserDutyPay();
shopUserDutyPay.setDutyId(shopUserDuty.getId()); shopUserDutyPay.setDutyId(shopUserDuty.getId());
shopUserDutyPay.setType(orderInfo.getPayType()); shopUserDutyPay.setType(orderInfo.getPayType());
shopUserDutyPay.setAmount(orderInfo.getOrderAmount()); shopUserDutyPay.setAmount(orderInfo.getOrderAmount());
@@ -204,7 +211,7 @@ public class DutyService {
shopUserDutyDetail.setProductId(orderDetail.getProductId()); shopUserDutyDetail.setProductId(orderDetail.getProductId());
shopUserDutyDetail.setProductName(orderDetail.getProductName()); shopUserDutyDetail.setProductName(orderDetail.getProductName());
detaiList.add(shopUserDutyDetail); detaiList.add(shopUserDutyDetail);
productSkuMapper.updateByskuId(orderDetail.getProductSkuId(),orderDetail.getNum()); productSkuMapper.updateByskuId(orderDetail.getProductSkuId(), orderDetail.getNum());
} }
if (detaiList.size() > 0) { if (detaiList.size() > 0) {
shopUserDutyDetailMapper.batchInsert(detaiList); shopUserDutyDetailMapper.batchInsert(detaiList);
@@ -244,35 +251,45 @@ public class DutyService {
detaiList.add(shopUserDutyDetail); detaiList.add(shopUserDutyDetail);
} }
productSkuMapper.updateByskuId(orderDetail.getProductSkuId(),orderDetail.getNum()); productSkuMapper.updateByskuId(orderDetail.getProductSkuId(), orderDetail.getNum());
} }
if (detaiList.size() > 0) { if (detaiList.size() > 0) {
shopUserDutyDetailMapper.batchInsert(detaiList); shopUserDutyDetailMapper.batchInsert(detaiList);
} }
} }
ShopUserDutyPay shopUserDutyPay = shopUserDutyPayMapper.selectByDuctIdAndType(shopUserDuty.getId(),orderInfo.getPayType()); ShopUserDutyPay shopUserDutyPay = shopUserDutyPayMapper.selectByDuctIdAndType(shopUserDuty.getId(), orderInfo.getPayType());
if (Objects.nonNull(shopUserDutyPay)){ if (Objects.nonNull(shopUserDutyPay)) {
shopUserDutyPay.setAmount(orderInfo.getOrderAmount().add(shopUserDutyPay.getAmount())); shopUserDutyPay.setAmount(orderInfo.getOrderAmount().add(shopUserDutyPay.getAmount()));
shopUserDutyPayMapper.updateByPrimaryKeySelective(shopUserDutyPay); shopUserDutyPayMapper.updateByPrimaryKeySelective(shopUserDutyPay);
}else { } else {
shopUserDutyPay=new ShopUserDutyPay(); shopUserDutyPay = new ShopUserDutyPay();
shopUserDutyPay.setDutyId(shopUserDuty.getId()); shopUserDutyPay.setDutyId(shopUserDuty.getId());
shopUserDutyPay.setType(orderInfo.getPayType()); shopUserDutyPay.setType(orderInfo.getPayType());
shopUserDutyPay.setAmount(orderInfo.getOrderAmount()); shopUserDutyPay.setAmount(orderInfo.getOrderAmount());
shopUserDutyPayMapper.insert(shopUserDutyPay); shopUserDutyPayMapper.insert(shopUserDutyPay);
} }
}else{ } else {
if (type.equals("close")){ if (type.equals("close")) {
JSONObject tokenJson = TokenUtil.parseParamFromToken(tbToken.getToken()); JSONObject tokenJson = TokenUtil.parseParamFromToken(tbToken.getToken());
Integer shopId = tokenJson.getInteger("shopId"); Integer shopId = tokenJson.getInteger("shopId");
Integer staffId = tokenJson.getInteger("staffId"); Integer staffId = tokenJson.getInteger("staffId");
cloudPrinterService.handoverprintData(token,day,""); cloudPrinterService.handoverprintData(token, day, "");
shopUserDutyMapper.updateStatusByTokenId(day,shopId,staffId); shopUserDutyMapper.updateStatusByTokenId(day, shopId, staffId);
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.getMessage(); e.getMessage();
} }
} }
private void subInventory(Integer shopId, Integer skuId, Integer num) {
String result = redisUtil.seckill(RedisCst.PRODUCT + shopId.toString() + ":" + skuId.toString(), num.toString());
TbProductSku tbProductSku = productSkuMapper.selectByPrimaryKey(skuId);
if (num > tbProductSku.getStockNumber()) {
productSkuMapper.updateStockNum(skuId,num);
} else {
productSkuMapper.updateByskuIdSub(skuId, num);
}
}
} }

View File

@@ -13,4 +13,5 @@ public class RedisCst {
public static final String ONLINE_USER = "ONLINE:USER"; public static final String ONLINE_USER = "ONLINE:USER";
public static final String CART = "CZG:CART:"; public static final String CART = "CZG:CART:";
public static final Object PRODUCT = "PRODUCT:";
} }

View File

@@ -577,4 +577,43 @@ public class RedisUtil {
} }
return 0L ; return 0L ;
} }
String secKillScript = "local prodid=KEYS[1];\r\n" +
"local usernum=KEYS[2];\r\n" +
"local num= redis.call(\"get\" ,prodid);\r\n" +
"if tonumber(num)<tonumber(usernum) then \r\n" +
" return 0;\r\n" +
"end\r\n" +
"if tonumber(num)<=0 then \r\n" +
" return 0;\r\n" +
"else \r\n" +
" redis.call(\"DECRBY\",prodid,tonumber(usernum));\r\n" +
"end\r\n" +
"return 1";
public String seckill(String key,String num) {
Jedis jedis = null;
try {
if (StringUtils.isEmpty(key)) {
return REDIS_FAILED+"";
}
// 从jedis池中获取一个jedis实例
jedis = pool.getResource();
if (database!=0) {
jedis.select(database);
}
Object result = jedis.eval(secKillScript, Arrays.asList( key,num), new ArrayList<>());
String reString = String.valueOf(result);
return reString;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放对象池即获取jedis实例使用后要将对象还回去
// 释放对象池即获取jedis实例使用后要将对象还回去
if (jedis != null) {
jedis.close();
}
}
return REDIS_FAILED+"";
}
} }

View File

@@ -336,8 +336,14 @@
<update id="updateByskuId"> <update id="updateByskuId">
update tb_product_sku set real_sales_number = real_sales_number + #{num} where id = #{productSkuId} update tb_product_sku set real_sales_number = real_sales_number + #{num} where id = #{productSkuId}
</update> </update>
<update id="updateByskuIdSub">
update tb_product_sku set stock_number = stock_number - #{num},real_sales_number = real_sales_number + #{num} where id = #{skuId}
</update>
<update id="updateStockNum" parameterType="java.lang.Integer">
update tb_product_sku set stock_number = 0 ,real_sales_number = real_sales_number + #{num} where id = #{skuId}
</update>
<select id="selectByShopIdAndProductIdAndSpec" resultMap="ResultMapWithBLOBs"> <select id="selectByShopIdAndProductIdAndSpec" resultMap="ResultMapWithBLOBs">
select * from tb_product_sku where shop_id=#{shopId} and product_id=#{productId} select * from tb_product_sku where shop_id=#{shopId} and product_id=#{productId}
<if test="spec != null and spec !=''"> <if test="spec != null and spec !=''">