RyTask.java
6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.linkwechat.quartz.task;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.Lists;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.elasticsearch.ElasticSearch;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeCustomerMessageTimeTask;
import com.linkwechat.wecom.mapper.WeCustomerMessageTimeTaskMapper;
import com.linkwechat.wecom.service.*;
import com.tencent.wework.FinanceUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
/**
* 定时任务调度测试
*
* @author ruoyi
*/
@Slf4j
@Component("ryTask")
public class RyTask {
@Autowired
private ElasticSearch elasticSearch;
@Autowired
private RedisCache redisCache;
@Autowired
private IWeChatContactMappingService weChatContactMappingService;
@Autowired
private IWeSensitiveService weSensitiveService;
@Autowired
private IWeSensitiveActHitService weSensitiveActHitService;
@Autowired
private IWeCustomerService weCustomerService;
@Autowired
private WeCustomerMessageTimeTaskMapper customerMessageTimeTaskMapper;
@Autowired
private IWeCustomerMessageService weCustomerMessageService;
public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
}
public void ryParams(String params) {
System.out.println("执行有参方法:" + params);
}
public void ryNoParams() {
System.out.println("执行无参方法");
}
public void FinanceTask(String corpId, String secret) throws IOException {
log.info("执行有参方法: params:{},{}", corpId, secret);
//创建索引
elasticSearch.createIndex2(WeConstans.WECOM_FINANCE_INDEX, elasticSearch.getFinanceMapping());
//从缓存中获取消息标识
Object seqObject = Optional.ofNullable(redisCache.getCacheObject(WeConstans.CONTACT_SEQ_KEY)).orElse(0L);
Long seqLong = Long.valueOf(String.valueOf(seqObject));
AtomicLong index = new AtomicLong(seqLong);
if (index.get() == 0) {
setRedisCacheSeqValue(index);
}
log.info(">>>>>>>seq:{}", index.get());
FinanceUtils.initSDK(corpId, secret);
List<JSONObject> chatDataList = FinanceUtils.getChatData(index.get(),
"",
"", redisCache);
if (CollectionUtil.isNotEmpty(chatDataList)) {
try {
List<JSONObject> elasticSearchEntities = weChatContactMappingService.saveWeChatContactMapping(chatDataList);
//获取敏感行为命中信息
weSensitiveActHitService.hitWeSensitiveAct(chatDataList);
List<Consumer<List<JSONObject>>> consumerList = Lists.newArrayList();
consumerList.add(weSensitiveService::hitSensitive);
elasticSearch.insertBatchAsync(WeConstans.WECOM_FINANCE_INDEX, elasticSearchEntities, consumerList);
} catch (Exception e) {
log.error("消息处理异常:ex:{}", e);
e.printStackTrace();
}
}
}
public void WeCustomers(){
//查询系统所有客户
List<WeCustomer> cacheList = redisCache.getCacheList(WeConstans.WECUSTOMERS_KEY);
if(CollectionUtils.isEmpty(cacheList)){
List<WeCustomer> customers = weCustomerService.selectWeCustomerList(null);
redisCache.setCacheList(WeConstans.WECUSTOMERS_KEY,customers);
}else {
List<WeCustomer> customers = weCustomerService.selectWeCustomerList(null);
List<WeCustomer> weCustomers = redisCache.getCacheList(WeConstans.WECUSTOMERS_KEY);
if(CollectionUtils.isNotEmpty(weCustomers) && weCustomers.size()<customers.size()){
redisCache.setCacheList(WeConstans.WECUSTOMERS_KEY,customers);
}
}
}
private void setRedisCacheSeqValue(AtomicLong index) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SortBuilder<?> sortBuilderPrice = SortBuilders.fieldSort(WeConstans.CONTACT_SEQ_KEY).order(SortOrder.DESC);
searchSourceBuilder.sort(sortBuilderPrice);
searchSourceBuilder.size(1);
List<JSONObject> searchResultList = elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, searchSourceBuilder, JSONObject.class);
searchResultList.stream().findFirst().ifPresent(result -> {
index.set(result.getLong(WeConstans.CONTACT_SEQ_KEY) + 1);
});
redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY, index);
}
/**
* @param corpId 企业id
* @param secret 会话密钥
*/
public void getPermitUserList(String corpId, String secret) {
log.info("执行有参方法: params:{},{}", corpId, secret);
}
public void messageTask(){
//获的当前时间的毫秒数
long currentTime = System.currentTimeMillis();
//customerMessageTimeTaskMapper
List<WeCustomerMessageTimeTask> weCustomerMessageTimeTasks = customerMessageTimeTaskMapper.selectWeCustomerMessageTimeTaskGteSettingTime(currentTime);
if(CollectionUtils.isNotEmpty(weCustomerMessageTimeTasks)){
weCustomerMessageTimeTasks.forEach(s->{
try {
weCustomerMessageService.sendMessgae(s.getMessageInfo(), s.getMessageId(),s.getCustomersInfo(),s.getGroupsInfo());
} catch (JsonProcessingException e) {
log.error("定时群发消息处理异常:ex:{}", e);
e.printStackTrace();
}
});
}
}
}