LogPushRunner.java
3.56 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
package vion.cron;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.date.TimeUtil;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.cron.CronUtil;
import org.dromara.hutool.cron.task.Task;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import vion.constant.RedisKeyEnum;
import vion.model.RStoreUser;
import vion.model.Store;
import vion.model.User;
import vion.service.IRStoreUserService;
import vion.service.IStoreService;
import vion.third.DingMod;
import vion.utils.JsonUtil;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author vion
* @date 2024/4/23
*/
@Component
@RequiredArgsConstructor
@Slf4j
public class LogPushRunner {
private final IStoreService storeService;
private final IRStoreUserService storeUserService;
private final DingMod dingMod;
private final RedissonClient redissonClient;
@Bean
public void scheduledPush() {
var storeList = storeService.lambdaQuery().isNotNull(Store::getCronStr).list();
Opt.ofEmptyAble(storeList)
.ifPresent(list -> {
var idList = list.stream().map(Store::getId).toList();
var storeUserMap = storeUserService.lambdaQuery()
.in(RStoreUser::getStoreId, idList).list()
.stream()
.filter(v -> v.getIsMain().equals(1))
.collect(Collectors.toMap(RStoreUser::getStoreId, RStoreUser::getUserId));
for (Store store : list) {
var task = getTask(store, storeUserMap);
CronUtil.schedule(store.getCronStr(), task);
}
if (!CronUtil.getScheduler().isStarted()) {
CronUtil.setMatchSecond(true);
CronUtil.start();
}
});
}
public Task getTask(Store store, Map<Long, Long> storeUserMap) {
return () -> {
var userId = storeUserMap.get(store.getId());
if (ObjUtil.isNull(userId)) {
log.info("storeId:{},该项目未设置项目经理,不发送日志提醒", store.getId());
return;
}
var user = (User) redissonClient.getBucket(RedisKeyEnum.DING_PREFIX.getVal() + RedisKeyEnum.USER_ID.getVal() + userId).get();
dingMod.workMsg(buildMsg(store.getName(), user.getUserid()));
log.info("storeId:{},项目发送日志提醒,提醒人:{}({})", store.getId(), user.getUsername(), user.getUserid());
};
}
String buildMsg(String storeName, String userid) {
var jsonObj = JsonUtil.createObj()
.put("agent_id", 2358374016L)
.put("userid_list", userid);
var content = JsonUtil.createObj()
.put("title", "项目日志待提交提醒")
.put("text", StrUtil.format("""
### 项目日志待提交提醒
#### 项目:[{}]请及时提交日志
#### 发送时间:{}""", storeName, TimeUtil.formatNormal(LocalDateTime.now())));
var msg = JsonUtil.createObj()
.put("msgtype", "markdown")
.set("markdown", content);
jsonObj.set("msg", msg);
return jsonObj.toString();
}
}