chen.lin
7 小时以前 a56420ff2042a3f6b1e824341a717a28c692cad4
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
392
393
394
395
396
397
398
399
400
401
402
403
package com.vincent.rsf.openApi.controller.phyz;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.openApi.entity.dto.CommonResponse;
import com.vincent.rsf.openApi.entity.phyz.*;
import com.vincent.rsf.openApi.feign.wms.WmsServerFeignClient;
import com.vincent.rsf.openApi.service.phyz.ErpReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
import static com.vincent.rsf.openApi.controller.AuthController.SIMULATED_DATA_ENABLE;
 
@RestController
@RequestMapping("/erp")
@Api("银座新工厂(五期)ERP接口")
@Slf4j
public class ERPController {
 
    public static JSONArray map = new JSONArray();
 
    @Resource
    private ErpReportService erpReportService;
 
    @Autowired(required = false)
    private WmsServerFeignClient wmsServerFeignClient;
 
 
    @ApiOperation("仓库信息同步")
    @PostMapping("/wareHouse/sync")
    public CommonResponse syncWareHouse(@RequestBody Object objParams) {
        if (Objects.isNull(objParams)) {
            throw new CoolException("参数不能为空!!");
        }
        // 返回模拟数据
        if (SIMULATED_DATA_ENABLE.equals("1")) {
            return CommonResponse.ok();
        }
 
        JSONArray params = paramsFormat(objParams);
        List<Warehouse> warehouseList = JSON.parseArray(params.toJSONString(), Warehouse.class);
        // 数据处理,转发server
        return CommonResponse.ok();
    }
 
    @ApiOperation("物料信息同步")
    @PostMapping("/mat/sync")
    public CommonResponse syncMaterial(@RequestBody Object objParams) {
        if (Objects.isNull(objParams)) {
            throw new CoolException("参数不能为空!!");
        }
        // 返回模拟数据
        if (SIMULATED_DATA_ENABLE.equals("1")) {
            return CommonResponse.ok();
        }
 
        JSONArray params = paramsFormat(objParams);
        List<Material> materialList = JSON.parseArray(params.toJSONString(), Material.class);
        // 数据处理,转发server
        return CommonResponse.ok();
    }
 
    @ApiOperation("客户信息同步")
    @PostMapping("/customer/sync")
    public CommonResponse syncCustomer(@RequestBody Object objParams) {
        if (Objects.isNull(objParams)) {
            throw new CoolException("参数不能为空!!");
        }
        // 返回模拟数据
        if (SIMULATED_DATA_ENABLE.equals("1")) {
            return CommonResponse.ok();
        }
 
        JSONArray params = paramsFormat(objParams);
        List<Customer> customerList = JSON.parseArray(params.toJSONString(), Customer.class);
        // 数据处理,转发server
        return CommonResponse.ok();
    }
 
    @ApiOperation("供应商信息同步")
    @PostMapping("/supplier/sync")
    public CommonResponse syncSupplier(@RequestBody Object objParams) {
        if (Objects.isNull(objParams)) {
            throw new CoolException("参数不能为空!!");
        }
        // 返回模拟数据
        if (SIMULATED_DATA_ENABLE.equals("1")) {
            return CommonResponse.ok();
        }
 
        JSONArray params = paramsFormat(objParams);
        List<Supplier> supplierList = JSON.parseArray(params.toJSONString(), Supplier.class);
        // 数据处理,转发server
        return CommonResponse.ok();
    }
 
    @ApiOperation("入/出库任务通知单")
    @PostMapping("/order/add")
    public CommonResponse addOrder(@RequestBody Object objParams) {
        if (Objects.isNull(objParams)) {
            throw new CoolException("参数不能为空!!");
        }
        // 返回模拟数据
        if (SIMULATED_DATA_ENABLE.equals("1")) {
            return CommonResponse.ok();
        }
 
        JSONArray params = paramsFormat(objParams);
        List<Order> orderList = JSON.parseArray(params.toJSONString(), Order.class);
        // 数据处理,转发server
        StringBuffer errorMsg = new StringBuffer();
        for (Order order : orderList) {
            String i = erpReportService.addOrderToServer(order);
            if (i.equals("200")){
                errorMsg.append(order.getOrderNo()+"下发成功;");
            } else {
                errorMsg.append(order.getOrderNo()+"下发失败,原因:"+i+";");
            }
        }
        return CommonResponse.ok(errorMsg.toString());
    }
 
    @ApiOperation("入/出库任务通知单取消")
    @PostMapping("/order/cancel")
    public CommonResponse cancelOrder(@RequestBody Object objParams) {
        if (Objects.isNull(objParams)) {
            throw new CoolException("参数不能为空!!");
        }
        // 返回模拟数据
        if (SIMULATED_DATA_ENABLE.equals("1")) {
            return CommonResponse.ok();
        }
 
        JSONArray params = paramsFormat(objParams);
        List<Order> orderList = JSON.parseArray(params.toJSONString(), Order.class);
        // 数据处理,转发server
        return CommonResponse.ok();
    }
 
