chen.lin
2 天以前 9140aee230de0ef41de9682a9353fbd372e2bcaa
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
package com.vincent.rsf.server.manager.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.server.manager.entity.CloudWmsNotifyLog;
import com.vincent.rsf.server.manager.mapper.CloudWmsNotifyLogMapper;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
@Service
public class CloudWmsNotifyLogServiceImpl extends ServiceImpl<CloudWmsNotifyLogMapper, CloudWmsNotifyLog> implements CloudWmsNotifyLogService {
 
    @Autowired
    private ConfigService configService;
 
    @Override
    public List<CloudWmsNotifyLog> listPending(int limit, int maxRetry) {
        Page<CloudWmsNotifyLog> page = new Page<>(1, Math.max(1, limit));
        LambdaQueryWrapper<CloudWmsNotifyLog> wrapper = new LambdaQueryWrapper<CloudWmsNotifyLog>()
                .eq(CloudWmsNotifyLog::getNotifyStatus, getNotifyStatusPending())
                .lt(CloudWmsNotifyLog::getRetryCount, maxRetry)
                .orderByAsc(CloudWmsNotifyLog::getId);
        return page(page, wrapper).getRecords();
    }
 
    @Override
    public String getReportTypeInOutResult() {
        return getConfigString(GlobalConfigCode.CLOUD_WMS_REPORT_TYPE_IN_OUT_RESULT, CloudWmsNotifyLog.REPORT_TYPE_IN_OUT_RESULT);
    }
 
    @Override
    public String getReportTypeInventoryAdjust() {
        return getConfigString(GlobalConfigCode.CLOUD_WMS_REPORT_TYPE_INVENTORY_ADJUST, CloudWmsNotifyLog.REPORT_TYPE_INVENTORY_ADJUST);
    }
 
    @Override
    public int getNotifyStatusPending() {
        return getConfigInt(GlobalConfigCode.CLOUD_WMS_NOTIFY_STATUS_PENDING, CloudWmsNotifyLog.NOTIFY_STATUS_PENDING);
    }
 
    @Override
    public int getNotifyStatusSuccess() {
        return getConfigInt(GlobalConfigCode.CLOUD_WMS_NOTIFY_STATUS_SUCCESS, CloudWmsNotifyLog.NOTIFY_STATUS_SUCCESS);
    }
 
    @Override
    public int getNotifyStatusFail() {
        return getConfigInt(GlobalConfigCode.CLOUD_WMS_NOTIFY_STATUS_FAIL, CloudWmsNotifyLog.NOTIFY_STATUS_FAIL);
    }
 
    private String getConfigString(String flag, String defaultVal) {
        Config c = configService.getOne(new LambdaQueryWrapper<Config>().eq(Config::getFlag, flag).last("LIMIT 1"));
        if (c != null && c.getVal() != null && !c.getVal().isEmpty()) {
            return c.getVal().trim();
        }
        return defaultVal;
    }
 
    private int getConfigInt(String flag, int defaultVal) {
        Integer v = getConfigInt(flag);
        return v != null ? v : defaultVal;
    }
 
    @Override
    public void fillFromConfig(CloudWmsNotifyLog log) {
        Integer maxRetry = getConfigInt(GlobalConfigCode.CLOUD_WMS_NOTIFY_MAX_RETRY);
        Integer interval = getConfigInt(GlobalConfigCode.CLOUD_WMS_NOTIFY_RETRY_INTERVAL_SECONDS);
        log.setMaxRetryCount(maxRetry);
        log.setRetryIntervalSeconds(interval);
    }
 
    /** 返回 null 表示未配置或解析失败 */
    private Integer getConfigInt(String flag) {
        try {
            Config c = configService.getOne(new LambdaQueryWrapper<Config>().eq(Config::getFlag, flag).last("LIMIT 1"));
            if (c != null && c.getVal() != null && !c.getVal().isEmpty()) {
                return Integer.parseInt(c.getVal().trim());
            }
        } catch (Exception ignored) {
        }
        return null;
    }
}