diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbShopOpenIdMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbShopOpenIdMapper.java new file mode 100644 index 0000000..1ddc42a --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbShopOpenIdMapper.java @@ -0,0 +1,34 @@ +package com.chaozhanggui.system.cashierservice.dao; + +import com.chaozhanggui.system.cashierservice.entity.TbShopOpenId; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** +* @author Administrator +* @description 针对表【tb_shop_open_id(商家openid信息表)】的数据库操作Mapper +* @createDate 2024-07-02 14:21:02 +* @Entity com.chaozhanggui.system.cashierservice.entity.TbShopOpenId +*/ +public interface TbShopOpenIdMapper { + + int deleteByPrimaryKey(Long id); + + int insert(TbShopOpenId record); + + int insertSelective(TbShopOpenId record); + + TbShopOpenId selectByPrimaryKey(Long id); + + int updateByPrimaryKeySelective(TbShopOpenId record); + + int updateByPrimaryKey(TbShopOpenId record); + + @Select("select * from tb_shop_open_id where open_id=#{openId}") + TbShopOpenId countByOpenId(@Param("openId") String openId); + + @Select("select * from tb_shop_open_id where shop_id=#{shopId} and status=1") + List selectByShopId(@Param("shopId") Integer shopId); +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbShopOpenId.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbShopOpenId.java new file mode 100644 index 0000000..1afc92f --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbShopOpenId.java @@ -0,0 +1,94 @@ +package com.chaozhanggui.system.cashierservice.entity; + +import java.io.Serializable; +import java.util.Date; +import lombok.Data; + +/** + * 商家openid信息表 + * @TableName tb_shop_open_id + */ +@Data +public class TbShopOpenId implements Serializable { + /** + * + */ + private Integer id; + + /** + * 店铺id + */ + private Integer shopId; + + /** + * 已经订阅消息的商家微信号 + */ + private String openId; + + /** + * + */ + private Integer status; + + /** + * + */ + private Date createTime; + + /** + * + */ + private Date updateTime; + + private static final long serialVersionUID = 1L; + + @Override + public boolean equals(Object that) { + if (this == that) { + return true; + } + if (that == null) { + return false; + } + if (getClass() != that.getClass()) { + return false; + } + TbShopOpenId other = (TbShopOpenId) that; + return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) + && (this.getShopId() == null ? other.getShopId() == null : this.getShopId().equals(other.getShopId())) + && (this.getOpenId() == null ? other.getOpenId() == null : this.getOpenId().equals(other.getOpenId())) + && (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus())) + && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime())) + && (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime())); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); + result = prime * result + ((getShopId() == null) ? 0 : getShopId().hashCode()); + result = prime * result + ((getOpenId() == null) ? 0 : getOpenId().hashCode()); + result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode()); + result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode()); + result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode()); + return result; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ["); + sb.append("Hash = ").append(hashCode()); + sb.append(", id=").append(id); + sb.append(", shopId=").append(shopId); + sb.append(", openId=").append(openId); + sb.append(", status=").append(status); + sb.append(", createTime=").append(createTime); + sb.append(", updateTime=").append(updateTime); + sb.append(", serialVersionUID=").append(serialVersionUID); + sb.append("]"); + return sb.toString(); + } +} \ 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 c56646d..08812ea 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -60,15 +60,18 @@ public class CartService { private final TbUserShopMsgMapper tbUserShopMsgMapper; private final WechatUtil wechatUtil; + private final TbShopOpenIdMapper shopOpenIdMapper; + @Autowired private RabbitProducer producer; @Qualifier("tbShopInfoMapper") @Autowired private TbShopInfoMapper tbShopInfoMapper; - public CartService(TbUserShopMsgMapper tbUserShopMsgMapper, WechatUtil wechatUtil) { + public CartService(TbUserShopMsgMapper tbUserShopMsgMapper, WechatUtil wechatUtil, TbShopOpenIdMapper shopOpenIdMapper) { this.tbUserShopMsgMapper = tbUserShopMsgMapper; this.wechatUtil = wechatUtil; + this.shopOpenIdMapper = shopOpenIdMapper; } public void initCart(JSONObject jsonObject) { @@ -402,7 +405,7 @@ public class CartService { redisUtil.getIncrNum(key, "2"); } - CompletableFuture.runAsync(() -> checkWarnLineAndSendMsg(productSku, product)); + CompletableFuture.runAsync(() -> checkWarnLineAndSendMsg(productSku, product, num)); } @@ -410,7 +413,7 @@ public class CartService { * 校验商品库存警戒线并通知商户 * @param productSku sku */ - private void checkWarnLineAndSendMsg(TbProductSku productSku, TbProduct product) { + private void checkWarnLineAndSendMsg(TbProductSku productSku, TbProduct product, Integer num) { if (productSku.getWarnLine() == null) { return; } @@ -432,10 +435,12 @@ public class CartService { (product.getIsDistribute() == 1 && product.getStockNumber() <= productSku.getWarnLine()) || (product.getIsDistribute() != 1) && productSku.getStockNumber() <= productSku.getWarnLine() ) { - TbUserShopMsg tbUserShopMsg = tbUserShopMsgMapper.selectByShopId(productSku.getShopId()); - wechatUtil.sendStockWarnMsg(shopInfo.getShopName(), product.getName(), - product.getIsDistribute() == 1 ? product.getStockNumber().toString() : productSku.getStockNumber().toString(), - "耗材库存不足,请及时补充。", tbUserShopMsg.getOpenId()); + List shopOpenIds = shopOpenIdMapper.selectByShopId(Integer.valueOf(product.getShopId())); + shopOpenIds.forEach(item -> { + wechatUtil.sendStockWarnMsg(shopInfo.getShopName(), product.getName(), + product.getIsDistribute() == 1 ? String.valueOf(product.getStockNumber()-num) : String.valueOf(productSku.getStockNumber() - num), + "耗材库存不足,请及时补充。", item.getOpenId()); + }); } } 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 390482f..b45e52b 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java @@ -1,5 +1,6 @@ package com.chaozhanggui.system.cashierservice.service; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.RandomUtil; import com.alibaba.fastjson.JSON; @@ -56,8 +57,11 @@ public class LoginService { @Autowired TbUserShopMsgMapper tbUserShopMsgMapper; + private final TbShopOpenIdMapper shopOpenIdMapper; - + public LoginService(TbShopOpenIdMapper shopOpenIdMapper) { + this.shopOpenIdMapper = shopOpenIdMapper; + } public Result wxBusinessLogin(String openId,String shopId){ @@ -69,13 +73,22 @@ public class LoginService { 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(); + shopOpenId.setOpenId(openId); + shopOpenId.setCreateTime(DateUtil.date()); + shopOpenId.setShopId(Integer.valueOf(shopId)); + shopOpenIdMapper.insert(shopOpenId); + } return Result.success(CodeEnum.SUCCESS,shopMsg); } diff --git a/src/main/resources/mapper/TbShopOpenIdMapper.xml b/src/main/resources/mapper/TbShopOpenIdMapper.xml new file mode 100644 index 0000000..67b9381 --- /dev/null +++ b/src/main/resources/mapper/TbShopOpenIdMapper.xml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + id,shop_id,open_id, + status,create_time,update_time + + + + + + delete from tb_shop_open_id + where id = #{id,jdbcType=INTEGER} + + + insert into tb_shop_open_id + ( id,shop_id,open_id + ,status,create_time,update_time + ) + values (#{id,jdbcType=INTEGER},#{shopId,jdbcType=INTEGER},#{openId,jdbcType=VARCHAR} + ,#{status,jdbcType=TINYINT},#{createTime,jdbcType=TIMESTAMP},#{updateTime,jdbcType=TIMESTAMP} + ) + + + insert into tb_shop_open_id + + id, + shop_id, + open_id, + status, + create_time, + update_time, + + + #{id,jdbcType=INTEGER}, + #{shopId,jdbcType=INTEGER}, + #{openId,jdbcType=VARCHAR}, + #{status,jdbcType=TINYINT}, + #{createTime,jdbcType=TIMESTAMP}, + #{updateTime,jdbcType=TIMESTAMP}, + + + + update tb_shop_open_id + + + shop_id = #{shopId,jdbcType=INTEGER}, + + + open_id = #{openId,jdbcType=VARCHAR}, + + + status = #{status,jdbcType=TINYINT}, + + + create_time = #{createTime,jdbcType=TIMESTAMP}, + + + update_time = #{updateTime,jdbcType=TIMESTAMP}, + + + where id = #{id,jdbcType=INTEGER} + + + update tb_shop_open_id + set + shop_id = #{shopId,jdbcType=INTEGER}, + open_id = #{openId,jdbcType=VARCHAR}, + status = #{status,jdbcType=TINYINT}, + create_time = #{createTime,jdbcType=TIMESTAMP}, + update_time = #{updateTime,jdbcType=TIMESTAMP} + where id = #{id,jdbcType=INTEGER} + +