cl
6 天以前 2a34b52125d5fc356d65ee1e8912845dd601d4e3
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package com.vincent.rsf.server.manager.schedules;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vincent.rsf.server.api.controller.erp.params.InOutResultBatchPayload;
import com.vincent.rsf.server.api.controller.erp.params.InOutResultReportParam;
import com.vincent.rsf.server.api.controller.erp.params.InventoryAdjustReportParam;
import com.vincent.rsf.server.api.service.CloudWmsReportService;
import com.vincent.rsf.server.manager.entity.CloudWmsNotifyLog;
import com.vincent.rsf.server.manager.service.CloudWmsNotifyLogService;
import com.vincent.rsf.server.system.constant.GlobalConfigCode;
import com.vincent.rsf.server.system.entity.Config;
import com.vincent.rsf.server.system.service.ConfigService;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
/** 云仓上报定时任务 */
@Slf4j
@Component
public class CloudWmsNotifySchedule {
 
    private static final int BATCH_LIMIT = -1;
    private static final int STORE_BODY_MAX_CHARS_DEFAULT = 2000;
 
    @Autowired
    private CloudWmsNotifyLogService cloudWmsNotifyLogService;
    @Autowired
    private CloudWmsReportService cloudWmsReportService;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private ConfigService configService;
 
    /** sending=1 且 Redis 占位已失、update_time 超时:补偿清零 */
    @Scheduled(cron = "0 0/2 * * * ?")
//    @Scheduled(cron = "0/5 * * * * ?")
    public void recoverStaleSending() {
        try {
            cloudWmsNotifyLogService.recoverStaleSendingWhenRedisMiss();
        } catch (Exception e) {
            log.warn("云仓 sending 补偿任务异常:{}", e.getMessage());
        }
    }
 
    @Scheduled(cron = "0/60 * * * * ?")
    public void syncCloudWmsNotify() {
        List<CloudWmsNotifyLog> pending = cloudWmsNotifyLogService.listPending(BATCH_LIMIT, -1);
        if (pending.isEmpty()) {
            log.debug("云仓上报调度:本轮待发送 0 条");
            return;
        }
        log.info("云仓上报调度:本轮待发送 {} 条", pending.size());
        dispatchPending(pending);
    }
 
    /** 同单多条合并上报 */
    private void dispatchPending(List<CloudWmsNotifyLog> pending) {
        String rtInOut = cloudWmsNotifyLogService.getReportTypeInOutResult();
        LinkedHashMap<String, List<CloudWmsNotifyLog>> inOutGroups = new LinkedHashMap<>();
        for (CloudWmsNotifyLog row : pending) {
            if (!rtInOut.equals(row.getReportType())) {
                continue;
            }
            String key = cloudWmsNotifyLogService.inOutMergeKeyFromRequestBody(row.getRequestBody());
            if (key == null) {
                continue;
            }
            inOutGroups.computeIfAbsent(key, k -> new ArrayList<>()).add(row);
        }
        Set<Long> done = new HashSet<>();
        for (CloudWmsNotifyLog row : pending) {
            Long rid = row.getId();
            if (rid != null && done.contains(rid)) {
                continue;
            }
            if (!rtInOut.equals(row.getReportType())) {
                safeProcessOne(row);
                if (rid != null) {
                    done.add(rid);
                }
                continue;
            }
            String key = cloudWmsNotifyLogService.inOutMergeKeyFromRequestBody(row.getRequestBody());
            if (key == null) {
                safeProcessOne(row);
                if (rid != null) {
                    done.add(rid);
                }
                continue;
            }
            List<CloudWmsNotifyLog> g = inOutGroups.get(key);
            if (g != null && g.size() >= 2) {
                safeProcessMergedInOutGroup(g);
                for (CloudWmsNotifyLog x : g) {
                    if (x.getId() != null) {
                        done.add(x.getId());
                    }
                }
            } else {
                safeProcessOne(row);
                if (rid != null) {
                    done.add(rid);
                }
            }
        }
    }
 
    private void safeProcessMergedInOutGroup(List<CloudWmsNotifyLog> group) {
        List<Long> claimedIds = new ArrayList<>();
        try {
            for (CloudWmsNotifyLog row : group) {
                Long id = row.getId();
                if (!cloudWmsNotifyLogService.tryClaimSending(id)) {
                    log.debug("云仓上报同批合并未抢到发送权 id={}", id);
                    return;
                }
                claimedIds.add(id);
            }
            processMergedInOut(group);
        } catch (Exception e) {
            log.warn("云仓上报同批合并异常:{}", e.getMessage());
        } finally {
            for (Long id : claimedIds) {
                cloudWmsNotifyLogService.clearSending(id);
            }
        }
    }
 
