TodoRpcService.java
7.21 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package cn.fw.valhalla.rpc.erp;
import cn.fw.data.base.domain.common.Message;
import cn.fw.erp.sdk.api.BacklogItemApi;
import cn.fw.erp.sdk.api.param.BacklogItemCancelReq;
import cn.fw.erp.sdk.api.param.BacklogItemCommitReq;
import cn.fw.erp.sdk.api.param.BacklogItemPlanReq;
import cn.fw.valhalla.rpc.erp.dto.BackLogItemDTO;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author : kurisu
* @className : TodoRpcService
* @description : erp待办rpc
* @date: 2020-08-18 15:53
*/
@Slf4j
@Service
public class TodoRpcService {
private final BacklogItemApi backlogItemApi;
private final StringRedisTemplate redisTemplate;
@Value("${follow.todo.failed-prefix}")
@Getter
private String keyPrefix;
@Value("${follow.todo.failed-prefix}:failed")
@Getter
private String failedKeyPrefix;
private final int maxCount = 5;
@Autowired
public TodoRpcService(final BacklogItemApi backlogItemApi,
final StringRedisTemplate redisTemplate) {
this.backlogItemApi = backlogItemApi;
this.redisTemplate = redisTemplate;
}
/**
* 生成待办
*
* @param dto
*/
public boolean push(BackLogItemDTO dto) {
final String func = "调用ERP系统生成待办";
final String key = generateKey(TodoOperationEnum.CREATE);
try {
log.info("{}:params: [{}]", func, dto);
if (dto.getCount() > maxCount) {
setToCache(generateFailKey(TodoOperationEnum.CREATE), dto);
return true;
}
BacklogItemPlanReq req = new BacklogItemPlanReq();
BeanUtils.copyProperties(dto, req);
req.setPlanTime(dto.getDataTime());
Message<Void> message = backlogItemApi.planBacklogItem(req);
log.info("{} 结果:{} desc: {}", func, message.isSuccess(), message.getResult());
if (!message.isSuccess()) {
setToCache(key, dto);
}
return message.isSuccess();
} catch (Exception e) {
log.error("{}失败,params:{}", func, dto, e);
setToCache(key, dto);
return false;
}
}
/**
* 完成待办
*
* @param dto
*/
public boolean complete(BackLogItemDTO dto) {
final String func = "调用ERP系统完成待办";
final String key = generateKey(TodoOperationEnum.COMPLETE);
try {
log.info("{}:params: [{}]", func, dto);
if (dto.getCount() > maxCount) {
setToCache(generateFailKey(TodoOperationEnum.COMPLETE), dto);
return true;
}
BacklogItemCommitReq req = new BacklogItemCommitReq();
BeanUtils.copyProperties(dto, req);
req.setCommitTime(dto.getDataTime());
Message<Void> message = backlogItemApi.commitBacklogItem(req);
log.info("{} 结果:{} desc: {}", func, message.isSuccess(), message.getResult());
if (!message.isSuccess()) {
setToCache(key, dto);
}
return message.isSuccess();
} catch (Exception e) {
log.error("{}失败,params:{}", func, dto, e);
setToCache(key, dto);
return false;
}
}
/**
* 取消待办
*
* @param dto
*/
public boolean cancel(BackLogItemDTO dto) {
final String func = "调用ERP系统取消待办";
final String key = generateKey(TodoOperationEnum.CANCEL);
try {
log.info("{}:params: [{}]", func, dto);
if (dto.getCount() > maxCount) {
setToCache(generateFailKey(TodoOperationEnum.CANCEL), dto);
return true;
}
BacklogItemCancelReq req = new BacklogItemCancelReq();
BeanUtils.copyProperties(dto, req);
Message<Void> message = backlogItemApi.cancelBacklogItem(req);
log.info("{} 结果:{} desc: {}", func, message.isSuccess(), message.getResult());
if (!message.isSuccess()) {
setToCache(key, dto);
}
return message.isSuccess();
} catch (Exception e) {
log.error("{}失败,params:{}", func, dto, e);
setToCache(key, dto);
return false;
}
}
public String generateKey(final TodoOperationEnum type) {
return String.format("%s:%s", getKeyPrefix(), type.name());
}
public String generateFailKey(final TodoOperationEnum type) {
return String.format("%s:%s", getFailedKeyPrefix(), type.name());
}
public Optional<BackLogItemDTO> getOneFromCache(final String key) {
try {
Boolean hasKey = redisTemplate.hasKey(key);
if (!Boolean.TRUE.equals(hasKey)) {
return Optional.empty();
}
BoundListOperations<String, String> ops = redisTemplate.boundListOps(key);
String jsonString = ops.leftPop();
BackLogItemDTO dto = JSONObject.parseObject(jsonString, BackLogItemDTO.class);
if (Objects.isNull(dto)) {
return Optional.empty();
}
return Optional.of(dto);
} catch (Exception e) {
log.error("缓存信息获取失败 key[{}]", key, e);
return Optional.empty();
}
}
public Collection<BackLogItemDTO> getAllFromCache(final String key) {
try {
Boolean hasKey = redisTemplate.hasKey(key);
if (!Boolean.TRUE.equals(hasKey)) {
return new HashSet<>();
}
BoundListOperations<String, String> ops = redisTemplate.boundListOps(key);
List<String> stringList = ops.range(0, -1);
redisTemplate.delete(key);
if (CollectionUtils.isEmpty(stringList)) {
return new HashSet<>();
}
return stringList.stream().map(str -> JSONObject.parseObject(str, BackLogItemDTO.class)).collect(Collectors.toSet());
} catch (Exception e) {
log.error("缓存信息获取失败 key[{}]", key, e);
return new HashSet<>();
}
}
private void setToCache(final String key, final BackLogItemDTO dto) {
try {
dto.setCount(dto.getCount() + 1);
BoundListOperations<String, String> ops = redisTemplate.boundListOps(key);
String jsonString = JSONObject.toJSONString(dto);
ops.rightPush(jsonString);
} catch (Exception e) {
log.error("缓存设置信息失败 key[{}]", key, e);
}
}
public enum TodoOperationEnum {
/**
* 创建
*/
CREATE,
/**
* 完成
*/
COMPLETE,
/**
* 取消
*/
CANCEL
}
}