GoodsSubscribeAlertServiceImpl.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package com.cyksj.service.goods.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.cyksj.common.constant.Constant;
  6. import com.cyksj.common.util.StringUtil;
  7. import com.cyksj.dto.RedisKey;
  8. import com.cyksj.mapper.GoodsDonMapper;
  9. import com.cyksj.mapper.GoodsDonSkuMapper;
  10. import com.cyksj.mapper.UserMapper;
  11. import com.cyksj.mapper.UserSubscribeAlertGoodsRecordMapper;
  12. import com.cyksj.model.entity.GoodsDon;
  13. import com.cyksj.model.entity.GoodsDonSku;
  14. import com.cyksj.model.entity.User;
  15. import com.cyksj.model.entity.UserSubscribeAlertGoodsRecord;
  16. import com.cyksj.redis.RedisService;
  17. import com.cyksj.service.goods.GoodsSubscribeAlertService;
  18. import com.cyksj.service.mail.MailService;
  19. import com.cyksj.service.sms.SmsService;
  20. import lombok.RequiredArgsConstructor;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.springframework.stereotype.Service;
  23. import java.util.List;
  24. /**
  25. * 项目名: yhlxj11111111
  26. * 文件名: GoodsSubscribeAlertServiceImpl
  27. * 创建者: JavaZou
  28. * 创建时间:2026/1/6 15:35
  29. */
  30. @RequiredArgsConstructor
  31. @Service
  32. @Slf4j
  33. public class GoodsSubscribeAlertServiceImpl implements GoodsSubscribeAlertService {
  34. private final UserSubscribeAlertGoodsRecordMapper userSubscribeAlertGoodsRecordMapper;
  35. private final UserMapper userMapper;
  36. private final GoodsDonMapper goodsDonMapper;
  37. private final SmsService smsService;
  38. private final MailService mailService;
  39. private final GoodsDonSkuMapper skuMapper;
  40. private final RedisService redisService;
  41. @Override
  42. public void alertBeforeSubscribeByGoodsId(Long goodsId) {
  43. DateTime now = DateTime.now();
  44. List<UserSubscribeAlertGoodsRecord> userSubscribeAlertGoodsRecords = userSubscribeAlertGoodsRecordMapper.selectList(Wrappers.lambdaQuery(UserSubscribeAlertGoodsRecord.class)
  45. .eq(UserSubscribeAlertGoodsRecord::getGoodsId, goodsId)
  46. .eq(UserSubscribeAlertGoodsRecord::getStatus, Boolean.FALSE)
  47. .le(UserSubscribeAlertGoodsRecord::getCreatedTime, now));
  48. if (userSubscribeAlertGoodsRecords.isEmpty()) {
  49. return;
  50. }
  51. String key = RedisKey.GOODS_SUBSCRIBE_ALERT_KEY + goodsId;
  52. if (!redisService.setNx(key, goodsId, 60L * 30)) {
  53. return;
  54. }
  55. try {
  56. GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
  57. for (UserSubscribeAlertGoodsRecord userSubscribeAlertGoodsRecord : userSubscribeAlertGoodsRecords) {
  58. userSubscribeAlertGoodsRecord.setStatus(Boolean.TRUE);
  59. userSubscribeAlertGoodsRecordMapper.updateById(userSubscribeAlertGoodsRecord);
  60. Long userId = userSubscribeAlertGoodsRecord.getUserId();
  61. User user = userMapper.selectById(userId);
  62. // //微信用户
  63. // if (StrUtil.isNotBlank(user.getUnionid())) {
  64. // //发送微信通知模板
  65. // Boolean success = templateCommonService.sendUserSubscribeAlertMsg(user, String.format(Constant.GOODS_SUBSCRIBE_ALERT_WX_MSG, goodsDon.getTitle()), goodsId);
  66. // if (!success) {
  67. // //失败后发送手机
  68. // sendPhoneNotifySubscribe(goodsDon.getTitle(), goodsId, userId);
  69. // }
  70. // continue;
  71. // }
  72. //首先用发手机号
  73. Boolean success = sendPhoneNotifySubscribe(goodsDon.getTitle(), goodsId, userId);
  74. if (!success) {
  75. //失败 并且是邮箱用户
  76. if (StrUtil.isNotBlank(user.getEmail())) {
  77. //邮箱用户
  78. String msg = String.format(Constant.GOODS_SUBSCRIBE_ALERT_EMAIL_MSG, goodsDon.getTitle(), goodsDon.getId());
  79. mailService.sendGoodsSubscribeAlertEmail(msg, user.getEmail());
  80. }
  81. }
  82. }
  83. } finally {
  84. if (redisService.hasKey(key)) {
  85. redisService.del(key);
  86. }
  87. }
  88. }
  89. @Override
  90. public void alertBeforeSubscribeBySkuId(Long skuId) {
  91. DateTime now = DateTime.now();
  92. List<UserSubscribeAlertGoodsRecord> userSubscribeAlertGoodsRecords = userSubscribeAlertGoodsRecordMapper.selectList(Wrappers.lambdaQuery(UserSubscribeAlertGoodsRecord.class)
  93. .eq(UserSubscribeAlertGoodsRecord::getSkuId, skuId)
  94. .eq(UserSubscribeAlertGoodsRecord::getStatus, Boolean.FALSE)
  95. .le(UserSubscribeAlertGoodsRecord::getCreatedTime, now));
  96. if (userSubscribeAlertGoodsRecords.isEmpty()) {
  97. return;
  98. }
  99. GoodsDonSku sku = skuMapper.selectById(skuId);
  100. GoodsDon goodsDon = goodsDonMapper.selectById(sku.getGoodsId());
  101. String key = RedisKey.GOODS_SUBSCRIBE_ALERT_KEY + goodsDon.getId() + ":" + skuId;
  102. if (!redisService.setNx(key, skuId, 60L * 30)) {
  103. return;
  104. }
  105. try {
  106. String title = goodsDon.getTitle() + sku.getSpecVal();
  107. for (UserSubscribeAlertGoodsRecord userSubscribeAlertGoodsRecord : userSubscribeAlertGoodsRecords) {
  108. userSubscribeAlertGoodsRecord.setStatus(Boolean.TRUE);
  109. userSubscribeAlertGoodsRecordMapper.updateById(userSubscribeAlertGoodsRecord);
  110. Long userId = userSubscribeAlertGoodsRecord.getUserId();
  111. User user = userMapper.selectById(userId);
  112. // //微信用户
  113. // if (StrUtil.isNotBlank(user.getUnionid())) {
  114. // //发送微信通知模板
  115. // Boolean success = templateCommonService.sendUserSubscribeAlertMsg(user, String.format(Constant.GOODS_SUBSCRIBE_ALERT_WX_MSG, title), goodsDon.getId());
  116. // if (!success) {
  117. // //未成功 查看其他绑定信息
  118. // sendPhoneNotifySubscribe(title, goodsDon.getId(), userId);
  119. // }
  120. // continue;
  121. // }
  122. //首先用发手机号
  123. Boolean success = sendPhoneNotifySubscribe(title, goodsDon.getId(), userId);
  124. if (!success) {
  125. //失败 并且是邮箱用户
  126. if (StrUtil.isNotBlank(user.getEmail())) {
  127. //邮箱用户
  128. String msg = String.format(Constant.GOODS_SUBSCRIBE_ALERT_EMAIL_MSG, title, goodsDon.getId());
  129. mailService.sendGoodsSubscribeAlertEmail(msg, user.getEmail());
  130. }
  131. }
  132. }
  133. } finally {
  134. if (redisService.hasKey(key)) {
  135. redisService.del(key);
  136. }
  137. }
  138. }
  139. public Boolean sendPhoneNotifySubscribe(String title, Long goodsId, Long userId) {
  140. String msg = String.format(Constant.GOODS_SUBSCRIBE_ALERT_MSG, title);
  141. try {
  142. smsService.sendSmsToRelationUser(userId, msg);
  143. return true;
  144. } catch (Exception e) {
  145. log.error("发送商品订阅通知短信失败:{}", StringUtil.getErrorText(e));
  146. }
  147. return false;
  148. }
  149. }