    private void processMergedInOut(List<CloudWmsNotifyLog> group) {
        Date now = new Date();
        List<InOutResultReportParam> lines = new ArrayList<>();
        try {
            for (CloudWmsNotifyLog row : group) {
                lines.addAll(cloudWmsNotifyLogService.parseInOutLinesFromRequestBody(row.getRequestBody()));
            }
        } catch (IOException e) {
            String msg = "反序列化失败: " + e.getMessage();
            for (CloudWmsNotifyLog row : group) {
                int nextRetry = (row.getRetryCount() == null ? 0 : row.getRetryCount()) + 1;
                setFailResult(row, row.getRequestBody(), msg, nextRetry, now, row.getMaxRetryCount());
            }
            return;
        }
        if (lines.isEmpty()) {
            return;
        }
        String mergedBody;
        try {
            mergedBody = objectMapper.writeValueAsString(new InOutResultBatchPayload().setLines(lines));
        } catch (JsonProcessingException e) {
            for (CloudWmsNotifyLog row : group) {
                int nextRetry = (row.getRetryCount() == null ? 0 : row.getRetryCount()) + 1;
                setFailResult(row, row.getRequestBody(), "合并请求体序列化失败: " + e.getMessage(), nextRetry, now, row.getMaxRetryCount());
            }
            return;
        }
        log.info("云仓上报开始(同单合并),ids={},requestBody={}", idsOf(group), mergedBody);
        try {
            Map<String, Object> res = cloudWmsReportService.reportInOutResults(lines);
            for (CloudWmsNotifyLog row : group) {
                int nextRetry = (row.getRetryCount() == null ? 0 : row.getRetryCount()) + 1;
                updateAfterNotify(row, mergedBody, res, nextRetry, now, row.getMaxRetryCount());
            }
        } catch (FeignException e) {
            String responseBody = e.contentUTF8();
            String fullMsg = "status=" + e.status() + ",message=" + e.getMessage()
                    + (responseBody == null || responseBody.isEmpty() ? "" : ",responseBody=" + responseBody);
            log.warn("云仓上报同批合并请求失败:{}", fullMsg);
            for (CloudWmsNotifyLog row : group) {
                int nextRetry = (row.getRetryCount() == null ? 0 : row.getRetryCount()) + 1;
                setFailResult(row, mergedBody, "请求异常: " + fullMsg, nextRetry, now, row.getMaxRetryCount());
            }
        } catch (Exception e) {
            log.warn("云仓上报同批合并请求失败:{}", e.getMessage());
            for (CloudWmsNotifyLog row : group) {
                int nextRetry = (row.getRetryCount() == null ? 0 : row.getRetryCount()) + 1;
                setFailResult(row, mergedBody, "请求异常: " + e.getMessage(), nextRetry, now, row.getMaxRetryCount());
            }
        }
    }
 
    private static List<Long> idsOf(List<CloudWmsNotifyLog> group) {
        List<Long> ids = new ArrayList<>(group.size());
        for (CloudWmsNotifyLog row : group) {
            ids.add(row.getId());
        }
        return ids;
    }
 
    private void safeProcessOne(CloudWmsNotifyLog logRecord) {
        Long id = logRecord.getId();
        if (!cloudWmsNotifyLogService.tryClaimSending(id)) {
            log.debug("云仓上报未抢到发送权 id={}", id);
            return;
        }
        try {
            processOne(logRecord);
        } catch (Exception e) {
            log.warn("云仓上报定时任务处理单条异常,id={},bizRef={}:{}", logRecord.getId(), logRecord.getBizRef(), e.getMessage());
        } finally {
            cloudWmsNotifyLogService.clearSending(id);
        }
    }
 
    private void processOne(CloudWmsNotifyLog logRecord) {
        String reportType = logRecord.getReportType();
        String requestBody = logRecord.getRequestBody();
        Date now = new Date();
        int nextRetry = (logRecord.getRetryCount() == null ? 0 : logRecord.getRetryCount()) + 1;
        int effectiveMaxRetry = logRecord.getMaxRetryCount();
        String rtInOut = cloudWmsNotifyLogService.getReportTypeInOutResult();
        String rtAdj = cloudWmsNotifyLogService.getReportTypeInventoryAdjust();
        log.info("云仓上报开始,id={},bizRef={},reportType={},attempt={},requestBody={}",
                logRecord.getId(), logRecord.getBizRef(), reportType, nextRetry, requestBody);
 
        try {
            if (rtInOut.equals(reportType)) {
                JsonNode root = objectMapper.readTree(requestBody);
                Map<String, Object> res;
                if (root.has("lines") && root.get("lines").isArray()) {
                    InOutResultBatchPayload batch = objectMapper.readValue(requestBody, InOutResultBatchPayload.class);
                    res = cloudWmsReportService.reportInOutResults(batch.getLines());
                } else {
                    InOutResultReportParam param = objectMapper.readValue(requestBody, InOutResultReportParam.class);
                    res = cloudWmsReportService.reportInOutResult(param);
                }
                updateAfterNotify(logRecord, requestBody, res, nextRetry, now, effectiveMaxRetry);
            } else if (rtAdj.equals(reportType)) {
                InventoryAdjustReportParam param = objectMapper.readValue(requestBody, InventoryAdjustReportParam.class);
                Map<String, Object> res = cloudWmsReportService.reportInventoryAdjust(param);
                updateAfterNotify(logRecord, requestBody, res, nextRetry, now, effectiveMaxRetry);
            } else {
                log.warn("未知上报类型,id={},reportType={}", logRecord.getId(), reportType);
                return;
            }
        } catch (JsonProcessingException e) {
            log.warn("云仓上报请求体反序列化失败,id={}:{}", logRecord.getId(), e.getMessage());
            setFailResult(logRecord, requestBody, "反序列化失败: " + e.getMessage(), nextRetry, now, effectiveMaxRetry);
        } catch (FeignException e) {
            String responseBody = e.contentUTF8();
            String fullMsg = "status=" + e.status() + ",message=" + e.getMessage()
                    + (responseBody == null || responseBody.isEmpty() ? "" : ",responseBody=" + responseBody);
            log.warn("云仓上报请求失败,id={},bizRef={}:{}", logRecord.getId(), logRecord.getBizRef(), fullMsg);
            setFailResult(logRecord, requestBody, "请求异常: " + fullMsg, nextRetry, now, effectiveMaxRetry);
        } catch (Exception e) {
            log.warn("云仓上报请求失败,id={},bizRef={}:{}", logRecord.getId(), logRecord.getBizRef(), e.getMessage());
            setFailResult(logRecord, requestBody, "请求异常: " + e.getMessage(), nextRetry, now, effectiveMaxRetry);
        }
    }
 
