From d82387e9d1ae3149daf430617c70494a5a7bbbb2 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Fri, 28 Jun 2024 14:15:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=8B=E5=8D=95?= =?UTF-8?q?=E5=BA=93=E5=AD=98=E9=A2=84=E8=AD=A6=E6=B6=88=E6=81=AF=E6=8E=A8?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/TbUserShopMsgMapper.java | 7 +- .../cashierservice/service/OrderService.java | 39 +++++++ .../cashierservice/util/WechatUtil.java | 104 ++++++++++++++++++ src/main/resources/application.yml | 1 + 4 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/chaozhanggui/system/cashierservice/util/WechatUtil.java 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 a0f9b47..53731d9 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbUserShopMsgMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbUserShopMsgMapper.java @@ -2,6 +2,8 @@ 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.springframework.stereotype.Component; @Component @@ -18,4 +20,7 @@ public interface TbUserShopMsgMapper { int updateByPrimaryKeySelective(TbUserShopMsg record); int updateByPrimaryKey(TbUserShopMsg record); -} \ No newline at end of file + + @Select("select * from tb_user_shop_msg where shop_id=#{shopId}") + TbUserShopMsg selectByShopId(@Param("shopId") String shopId); +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java index 15a4d7e..06fb2b2 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java @@ -23,6 +23,7 @@ import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; @@ -64,10 +65,19 @@ public class OrderService { @Autowired RabbitProducer producer; + private final WechatUtil wechatUtil; + + private final TbUserShopMsgMapper tbUserShopMsgMapper; + private static ConcurrentHashMap> codeMap = new ConcurrentHashMap<>(); private static ConcurrentHashMap> userMap = new ConcurrentHashMap<>(); + public OrderService(WechatUtil wechatUtil, TbUserShopMsgMapper tbUserShopMsgMapper) { + this.wechatUtil = wechatUtil; + this.tbUserShopMsgMapper = tbUserShopMsgMapper; + } + @Transactional(rollbackFor = Exception.class) public Result createCart(String masterId, String productId, String shopId, Integer skuId, Integer number, String userId, String clientType, Integer cartId, String isGift, String isPack, String uuid, String type) { @@ -220,10 +230,39 @@ public class OrderService { jsonObject.put("cartId",cart.getId()); jsonObject.put("type","create"); producer.cons(jsonObject.toString()); + CompletableFuture.runAsync(() -> checkWarnLineAndSendMsg(skuWithBLOBs, product, shopInfo)); return Result.success(CodeEnum.SUCCESS, masterId); } + /** + * 校验商品库存警戒线并通知商户 + * @param productSku sku + */ + private void checkWarnLineAndSendMsg(TbProductSku productSku, TbProduct product, TbShopInfo shopInfo) { + if (productSku.getWarnLine() == null) { + return; + } + + + if (productSku.getStockNumber() == null) { + productSku.setStockNumber((double) 0); + } + + if (product.getStockNumber() == null) { + product.setStockNumber(0); + } + if ( + (product.getIsDistribute() == 1 && productSku.getStockNumber() <= productSku.getWarnLine()) + || (product.getIsDistribute() != 1) && product.getStockNumber() <= productSku.getWarnLine() + ) { + TbUserShopMsg tbUserShopMsg = tbUserShopMsgMapper.selectByShopId(productSku.getShopId()); + wechatUtil.sendStockWarnMsg(shopInfo.getShopName(), product.getName(), + product.getIsDistribute() == 1 ? productSku.getStockNumber().toString() : product.getStockNumber().toString(), + "耗材库存不足,请及时补充。", tbUserShopMsg.getOpenId()); + } + } + public Result queryCart(String masterId, String shopId) { if (StringUtils.isEmpty(shopId)) { return Result.fail(CodeEnum.SHOPINFONOEXIST); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/util/WechatUtil.java b/src/main/java/com/chaozhanggui/system/cashierservice/util/WechatUtil.java new file mode 100644 index 0000000..e778af7 --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/util/WechatUtil.java @@ -0,0 +1,104 @@ +package com.chaozhanggui.system.cashierservice.util; + +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.http.HttpRequest; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +@Component +@Slf4j +public class WechatUtil { + @Value("${wx.msg.appId}") + private String appId; + + @Value("${wx.msg.secrete}") + private String secrete; + + @Value("${wx.msg.warnMsgTmpId}") + private String msgTmpId; + + static LinkedHashMap linkedHashMap=new LinkedHashMap<>(); + + static { + + linkedHashMap.put("40001","获取 access_token 时 AppSecret 错误,或者 access_token 无效。请开发者认真比对 AppSecret 的正确性,或查看是否正在为恰当的公众号调用接口"); + linkedHashMap.put("40003","不合法的 OpenID ,请开发者确认 OpenID (该用户)是否已关注公众号,或是否是其他公众号的 OpenID"); + linkedHashMap.put("40014","不合法的 access_token ,请开发者认真比对 access_token 的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口"); + linkedHashMap.put("40037","不合法的 template_id"); + linkedHashMap.put("43101","用户未订阅消息"); + linkedHashMap.put("43107","订阅消息能力封禁"); + linkedHashMap.put("43108","并发下发消息给同一个粉丝"); + linkedHashMap.put("45168","命中敏感词"); + linkedHashMap.put("47003","参数错误"); + + } + + public JSONObject getAccessToken(){ + String requestUrl = "https://api.weixin.qq.com/cgi-bin/token"; + Map requestUrlParam = new HashMap<>(); + //小程序appId + requestUrlParam.put("appid", appId); + //小程序secret + requestUrlParam.put("secret", secrete); + //默认参数 + requestUrlParam.put("grant_type", "client_credential"); + JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(requestUrl,requestUrlParam)); + return jsonObject; + } + + public static void main(String[] args) { + String id ="kSxJL9TR4s_UmOmNLE"; +// sendStockWarnMsg("123", "1231", "1231", "23321", id); + } + + public JSONObject sendStockWarnMsg(String shopName, String productName, String stock, String note, String toUserOpenId) { + Map data = new HashMap() {{ + put("thing1", new HashMap(){{ + put("value", shopName); + }}); + put("thing6", new HashMap(){{ + put("value", productName); + }}); + put("number7", new HashMap(){{ + put("value", stock); + }}); + put("thing5", new HashMap(){{ + put("value", note); + }}); + }}; + log.info("开始发送库存预警消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data); + return sendTempMsg(msgTmpId, toUserOpenId, data); + } + + + public JSONObject sendTempMsg(String tempId, String toUserOpenId, Map data) { + log.info("开始发送微信模板消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data); + JSONObject object= getAccessToken(); + String accessToken=object.get("access_token")+""; + + JSONObject object1=new JSONObject(); + + object1.put("template_id", tempId); + object1.put("touser", toUserOpenId); + object1.put("data",data); + + object1.put("miniprogram_state","trial"); + object1.put("lang","zh_CN"); + + String response= HttpRequest.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".concat(accessToken)).body(object1.toString()).execute().body(); + log.info("微信模板消息发送成功,相应内容:{}",response); + JSONObject resObj=JSONObject.parseObject(response); + if(ObjectUtil.isNotEmpty(resObj)&&ObjectUtil.isNotNull(resObj)&&"0".equals(resObj.get("errcode")+"")){ + return resObj; + } + + throw new RuntimeException(linkedHashMap.getOrDefault(resObj.get("errcode") + "", "未知错误")); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d1dc4d3..8e2be80 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -48,6 +48,7 @@ wx: msg: appId: wxcf0fe8cdba153fd6 secrete: c33e06467c6879a62af633d50ed6b720 + warnMsgTmpId: IZ-l9p9yBgcvhRR0uN6cBQPkWJ5i05zyWMkfeCPaAmY From 5a61e994a1236a7de04a0298d48a198d8905943a Mon Sep 17 00:00:00 2001 From: GYJ <1157756119@qq.com> Date: Fri, 28 Jun 2024 14:57:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=B8=8B=E5=8D=95=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E5=85=B1=E4=BA=AB=E5=BA=93=E5=AD=98=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E5=95=86=E5=93=81=E5=92=8Csku=E5=BA=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/cashierservice/service/OrderService.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java index 06fb2b2..e51b0e5 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java @@ -111,7 +111,11 @@ public class OrderService { if("1".equals(product.getIsStock().toString())){ - if(product.getId().intValue()==Integer.valueOf(skuWithBLOBs.getProductId()).intValue()){ + if ("1".equals(product.getIsDistribute().toString())) { + if(product.getStockNumber()-number<0){ + return Result.fail(CodeEnum.STOCKERROR); + } + } else if (product.getId().intValue()==Integer.valueOf(skuWithBLOBs.getProductId()).intValue()){ if(skuWithBLOBs.getStockNumber()-number<0){ return Result.fail(CodeEnum.STOCKERROR); }