自动化立体仓库 - WMS系统
#
Administrator
1 天以前 16bd33b89e43278a1080a5f322356675522c84ee
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
package com.zy.asrs.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.mapper.ErpTokenMapper;
import com.zy.asrs.entity.ErpToken;
import com.zy.asrs.service.ErpTokenService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.core.common.Cools;
import com.zy.common.utils.HttpHandler;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
@Service("erpTokenService")
public class ErpTokenServiceImpl extends ServiceImpl<ErpTokenMapper, ErpToken> implements ErpTokenService {
 
    @Autowired
    private ConfigService configService;
 
    @Override
    public JSONObject refreshToken() {
        String appId = getConfigValue("ErpAppId", "jxlk");
        String appSecrect = getConfigValue("ErpAppSecrect", "jxlk123@jxlk");
        String url = getConfigValue("ErpTokenUrl", "http://127.0.0.1:8080/thb-cloud/rest");
 
        JSONObject result = callRest(url, buildTokenParams(appId, appSecrect));
        if (result == null) {
            return error("ERP token获取失败:空响应");
        }
        if (!Boolean.TRUE.equals(result.getBoolean("success"))) {
            return result;
        }
        JSONObject data = result.getJSONObject("data");
        String token = data == null ? null : data.getString("access_token");
        if (Cools.isEmpty(token)) {
            return error("ERP token获取失败:响应未包含access_token");
        }
        Date now = new Date();
        this.baseMapper.upsertToken(appId, appSecrect, token, now);
        return result;
    }
 
    @Override
    public String getAccessToken() {
        String appId = getConfigValue("ErpAppId", "jxlk");
        ErpToken one = this.selectOne(new EntityWrapper<ErpToken>().eq("app_id", appId));
        if (one != null && !Cools.isEmpty(one.getToken())) {
            return one.getToken();
        }
        JSONObject refreshed = refreshToken();
        if (refreshed == null || !Boolean.TRUE.equals(refreshed.getBoolean("success"))) {
            return null;
        }
        ErpToken again = this.selectOne(new EntityWrapper<ErpToken>().eq("app_id", appId));
        return again == null ? null : again.getToken();
    }
 
    @Override
    public JSONObject stockIn(String labelno) {
        return callWareApi("stockIn", params -> {
            params.put("labelno", labelno);
        });
    }
 
    @Override
    public JSONObject stockOut(String labelno, String tlocation) {
        return callWareApi("stockOut", params -> {
            params.put("labelno", labelno);
            params.put("tlocation", Cools.isEmpty(tlocation) ? "W001" : tlocation);
        });
    }
 
    @Override
    public JSONObject stockBack(String labelno, Double qty, String tlocation) {
        return callWareApi("stockBack", params -> {
            params.put("labelno", labelno);
            params.put("qty", qty);
            params.put("tlocation", Cools.isEmpty(tlocation) ? "RW" : tlocation);
        });
    }
 
    private JSONObject callWareApi(String eventcode, ParamAppender appender) {
        String url = getConfigValue("ErpTokenUrl", "http://127.0.0.1:8080/thb-cloud/rest");
        String appId = getConfigValue("ErpAppId", "jxlk");
        String token = getAccessToken();
        if (Cools.isEmpty(token)) {
            return error("ERP access_token为空");
        }
        Map<String, Object> params = new HashMap<>();
        params.put("funid", "ware_api");
        params.put("eventcode", eventcode);
        params.put("user_code", appId);
        params.put("access_token", token);
        appender.append(params);
        JSONObject result = callRest(url, params);
        if (result != null) {
            return result;
        }
        return error("ERP接口调用失败:空响应");
    }
 
    private Map<String, Object> buildTokenParams(String appId, String appSecrect) {
        Map<String, Object> params = new HashMap<>();
        params.put("funid", "rest_app");
        params.put("eventcode", "get_token");
        params.put("user_code", appId);
        params.put("appid", appId);
        params.put("appsecrect", appSecrect);
        return params;
    }
 
    private JSONObject callRest(String url, Map<String, Object> params) {
        try {
            URI uri = URI.create(url);
            String host = uri.getHost();
            int port = uri.getPort();
            String scheme = uri.getScheme();
            String path = uri.getRawPath();
            if (Cools.isEmpty(host) || Cools.isEmpty(scheme) || Cools.isEmpty(path)) {
                return error("ERP接口地址配置错误:" + url);
            }
            String base = host + (port > 0 ? ":" + port : "");
            String resp = new HttpHandler.Builder()
                    .setUri(base)
                    .setHttps("https".equalsIgnoreCase(scheme))
                    .setPath(path)
                    .setParams(params)
                    .build()
                    .doPost();
            if (Cools.isEmpty(resp)) {
                return null;
            }
            return JSONObject.parseObject(resp);
        } catch (Exception e) {
            return error("ERP接口调用异常:" + e.getMessage());
        }
    }
 
    private String getConfigValue(String code, String defaultValue) {
        try {
            Config config = configService.selectConfigByCode(code);
            if (config != null && !Cools.isEmpty(config.getValue())) {
                return String.valueOf(config.getValue()).trim();
            }
        } catch (Exception ignored) {
        }
        return defaultValue;
    }
 
    private JSONObject error(String msg) {
        JSONObject obj = new JSONObject();
        obj.put("success", false);
        obj.put("message", msg);
        return obj;
    }
 
    private interface ParamAppender {
        void append(Map<String, Object> params);
    }
}