    private void updateAfterNotify(CloudWmsNotifyLog logRecord, String requestBody, Map<String, Object> res, int nextRetry, Date now, int effectiveMaxRetry) {
        String responseJson;
        try {
            responseJson = res != null ? objectMapper.writeValueAsString(res) : "null";
        } catch (JsonProcessingException e) {
            responseJson = String.valueOf(res);
        }
        Object codeObj = res != null ? res.get("code") : null;
        Object statusObj = res != null ? res.get("status") : null;
        boolean success = Integer.valueOf(200).equals(codeObj) || Integer.valueOf(200).equals(statusObj);
        int status = success ? cloudWmsNotifyLogService.getNotifyStatusSuccess() : cloudWmsNotifyLogService.getNotifyStatusFail();
        logRecord.setLastRequestBody(truncateForStore(requestBody));
        logRecord.setLastResponseBody(truncateForStore(responseJson));
        logRecord.setLastNotifyTime(now);
        logRecord.setRetryCount(nextRetry);
        logRecord.setNotifyStatus(status);
        logRecord.setSending(0);
        logRecord.setUpdateTime(now);
        cloudWmsNotifyLogService.updateById(logRecord);
        log.info("云仓上报结束,id={},bizRef={},attempt={},notifyStatus={},responseBody={}",
                logRecord.getId(), logRecord.getBizRef(), nextRetry, status, responseJson);
    }
 
    private void setFailResult(CloudWmsNotifyLog logRecord, String requestBody, String errorMsg, int nextRetry, Date now, int effectiveMaxRetry) {
        logRecord.setLastRequestBody(truncateForStore(requestBody));
        logRecord.setLastResponseBody(truncateForStore(errorMsg));
        logRecord.setLastNotifyTime(now);
        logRecord.setRetryCount(nextRetry);
        int status = !isInfiniteRetry(effectiveMaxRetry) && nextRetry >= effectiveMaxRetry
                ? cloudWmsNotifyLogService.getNotifyStatusFail()
                : cloudWmsNotifyLogService.getNotifyStatusPending();
        logRecord.setNotifyStatus(status);
        logRecord.setSending(0);
        logRecord.setUpdateTime(now);
        cloudWmsNotifyLogService.updateById(logRecord);
        log.warn("云仓上报失败,id={},bizRef={},attempt={},notifyStatus={},error={}",
                logRecord.getId(), logRecord.getBizRef(), nextRetry, logRecord.getNotifyStatus(), errorMsg);
    }
 
    /** maxRetry = -1 表示无限重发 */
    private boolean isInfiniteRetry(Integer maxRetry) {
        return maxRetry != null && maxRetry == -1;
    }
 
    private String truncateForStore(String body) {
        int maxChars = resolveStoreBodyMaxChars();
        if (body == null || body.length() <= maxChars) {
            return body;
        }
        return body.substring(0, maxChars);
    }
 
    private int resolveStoreBodyMaxChars() {
        try {
            Config cfg = configService.getCachedOrLoad(GlobalConfigCode.CLOUD_WMS_NOTIFY_STORE_BODY_MAX_CHARS);
            if (cfg == null || cfg.getVal() == null || cfg.getVal().trim().isEmpty()) {
                return STORE_BODY_MAX_CHARS_DEFAULT;
            }
            int parsed = Integer.parseInt(cfg.getVal().trim());
            return parsed > 0 ? parsed : STORE_BODY_MAX_CHARS_DEFAULT;
        } catch (Exception e) {
            return STORE_BODY_MAX_CHARS_DEFAULT;
        }
    }
}