| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package com.cyksj.service.goods.impl;
- import cn.hutool.core.date.DateTime;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.cyksj.common.constant.Constant;
- import com.cyksj.common.util.StringUtil;
- import com.cyksj.dto.RedisKey;
- import com.cyksj.mapper.GoodsDonMapper;
- import com.cyksj.mapper.GoodsDonSkuMapper;
- import com.cyksj.mapper.UserMapper;
- import com.cyksj.mapper.UserSubscribeAlertGoodsRecordMapper;
- import com.cyksj.model.entity.GoodsDon;
- import com.cyksj.model.entity.GoodsDonSku;
- import com.cyksj.model.entity.User;
- import com.cyksj.model.entity.UserSubscribeAlertGoodsRecord;
- import com.cyksj.redis.RedisService;
- import com.cyksj.service.goods.GoodsSubscribeAlertService;
- import com.cyksj.service.mail.MailService;
- import com.cyksj.service.sms.SmsService;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import java.util.List;
- /**
- * 项目名: yhlxj11111111
- * 文件名: GoodsSubscribeAlertServiceImpl
- * 创建者: JavaZou
- * 创建时间:2026/1/6 15:35
- */
- @RequiredArgsConstructor
- @Service
- @Slf4j
- public class GoodsSubscribeAlertServiceImpl implements GoodsSubscribeAlertService {
- private final UserSubscribeAlertGoodsRecordMapper userSubscribeAlertGoodsRecordMapper;
- private final UserMapper userMapper;
- private final GoodsDonMapper goodsDonMapper;
- private final SmsService smsService;
- private final MailService mailService;
- private final GoodsDonSkuMapper skuMapper;
- private final RedisService redisService;
- @Override
- public void alertBeforeSubscribeByGoodsId(Long goodsId) {
- DateTime now = DateTime.now();
- List<UserSubscribeAlertGoodsRecord> userSubscribeAlertGoodsRecords = userSubscribeAlertGoodsRecordMapper.selectList(Wrappers.lambdaQuery(UserSubscribeAlertGoodsRecord.class)
- .eq(UserSubscribeAlertGoodsRecord::getGoodsId, goodsId)
- .eq(UserSubscribeAlertGoodsRecord::getStatus, Boolean.FALSE)
- .le(UserSubscribeAlertGoodsRecord::getCreatedTime, now));
- if (userSubscribeAlertGoodsRecords.isEmpty()) {
- return;
- }
- String key = RedisKey.GOODS_SUBSCRIBE_ALERT_KEY + goodsId;
- if (!redisService.setNx(key, goodsId, 60L * 30)) {
- return;
- }
- try {
- GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
- for (UserSubscribeAlertGoodsRecord userSubscribeAlertGoodsRecord : userSubscribeAlertGoodsRecords) {
- userSubscribeAlertGoodsRecord.setStatus(Boolean.TRUE);
- userSubscribeAlertGoodsRecordMapper.updateById(userSubscribeAlertGoodsRecord);
- Long userId = userSubscribeAlertGoodsRecord.getUserId();
- User user = userMapper.selectById(userId);
- // //微信用户
- // if (StrUtil.isNotBlank(user.getUnionid())) {
- // //发送微信通知模板
- // Boolean success = templateCommonService.sendUserSubscribeAlertMsg(user, String.format(Constant.GOODS_SUBSCRIBE_ALERT_WX_MSG, goodsDon.getTitle()), goodsId);
- // if (!success) {
- // //失败后发送手机
- // sendPhoneNotifySubscribe(goodsDon.getTitle(), goodsId, userId);
- // }
- // continue;
- // }
- //首先用发手机号
- Boolean success = sendPhoneNotifySubscribe(goodsDon.getTitle(), goodsId, userId);
- if (!success) {
- //失败 并且是邮箱用户
- if (StrUtil.isNotBlank(user.getEmail())) {
- //邮箱用户
- String msg = String.format(Constant.GOODS_SUBSCRIBE_ALERT_EMAIL_MSG, goodsDon.getTitle(), goodsDon.getId());
- mailService.sendGoodsSubscribeAlertEmail(msg, user.getEmail());
- }
- }
- }
- } finally {
- if (redisService.hasKey(key)) {
- redisService.del(key);
- }
- }
- }
- @Override
- public void alertBeforeSubscribeBySkuId(Long skuId) {
- DateTime now = DateTime.now();
- List<UserSubscribeAlertGoodsRecord> userSubscribeAlertGoodsRecords = userSubscribeAlertGoodsRecordMapper.selectList(Wrappers.lambdaQuery(UserSubscribeAlertGoodsRecord.class)
- .eq(UserSubscribeAlertGoodsRecord::getSkuId, skuId)
- .eq(UserSubscribeAlertGoodsRecord::getStatus, Boolean.FALSE)
- .le(UserSubscribeAlertGoodsRecord::getCreatedTime, now));
- if (userSubscribeAlertGoodsRecords.isEmpty()) {
- return;
- }
- GoodsDonSku sku = skuMapper.selectById(skuId);
- GoodsDon goodsDon = goodsDonMapper.selectById(sku.getGoodsId());
- String key = RedisKey.GOODS_SUBSCRIBE_ALERT_KEY + goodsDon.getId() + ":" + skuId;
- if (!redisService.setNx(key, skuId, 60L * 30)) {
- return;
- }
- try {
- String title = goodsDon.getTitle() + sku.getSpecVal();
- for (UserSubscribeAlertGoodsRecord userSubscribeAlertGoodsRecord : userSubscribeAlertGoodsRecords) {
- userSubscribeAlertGoodsRecord.setStatus(Boolean.TRUE);
- userSubscribeAlertGoodsRecordMapper.updateById(userSubscribeAlertGoodsRecord);
- Long userId = userSubscribeAlertGoodsRecord.getUserId();
- User user = userMapper.selectById(userId);
- // //微信用户
- // if (StrUtil.isNotBlank(user.getUnionid())) {
- // //发送微信通知模板
- // Boolean success = templateCommonService.sendUserSubscribeAlertMsg(user, String.format(Constant.GOODS_SUBSCRIBE_ALERT_WX_MSG, title), goodsDon.getId());
- // if (!success) {
- // //未成功 查看其他绑定信息
- // sendPhoneNotifySubscribe(title, goodsDon.getId(), userId);
- // }
- // continue;
- // }
- //首先用发手机号
- Boolean success = sendPhoneNotifySubscribe(title, goodsDon.getId(), userId);
- if (!success) {
- //失败 并且是邮箱用户
- if (StrUtil.isNotBlank(user.getEmail())) {
- //邮箱用户
- String msg = String.format(Constant.GOODS_SUBSCRIBE_ALERT_EMAIL_MSG, title, goodsDon.getId());
- mailService.sendGoodsSubscribeAlertEmail(msg, user.getEmail());
- }
- }
- }
- } finally {
- if (redisService.hasKey(key)) {
- redisService.del(key);
- }
- }
- }
- public Boolean sendPhoneNotifySubscribe(String title, Long goodsId, Long userId) {
- String msg = String.format(Constant.GOODS_SUBSCRIBE_ALERT_MSG, title);
- try {
- smsService.sendSmsToRelationUser(userId, msg);
- return true;
- } catch (Exception e) {
- log.error("发送商品订阅通知短信失败:{}", StringUtil.getErrorText(e));
- }
- return false;
- }
- }
|