Commit f4910866 by danmo

添加群发任务发送执行器处理群发

1 parent 97aa8787
......@@ -72,7 +72,7 @@ public class WeGroupMessageTemplateController extends BaseController {
* 删除群发消息模板
*/
//@PreAuthorize("@ss.hasPermi('linkwechat:template:remove')")
@ApiOperation(value = "删除群发消息模板", httpMethod = "DELETE")
@ApiOperation(value = "取消发送", httpMethod = "DELETE")
@Log(title = "群发消息模板", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable Long[] ids) {
......
......@@ -410,5 +410,12 @@ public class WeConstans {
public static final String CONVERSATION_MSG_CHANNEL = "CONVERSATION_MSG_CHANNEL";
/**
* 群发消息任务key
*/
public static final String WEGROUPMSGTIMEDTASK_KEY = "weGroupMessageTimedTask";
/**
* 群发消息任务开关
*/
public static Boolean WEGROUPMSGTIMEDTASK_SWITCH = true;
}
......@@ -236,6 +236,17 @@ public class RedisCache {
* 删除元素 zrem
*
* @param key
* @param start
* @param end
*/
public Long removeRangeCacheZSet(String key, long start, long end) {
return redisTemplate.opsForZSet().removeRange(key, start,end);
}
/**
* 删除元素 zrem
*
* @param key
* @param value
*/
public long removeCacheZSet(String key, String value) {
......
......@@ -52,12 +52,6 @@ public class PullGroupMessageTask {
@Autowired
private IWeMaterialService weMaterialService;
@Autowired
private WeCustomerMessagePushClient customerMessagePushClient;
@Autowired
private RedisCache redisCache;
/**
* 同步开始时间
*/
......@@ -255,52 +249,4 @@ public class PullGroupMessageTask {
groupMessageTaskService.addOrUpdateBatchByCondition(taskList);
sendResultService.addOrUpdateBatchByCondition(sendResultlist);
}
public void groupMessageTaskHandler(){
Long cacheZSetSize = redisCache.getCacheZSetSize(WeConstans.WEGROUPMSGTIMEDTASK_KEY);
if(cacheZSetSize > 0){
Set<ZSetOperations.TypedTuple<String>> typedTuples = redisCache.rangeWithScore(WeConstans.WEGROUPMSGTIMEDTASK_KEY, 0, 0);
typedTuples.forEach(item -> {
long score = Objects.requireNonNull(item.getScore()).longValue();
if(score < System.currentTimeMillis()){
//当时间小于当前时间时删除第一个记录,并发送群发消息
log.info("获取到消息,执行群发任务: {}",item.getValue());
redisCache.removeCacheZSet(WeConstans.WEGROUPMSGTIMEDTASK_KEY,item.getValue());
WeAddGroupMessageQuery query = JSONObject.parseObject(item.getValue(), WeAddGroupMessageQuery.class);
List<WeAddGroupMessageQuery.SenderInfo> senderList = query.getSenderList();
List<WeGroupMessageList> weGroupMessageLists = new ArrayList<>();
WeGroupMessageList weGroupMessageList = groupMessageListService.getOne(new LambdaQueryWrapper<WeGroupMessageList>()
.eq(WeGroupMessageList::getMsgTemplateId, query.getId()).last("limit 1"));
if(weGroupMessageList != null && StringUtils.isEmpty(weGroupMessageList.getMsgId())){
senderList.forEach(sender ->{
WeAddMsgTemplateQuery templateQuery = new WeAddMsgTemplateQuery();
templateQuery.setChat_type(query.getChatType());
templateQuery.setSender(sender.getUserId());
if(ObjectUtil.equal(1,query.getChatType())){
templateQuery.setExternal_userid(sender.getCustomerList());
}
templateQuery.setAttachments(query.getAttachmentsList());
if(StringUtils.isNotEmpty(query.getContent())){
WeAddMsgTemplateQuery.Text text = new WeAddMsgTemplateQuery.Text();
text.setContent(query.getContent());
templateQuery.setText(text);
}
SendMessageResultDto resultDto = customerMessagePushClient.addMsgTemplate(templateQuery);
if(resultDto != null && ObjectUtil.equal(WeConstans.WE_SUCCESS_CODE,resultDto.getErrcode())){
String msgid = resultDto.getMsgid();
Long msgTemplateId = query.getId();
WeGroupMessageList messageList = new WeGroupMessageList();
messageList.setMsgId(msgid);
weGroupMessageLists.add(messageList);
groupMessageListService.update(messageList,new LambdaQueryWrapper<WeGroupMessageList>()
.eq(WeGroupMessageList::getMsgTemplateId,msgTemplateId)
.eq(WeGroupMessageList::getUserId,sender.getUserId()));
}
});
}
}
});
}
}
}
package com.linkwechat.wecom.handler;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeCustomerMessagePushClient;
import com.linkwechat.wecom.domain.WeGroupMessageList;
import com.linkwechat.wecom.domain.WeGroupMessageTemplate;
import com.linkwechat.wecom.domain.dto.message.SendMessageResultDto;
import com.linkwechat.wecom.domain.query.WeAddGroupMessageQuery;
import com.linkwechat.wecom.domain.query.WeAddMsgTemplateQuery;
import com.linkwechat.wecom.service.IWeGroupMessageListService;
import com.linkwechat.wecom.service.IWeGroupMessageTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.ZSetOperations;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author danmo
* @description 群发任务发送执行器
* @date 2021/10/29 11:43
**/
@Slf4j
public class WeGroupMessageTaskHandler implements ApplicationRunner {
@Autowired
private IWeGroupMessageListService groupMessageListService;
@Autowired
private IWeGroupMessageTemplateService groupMessageTemplateService;
@Autowired
private WeCustomerMessagePushClient customerMessagePushClient;
@Autowired
private RedisCache redisCache;
private AtomicLong templateId;
@Override
public void run(ApplicationArguments args) {
ThreadUtil.execute(this::groupMessageTaskHandler);
}
public void groupMessageTaskHandler() {
while (WeConstans.WEGROUPMSGTIMEDTASK_SWITCH) {
try {
Set<ZSetOperations.TypedTuple<String>> typedTuples = redisCache.rangeWithScore(WeConstans.WEGROUPMSGTIMEDTASK_KEY, 0, 0);
Optional.ofNullable(typedTuples).orElseGet(HashSet::new).forEach(item -> {
long score = Objects.requireNonNull(item.getScore()).longValue();
if (score < System.currentTimeMillis()) {
//当时间小于当前时间时删除第一个记录,并发送群发消息
log.info("获取到消息,执行群发任务: {}", item.getValue());
WeAddGroupMessageQuery query = JSONObject.parseObject(item.getValue(), WeAddGroupMessageQuery.class);
if (query != null) {
templateId = new AtomicLong(query.getId());
Optional.of(query).map(WeAddGroupMessageQuery::getSenderList).orElseGet(ArrayList::new).forEach(sender -> {
WeAddMsgTemplateQuery templateQuery = new WeAddMsgTemplateQuery();
templateQuery.setChat_type(query.getChatType());
templateQuery.setSender(sender.getUserId());
if (ObjectUtil.equal(1, query.getChatType())) {
templateQuery.setExternal_userid(sender.getCustomerList());
}
templateQuery.setAttachments(query.getAttachmentsList());
if (StringUtils.isNotEmpty(query.getContent())) {
WeAddMsgTemplateQuery.Text text = new WeAddMsgTemplateQuery.Text();
text.setContent(query.getContent());
templateQuery.setText(text);
}
SendMessageResultDto resultDto = customerMessagePushClient.addMsgTemplate(templateQuery);
if (resultDto != null && ObjectUtil.equal(WeConstans.WE_SUCCESS_CODE, resultDto.getErrcode())) {
String msgid = resultDto.getMsgid();
Long msgTemplateId = query.getId();
WeGroupMessageList messageList = new WeGroupMessageList();
messageList.setMsgId(msgid);
groupMessageListService.update(messageList, new LambdaQueryWrapper<WeGroupMessageList>()
.eq(WeGroupMessageList::getMsgTemplateId, msgTemplateId)
.eq(WeGroupMessageList::getUserId, sender.getUserId()));
}
});
}
}
});
} catch (Exception e) {
e.printStackTrace();
if (templateId != null) {
WeGroupMessageTemplate template = new WeGroupMessageTemplate();
template.setId(templateId.get());
template.setStatus(-1);
groupMessageTemplateService.updateById(template);
}
} finally {
redisCache.removeRangeCacheZSet(WeConstans.WEGROUPMSGTIMEDTASK_KEY, 0, 0);
}
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!