diff --git a/.gitignore b/.gitignore index b83d222..a637660 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,12 @@ -/target/ +### IDEA ### +/target/**/* +/log/* +.idea/* +*.iml +*/target/* +*/*.iml +/.gradle/ +/application.pid +/target/* +**.jar +**.log \ No newline at end of file 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..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,6 +38,9 @@ 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}") + 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..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,6 +41,9 @@ 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}") + int decrStockUnCheck(@Param("id") String id, @Param("num") Integer num); } 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..1f2a610 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,33 @@ 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/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 6d2fb0c..c7ee8ea 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -301,30 +301,39 @@ 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("该商品不存在"); } + + Integer buyNum = jsonObject.getInteger("num"); + 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", "该商品已售罄"); 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("该商品已售罄"); } } 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", "该商品已售罄"); 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("该商品已售罄"); @@ -333,7 +342,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)))); @@ -387,6 +395,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,18 +563,26 @@ 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"); 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; } + // 发送判断耗材是否耗尽消息 + 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()); @@ -778,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/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/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); } 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("库存不足,下单失败"); + } + } + } } 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 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