商品模块代码提交
This commit is contained in:
parent
fd257f1b3b
commit
3f823919bf
|
|
@ -0,0 +1,61 @@
|
|||
package com.czg.mq;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import com.czg.config.RabbitConstants;
|
||||
import com.czg.order.entity.MqLog;
|
||||
import com.czg.order.service.MqLogService;
|
||||
import com.czg.order.service.OrderInfoRpcService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 订单mq消息处理器
|
||||
*
|
||||
* @author Administrator
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class OrderMqListener {
|
||||
|
||||
@Resource
|
||||
private OrderInfoRpcService orderInfoRpcService;
|
||||
@Resource
|
||||
private MqLogService mqLogService;
|
||||
|
||||
@RabbitListener(queues = {"${spring.profiles.active}-" + RabbitConstants.Queue.ORDER_STOCK_QUEUE})
|
||||
public void orderStockSubtract(String orderId) {
|
||||
long startTime = DateUtil.date().getTime();
|
||||
log.info("接收到订单支付成功扣减库存消息:{}", orderId);
|
||||
MqLog mqLog = new MqLog().setQueue(RabbitConstants.Queue.ORDER_STOCK_QUEUE).setMsg(orderId).setType("orderStockSubtract").setPlat("java.order").setCreateTime(DateUtil.date().toLocalDateTime());
|
||||
try {
|
||||
orderInfoRpcService.paySuccessCallback(Long.valueOf(orderId));
|
||||
} catch (Exception e) {
|
||||
log.error("订单支付成功扣减库存失败", e);
|
||||
String errorInfo = ExceptionUtil.stacktraceToString(e);
|
||||
mqLog.setErrInfo(errorInfo);
|
||||
mqLog.setDuration(DateUtil.date().getTime() - startTime);
|
||||
mqLog.setFailTime(DateUtil.date().toLocalDateTime());
|
||||
mqLogService.save(mqLog);
|
||||
}
|
||||
}
|
||||
|
||||
@RabbitListener(queues = {"${spring.profiles.active}-" + RabbitConstants.Queue.ORDER_CANCEL_QUEUE})
|
||||
public void orderStockRecover(String orderId) {
|
||||
long startTime = DateUtil.date().getTime();
|
||||
log.info("接收到订单取消恢复库存消息:{}", orderId);
|
||||
MqLog mqLog = new MqLog().setQueue(RabbitConstants.Queue.ORDER_CANCEL_QUEUE).setMsg(orderId).setType("orderStockRecover").setPlat("java.order").setCreateTime(DateUtil.date().toLocalDateTime());
|
||||
try {
|
||||
orderInfoRpcService.orderCancelCallback(Long.valueOf(orderId));
|
||||
} catch (Exception e) {
|
||||
log.error("订单取消恢复库存失败", e);
|
||||
String errorInfo = ExceptionUtil.stacktraceToString(e);
|
||||
mqLog.setErrInfo(errorInfo);
|
||||
mqLog.setDuration(DateUtil.date().getTime() - startTime);
|
||||
mqLog.setFailTime(DateUtil.date().toLocalDateTime());
|
||||
mqLogService.save(mqLog);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,12 +3,12 @@ package com.czg.mq;
|
|||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.czg.order.entity.MqLog;
|
||||
import com.czg.account.entity.PrintMachine;
|
||||
import com.czg.order.service.MqLogService;
|
||||
import com.czg.account.service.PrintMachineService;
|
||||
import com.czg.config.RabbitConstants;
|
||||
import com.czg.order.entity.MqLog;
|
||||
import com.czg.order.entity.OrderInfo;
|
||||
import com.czg.order.service.MqLogService;
|
||||
import com.czg.order.service.OrderInfoService;
|
||||
import com.czg.service.order.print.PrinterHandler;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
|
|
@ -24,6 +24,7 @@ import java.util.List;
|
|||
|
||||
/**
|
||||
* 打印mq消息处理器
|
||||
*
|
||||
* @author Administrator
|
||||
*/
|
||||
@Component
|
||||
|
|
@ -44,7 +45,7 @@ public class PrintMqListener {
|
|||
public void orderPrint(String orderId) {
|
||||
long startTime = DateUtil.date().getTime();
|
||||
log.info("接收到订单打印消息:{}", orderId);
|
||||
MqLog mqLog = new MqLog().setQueue(RabbitConstants.Queue.ORDER_MACHINE_PRINT_QUEUE).setMsg(orderId).setType("orderPrint").setPlat("java.account").setCreateTime(DateUtil.date().toLocalDateTime());
|
||||
MqLog mqLog = new MqLog().setQueue(RabbitConstants.Queue.ORDER_MACHINE_PRINT_QUEUE).setMsg(orderId).setType("orderPrint").setPlat("java.order").setCreateTime(DateUtil.date().toLocalDateTime());
|
||||
try {
|
||||
OrderInfo orderInfo = orderInfoService.getById(orderId);
|
||||
if (orderInfo == null) {
|
||||
|
|
@ -57,7 +58,7 @@ public class PrintMqListener {
|
|||
// printPlaceTicket(isReturn, machine, orderInfo, shopInfo);
|
||||
});
|
||||
|
||||
}catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
log.error("订单打印失败", e);
|
||||
mqLog.setErrInfo(JSONObject.toJSONString(e));
|
||||
mqLog.setDuration(DateUtil.date().getTime() - startTime);
|
||||
|
|
@ -68,10 +69,11 @@ public class PrintMqListener {
|
|||
|
||||
/**
|
||||
* 获取可用打印机
|
||||
* @param shopId 店铺id
|
||||
* @param subType 打印类型(分类)label标签 cash小票 kitchen出品
|
||||
*
|
||||
* @param shopId 店铺id
|
||||
* @param subType 打印类型(分类)label标签 cash小票 kitchen出品
|
||||
* @param printMethod 打印方式 all-全部打印 normal-仅打印结账单「前台」one-仅打印制作单「厨房」
|
||||
* @param printType 打印类型,JSON数组 refund-确认退款单 handover-交班单 queue-排队取号
|
||||
* @param printType 打印类型,JSON数组 refund-确认退款单 handover-交班单 queue-排队取号
|
||||
* @return 打印机列表
|
||||
*/
|
||||
private List<PrintMachine> getPrintMachine(Long shopId, String subType, String printMethod, String printType) {
|
||||
|
|
|
|||
|
|
@ -3,17 +3,16 @@ package com.czg.order.entity;
|
|||
import com.mybatisflex.annotation.Id;
|
||||
import com.mybatisflex.annotation.KeyType;
|
||||
import com.mybatisflex.annotation.Table;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 实体类。
|
||||
*
|
||||
|
|
@ -45,7 +44,7 @@ public class MqLog implements Serializable {
|
|||
private String msg;
|
||||
|
||||
/**
|
||||
* 消息类型 orderPrint订单打印
|
||||
* 消息类型 orderPrint订单打印 orderStockSubtract 订单库存扣减 orderStockRecover 订单库存恢复
|
||||
*/
|
||||
private String type;
|
||||
|
||||
|
|
|
|||
|
|
@ -140,4 +140,11 @@ public interface OrderInfoRpcService {
|
|||
* @param orderId 订单id
|
||||
*/
|
||||
void paySuccessCallback(Long orderId);
|
||||
|
||||
/**
|
||||
* 订单取消回调 恢复商品库存及耗材库存
|
||||
*
|
||||
* @param orderId 订单id
|
||||
*/
|
||||
void orderCancelCallback(Long orderId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,4 +18,12 @@ public interface ProductRpcService {
|
|||
* @param dataList 库存扣减数据
|
||||
*/
|
||||
void paySuccessSubtractStock(Long orderId, List<Map<String, Object>> dataList);
|
||||
|
||||
/**
|
||||
* 订单取消后恢复库存
|
||||
*
|
||||
* @param orderId 订单ID
|
||||
* @param dataList 库存恢复数据
|
||||
*/
|
||||
void orderCancelRecoverStock(Long orderId, List<Map<String, Object>> dataList);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
|
|
|
|||
Loading…
Reference in New Issue