From b32b24a608f3c0f181b155d8c1b75b5ed815d73a Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Tue, 2 Jul 2024 16:40:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=A2=E5=8D=95=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=E8=80=97=E6=9D=90=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/RedisKeyExpirationListener.java | 13 +++++++++++- .../cashier/cons/rabbit/RabbitConstants.java | 8 +++++++ .../impl/order/TbOrderInfoServiceImpl.java | 21 ++++++++++++++++++- .../TbProductStocktakinServiceImpl.java | 7 ++++--- 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 eladmin-system/src/main/java/cn/ysk/cashier/cons/rabbit/RabbitConstants.java diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java b/eladmin-system/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java index 3d6fcf4e..b7c97a4d 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/config/RedisKeyExpirationListener.java @@ -1,10 +1,13 @@ package cn.ysk.cashier.config; +import cn.ysk.cashier.cons.rabbit.RabbitConstants; import cn.ysk.cashier.service.order.TbOrderInfoService; import cn.ysk.cashier.service.shop.TbShopInfoService; import cn.ysk.cashier.system.service.UserService; import cn.ysk.cashier.utils.CacheKey; import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -18,6 +21,7 @@ import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.UUID; @Slf4j @Configuration @@ -35,6 +39,12 @@ public class RedisKeyExpirationListener implements MessageListener { @Autowired private TbOrderInfoService tbOrderInfoService; + private final RabbitTemplate rabbitTemplate; + + public RedisKeyExpirationListener(RabbitTemplate rabbitTemplate) { + this.rabbitTemplate = rabbitTemplate; + } + //redis key失效监听 @Bean public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) { @@ -55,10 +65,11 @@ public class RedisKeyExpirationListener implements MessageListener { log.info("商户到期 账户名为:{}",account); shopInfoService.update(account); userServiceu.upEnableByusername(account); + }else if(expiredKey.startsWith(CacheKey.ORDER_EXPIRED)){ log.info("监听到订单过期,{}",expiredKey); String orderId = expiredKey.substring(CacheKey.ORDER_EXPIRED.length()); tbOrderInfoService.expired(orderId); } } -} \ No newline at end of file +} diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/cons/rabbit/RabbitConstants.java b/eladmin-system/src/main/java/cn/ysk/cashier/cons/rabbit/RabbitConstants.java new file mode 100644 index 00000000..e83b22dd --- /dev/null +++ b/eladmin-system/src/main/java/cn/ysk/cashier/cons/rabbit/RabbitConstants.java @@ -0,0 +1,8 @@ +package cn.ysk.cashier.cons.rabbit; + +public interface RabbitConstants { + String CONS_COLLECT_PUT="cons_collect_put"; + String CONS_COLLECT_ROUTINGKEY_PUT = "cons_collect_routingkey_put"; + + +} diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/order/TbOrderInfoServiceImpl.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/order/TbOrderInfoServiceImpl.java index 74b45a39..2d1cf241 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/order/TbOrderInfoServiceImpl.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/order/TbOrderInfoServiceImpl.java @@ -1,6 +1,7 @@ package cn.ysk.cashier.service.impl.order; import cn.hutool.core.util.ObjectUtil; +import cn.ysk.cashier.cons.rabbit.RabbitConstants; import cn.ysk.cashier.dto.order.TbOrderInfoDto; import cn.ysk.cashier.dto.order.TbOrderInfoQueryCriteria; import cn.ysk.cashier.dto.order.TbPayCountQueryCriteria; @@ -24,10 +25,13 @@ import cn.ysk.cashier.thirdpay.service.ThirdPayService; import cn.ysk.cashier.utils.*; import cn.ysk.cashier.vo.TbOrderInfoVo; import cn.ysk.cashier.vo.TbOrderPayCountVo; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.*; @@ -66,6 +70,7 @@ public class TbOrderInfoServiceImpl implements TbOrderInfoService { private final RedisUtils redisUtils; private final ThirdPayService thirdPayService; private final TbCashierCartRepository cartRepository; + private final RabbitTemplate rabbitTemplate; @Value("${thirdPay.url}") private String url; @@ -232,11 +237,20 @@ public class TbOrderInfoServiceImpl implements TbOrderInfoService { @Override @Transactional(rollbackFor = Exception.class) public void expired(String orderId) { + //修改耗材数据 + JSONObject jsonObject1=new JSONObject(); + jsonObject1.put("type","delete"); + Optional byId = tbOrderInfoRepository.findById(Integer.valueOf(orderId)); if (byId != null && byId.isPresent()) { TbOrderInfo tbOrderInfo = byId.get(); if (tbOrderInfo.getStatus().equals("unpaid")) { upOrderStatus(tbOrderInfo); + jsonObject1.put("orderId",tbOrderInfo.getId()); + + // 发送取消订单mq消息 + rabbitTemplate.convertAndSend(RabbitConstants.CONS_COLLECT_PUT, RabbitConstants.CONS_COLLECT_ROUTINGKEY_PUT, + jsonObject1.toJSONString(), new CorrelationData(UUID.randomUUID().toString())); } else if (tbOrderInfo.getStatus().equals("paying") && (StringUtils.isBlank(tbOrderInfo.getPayType()) || !tbOrderInfo.getPayType().equals("deposit"))) { @@ -249,6 +263,11 @@ public class TbOrderInfoServiceImpl implements TbOrderInfoService { redisUtils.set(CacheKey.ORDER_EXPIRED + tbOrderInfo.getId(), tbOrderInfo.getId(), 60 * 2); } else { upOrderStatus(tbOrderInfo); + jsonObject1.put("orderId",tbOrderInfo.getId()); + + // 发送取消订单mq消息 + rabbitTemplate.convertAndSend(RabbitConstants.CONS_COLLECT_PUT, RabbitConstants.CONS_COLLECT_ROUTINGKEY_PUT, + jsonObject1.toJSONString(), new CorrelationData(UUID.randomUUID().toString())); } } } @@ -420,4 +439,4 @@ public class TbOrderInfoServiceImpl implements TbOrderInfoService { } FileUtil.downloadExcel(list, response); } -} \ No newline at end of file +} diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/productimpl/TbProductStocktakinServiceImpl.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/productimpl/TbProductStocktakinServiceImpl.java index 6e1e8cd5..2f953ab9 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/productimpl/TbProductStocktakinServiceImpl.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/productimpl/TbProductStocktakinServiceImpl.java @@ -94,7 +94,7 @@ public class TbProductStocktakinServiceImpl implements TbProductStocktakinServic // 查询单位 TbShopUnit tbShopUnit = tbShopUnitRepository.findById(product.getUnitId()).orElse(null); - int phaseNum = productStocktakinDTO.getStocktakinNum() - product.getStockNumber(); + int phaseNum; long times = Instant.now().toEpochMilli(); TbProductStockOperate stockOperate = new TbProductStockOperate(); @@ -125,12 +125,12 @@ public class TbProductStocktakinServiceImpl implements TbProductStocktakinServic snap.put("coverImg", product.getCoverImg()); snap.put("productId", product.getId()); snap.put("name", product.getName()); - snap.put("number", phaseNum); snap.put("unitName", tbShopUnit == null ? null : tbShopUnit.getName()); // snap.put("specSnap", tbShopUnit == null ? null : tbShopUnit.getName()); // 共享库存 if (product.getIsDistribute() == 1) { + phaseNum = productStocktakinDTO.getStocktakinNum() - product.getStockNumber(); round = (int) Math.floor( product.getStockNumber()); productStocktakin.setPhasePrice( productStocktakinDTO.getPrice().multiply(BigDecimal.valueOf(phaseNum)) @@ -148,13 +148,13 @@ public class TbProductStocktakinServiceImpl implements TbProductStocktakinServic snap.put("stockNumber", product.getStockNumber()); }else { - if (productStocktakinDTO.getSkuId() == null) { throw new BadRequestException("非共享库存必须传入skuId"); } TbProductSku productSku = producSkutMapper.selectByProIdAndId(productStocktakinDTO.getSkuId(), productStocktakinDTO.getProductId()); + phaseNum = (int) (productStocktakinDTO.getStocktakinNum() - productSku.getStockNumber()); productStocktakinDTO.setPrice(productSku.getSalePrice()); @@ -177,6 +177,7 @@ public class TbProductStocktakinServiceImpl implements TbProductStocktakinServic snap.put("stockNumber", productSku.getStockNumber()); } + snap.put("number", phaseNum); productStocktakin.setPrice(productStocktakinDTO.getPrice()); productStockDetail.setStockNumber((double) phaseNum); productStockDetail.setCostAmount(productStocktakinDTO.getPrice().multiply(BigDecimal.valueOf(Math.abs(phaseNum))));