#
Junjie
2026-01-15 e14a4372b6bd4a38e40a3a68bde32350d96071ab
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package com.zy.core.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.exception.CoolException;
import com.zy.asrs.entity.BasCrnp;
import com.zy.asrs.entity.BasDualCrnp;
import com.zy.asrs.entity.BasStation;
import com.zy.asrs.entity.HttpRequestLog;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.BasCrnpService;
import com.zy.asrs.service.BasDualCrnpService;
import com.zy.asrs.service.BasStationService;
import com.zy.asrs.service.HttpRequestLogService;
import com.zy.asrs.service.WrkMastService;
import com.zy.asrs.utils.Utils;
import com.zy.common.entity.FindCrnNoResult;
import com.zy.common.service.CommonService;
import com.zy.common.utils.HttpHandler;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
@Component
public class WmsOperateUtils {
 
    @Autowired
    private ConfigService configService;
    @Autowired
    private HttpRequestLogService httpRequestLogService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private BasCrnpService basCrnpService;
    @Autowired
    private BasDualCrnpService basDualCrnpService;
    @Autowired
    private BasStationService basStationService;
    @Autowired
    private RedisUtil redisUtil;
 
    // 申请入库任务
    public synchronized String applyInTask(String barcode, Integer sourceStaNo, Integer locType1) {
        Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
        if (systemConfigMapObj == null) {
            News.error("系统Config缓存失效");
            return null;
        }
        HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
 
        String wmsUrl = systemConfigMap.get("wmsSystemUri");
        if (wmsUrl == null) {
            News.error("未配置WMS系统URI,配置文件Code编码:wmsSystemUri");
            return null;
        }
 
        String wmsSystemInUrl = systemConfigMap.get("wmsSystemInUrl");
        if (wmsSystemInUrl == null) {
            News.error("未配置WMS入库接口地址,配置文件Code编码:wmsSystemInUrl");
            return null;
        }
 
        HashMap<String, Object> requestParam = new HashMap<>();
        String response = null;
        int result = 0;
        try {
            BasStation basStation = basStationService
                    .selectOne(new EntityWrapper<BasStation>().eq("station_id", sourceStaNo));
            if (basStation == null) {
                News.error("站点{}不存在", sourceStaNo);
                return null;
            }
 
            String stationNo = String.valueOf(sourceStaNo);
            if (!Cools.isEmpty(basStation.getStationAlias())) {
                stationNo = basStation.getStationAlias();
            }
 
            requestParam.put("barcode", barcode);
            requestParam.put("sourceStaNo", stationNo);
            requestParam.put("locType1", locType1);
            requestParam.put("row", Utils.getInTaskEnableRow(sourceStaNo));
 
            response = new HttpHandler.Builder()
                    .setUri(wmsUrl)
                    .setPath(wmsSystemInUrl)
                    .setJson(JSON.toJSONString(requestParam))
                    .setTimeout(30, TimeUnit.SECONDS)
                    .build()
                    .doPost();
            if (response != null) {
                JSONObject jsonObject = JSON.parseObject(response);
                if (jsonObject.getInteger("code") == 200) {
                    result = 1;
                    News.info("请求WMS入库接口成功!!!url:{};request:{};response:{}", wmsUrl + wmsSystemInUrl,
                            JSON.toJSONString(requestParam), response);
                } else {
                    News.info("请求WMS入库接口失败,接口返回Code异常!!!url:{};request:{};response:{}", wmsUrl + wmsSystemInUrl,
                            JSON.toJSONString(requestParam), response);
                }
            } else {
                News.info("请求WMS入库接口失败,接口未响应!!!url:{};request:{};response:{}", wmsUrl + wmsSystemInUrl,
                        JSON.toJSONString(requestParam), response);
            }
        } catch (Exception e) {
            News.error("请求WMS入库接口异常!!!url:{};request:{};response:{}", wmsUrl + wmsSystemInUrl,
                    JSON.toJSONString(requestParam), response, e);
        } finally {
            HttpRequestLog httpRequestLog = new HttpRequestLog();
            httpRequestLog.setName(wmsUrl + wmsSystemInUrl);
            httpRequestLog.setRequest(JSON.toJSONString(requestParam));
            httpRequestLog.setResponse(response);
            httpRequestLog.setCreateTime(new Date());
            httpRequestLog.setResult(result);
            httpRequestLogService.insert(httpRequestLog);
        }
        return response;
    }
 
