商品模块代码提交

This commit is contained in:
Tankaikai
2025-03-01 15:16:58 +08:00
parent fd257f1b3b
commit 3f823919bf
9 changed files with 192 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
package com.czg.service.order.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import com.czg.account.vo.HandoverCategoryListVo;
import com.czg.account.vo.HandoverProductListVo;
import com.czg.exception.CzgException;
@@ -11,6 +12,7 @@ import com.czg.product.service.ProductRpcService;
import com.czg.service.order.mapper.OrderDetailMapper;
import com.czg.service.order.mapper.OrderInfoMapper;
import com.mybatisflex.core.query.QueryWrapper;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
@@ -105,6 +107,7 @@ public class OrderInfoRpcServiceImpl implements OrderInfoRpcService {
}
@Override
@GlobalTransactional
@Transactional(rollbackFor = Exception.class)
public void paySuccessCallback(Long orderId) {
// 下单后商品库存扣减,耗材扣减,流水记录
@@ -124,12 +127,44 @@ public class OrderInfoRpcServiceImpl implements OrderInfoRpcService {
Map<String, Object> data = new HashMap<>(16);
Long productId = orderDetail.getProductId();
BigDecimal num = orderDetail.getNum();
BigDecimal refundNum = orderDetail.getRefundNum();
data.put("shopId", shopId);
data.put("productId", productId);
data.put("num", num);
data.put("num", NumberUtil.sub(num, refundNum));
dataList.add(data);
}
// 调用商品服务扣减库存
productRpcService.paySuccessSubtractStock(orderId, dataList);
}
@Override
@GlobalTransactional
@Transactional(rollbackFor = Exception.class)
public void orderCancelCallback(Long orderId) {
// 订单取消后商品库存恢复,耗材恢复,流水记录
OrderInfo orderInfo = orderInfoMapper.selectOneById(orderId);
if (orderInfo == null) {
throw new CzgException("订单不存在");
}
// 查询订单下商品
List<OrderDetail> detailList = orderDetailMapper.selectListByQuery(QueryWrapper.create().eq(OrderDetail::getOrderId, orderId));
if (CollUtil.isEmpty(detailList)) {
throw new CzgException("该订单下不存在商品");
}
Long shopId = orderInfo.getShopId();
// 封装扣减库存数据
List<Map<String, Object>> dataList = new ArrayList<>();
for (OrderDetail orderDetail : detailList) {
Map<String, Object> data = new HashMap<>(16);
Long productId = orderDetail.getProductId();
BigDecimal num = orderDetail.getNum();
BigDecimal refundNum = orderDetail.getRefundNum();
data.put("shopId", shopId);
data.put("productId", productId);
data.put("num", NumberUtil.sub(num, refundNum));
dataList.add(data);
}
// 调用商品服务恢复库存
productRpcService.orderCancelRecoverStock(orderId, dataList);
}
}

View File

@@ -36,5 +36,5 @@ public interface ProductMapper extends BaseMapper<Product> {
List<Product> selectCouponProBySaleNum();
void updateProductStockNum(@Param("id") Long id, @Param("shopId") Long shopId, @Param("subtractNum") BigDecimal subtractNum);
void updateProductStockNum(@Param("id") Long id, @Param("shopId") Long shopId, @Param("num") String type, @Param("num") BigDecimal num);
}

View File

@@ -56,7 +56,7 @@ public class ProductRpcServiceImpl implements ProductRpcService {
return;
}
for (ProductStockSubtractDTO dto : list) {
productMapper.updateProductStockNum(dto.getProductId(), dto.getShopId(), dto.getNum());
productMapper.updateProductStockNum(dto.getProductId(), dto.getShopId(), "sub", dto.getNum());
// 查询商品绑定耗材信息
List<ProdConsRelation> relationList = prodConsRelationMapper.selectListByQuery(QueryWrapper.create().eq(ProdConsRelation::getProductId, dto.getProductId()));
if (CollUtil.isEmpty(relationList)) {
@@ -83,7 +83,6 @@ public class ProductRpcServiceImpl implements ProductRpcService {
// 插入耗材流水记录
Long shopId = consInfo.getShopId();
ConsStockFlow consStockFlow = new ConsStockFlow();
consStockFlow.setId(0L);
consStockFlow.setShopId(shopId);
consStockFlow.setVendorId(null);
consStockFlow.setInOutType(InOutTypeEnum.OUT.value());
@@ -107,4 +106,62 @@ public class ProductRpcServiceImpl implements ProductRpcService {
}
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void orderCancelRecoverStock(Long orderId, List<Map<String, Object>> dataList) {
List<ProductStockSubtractDTO> list = BeanUtil.copyToList(dataList, ProductStockSubtractDTO.class);
if (CollUtil.isEmpty(list)) {
return;
}
for (ProductStockSubtractDTO dto : list) {
productMapper.updateProductStockNum(dto.getProductId(), dto.getShopId(), "add", dto.getNum());
// 查询商品绑定耗材信息
List<ProdConsRelation> relationList = prodConsRelationMapper.selectListByQuery(QueryWrapper.create().eq(ProdConsRelation::getProductId, dto.getProductId()));
if (CollUtil.isEmpty(relationList)) {
continue;
}
for (ProdConsRelation prodConsRelation : relationList) {
// 耗材id
Long consInfoId = prodConsRelation.getConsInfoId();
// 耗材消耗数量
BigDecimal surplusStock = prodConsRelation.getSurplusStock();
if (surplusStock == null) {
continue;
}
// 实际消耗数量 = 耗材消耗数量 * 商品购买数量
surplusStock = NumberUtil.mul(surplusStock, dto.getNum());
ConsInfo consInfo = consInfoMapper.selectOneById(consInfoId);
if (consInfo == null) {
continue;
}
BigDecimal stockNumber = consInfo.getStockNumber();
consInfo.setStockNumber(NumberUtil.add(stockNumber, surplusStock));
// 更新耗材库存数量
consInfoMapper.update(consInfo);
// 插入耗材流水记录
Long shopId = consInfo.getShopId();
ConsStockFlow consStockFlow = new ConsStockFlow();
consStockFlow.setShopId(shopId);
consStockFlow.setInOutType(InOutTypeEnum.IN.value());
consStockFlow.setInOutItem(InOutItemEnum.ORDER_IN.value());
consStockFlow.setInOutDate(LocalDate.now());
consStockFlow.setConId(consInfo.getId());
consStockFlow.setConName(consInfo.getConName());
consStockFlow.setUnitName(consInfo.getConUnit());
consStockFlow.setBeforeNumber(stockNumber);
consStockFlow.setInOutNumber(surplusStock);
consStockFlow.setAfterNumber(consInfo.getStockNumber());
consStockFlow.setSubTotal(NumberUtil.mul(surplusStock, consInfo.getPrice()));
consStockFlow.setProductId(dto.getProductId());
//consStockFlow.setSkuId(0L);
consStockFlow.setOrderId(orderId);
Long createUserId = StpKit.USER.getLoginIdAsLong();
String createUserName = StpKit.USER.getAccount();
consStockFlow.setCreateUserId(createUserId);
consStockFlow.setCreateUserName(createUserName);
consStockFlowMapper.insert(consStockFlow);
}
}
}
}

View File

@@ -161,7 +161,12 @@
</select>
<update id="updateProductStockNum">
update tb_product
set stock_number = stock_number - #{subtractNum}
<if test="type == 'add">
set stock_number = stock_number + #{num}
</if>
<if test="type == 'sub">
set stock_number = stock_number - #{num}
</if>
where id = #{id}
and is_stock = 1
and shop_id = #{shopId}