| | |
| | | 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 lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | |
| | | public class CloudWmsNotifySchedule { |
| | | |
| | | private static final int BATCH_LIMIT = 50; |
| | | private static final int STORE_BODY_MAX_CHARS_DEFAULT = 2000; |
| | | |
| | | @Autowired |
| | | private CloudWmsNotifyLogService cloudWmsNotifyLogService; |
| | |
| | | private CloudWmsReportService cloudWmsReportService; |
| | | @Autowired |
| | | private ObjectMapper objectMapper; |
| | | @Autowired |
| | | private ConfigService configService; |
| | | |
| | | @Scheduled(cron = "0/30 * * * * ?") |
| | | public void syncCloudWmsNotify() { |
| | |
| | | if (logRecord.getLastNotifyTime() != null) { |
| | | long elapsed = (nowMs - logRecord.getLastNotifyTime().getTime()) / 1000; |
| | | if (elapsed < intervalSeconds) { |
| | | log.info("云仓上报待办跳过:未到重试间隔,id={},bizRef={},elapsed={}s,interval={}s", |
| | | logRecord.getId(), logRecord.getBizRef(), elapsed, intervalSeconds); |
| | | // log.info("云仓上报待办跳过:未到重试间隔,id={},bizRef={},elapsed={}s,interval={}s", |
| | | // logRecord.getId(), logRecord.getBizRef(), elapsed, intervalSeconds); |
| | | continue; |
| | | } |
| | | } |
| | |
| | | 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(requestBody); |
| | | logRecord.setLastResponseBody(responseJson); |
| | | logRecord.setLastRequestBody(truncateForStore(requestBody)); |
| | | logRecord.setLastResponseBody(truncateForStore(responseJson)); |
| | | logRecord.setLastNotifyTime(now); |
| | | logRecord.setRetryCount(nextRetry); |
| | | logRecord.setNotifyStatus(status); |
| | |
| | | } |
| | | |
| | | private void setFailResult(CloudWmsNotifyLog logRecord, String requestBody, String errorMsg, int nextRetry, Date now, int effectiveMaxRetry) { |
| | | logRecord.setLastRequestBody(requestBody); |
| | | logRecord.setLastResponseBody(errorMsg); |
| | | logRecord.setLastRequestBody(truncateForStore(requestBody)); |
| | | logRecord.setLastResponseBody(truncateForStore(errorMsg)); |
| | | logRecord.setLastNotifyTime(now); |
| | | logRecord.setRetryCount(nextRetry); |
| | | // logRecord.setNotifyStatus(nextRetry >= effectiveMaxRetry ? cloudWmsNotifyLogService.getNotifyStatusFail() : cloudWmsNotifyLogService.getNotifyStatusPending()); |
| | |
| | | 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; |
| | | } |
| | | } |
| | | } |