    @ApiOperation("库存查询明细")
    @PostMapping("/inventory/details")
    public CommonResponse queryInventoryDetails(@RequestBody InventoryQueryCondition condition) {
        if (SIMULATED_DATA_ENABLE.equals("1")) {
            String x = "[\n" +
                    "  {\n" +
                    "    \"locId\": \"LOC-A-01-01\",\n" +
                    "    \"wareHouseId\": \"WH001\",\n" +
                    "    \"wareHouseName\": \"原料仓库\",\n" +
                    "    \"palletId\": \"PALLET001\",\n" +
                    "    \"matNr\": \"MAT10001\",\n" +
                    "    \"makTx\": \"钢材Q235\",\n" +
                    "    \"spec\": \"国标GB/T700-2006\",\n" +
                    "    \"anfme\": 10.5,\n" +
                    "    \"unit\": \"吨\",\n" +
                    "    \"status\": \"可用\",\n" +
                    "    \"orderType\": 1,\n" +
                    "    \"orderNo\": \"Order202698921\",\n" +
                    "    \"prepareType\": 1,\n" +
                    "    \"planNo\": \"PLAN202601060001\",\n" +
                    "    \"batch\": \"BATCH20260106001\",\n" +
                    "    \"stockOrgId\": \"ORG001\"\n" +
                    "  },\n" +
                    "  {\n" +
                    "    \"locId\": \"LOC-B-02-03\",\n" +
                    "    \"wareHouseId\": \"WH002\",\n" +
                    "    \"wareHouseName\": \"成品仓库\",\n" +
                    "    \"palletId\": \"PALLET002\",\n" +
                    "    \"matNr\": \"MAT20001\",\n" +
                    "    \"makTx\": \"电机组件\",\n" +
                    "    \"spec\": \"380V 50Hz\",\n" +
                    "    \"anfme\": 50,\n" +
                    "    \"unit\": \"台\",\n" +
                    "    \"status\": \"可用\",\n" +
                    "    \"orderType\": \"1\",\n" +
                    "    \"orderNo\": \"SO202601060001\",\n" +
                    "    \"prepareType\": 1,\n" +
                    "    \"planNo\": \"PLAN202601060002\",\n" +
                    "    \"batch\": \"BATCH20260106002\",\n" +
                    "    \"stockOrgId\": \"ORG001\"\n" +
                    "  }\n" +
                    "]";
            if (map == null || map.isEmpty()) {
                map = JSON.parseArray(x);
            }
            return CommonResponse.ok(JSONArray.parseArray(map.toJSONString(), InventoryDetails.class));
        }
 
        try {
            if (wmsServerFeignClient == null) {
                log.warn("WmsServerFeignClient未注入,无法进行调用");
                return CommonResponse.error("服务未初始化");
            }
            
            // 参数验证
            if (condition == null) {
                return CommonResponse.error("查询条件不能为空");
            }
            
            log.info("库存查询明细请求参数: {}", JSON.toJSONString(condition));
            
            // 直接传递实体类,Feign会自动序列化为JSON
            R result = wmsServerFeignClient.queryInventoryDetails(condition);
            
            log.info("库存查询明细返回结果: {}", JSON.toJSONString(result));
            
            if (result != null) {
                // R类继承自HashMap,使用get方法获取值
                Integer code = (Integer) result.get("code");
                String msg = (String) result.get("msg");
                Object data = result.get("data");
                
                if (code != null && code == 200) {
                    // 将Map列表转换为InventoryDetails列表
                    if (data != null) {
                        @SuppressWarnings("unchecked")
                        List<Map<String, Object>> dataList = (List<Map<String, Object>>) data;
                        List<InventoryDetails> inventoryDetails = new ArrayList<>();
                        for (Map<String, Object> item : dataList) {
                            InventoryDetails details = JSON.parseObject(JSON.toJSONString(item), InventoryDetails.class);
                            inventoryDetails.add(details);
                        }
                        return CommonResponse.ok(inventoryDetails);
                    } else {
                        return CommonResponse.ok(new ArrayList<>());
                    }
                } else {
                    return CommonResponse.error(msg != null ? msg : "查询失败");
                }
            } else {
                return CommonResponse.error("查询失败:返回结果为空");
            }
        } catch (Exception e) {
            log.error("库存查询明细失败", e);
            // 过滤错误消息中的URL,只保留错误类型
            String errorMessage = e.getMessage();
            if (errorMessage != null) {
                // 如果包含"executing",说明是HTTP请求错误,去掉URL部分
                if (errorMessage.contains("executing")) {
                    int executingIndex = errorMessage.indexOf("executing");
                    if (executingIndex > 0) {
                        // 提取"executing"之前的部分(如"Read timed out")
                        errorMessage = errorMessage.substring(0, executingIndex).trim();
                    } else {
                        // 如果"executing"在开头,使用默认错误消息
                        errorMessage = "请求超时";
                    }
                }
                // 如果包含"http://"或"https://",也尝试去掉URL部分
                else if (errorMessage.contains("http://") || errorMessage.contains("https://")) {
                    // 使用正则表达式去掉URL
                    errorMessage = errorMessage.replaceAll("https?://[^\\s]+", "").trim();
                    if (errorMessage.isEmpty()) {
                        errorMessage = "请求失败";
                    }
                }
            }
            return CommonResponse.error("查询失败:" + (errorMessage != null && !errorMessage.isEmpty() ? errorMessage : "未知错误"));
        }
    }
 