    /**
     * 异步申请入库任务 - 非阻塞版本
     * 将请求提交到线程池异步执行,结果存储到Redis中
     * 
     * @param barcode     托盘码
     * @param sourceStaNo 站点编号
     * @param locType1    托盘高度
     */
    public void applyInTaskAsync(String barcode, Integer sourceStaNo, Integer locType1) {
        String requestKey = RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST.key + barcode + "_" + sourceStaNo;
        String responseKey = RedisKeyType.ASYNC_WMS_IN_TASK_RESPONSE.key + barcode + "_" + sourceStaNo;
 
        // 检查是否已有请求在进行中
        Object existingRequest = redisUtil.get(requestKey);
        if (existingRequest != null) {
            return; // 已有请求在进行中,跳过
        }
 
        // 标记请求进行中,设置60秒超时
        redisUtil.set(requestKey, "processing", 60);
 
        // 提交异步任务
        new Thread(() -> {
            try {
                String response = applyInTask(barcode, sourceStaNo, locType1);
                if (response != null) {
                    // 存储响应结果,设置60秒超时
                    redisUtil.set(responseKey, response, 60);
                    News.info("异步WMS入库请求完成,barcode={},stationId={},response={}", barcode, sourceStaNo, response);
                } else {
                    // 请求失败,存储失败标记
                    redisUtil.set(responseKey, "FAILED", 10);
                    News.error("异步WMS入库请求失败,barcode={},stationId={}", barcode, sourceStaNo);
                }
            } catch (Exception e) {
                News.error("异步WMS入库请求异常,barcode={},stationId={},error={}", barcode, sourceStaNo, e.getMessage());
                redisUtil.set(responseKey, "ERROR:" + e.getMessage(), 10);
            } finally {
                // 清除请求进行中标记
                redisUtil.del(requestKey);
            }
        }).start();
    }
 
    /**
     * 查询异步入库任务请求结果
     * 
     * @param barcode   托盘码
     * @param stationId 站点编号
     * @return 响应结果,null表示还未完成或未找到
     */
    public String queryAsyncInTaskResponse(String barcode, Integer stationId) {
        String responseKey = RedisKeyType.ASYNC_WMS_IN_TASK_RESPONSE.key + barcode + "_" + stationId;
        Object response = redisUtil.get(responseKey);
        if (response != null) {
            // 获取后删除,避免重复处理
            redisUtil.del(responseKey);
            return response.toString();
        }
        return null;
    }
 
    /**
     * 检查是否有异步请求正在进行中
     * 
     * @param barcode   托盘码
     * @param stationId 站点编号
     * @return true表示正在请求中
     */
    public boolean isAsyncRequestInProgress(String barcode, Integer stationId) {
        String requestKey = RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST.key + barcode + "_" + stationId;
        return redisUtil.get(requestKey) != null;
    }
 
