|
@@ -0,0 +1,124 @@
|
|
|
+package com.qs.mp.quartz.task;
|
|
|
+
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
+import com.qs.mp.admin.domain.GroupBuyingMsg;
|
|
|
+import com.qs.mp.admin.domain.Marketing;
|
|
|
+import com.qs.mp.admin.domain.MarketingMsg;
|
|
|
+import com.qs.mp.admin.domain.vo.GroupBuying;
|
|
|
+import com.qs.mp.admin.service.IGroupBuyingMsgService;
|
|
|
+import com.qs.mp.admin.service.IMarketingMsgService;
|
|
|
+import com.qs.mp.admin.service.IMarketingService;
|
|
|
+import com.qs.mp.common.core.redis.DistributedLocker;
|
|
|
+import com.qs.mp.common.enums.MarketingStatusEnum;
|
|
|
+import com.qs.mp.common.enums.UserTypeEnum;
|
|
|
+import com.qs.mp.common.utils.DateUtils;
|
|
|
+import com.qs.mp.common.utils.LogUtil;
|
|
|
+import com.qs.mp.framework.redis.RedisLockKey;
|
|
|
+import com.qs.mp.framework.service.IWxSubscribeMessage;
|
|
|
+import com.qs.mp.user.domain.MarketingUserCode;
|
|
|
+import com.qs.mp.user.service.IMarketingUserCodeService;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.aop.framework.AopContext;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 拼团活动相关任务
|
|
|
+ *
|
|
|
+ * @author yang.zhao
|
|
|
+ */
|
|
|
+@Component("groupBuyingTask")
|
|
|
+public class GroupBuyingTask {
|
|
|
+
|
|
|
+ protected final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DistributedLocker distributedLocker;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IWxSubscribeMessage wxSubscribeMessage;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IMarketingMsgService marketingMsgService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IGroupBuyingMsgService groupBuyingMsgService;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定时发送一定数量的活动订阅消息
|
|
|
+ * @param limit
|
|
|
+ * @param type 1开奖消息,2活动开始消息
|
|
|
+ */
|
|
|
+ public void sendMsgByLimit(Integer limit, Integer type) {
|
|
|
+
|
|
|
+ String lockKey = "GROUPBUYING_SEND_MSG_LIMIT_KEY";
|
|
|
+ // 同时只能有一个线程发送活动通知任务,自动续期
|
|
|
+ if (!distributedLocker.tryLock(lockKey,0, -1, TimeUnit.SECONDS)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<GroupBuyingMsg> groupBuyingMsgList = new ArrayList<>();
|
|
|
+ if (1 == type){
|
|
|
+ groupBuyingMsgList = groupBuyingMsgService.list(new LambdaQueryWrapper<GroupBuyingMsg>()
|
|
|
+ .eq(GroupBuyingMsg::getType, type)
|
|
|
+ .last("limit " + limit));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取活动通知信息
|
|
|
+ /*marketingMsgList = marketingMsgService.list(new LambdaQueryWrapper<MarketingMsg>()
|
|
|
+ .select(MarketingMsg::getMarketingId, MarketingMsg::getUserId, MarketingMsg::getContext)
|
|
|
+ .eq(MarketingMsg::getType, 3)
|
|
|
+ .groupBy(MarketingMsg::getMarketingId, MarketingMsg::getUserId, MarketingMsg::getContext)
|
|
|
+ .last("limit " + limit));*/
|
|
|
+
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(groupBuyingMsgList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (GroupBuyingMsg groupBuyingMsg : groupBuyingMsgList) {
|
|
|
+ Marketing marketing = new Marketing();
|
|
|
+ marketing.setId(groupBuyingMsg.getGroupbuyingId());
|
|
|
+ marketing.setTitle(groupBuyingMsg.getContext());
|
|
|
+
|
|
|
+ if (type == 1) {
|
|
|
+ // 发送活动开奖通知
|
|
|
+ wxSubscribeMessage.sendMarketingLottery(groupBuyingMsg.getUserId(), marketing);
|
|
|
+ } else if (type == 2){
|
|
|
+ // 发送活动开始通知
|
|
|
+ wxSubscribeMessage.sendMarketingStart(groupBuyingMsg.getUserId(), marketing);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除已发送的消息
|
|
|
+ marketingMsgService.removeByIds(groupBuyingMsgList.stream().map(GroupBuyingMsg::getId).collect(Collectors.toList()));
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtil.error(logger, e, "发送拼团活动订阅通知异常");
|
|
|
+ } finally {
|
|
|
+ distributedLocker.unlock(lockKey);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|