From c88b085a4e352d832bd6e16371c4aa3c961fe663 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Thu, 4 Jul 2024 16:40:33 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=9C=AA=E5=BC=80=E5=90=AF=E5=BA=93?= =?UTF-8?q?=E5=AD=98=E4=B8=8D=E6=A0=A1=E9=AA=8C=E5=BA=93=E5=AD=98=E6=95=B0?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cashierservice/dao/TbProductMapper.java | 3 +++ .../dao/TbProductSkuMapper.java | 3 +++ .../cashierservice/service/CartService.java | 22 +++++++++++++++++-- .../service/ProductService.java | 12 ++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) 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 377ca01..e26f137 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductMapper.java @@ -40,4 +40,7 @@ public interface TbProductMapper { @Update("update tb_product set stock_number=stock_number-#{num} WHERE id=#{id} and stock_number-#{num} > 0") int decrStock(@Param("id") String id, @Param("num") Integer num); + + @Update("update tb_product set stock_number=stock_number-#{num} WHERE id=#{id}") + int decrStockUnCheck(@Param("id") String id, @Param("num") Integer num); } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductSkuMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductSkuMapper.java index 73a9a1d..0376a71 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductSkuMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductSkuMapper.java @@ -43,4 +43,7 @@ public interface TbProductSkuMapper { @Update("update tb_product_sku set stock_number=stock_number-#{num} WHERE id=#{id} and stock_number-#{num} > 0") int decrStock(@Param("id") String id, @Param("num") Integer num); + + @Update("update tb_product_sku set stock_number=stock_number-#{num} WHERE id=#{id}") + int decrStockUnCheck(@Param("id") String id, @Param("num") Integer num); } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java index 6d2fb0c..32fbde1 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -301,6 +301,8 @@ public class CartService { jsonObject1.put("status", "fail"); jsonObject1.put("msg", "该商品不存在"); jsonObject1.put("data", new ArrayList<>()); + jsonObject1.put("reqData", jsonObject); + PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true); log.error("该商品不存在 productId:{}", productId); throw new MsgException("该商品不存在"); @@ -314,6 +316,8 @@ public class CartService { jsonObject1.put("status", "fail"); jsonObject1.put("msg", "该商品已售罄"); jsonObject1.put("data", new ArrayList<>()); + jsonObject1.put("reqData", jsonObject); + PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true); log.error("该商品已售罄 productId:{}", productId); throw new MsgException("该商品已售罄"); @@ -325,6 +329,8 @@ public class CartService { jsonObject1.put("status", "fail"); jsonObject1.put("msg", "该商品已售罄"); jsonObject1.put("data", new ArrayList<>()); + jsonObject1.put("reqData", jsonObject); + PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true); log.error("该商品已售罄 productId:{}", productId); throw new MsgException("该商品已售罄"); @@ -387,6 +393,8 @@ public class CartService { jsonObject1.put("type", jsonObject.getString("type")); jsonObject1.put("data", jsonArray); jsonObject1.put("amount", amount); + jsonObject1.put("reqData", jsonObject); + PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), jsonObject.getString("tableId").concat("-").concat(shopId), "", false); } catch (Exception e) { log.error("长链接错误 createCart{}", e.getMessage()); @@ -553,7 +561,11 @@ public class CartService { log.info("开始修改库存,商品id:{},商品名:{}", tbProduct1.getId(), tbProduct1.getName()); // 修改库存 try { - productService.updateStock(tbProduct.getProductId(), tbProduct.getId(), cashierCart.getNumber(), tbProduct1.getIsDistribute() == 1); + if (tbProduct1.getIsStock() == 1) { + productService.updateStock(tbProduct.getProductId(), tbProduct.getId(), cashierCart.getNumber(), tbProduct1.getIsDistribute() == 1); + }else { + productService.updateStockAndNoCheck(tbProduct.getProductId(), tbProduct.getId(), cashierCart.getNumber(), tbProduct1.getIsDistribute() == 1); + } }catch (Exception e) { JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("status", "fail"); @@ -565,6 +577,12 @@ public class CartService { return; } + // 发送判断耗材是否耗尽消息 + JSONObject objectMsg=new JSONObject(); + objectMsg.put("skuId",Integer.valueOf(cashierCart.getSkuId())); + objectMsg.put("shopId",Integer.valueOf(cashierCart.getShopId())); + producer.con_msg(objectMsg.toString()); + totalAmount = totalAmount.add(cashierCart.getTotalAmount()); packAMount = packAMount.add(cashierCart.getPackFee()); originAmount = originAmount.add(cashierCart.getTotalAmount()); @@ -759,7 +777,7 @@ public class CartService { jsonObject2.put("orderId", orderInfo.getId()); jsonObject2.put("type", "create"); jsonObject2.put("cartId", cashierCart.getId()); - log.info("开始发送mq消息,消耗库存,消息内容:{}", jsonObject2); + log.info("开始发送mq消息,消耗耗材,消息内容:{}", jsonObject2); producer.cons(jsonObject2.toString()); } redisUtil.saveMessage(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId")).concat("-").concat(shopId), array.toJSONString()); 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 c890ee7..89edf00 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java @@ -465,4 +465,16 @@ public class ProductService { } } } + + public void updateStockAndNoCheck(String id, Integer skuId, Integer buyNum, boolean isDistribute) { + if (isDistribute) { + if (tbProductMapper.decrStockUnCheck(String.valueOf(id), buyNum) < 1) { + throw new MsgException("库存不足,下单失败"); + } + }else { + if (tbProductSkuMapper.decrStockUnCheck(String.valueOf(skuId), buyNum) < 1) { + throw new MsgException("库存不足,下单失败"); + } + } + } } From 3ea12c88fe6cc686832d92810f0f522255edd53e Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Fri, 5 Jul 2024 09:12:09 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=B4=AD=E7=89=A9?= =?UTF-8?q?=E8=BD=A6=E6=A0=A1=E9=AA=8C=E5=BA=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cashierservice/service/CartService.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java index 32fbde1..e9635e5 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -307,11 +307,26 @@ public class CartService { log.error("该商品不存在 productId:{}", productId); throw new MsgException("该商品不存在"); } + + Integer buyNum = jsonObject.getInteger("num"); + + if (buyNum <= 0) { + JSONObject jsonObject1 = new JSONObject(); + jsonObject1.put("status", "fail"); + jsonObject1.put("msg", "参数非法"); + jsonObject1.put("data", new ArrayList<>()); + jsonObject1.put("reqData", jsonObject); + + PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true); + log.error("参数非法 productId:{}", productId); + throw new MsgException("该商品已售罄"); + } + String skuId = jsonObject.getString("skuId"); if (tbProduct.getIsStock() == 1) { // 1:共享库存 0:独立库存 if (Integer.valueOf(tbProduct.getIsDistribute()).equals(1)) { - if (tbProduct.getIsPauseSale().equals(1)) {//是否售罄 + if (tbProduct.getIsPauseSale() == 1 || tbProduct.getStockNumber() - buyNum < 0) {//是否售罄 JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("status", "fail"); jsonObject1.put("msg", "该商品已售罄"); @@ -324,7 +339,7 @@ public class CartService { } } else { TbProductSkuWithBLOBs tbProductSkuWithBLOBs = productSkuMapper.selectByPrimaryKey(Integer.valueOf(skuId)); - if(tbProductSkuWithBLOBs.getIsPauseSale().equals(1)){//是否售罄 + if(tbProductSkuWithBLOBs.getIsPauseSale() == 1 || tbProductSkuWithBLOBs.getStockNumber() - buyNum < 0){//是否售罄 JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("status", "fail"); jsonObject1.put("msg", "该商品已售罄"); @@ -339,7 +354,6 @@ public class CartService { } JSONArray jsonArray = new JSONArray(); BigDecimal amount = BigDecimal.ZERO; - Integer buyNum = jsonObject.getInteger("num"); if (redisUtil.exists(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId").concat("-").concat(shopId)))) { JSONArray array = JSON.parseArray(redisUtil.getMessage(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId").concat("-").concat(shopId)))); From 6908d8eaf3641a09e35cdf2424b1c917778e9855 Mon Sep 17 00:00:00 2001 From: wangw <1594593906@qq.com> Date: Fri, 5 Jul 2024 09:51:42 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=2050MB?= =?UTF-8?q?=20=E8=BF=9B=E8=A1=8C=E4=B8=80=E6=AC=A1=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 12 +++++++++++- src/main/resources/application.yml | 7 ++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index b83d222..7425e2a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,11 @@ -/target/ +### IDEA ### +/target/**/* +/log/* +.idea/* +*.iml +*/target/* +*/*.iml +/.gradle/ +/application.pid +/target/* +**.jar \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d784451..9ef5971 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -59,18 +59,19 @@ logging: file: # 切记,该文件表示正在产出日志的日志文件。并不会打包,当文件大于max-file-size,会根据file-name-pattern格式打包 # 名称为log/cashier-client.log文件夹会在项目根目录下,打包后会在启动包同目录下;名称为/log/cashier-client.log的文件夹会在项目所在磁盘的跟目录下 - name: log/cashierService.log + name: wx.log logback: rollingpolicy: # 单文件的大小,默认10M, 超过之后打包成一个日志文件 - max-file-size: 10MB + max-file-size: 50MB # 日志保存的天数 max-history: 30 # 打包文件格式,默认: ${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz,书写格式为:文件路径/文件名.%i.文件后缀,其中%i不可省去,否则无日志显示 # 例如: 日期为:2021/11/5 ,则打包文件之后为: log/ota.2021-11-05.0.gz,0表示日志的第一部分,后续就是,1,2,3... # 如果是压缩包,里面会多一个名log/ota.2021-11-05.0的日志文件 # 如下面的例子,打包之后为: log/2021-11/cashier-client.2020-11-5.0.log,这是一个日志文件 - file-name-pattern: log/%d{yyyy-MM}/cashierService.%d{yyyy-MM-dd}.%i.log +# file-name-pattern: log/%d{yyyy-MM}/cashierService.%d{yyyy-MM-dd}.%i.log + file-name-pattern: log/%d{yyyy-MM}/cashierService.%d{yyyy-MM-dd}.%i.log.gz #阿里云相关配置 aliyun: keyid: LTAI5tPdEfYSZcqHbjCrtPRD From b236cac065247d1c70879e33083800bd487716c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E9=B9=8F=E8=BE=89?= <18322780655@163.com> Date: Fri, 5 Jul 2024 10:05:44 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=80=97=E6=9D=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/TbUserShopMsgMapper.java | 21 ++++----- .../cashierservice/entity/TbUserShopMsg.java | 12 ++++- .../cashierservice/service/LoginService.java | 10 +---- .../generator-mapper/generatorConfig.xml | 2 +- .../resources/mapper/TbUserShopMsgMapper.xml | 45 ++++++++++++------- 5 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbUserShopMsgMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbUserShopMsgMapper.java index c6f5a8a..425981a 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbUserShopMsgMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbUserShopMsgMapper.java @@ -1,36 +1,31 @@ package com.chaozhanggui.system.cashierservice.dao; import com.chaozhanggui.system.cashierservice.entity.TbUserShopMsg; +import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; -import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Component; -/** -* @author Administrator -* @description 针对表【tb_user_shop_msg】的数据库操作Mapper -* @createDate 2024-06-28 09:55:30 -* @Entity com.chaozhanggui.system.cashierservice.entity.TbUserShopMsg -*/ @Component @Mapper public interface TbUserShopMsgMapper { - int deleteByPrimaryKey(Integer shopId); - - int deleteByPrimaryKey(Long id); + int deleteByPrimaryKey(Integer id); int insert(TbUserShopMsg record); int insertSelective(TbUserShopMsg record); - TbUserShopMsg selectByPrimaryKey(Integer shopId); - TbUserShopMsg selectByPrimaryKey(Long id); + TbUserShopMsg selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(TbUserShopMsg record); int updateByPrimaryKey(TbUserShopMsg record); + @Select("select * from tb_user_shop_msg where shop_id=#{shopId}") TbUserShopMsg selectByShopId(@Param("shopId") String shopId); -} + + + TbUserShopMsg selectByShopIdAndOpenId(@Param("shopId") Integer shopId,@Param("openId") String openId); +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserShopMsg.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserShopMsg.java index 22c4663..e1bedc8 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserShopMsg.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserShopMsg.java @@ -4,6 +4,8 @@ import java.io.Serializable; import java.util.Date; public class TbUserShopMsg implements Serializable { + private Integer id; + private Integer shopId; private String openId; @@ -18,6 +20,14 @@ public class TbUserShopMsg implements Serializable { private static final long serialVersionUID = 1L; + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + public Integer getShopId() { return shopId; } @@ -65,4 +75,4 @@ public class TbUserShopMsg implements Serializable { public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } -} +} \ No newline at end of file diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java index a36fb42..3f87abb 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java @@ -65,22 +65,16 @@ public class LoginService { public Result wxBusinessLogin(String openId,String shopId){ - TbUserShopMsg shopMsg= tbUserShopMsgMapper.selectByPrimaryKey(Integer.valueOf(shopId)); + TbUserShopMsg shopMsg= tbUserShopMsgMapper.selectByShopIdAndOpenId(Integer.valueOf(shopId),openId); if(Objects.isNull(shopMsg)){ shopMsg=new TbUserShopMsg(); - shopMsg.setShopId(Integer.valueOf(shopId)); shopMsg.setOpenId(openId); shopMsg.setCreateTime(new Date()); shopMsg.setStatus("1"); - - tbUserShopMsgMapper.insert(shopMsg); - }else { - shopMsg.setOpenId(openId); - shopMsg.setUpdateTime(new Date()); - tbUserShopMsgMapper.updateByPrimaryKey(shopMsg); } + // 为商家绑定openid if (shopOpenIdMapper.countByOpenId(openId) == null) { TbShopOpenId shopOpenId = new TbShopOpenId(); diff --git a/src/main/resources/generator-mapper/generatorConfig.xml b/src/main/resources/generator-mapper/generatorConfig.xml index a6c8bea..007174a 100644 --- a/src/main/resources/generator-mapper/generatorConfig.xml +++ b/src/main/resources/generator-mapper/generatorConfig.xml @@ -52,7 +52,7 @@ -
diff --git a/src/main/resources/mapper/TbUserShopMsgMapper.xml b/src/main/resources/mapper/TbUserShopMsgMapper.xml index 25fdc4c..3220910 100644 --- a/src/main/resources/mapper/TbUserShopMsgMapper.xml +++ b/src/main/resources/mapper/TbUserShopMsgMapper.xml @@ -2,7 +2,8 @@ - + + @@ -10,29 +11,32 @@ - shop_id, open_id, remark, status, create_time, update_time + id, shop_id, open_id, remark, status, create_time, update_time delete from tb_user_shop_msg - where shop_id = #{shopId,jdbcType=INTEGER} + where id = #{id,jdbcType=INTEGER} - insert into tb_user_shop_msg (shop_id, open_id, remark, - status, create_time, update_time - ) - values (#{shopId,jdbcType=INTEGER}, #{openId,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, - #{status,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP} - ) + insert into tb_user_shop_msg (id, shop_id, open_id, + remark, status, create_time, + update_time) + values (#{id,jdbcType=INTEGER}, #{shopId,jdbcType=INTEGER}, #{openId,jdbcType=VARCHAR}, + #{remark,jdbcType=VARCHAR}, #{status,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, + #{updateTime,jdbcType=TIMESTAMP}) insert into tb_user_shop_msg + + id, + shop_id, @@ -53,6 +57,9 @@ + + #{id,jdbcType=INTEGER}, + #{shopId,jdbcType=INTEGER}, @@ -76,6 +83,9 @@ update tb_user_shop_msg + + shop_id = #{shopId,jdbcType=INTEGER}, + open_id = #{openId,jdbcType=VARCHAR}, @@ -92,15 +102,20 @@ update_time = #{updateTime,jdbcType=TIMESTAMP}, - where shop_id = #{shopId,jdbcType=INTEGER} + where id = #{id,jdbcType=INTEGER} update tb_user_shop_msg - set open_id = #{openId,jdbcType=VARCHAR}, + set shop_id = #{shopId,jdbcType=INTEGER}, + open_id = #{openId,jdbcType=VARCHAR}, remark = #{remark,jdbcType=VARCHAR}, status = #{status,jdbcType=VARCHAR}, create_time = #{createTime,jdbcType=TIMESTAMP}, update_time = #{updateTime,jdbcType=TIMESTAMP} - where shop_id = #{shopId,jdbcType=INTEGER} + where id = #{id,jdbcType=INTEGER} - + + + \ No newline at end of file From fd465c9fe75c2280b2e06ed7d3103b0dc58a2c22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E9=B9=8F=E8=BE=89?= <18322780655@163.com> Date: Fri, 5 Jul 2024 10:13:55 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=80=97=E6=9D=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/TbUserShopMsgMapper.java | 2 + .../cashierservice/entity/TbUserShopMsg.java | 12 ++++- .../cashierservice/service/LoginService.java | 10 +---- .../resources/mapper/TbUserShopMsgMapper.xml | 45 ++++++++++++------- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbUserShopMsgMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbUserShopMsgMapper.java index c6f5a8a..d65a62d 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbUserShopMsgMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbUserShopMsgMapper.java @@ -32,5 +32,7 @@ public interface TbUserShopMsgMapper { @Select("select * from tb_user_shop_msg where shop_id=#{shopId}") TbUserShopMsg selectByShopId(@Param("shopId") String shopId); + + TbUserShopMsg selectByShopIdAndOpenId(@Param("shopId") Integer shopId,@Param("openId") String openId); } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserShopMsg.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserShopMsg.java index 22c4663..e1bedc8 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserShopMsg.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbUserShopMsg.java @@ -4,6 +4,8 @@ import java.io.Serializable; import java.util.Date; public class TbUserShopMsg implements Serializable { + private Integer id; + private Integer shopId; private String openId; @@ -18,6 +20,14 @@ public class TbUserShopMsg implements Serializable { private static final long serialVersionUID = 1L; + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + public Integer getShopId() { return shopId; } @@ -65,4 +75,4 @@ public class TbUserShopMsg implements Serializable { public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } -} +} \ No newline at end of file diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java index a36fb42..3f87abb 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java @@ -65,22 +65,16 @@ public class LoginService { public Result wxBusinessLogin(String openId,String shopId){ - TbUserShopMsg shopMsg= tbUserShopMsgMapper.selectByPrimaryKey(Integer.valueOf(shopId)); + TbUserShopMsg shopMsg= tbUserShopMsgMapper.selectByShopIdAndOpenId(Integer.valueOf(shopId),openId); if(Objects.isNull(shopMsg)){ shopMsg=new TbUserShopMsg(); - shopMsg.setShopId(Integer.valueOf(shopId)); shopMsg.setOpenId(openId); shopMsg.setCreateTime(new Date()); shopMsg.setStatus("1"); - - tbUserShopMsgMapper.insert(shopMsg); - }else { - shopMsg.setOpenId(openId); - shopMsg.setUpdateTime(new Date()); - tbUserShopMsgMapper.updateByPrimaryKey(shopMsg); } + // 为商家绑定openid if (shopOpenIdMapper.countByOpenId(openId) == null) { TbShopOpenId shopOpenId = new TbShopOpenId(); diff --git a/src/main/resources/mapper/TbUserShopMsgMapper.xml b/src/main/resources/mapper/TbUserShopMsgMapper.xml index 25fdc4c..3220910 100644 --- a/src/main/resources/mapper/TbUserShopMsgMapper.xml +++ b/src/main/resources/mapper/TbUserShopMsgMapper.xml @@ -2,7 +2,8 @@ - + + @@ -10,29 +11,32 @@ - shop_id, open_id, remark, status, create_time, update_time + id, shop_id, open_id, remark, status, create_time, update_time delete from tb_user_shop_msg - where shop_id = #{shopId,jdbcType=INTEGER} + where id = #{id,jdbcType=INTEGER} - insert into tb_user_shop_msg (shop_id, open_id, remark, - status, create_time, update_time - ) - values (#{shopId,jdbcType=INTEGER}, #{openId,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, - #{status,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP} - ) + insert into tb_user_shop_msg (id, shop_id, open_id, + remark, status, create_time, + update_time) + values (#{id,jdbcType=INTEGER}, #{shopId,jdbcType=INTEGER}, #{openId,jdbcType=VARCHAR}, + #{remark,jdbcType=VARCHAR}, #{status,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, + #{updateTime,jdbcType=TIMESTAMP}) insert into tb_user_shop_msg + + id, + shop_id, @@ -53,6 +57,9 @@ + + #{id,jdbcType=INTEGER}, + #{shopId,jdbcType=INTEGER}, @@ -76,6 +83,9 @@ update tb_user_shop_msg + + shop_id = #{shopId,jdbcType=INTEGER}, + open_id = #{openId,jdbcType=VARCHAR}, @@ -92,15 +102,20 @@ update_time = #{updateTime,jdbcType=TIMESTAMP}, - where shop_id = #{shopId,jdbcType=INTEGER} + where id = #{id,jdbcType=INTEGER} update tb_user_shop_msg - set open_id = #{openId,jdbcType=VARCHAR}, + set shop_id = #{shopId,jdbcType=INTEGER}, + open_id = #{openId,jdbcType=VARCHAR}, remark = #{remark,jdbcType=VARCHAR}, status = #{status,jdbcType=VARCHAR}, create_time = #{createTime,jdbcType=TIMESTAMP}, update_time = #{updateTime,jdbcType=TIMESTAMP} - where shop_id = #{shopId,jdbcType=INTEGER} + where id = #{id,jdbcType=INTEGER} - + + + \ No newline at end of file From 979e2b35678c4930c7a4ca10da2ba9d06368995a Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Fri, 5 Jul 2024 11:31:37 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E5=BA=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chaozhanggui/system/cashierservice/dao/TbProductMapper.java | 2 +- .../system/cashierservice/dao/TbProductSkuMapper.java | 2 +- .../chaozhanggui/system/cashierservice/service/CartService.java | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) 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 e26f137..b4c5795 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductMapper.java @@ -38,7 +38,7 @@ public interface TbProductMapper { void updateStockById(@Param("productId") String productId, @Param("num") Integer num); - @Update("update tb_product set stock_number=stock_number-#{num} WHERE id=#{id} and stock_number-#{num} > 0") + @Update("update tb_product set stock_number=stock_number-#{num} WHERE id=#{id} and stock_number-#{num} >= 0") int decrStock(@Param("id") String id, @Param("num") Integer num); @Update("update tb_product set stock_number=stock_number-#{num} WHERE id=#{id}") diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductSkuMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductSkuMapper.java index 0376a71..61d7b13 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductSkuMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbProductSkuMapper.java @@ -41,7 +41,7 @@ public interface TbProductSkuMapper { List selectSkus(@Param("list") List productId); List selectSku(@Param("productId") String productId); - @Update("update tb_product_sku set stock_number=stock_number-#{num} WHERE id=#{id} and stock_number-#{num} > 0") + @Update("update tb_product_sku set stock_number=stock_number-#{num} WHERE id=#{id} and stock_number-#{num} >= 0") int decrStock(@Param("id") String id, @Param("num") Integer num); @Update("update tb_product_sku set stock_number=stock_number-#{num} WHERE id=#{id}") diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java index e9635e5..747ac45 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -585,8 +585,6 @@ public class CartService { jsonObject1.put("status", "fail"); jsonObject1.put("msg", "商品库存不足" + tbProduct1.getName()); jsonObject1.put("data", new ArrayList<>()); - PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true); - PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true); return; } From b16ad91a0612ce5b5581a031552ef68c97776601 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Fri, 5 Jul 2024 11:58:29 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E5=BA=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/cashierservice/service/CartService.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java index 747ac45..6a73a99 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -310,18 +310,6 @@ public class CartService { Integer buyNum = jsonObject.getInteger("num"); - if (buyNum <= 0) { - JSONObject jsonObject1 = new JSONObject(); - jsonObject1.put("status", "fail"); - jsonObject1.put("msg", "参数非法"); - jsonObject1.put("data", new ArrayList<>()); - jsonObject1.put("reqData", jsonObject); - - PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true); - log.error("参数非法 productId:{}", productId); - throw new MsgException("该商品已售罄"); - } - String skuId = jsonObject.getString("skuId"); if (tbProduct.getIsStock() == 1) { // 1:共享库存 0:独立库存 From ccd247cff22a9e6e15e90c23780ffcceba8d7779 Mon Sep 17 00:00:00 2001 From: wangw <1594593906@qq.com> Date: Fri, 5 Jul 2024 14:16:46 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=B8=8B=E5=8D=95=20=E6=89=93=E7=A5=A8?= =?UTF-8?q?=E9=87=8D=E8=AF=95=E6=9C=BA=E5=88=B6=204=E7=A7=92=E9=87=8D?= =?UTF-8?q?=E8=AF=95=205=E6=AC=A1=205=E6=AC=A1=E5=90=8E=E4=B8=A2=E5=A4=B1?= =?UTF-8?q?=20=E4=B8=8B=E5=8D=95=20order=E6=95=B0=E6=8D=AE=20=E4=B8=8D?= =?UTF-8?q?=E5=8F=91=E7=BB=99=E7=94=9F=E6=88=90=E8=AE=A2=E5=8D=95=E7=9A=84?= =?UTF-8?q?=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- .../netty/PushToAppChannelHandlerAdapter.java | 28 +++ .../PushToClientChannelHandlerAdapter.java | 204 +++++++++++------- .../cashierservice/service/CartService.java | 3 +- .../cashierservice/service/PayService.java | 2 +- 5 files changed, 161 insertions(+), 79 deletions(-) diff --git a/.gitignore b/.gitignore index 7425e2a..a637660 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ /.gradle/ /application.pid /target/* -**.jar \ No newline at end of file +**.jar +**.log \ No newline at end of file diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToAppChannelHandlerAdapter.java b/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToAppChannelHandlerAdapter.java index e3ccf17..339735f 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToAppChannelHandlerAdapter.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToAppChannelHandlerAdapter.java @@ -197,6 +197,34 @@ public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter { sendMessage(ctx, str); } + + /** + * 发送给当前用户以外的用户 + */ + @Async + public void AppSendInfo(String message, String tableId, String userId) { + log.info("netty连接 发送消息 除当前用户 tableId:{} userId:{}", tableId, userId); + if (webSocketMap.containsKey(tableId)) { + ConcurrentHashMap webSockets = webSocketMap.get(tableId); + if (!webSockets.isEmpty()) { + for (String user : webSockets.keySet()) { + if (StringUtils.isNotBlank(userId) && userId.equals(user)) { + continue; + } + ChannelHandlerContext ctx = webSockets.get(user); + if (ctx != null) { + sendMesToApp(message, ctx); + } else { + log.info("netty连接 发送消息 除当前用户 userId:{} 失败", user); + } + } + } else { + log.info("netty连接 发送消息 除当前用户 tableId:{} 失败", tableId); + } + } + + } + @Async public void AppSendInfo(String message, String tableId,String userId, boolean userFlag) { log.info("netty连接 发送消息 tableId:{} userId:{} userFlag:{} message:{}",tableId,userId,userFlag, JSONUtil.toJSONString(message)); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToClientChannelHandlerAdapter.java b/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToClientChannelHandlerAdapter.java index ff709fc..f8a7825 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToClientChannelHandlerAdapter.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToClientChannelHandlerAdapter.java @@ -2,17 +2,20 @@ package com.chaozhanggui.system.cashierservice.netty; import com.alibaba.fastjson.JSONObject; import com.chaozhanggui.system.cashierservice.netty.config.NettyChannelHandlerAdapter; -import com.chaozhanggui.system.cashierservice.util.JSONUtil; import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicInteger; + +import io.netty.util.concurrent.Future; +import io.netty.util.concurrent.GenericFutureListener; /** @@ -32,7 +35,10 @@ public class PushToClientChannelHandlerAdapter extends NettyChannelHandlerAdapte * [ctx, shopId:clientId] */ private static Map clientIdMap = new ConcurrentHashMap<>(); - + private static Map> retryQueue = new HashMap<>(); + private static ConcurrentHashMap retryCounts = new ConcurrentHashMap<>(); + private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3); + int maxRetryAttempts = 5; private String clientId = ""; private String shopId = ""; @@ -51,8 +57,8 @@ public class PushToClientChannelHandlerAdapter extends NettyChannelHandlerAdapte } @Override - public void channelInactive(ChannelHandlerContext ctx) { - log.info("netty连接client 长连接关闭:{}, {}",clientId,shopId); + public void channelInactive(ChannelHandlerContext ctx) { + log.info("netty连接client 长连接关闭:{}, {}", clientId, shopId); ctx.close(); removeCtx(ctx); } @@ -68,9 +74,6 @@ public class PushToClientChannelHandlerAdapter extends NettyChannelHandlerAdapte ConcurrentHashMap tableMap = webSocketMap.get(split[0]); if (tableMap != null && !tableMap.isEmpty() && tableMap.size() > 0) { tableMap.remove(split[1]); - if (tableMap.isEmpty() || tableMap.size() == 0) { - webSocketMap.remove(split[0]); - } } } clientIdMap.remove(ctx); @@ -92,43 +95,49 @@ public class PushToClientChannelHandlerAdapter extends NettyChannelHandlerAdapte JSONObject jsonObject = new JSONObject(); if (StringUtils.isNotEmpty(msg)) { jsonObject = JSONObject.parseObject(msg); - }else { - log.info("netty连接client 接收到空数据:{}",msg); + } else { + log.info("netty连接client 接收到空数据:{}", msg); } String type = jsonObject.getString("type"); - if(type.equals("heartbeat")){//心跳 - log.info("netty连接client 接收到心跳数据:shop:{} clientId:{} meg:{}",shopId,clientId,msg); - }else { + if (type.equals("heartbeat")) {//心跳 + log.info("netty连接client 接收到心跳数据:shop:{} clientId:{} meg:{}", shopId, clientId, msg); + } else { if (type.equals("connect")) { String clientId = jsonObject.getString("clientId"); String shopId = jsonObject.getString("shopId"); - if (StringUtils.isBlank(type) || StringUtils.isBlank(shopId) || StringUtils.isBlank(clientId)) { - log.info("netty连接client 建立连接请求失败:{}",jsonObject); + if (StringUtils.isBlank(type) || StringUtils.isBlank(shopId) || StringUtils.isBlank(clientId)) { + log.info("netty连接client 建立连接请求失败:{}", jsonObject); channelInactive(ctx); return; } - - log.info("netty连接client 接收到数据 建立连接参数 param:{}",jsonObject); - this.clientId=clientId; - this.shopId=shopId; - if (webSocketMap.containsKey(shopId)) { - ConcurrentHashMap clientSocketMap = webSocketMap.get(shopId); - ChannelHandlerContext channelHandlerContext = clientSocketMap.get(clientId); - if (channelHandlerContext != null) { - channelHandlerContext.close(); - } - clientSocketMap.put(clientId, ctx); - } else { - ConcurrentHashMap clientSocketMap = new ConcurrentHashMap<>(); - clientSocketMap.put(clientId, ctx); - webSocketMap.put(shopId,clientSocketMap); - } + log.info("netty连接client 接收到数据 建立连接参数 param:{}", jsonObject); + this.clientId = clientId; + this.shopId = shopId; + webSocketMap.computeIfAbsent(shopId, k -> new ConcurrentHashMap<>()).put(clientId, ctx); clientIdMap.put(ctx, shopId + ":" + clientId); JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("status", "success"); jsonObject1.put("msg", "连接成功"); jsonObject1.put("type", "connect"); sendMesToApp(jsonObject1.toString(), ctx); + }else if(type.equals("send")){ + String orderNo = jsonObject.getString("orderNo"); +// log.info("netty连接client 接收到send 数据 param:{} orderNo:{} shopId:{}", jsonObject,orderNo,shopId); + if (retryQueue.containsKey(shopId)) { + // 获取内层Map + Queue queue = retryQueue.get(shopId); + if (queue != null) { + Iterator iterator = queue.iterator(); + while (iterator.hasNext()) { + JSONObject customObject = iterator.next(); +// log.info("netty连接client 接收到send 数据 retryQueue数据:{}", customObject); + if (customObject.getJSONObject("orderInfo").getString("orderNo").equals(orderNo)) { + iterator.remove(); + retryCounts.remove(orderNo); + } + } + } + } } } //业务逻辑代码处理框架。。。 @@ -139,53 +148,96 @@ public class PushToClientChannelHandlerAdapter extends NettyChannelHandlerAdapte sendMessage(ctx, str); } - /** - * @param message 发送的消息内容 - * @param shopId 店铺Id - * @param clientId 客户端Id - * @param userFlag - * 为true 单发给clientId - * 为false 群发 shopId为空 发给所有人 - */ @Async - public void AppSendInfo(String message, String shopId,String clientId, boolean userFlag) { - log.info("netty连接client 发送消息 shopId:{} clientId:{} userFlag:{} message:{}",shopId,clientId,userFlag, JSONUtil.toJSONString(message)); - if (userFlag) { - if (webSocketMap.containsKey(shopId)) { - ConcurrentHashMap webSockets = webSocketMap.get(shopId); - if(!webSockets.isEmpty()){ - if (StringUtils.isNotBlank(clientId)) { - ChannelHandlerContext ctx = webSockets.get(clientId); - if (ctx != null) { - sendMesToApp(message,ctx); - } - } + public void sendMesToApp(String shopId,String orderNo,JSONObject str, ChannelHandlerContext ctx) { + ctx.channel().writeAndFlush(new TextWebSocketFrame(str.toString())).addListener((GenericFutureListener>) future -> { + if (!future.isSuccess()) { + if (shopId != null) { + retryQueue.computeIfAbsent(shopId, k -> new ConcurrentLinkedQueue<>()).offer(str); + scheduleRetry(shopId,orderNo); } } - } else { - if (StringUtils.isEmpty(shopId)) { - // 向所有用户发送信息 - for (ConcurrentHashMap value : webSocketMap.values()) { - for (ChannelHandlerContext ctx : value.values()) { - sendMesToApp(message,ctx); - } - } - } else if (webSocketMap.containsKey(shopId)) { - ConcurrentHashMap webSockets = webSocketMap.get(shopId); - if(!webSockets.isEmpty()) { - for (String user : webSockets.keySet()) { - ChannelHandlerContext ctx = webSockets.get(user); - if (ctx != null) { - log.info("netty连接client 发送消息 桌码群发 clientId:{}",user); - sendMesToApp(message,ctx); - }else { - log.info("netty连接client 发送消息 桌码群发 clientId:{} 失败",user); - } - } - }else { - log.info("netty连接client 发送消息 桌码群发 clientId:{} 失败",clientId); - } + }); + } + + /** + * @param message 发送的消息内容 + * @param shopId 店铺Id + // * @param clientId 客户端Id + // * @param userFlag 为true 单发给clientId + * 为false 群发 shopId为空 发给所有人 + */ +// @Async +// public void AppSendInfo(String message, String shopId,String clientId, boolean userFlag) { +// log.info("netty连接client 发送消息 shopId:{} clientId:{} userFlag:{} message:{}",shopId,clientId,userFlag, JSONUtil.toJSONString(message)); +// if (userFlag) { +// if (webSocketMap.containsKey(shopId)) { +// ConcurrentHashMap webSockets = webSocketMap.get(shopId); +// if(!webSockets.isEmpty()){ +// if (StringUtils.isNotBlank(clientId)) { +// ChannelHandlerContext ctx = webSockets.get(clientId); +// if (ctx != null) { +// sendMesToApp(message,ctx); +// } +// } +// } +// } +// } else { +// if (StringUtils.isEmpty(shopId)) { +// // 向所有用户发送信息 +// for (ConcurrentHashMap value : webSocketMap.values()) { +// for (ChannelHandlerContext ctx : value.values()) { +// sendMesToApp(message,ctx); +// } +// } +// } else if (webSocketMap.containsKey(shopId)) { +// ConcurrentHashMap webSockets = webSocketMap.get(shopId); +// if((!webSockets.isEmpty() && !webSockets.keySet().isEmpty())) { +// for (String user : webSockets.keySet()) { +// ChannelHandlerContext ctx = webSockets.get(user); +// if (ctx != null) { +// log.info("netty连接client 发送消息 桌码群发 clientId:{}",user); +// sendMesToApp(message,ctx); +// }else { +// log.info("netty连接client 发送消息 桌码群发 clientId:{} 失败",user); +// } +// } +// }else { +// log.info("netty连接client 发送消息 桌码群发 clientId:{} 失败",clientId); +// } +// } +// } +// } + public void AppSendInfoV1(String shopId, String orderNo, JSONObject message) { + log.info("netty连接client 发送消息 shopId:{} clientId:{} userFlag:{} message:{}", shopId, message.get("orderInfo")); + retryQueue.computeIfAbsent(shopId, k -> new ConcurrentLinkedQueue<>()).offer(message); + scheduleRetry(shopId, orderNo); + ConcurrentHashMap webSockets = webSocketMap.get(shopId); + if (webSockets != null) { + for (ChannelHandlerContext ctx : webSockets.values()) { + sendMesToApp(shopId, orderNo, message, ctx); + log.info("netty连接client 向:{}发送消息", shopId); } } } + + // 定时重试方法 + private void scheduleRetry(String shopId,String orderNo) { +// log.info("定时重发"); + scheduler.schedule(() -> { + Queue shopRetryQueue = retryQueue.get(shopId); + if (shopRetryQueue != null) { + JSONObject message = shopRetryQueue.poll(); + if (message != null) { + AtomicInteger retryCount = retryCounts.computeIfAbsent(orderNo, k -> new AtomicInteger(0)); + int currentRetryCount = retryCount.incrementAndGet(); + if (currentRetryCount <= maxRetryAttempts) { + AppSendInfoV1(shopId,orderNo,message); + } else { + log.error("重试次数超过最大限制,放弃重试。shopId: {}, orderNo: {}", shopId, orderNo); + } + } + } + }, 4, TimeUnit.SECONDS); + } } \ No newline at end of file diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java index 6a73a99..c7ee8ea 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -796,7 +796,8 @@ public class CartService { jsonObject12.put("amount", BigDecimal.ZERO); jsonObject12.put("data", new JSONArray()); - PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject12.toString(), jsonObject.getString("tableId").concat("-").concat(shopId), "", false); +// PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject12.toString(), jsonObject.getString("tableId").concat("-").concat(shopId), "", false); + PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject12.toString(), jsonObject.getString("tableId").concat("-").concat(shopId), jsonObject.getString("userId")); redisUtil.saveMessage(RedisCst.ORDER_EXPIRED.concat(orderId.toString()), orderId.toString(), 60 * 16L); 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 585aecc..e2d76ed 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java @@ -1208,7 +1208,7 @@ public class PayService { // e.printStackTrace(); // } // } - PushToClientChannelHandlerAdapter.getInstance().AppSendInfo(client.toString(), orderInfo.getShopId(), "", false); + PushToClientChannelHandlerAdapter.getInstance().AppSendInfoV1(orderInfo.getShopId(),orderInfo.getOrderNo(), client); }