diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/ShopSongController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/ShopSongController.java index 22b2839..ab1412d 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/ShopSongController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/ShopSongController.java @@ -1,5 +1,6 @@ package com.chaozhanggui.system.cashierservice.controller; +import com.chaozhanggui.system.cashierservice.annotation.LimitSubmit; import com.chaozhanggui.system.cashierservice.entity.dto.SongOrderDTO; import com.chaozhanggui.system.cashierservice.service.TbShopSongService; import com.chaozhanggui.system.cashierservice.sign.Result; @@ -80,6 +81,7 @@ public class ShopSongController { * @param songOrderDTO 订单信息 * @return result */ + @LimitSubmit(key = "song_order_pay:#openId") @PostMapping public Result createOrder( @RequestHeader("openId") String openId, diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductMapper.java index b4c5795..491fdce 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductMapper.java @@ -19,6 +19,7 @@ public interface TbProductMapper { TbProduct selectById(Integer id); List selectByIdIn(@Param("ids") String ids); + List selectByIdInAndCheck(@Param("ids") String ids); List selectByIds(@Param("list") List ids); Integer selectByQcode(@Param("code") String code,@Param("productId") Integer productId,@Param("shopId") String shopId); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbShopSongOrderMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbShopSongOrderMapper.java index 4af4ca5..8664534 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbShopSongOrderMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbShopSongOrderMapper.java @@ -35,7 +35,7 @@ public interface TbShopSongOrderMapper { " LEFT JOIN tb_shop_song AS b ON a.song_id = b.id\n" + " WHERE\n" + " a.open_id = #{openId} and a.shop_id=#{shopId} " + - " AND a.state = #{state}") + " AND a.state = #{state} order by a.state asc, a.create_time desc") List> selectByUserId(@Param("openId") String openId, @Param("state") Integer state, Integer shopId); @Select(" SELECT\n" + @@ -61,7 +61,7 @@ public interface TbShopSongOrderMapper { " tb_shop_song_order AS a\n" + " LEFT JOIN tb_shop_song AS b ON a.song_id = b.id\n" + " WHERE\n" + - " a.open_id = #{openId} and a.state in (1, 2, 3) and a.shop_id=#{shopId}") + " a.open_id = #{openId} and a.state in (1, 2, 3) and a.shop_id=#{shopId} order by a.state asc, a.create_time desc") List> selectActiveOrderByUserId(@Param("openId") String openId, Integer shopId); @Select("SELECT a.song_name songName, a.id, b.img FROM `tb_shop_song_order` as a left join tb_shop_song as b on a.song_id=b.id where state=2 limit 1") diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/RabbitConstants.java b/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/RabbitConstants.java index b805fd1..01a8b62 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/RabbitConstants.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/RabbitConstants.java @@ -59,4 +59,7 @@ public interface RabbitConstants { public static final String CONS_MSG_COLLECT_ROUTINGKEY_PUT = "cons_msg_collect_routingkey_put"; + // 库存记录交换机 + String EXCHANGE_STOCK_RECORD = "exchange.stock.record"; + String ROUTING_STOCK_RECORD_SALE = "routing.stock.record.sale"; } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/redis/RedisCst.java b/src/main/java/com/chaozhanggui/system/cashierservice/redis/RedisCst.java index 398473b..d305be3 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/redis/RedisCst.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/redis/RedisCst.java @@ -25,4 +25,5 @@ public class RedisCst { // 创建订单锁 public static final String CREATE_ORDER_LOCK = "CREATE_ORDER_LOCK:"; public static final String SEND_STOCK_WARN_MSG = "SEND_STOCK_WARN_MSG:"; + public static final String SONG_PAY_LOCK = "song_pay_lock:"; } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java index a6375d4..b28db1d 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java @@ -147,6 +147,8 @@ public class PayService { TbActivateMapper tbActivateMapper; private final TbShopSongOrderService shopSongOrderService; + @Autowired + private MQUtils mQUtils; public PayService(@Qualifier("tbShopSongOrderServiceImpl") TbShopSongOrderService shopSongOrderService) { this.shopSongOrderService = shopSongOrderService; @@ -268,7 +270,9 @@ public class PayService { } PublicResp publicResp = thirdPayService.scanpay(thirdUrl, thirdApply.getAppId(), - reqbody, reqbody, payment.getAmount().setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).longValue(), "WECHAT", thirdApply.getSmallAppid(), openId, ip, DateUtils.getsdfTimesSS(), thirdApply.getStoreId(), callFSTBack, null, thirdApply.getAppToken()); + reqbody, reqbody, + payment.getAmount().setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).longValue(), + "WECHAT", thirdApply.getSmallAppid(), openId, ip, DateUtils.getsdfTimesSS(), thirdApply.getStoreId(), callFSTBack, null, thirdApply.getAppToken()); if (ObjectUtil.isNotNull(publicResp) && ObjectUtil.isNotEmpty(publicResp)) { if ("000000".equals(publicResp.getCode())) { WxScanPayResp wxScanPayResp = publicResp.getObjData(); @@ -878,6 +882,12 @@ public class PayService { producer.printMechine(orderInfo.getId() + ""); sendOrderToClient(orderInfo); redisUtil.deleteByKey(RedisCst.ORDER_EXPIRED.concat(orderInfo.getId().toString())); + + // 发送mq消息并保存库存记录 + JSONObject data = new JSONObject(); + data.put("orderId", orderInfo.getId()); + data.put("plat", "miniApp"); + mQUtils.sendStockSaleMsg(data); return "SUCCESS"; } @@ -923,6 +933,12 @@ public class PayService { producer.printCoupons(coupons.toJSONString()); sendOrderToClient(orderInfo); redisUtil.deleteByKey(RedisCst.ORDER_EXPIRED.concat(orderInfo.getId().toString())); + + // 发送mq消息并保存库存记录 + JSONObject data = new JSONObject(); + data.put("orderId", orderInfo.getId()); + data.put("plat", "miniApp"); + mQUtils.sendStockSaleMsg(data); return "SUCCESS"; } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java index 70a3ee6..db0eb46 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java @@ -138,7 +138,8 @@ public class ProductService { if (ObjectUtil.isNotEmpty(in) && ObjectUtil.isNotNull(in)) { - List products = tbProductMapper.selectByIdIn(in); +// List products = tbProductMapper.selectByIdIn(in); + List products = tbProductMapper.selectByIdInAndCheck(in); if (ObjectUtil.isNotEmpty(products) && products.size() > 0) { products.parallelStream().forEach(it -> { Integer sum = tbProductMapper.selectByQcode(code, it.getId(), it.getShopId()); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbShopSongServiceImpl.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbShopSongServiceImpl.java index 65b53aa..d0c1eb4 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbShopSongServiceImpl.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/impl/TbShopSongServiceImpl.java @@ -9,6 +9,7 @@ import com.chaozhanggui.system.cashierservice.entity.TbShopSong; import com.chaozhanggui.system.cashierservice.entity.TbShopSongOrder; import com.chaozhanggui.system.cashierservice.entity.dto.SongOrderDTO; import com.chaozhanggui.system.cashierservice.exception.MsgException; +import com.chaozhanggui.system.cashierservice.redis.RedisCst; import com.chaozhanggui.system.cashierservice.service.PayService; import com.chaozhanggui.system.cashierservice.service.TbShopSongOrderService; import com.chaozhanggui.system.cashierservice.service.TbShopSongService; diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/util/MQUtils.java b/src/main/java/com/chaozhanggui/system/cashierservice/util/MQUtils.java new file mode 100644 index 0000000..d7178fd --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/util/MQUtils.java @@ -0,0 +1,26 @@ +package com.chaozhanggui.system.cashierservice.util; + +import com.alibaba.fastjson.JSONObject; +import com.chaozhanggui.system.cashierservice.rabbit.RabbitConstants; +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class MQUtils { + private final RabbitTemplate rabbitTemplate; + + public MQUtils(RabbitTemplate rabbitTemplate) { + this.rabbitTemplate = rabbitTemplate; + } + + private void sendMsg(String exchange, String routingKey, T data, String note) { + log.info("开始发送{}mq消息, exchange: {}, routingKey: {}, data: {}", note, exchange, routingKey, data); + rabbitTemplate.convertAndSend(exchange, routingKey, JSONObject.toJSONString(data)); + } + + public void sendStockSaleMsg(T data) { + sendMsg(RabbitConstants.EXCHANGE_STOCK_RECORD, RabbitConstants.ROUTING_STOCK_RECORD_SALE, data, "商品售出增加库存记录"); + } +} diff --git a/src/main/resources/mapper/TbProductMapper.xml b/src/main/resources/mapper/TbProductMapper.xml index c4333ed..f37e11e 100644 --- a/src/main/resources/mapper/TbProductMapper.xml +++ b/src/main/resources/mapper/TbProductMapper.xml @@ -928,6 +928,15 @@ order by tb.sort asc + +