    // 申请任务重新分配库位
    public synchronized String applyReassignTaskLocNo(Integer taskNo, Integer stationId) {
        String wmsUrl = null;
        Config wmsSystemUriConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "wmsSystemUri"));
        if (wmsSystemUriConfig != null) {
            wmsUrl = wmsSystemUriConfig.getValue();
        }
 
        if (wmsUrl == null) {
            News.error("未配置WMS系统URI,配置文件Code编码:wmsSystemUri");
            return null;
        }
 
        String wmsSystemReassignInTaskUrl = null;
        Config wmsSystemReassignInTaskUrlConfig = configService
                .selectOne(new EntityWrapper<Config>().eq("code", "wmsSystemReassignInTaskUrl"));
        if (wmsSystemReassignInTaskUrlConfig != null) {
            wmsSystemReassignInTaskUrl = wmsSystemReassignInTaskUrlConfig.getValue();
        }
 
        if (wmsSystemReassignInTaskUrl == null) {
            News.error("未配置WMS任务重新分配入库库位接口地址,配置文件Code编码:wmsSystemReassignInTaskUrl");
            return null;
        }
 
        WrkMast wrkMast = wrkMastService.selectByWorkNo(taskNo);
        if (wrkMast == null) {
            News.info("无法找到对应任务,工作号={}", taskNo);
            return null;
        }
 
        HashMap<String, Object> requestParam = new HashMap<>();
        String response = null;
        int result = 0;
        try {
            List<Integer> excludeCrnList = new ArrayList<>();
            List<Integer> excludeDualCrnList = new ArrayList<>();
            if (!Cools.isEmpty(wrkMast.getCrnNo())) {
                excludeCrnList.add(wrkMast.getCrnNo());
            }
            if (!Cools.isEmpty(wrkMast.getDualCrnNo())) {
                excludeDualCrnList.add(wrkMast.getDualCrnNo());
            }
 
            requestParam.put("taskNo", wrkMast.getWmsWrkNo());
            requestParam.put("row", Utils.getInTaskEnableRow(stationId, excludeCrnList, excludeDualCrnList, false));
 
            response = new HttpHandler.Builder()
                    .setUri(wmsUrl)
                    .setPath(wmsSystemReassignInTaskUrl)
                    .setJson(JSON.toJSONString(requestParam))
                    .setTimeout(30, TimeUnit.SECONDS)
                    .build()
                    .doPost();
            if (response != null) {
                JSONObject jsonObject = JSON.parseObject(response);
                if (jsonObject.getInteger("code") == 200) {
                    result = 1;
                    News.info("请求申请任务重新分配入库接口成功!!!url:{};request:{};response:{}", wmsUrl + wmsSystemReassignInTaskUrl,
                            JSON.toJSONString(requestParam), response);
                } else {
                    News.info("请求申请任务重新分配入库接口失败,接口返回Code异常!!!url:{};request:{};response:{}",
                            wmsUrl + wmsSystemReassignInTaskUrl, JSON.toJSONString(requestParam), response);
                }
            } else {
                News.info("请求申请任务重新分配入库接口失败,接口未响应!!!url:{};request:{};response:{}", wmsUrl + wmsSystemReassignInTaskUrl,
                        JSON.toJSONString(requestParam), response);
            }
        } catch (Exception e) {
            News.error("请求申请任务重新分配入库接口异常!!!url:{};request:{}; response:{}", wmsUrl + wmsSystemReassignInTaskUrl,
                    JSON.toJSONString(requestParam), response, e);
        } finally {
            HttpRequestLog httpRequestLog = new HttpRequestLog();
            httpRequestLog.setName(wmsUrl + wmsSystemReassignInTaskUrl);
            httpRequestLog.setRequest(JSON.toJSONString(requestParam));
            httpRequestLog.setResponse(response);
            httpRequestLog.setCreateTime(new Date());
            httpRequestLog.setResult(result);
            httpRequestLogService.insert(httpRequestLog);
        }
        return response;
    }
 
    // 申请在库库位更换库位
    public synchronized String applyChangeLocNo(String locNo) {
        String wmsUrl = null;
        Config wmsSystemUriConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "wmsSystemUri"));
        if (wmsSystemUriConfig != null) {
            wmsUrl = wmsSystemUriConfig.getValue();
        }
 
        if (wmsUrl == null) {
            News.error("未配置WMS系统URI,配置文件Code编码:wmsSystemUri");
            return null;
        }
 
        String wmsSystemChangeLocNoUrl = null;
        Config wmsSystemChangeLocNoUrlConfig = configService
                .selectOne(new EntityWrapper<Config>().eq("code", "wmsSystemChangeLocNoUrl"));
        if (wmsSystemChangeLocNoUrlConfig != null) {
            wmsSystemChangeLocNoUrl = wmsSystemChangeLocNoUrlConfig.getValue();
        }
 
        if (wmsSystemChangeLocNoUrl == null) {
            News.error("未配置申请在库库位更换库位接口地址,配置文件Code编码:wmsSystemChangeLocNoUrl");
            return null;
        }
 
        FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
        if (findCrnNoResult == null) {
            return null;
        }
        Integer crnNo = findCrnNoResult.getCrnNo();
        List<Integer> crnRows = new ArrayList<>();
 
        if (findCrnNoResult.getCrnType().equals(SlaveType.Crn)) {
            BasCrnp basCrnp = basCrnpService.selectOne(new EntityWrapper<BasCrnp>().eq("crn_no", crnNo));
            if (basCrnp == null) {
                return null;
            }
            List<List<Integer>> rowList = basCrnp.getControlRows$();
            for (List<Integer> list : rowList) {
                crnRows.addAll(list);
            }
        } else if (findCrnNoResult.getCrnType().equals(SlaveType.DualCrn)) {
            BasDualCrnp basDualCrnp = basDualCrnpService
                    .selectOne(new EntityWrapper<BasDualCrnp>().eq("crn_no", crnNo));
            if (basDualCrnp == null) {
                return null;
            }
            List<List<Integer>> rowList = basDualCrnp.getControlRows$();
            for (List<Integer> list : rowList) {
                crnRows.addAll(list);
            }
        } else {
            throw new CoolException("未知设备类型");
        }
 
        HashMap<String, Object> requestParam = new HashMap<>();
        String response = null;
        int result = 0;
        try {
            requestParam.put("locNo", locNo);
            requestParam.put("row", crnRows);
 
            response = new HttpHandler.Builder()
                    .setUri(wmsUrl)
                    .setPath(wmsSystemChangeLocNoUrl)
                    .setJson(JSON.toJSONString(requestParam))
                    .setTimeout(30, TimeUnit.SECONDS)
                    .build()
                    .doPost();
 
            if (response != null) {
                JSONObject jsonObject = JSON.parseObject(response);
                if (jsonObject.getInteger("code") == 200) {
                    result = 1;
                    News.info("请求WMS申请更换库位接口成功!!!url:{};request:{};response:{}", wmsUrl + wmsSystemChangeLocNoUrl,
                            JSON.toJSONString(requestParam), response);
                } else {
                    News.info("请求WMS申请更换库位接口失败,接口返回Code异常!!!url:{};request:{};response:{}",
                            wmsUrl + wmsSystemChangeLocNoUrl, JSON.toJSONString(requestParam), response);
                }
            } else {
                News.info("请求WMS申请更换库位接口失败,接口未响应!!!url:{};request:{};response:{}", wmsUrl + wmsSystemChangeLocNoUrl,
                        JSON.toJSONString(requestParam), response);
            }
        } catch (Exception e) {
            News.error("请求WMS申请更换库位接口异常!!!url:{};request:{};response:{}", wmsUrl + wmsSystemChangeLocNoUrl,
                    JSON.toJSONString(requestParam), response, e);
        } finally {
            HttpRequestLog httpRequestLog = new HttpRequestLog();
            httpRequestLog.setName(wmsUrl + wmsSystemChangeLocNoUrl);
            httpRequestLog.setRequest(JSON.toJSONString(requestParam));
            httpRequestLog.setResponse(response);
            httpRequestLog.setCreateTime(new Date());
            httpRequestLog.setResult(result);
            httpRequestLogService.insert(httpRequestLog);
        }
        return response;
    }
 
}