FollowRecordTask.java
6.62 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package cn.fw.valhalla.controller.task;
import cn.fw.valhalla.common.utils.DateUtil;
import cn.fw.valhalla.domain.db.follow.FollowRecord;
import cn.fw.valhalla.domain.db.follow.FollowTask;
import cn.fw.valhalla.domain.enums.FollowTypeEnum;
import cn.fw.valhalla.domain.enums.TaskStateEnum;
import cn.fw.valhalla.rpc.erp.TodoRpcService;
import cn.fw.valhalla.rpc.erp.dto.BackLogItemDTO;
import cn.fw.valhalla.service.bus.follow.FollowBizService;
import cn.fw.valhalla.service.data.FollowRecordService;
import cn.fw.valhalla.service.data.FollowTaskService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
/**
* @author : kurisu
* @className : FollowRecordTask
* @description : 跟进任务定时任务
* @date: 2020-08-25 16:56
*/
@Component
@ConditionalOnProperty(prefix = "task", name = "switch", havingValue = "on")
public class FollowRecordTask {
private final FollowRecordService followRecordService;
private final FollowBizService followBizService;
private final TodoRpcService todoRpcService;
private final FollowTaskService followTaskService;
@Value("${follow.todo.FMCode}")
@Getter
private String FMCode;
@Value("${follow.todo.RMCode}")
@Getter
private String RMCode;
@Value("${follow.todo.IRCode}")
@Getter
private String IRCode;
@Value("${follow.todo.ACCode}")
@Getter
private String ACCode;
@Autowired
public FollowRecordTask(final FollowRecordService followRecordService,
final FollowBizService followBizService,
final TodoRpcService todoRpcService,
final FollowTaskService followTaskService) {
this.followRecordService = followRecordService;
this.followBizService = followBizService;
this.todoRpcService = todoRpcService;
this.followTaskService = followTaskService;
}
/**
* 处理跟进逾期
*/
@Scheduled(initialDelay = 1000 * 15, fixedRate = 1000 * 5)
@Transactional(rollbackFor = Exception.class)
public void endTaskRecord() {
List<FollowRecord> list = followRecordService.list(Wrappers.<FollowRecord>lambdaQuery()
.eq(FollowRecord::getOutTime, Boolean.FALSE)
.le(FollowRecord::getDeadline, DateUtil.localDateTime2Date(LocalDateTime.now()))
.isNull(FollowRecord::getFollowTime)
.last("limit 0, 500")
);
if (CollectionUtils.isEmpty(list)) {
return;
}
for (FollowRecord record : list) {
followBizService.overdueProcessing(record);
}
}
/**
* 普通待办指定时间才推送
* 推送到待办系统
*/
@Scheduled(cron = "0/8 30/1 8-18 * * *")
@Transactional(rollbackFor = Exception.class)
public void push2NorTodo() {
List<FollowRecord> list = followRecordService.list(Wrappers.<FollowRecord>lambdaQuery()
.eq(FollowRecord::getOutTime, Boolean.FALSE)
.eq(FollowRecord::getAddTodo, Boolean.FALSE)
.ne(FollowRecord::getType, FollowTypeEnum.AC)
.le(FollowRecord::getPlanTime, DateUtil.localDateTime2Date(LocalDateTime.now()))
.last("limit 0,500")
);
execute(list);
}
/**
* 事故车待办随时推送
* 推送到待办系统
*/
@Scheduled(initialDelay = 1500, fixedRate = 1000 * 3)
@Transactional(rollbackFor = Exception.class)
public void push2AccTodo() {
List<FollowRecord> list = followRecordService.list(Wrappers.<FollowRecord>lambdaQuery()
.eq(FollowRecord::getOutTime, Boolean.FALSE)
.eq(FollowRecord::getType, FollowTypeEnum.AC)
.eq(FollowRecord::getAddTodo, Boolean.FALSE)
.le(FollowRecord::getPlanTime, DateUtil.localDateTime2Date(LocalDateTime.now()))
.last("limit 0,50")
);
execute(list);
}
/**
* 完成待办的重试
*/
@Scheduled(initialDelay = 1500, fixedRate = 15 * 1000)
public void retryCompleteTodoItem() {
String key = todoRpcService.generateKey(TodoRpcService.TodoOperationEnum.COMPLETE);
Collection<BackLogItemDTO> all = todoRpcService.getAllFromCache(key);
all.forEach(todoRpcService::complete);
}
/**
* 取消待办的重试
*/
@Scheduled(initialDelay = 1000, fixedRate = 10 * 1000)
public void retryCancelTodoItem() {
String key = todoRpcService.generateKey(TodoRpcService.TodoOperationEnum.CANCEL);
Collection<BackLogItemDTO> all = todoRpcService.getAllFromCache(key);
all.forEach(todoRpcService::cancel);
}
private void execute(List<FollowRecord> list) {
if (CollectionUtils.isEmpty(list)) {
return;
}
List<Long> idList = new ArrayList<>();
List<Long> failIdList = new ArrayList<>();
for (FollowRecord record : list) {
FollowTask task = followTaskService.getById(record.getTaskId());
if (Objects.nonNull(task) && TaskStateEnum.ONGOING.equals(task.getState())) {
BackLogItemDTO dto = new BackLogItemDTO(record.getUserId(), getItemCode(record.getType()), String.valueOf(record.getId()), record.getPlanTime(), record.getShopId());
dto.setExpireTime(record.getDeadline());
boolean pushed = todoRpcService.push(dto);
if (pushed) {
idList.add(record.getId());
}
} else {
failIdList.add(record.getId());
}
}
followRecordService.addTodoBatchById(idList);
if (!CollectionUtils.isEmpty(failIdList)) {
followRecordService.removeByIds(failIdList);
}
}
private String getItemCode(FollowTypeEnum type) {
switch (type) {
case AC:
return getACCode();
case FM:
return getFMCode();
case IR:
return getIRCode();
case RM:
return getRMCode();
default:
return getFMCode();
}
}
}