    @ApiOperation("库存查询汇总")
    @PostMapping("/inventory/summary")
    public CommonResponse queryInventorySummary(@RequestBody JSONObject params) {
        if (SIMULATED_DATA_ENABLE.equals("1")) {
            String s = "{\n" +
                    "  \"code\": 200,\n" +
                    "  \"msg\": \"操作成功\",\n" +
                    "  \"data\": [\n" +
                    "    {\n" +
                    "      \"wareHouseId\": \"WH001\",\n" +
                    "      \"wareHouseName\": \"原料仓库\",\n" +
                    "      \"matNr\": \"MAT10001\",\n" +
                    "      \"makTx\": \"钢材Q235\",\n" +
                    "      \"spec\": \"国标GB/T700-2006\",\n" +
                    "      \"anfme\": 10.5,\n" +
                    "      \"unit\": \"吨\",\n" +
                    "      \"stockOrgId\": \"ORG001\",\n" +
                    "      \"batch\": \"BATCH20260106001\",\n" +
                    "      \"planNo\": \"Plan20260106006\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"wareHouseId\": \"WH001\",\n" +
                    "      \"wareHouseName\": \"原料仓库\",\n" +
                    "      \"matNr\": \"MAT10002\",\n" +
                    "      \"makTx\": \"铝型材6061\",\n" +
                    "      \"spec\": \"国标GB/T3190-2008\",\n" +
                    "      \"anfme\": 20.3,\n" +
                    "      \"unit\": \"吨\",\n" +
                    "      \"stockOrgId\": \"ORG001\",\n" +
                    "      \"batch\": \"BATCH20260106002\",\n" +
                    "      \"planNo\": \"Plan20260106005\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"wareHouseId\": \"WH002\",\n" +
                    "      \"wareHouseName\": \"成品仓库\",\n" +
                    "      \"matNr\": \"MAT30001\",\n" +
                    "      \"makTx\": \"电机成品\",\n" +
                    "      \"spec\": \"380V 50Hz 15KW\",\n" +
                    "      \"anfme\": 100,\n" +
                    "      \"unit\": \"台\",\n" +
                    "      \"stockOrgId\": \"ORG001\",\n" +
                    "      \"batch\": \"BATCH20260106003\",\n" +
                    "      \"planNo\": \"Plan20260106004\"\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}";
            return JSONObject.parseObject(s, CommonResponse.class);
        }
 
        InventoryQueryCondition condition = JSON.parseObject(params.toJSONString(), InventoryQueryCondition.class);
        // 数据处理,转发server
        List<InventorySummary> inventorySummaries = Lists.newArrayList();
        return new CommonResponse().setCode(200).setData(inventorySummaries);
 
    }
 
    @ApiOperation("盘点结果确认")
    @PostMapping("/check/confirm")
    public CommonResponse checkConfirm(@RequestBody Object objParams) {
        if (Objects.isNull(objParams)) {
            throw new CoolException("参数不能为空!!");
        }
        // 返回模拟数据
        if (SIMULATED_DATA_ENABLE.equals("1")) {
            return CommonResponse.ok();
        }
 
        JSONArray params = paramsFormat(objParams);
        CheckOrder checkResult = JSON.parseObject(params.toJSONString(), CheckOrder.class);
        // 数据处理,转发server
        return CommonResponse.ok();
    }
 
    /**
     * 兼容JSONObject和JSONArray格式数据
     *
     * @param data json格式参数
     * @return JSONArray格式数据
     */
    public static JSONArray paramsFormat(Object data) {
        if (Objects.isNull(data)) {
            return new JSONArray();
        }
 
        try {
            String jsonStr = JSON.toJSONString(data);
            if (jsonStr.startsWith("[")) {
                return JSON.parseArray(jsonStr);
            } else if (jsonStr.startsWith("{")) {
                JSONArray params = new JSONArray();
                params.add(JSON.parseObject(jsonStr));
                return params;
            }
        } catch (Exception e) {
            // 解析失败,返回空数组
            log.error("转换参数为json格式错误", e);
        }
 
        // 默认返回空数组
        return new JSONArray();
    }
 
    // region 测试推送功能
    @ApiOperation("登录")
    @PostMapping("/loginBySign")
    public CommonResponse loginBySign() {
        try {
            return CommonResponse.ok(erpReportService.loginBySign());
        } catch (Exception e) {
            log.error("erp, loginBySign", e);
        }
        return CommonResponse.ok();
    }
 
    @ApiOperation("入/出库任务回调")
    @PostMapping("/reportInOrOutBound")
    public CommonResponse reportInOrOutBound(@RequestBody Object objParams) {
        try {
            return erpReportService.reportInOrOutBound(objParams);
        } catch (Exception e) {
            log.error("erp, reportInOrOutBound", e);
        }
        return CommonResponse.ok();
    }
    // endregion
 
}