chen.lin
16 小时以前 82065a03737fa1370eb9f4f01ab5332933baf08a
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
package com.vincent.rsf.server.api.service.impl;
 
import com.vincent.rsf.server.api.config.RemotesInfoProperties;
import com.vincent.rsf.server.api.controller.erp.params.InOutResultReportParam;
import com.vincent.rsf.server.api.controller.erp.params.InventoryAdjustReportParam;
import com.vincent.rsf.server.api.feign.CloudWmsErpFeignClient;
import com.vincent.rsf.server.api.service.CloudWmsReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 立库侧请求云仓:入/出库结果上报(9.1)、库存调整主动上报(9.2)、物料基础信息同步。
 * 使用 OpenFeign 调用;可选 HttpEntity(RestTemplate) 方式已注释保留。
 */
@Slf4j
@Service
public class CloudWmsReportServiceImpl implements CloudWmsReportService {
 
    @Autowired
    private RemotesInfoProperties erpApi;
 
    @Autowired
    private RemotesInfoProperties.ApiInfo erpApiInfo;
 
    @Autowired
    private CloudWmsErpFeignClient cloudWmsErpFeignClient;
 
    /**
     * 可选:改用 HttpEntity(RestTemplate) 调用云仓时启用。
     */
    // @Autowired
    // private RestTemplate restTemplate;
 
    @Override
    public Map<String, Object> syncMatnrsToCloud(Object body) {
        if (!isCloudWmsConfigured()) {
            log.warn("ErpApi(云仓WMS) 未配置 host,跳过物料基础信息同步");
            return stubSuccess("云仓地址未配置,未实际同步");
        }
        return cloudWmsErpFeignClient.syncMatnrs(body != null ? body : new HashMap<>());
    }
 
    @Override
    public Map<String, Object> reportInOutResult(InOutResultReportParam param) {
        if (param == null) {
            return resultMap(400, "参数不能为空", null);
        }
        if (!isCloudWmsConfigured()) {
            log.warn("ErpApi(云仓WMS) 未配置 host,跳过 9.1 入/出库结果上报,订单:{}", param.getOrderNo());
            return stubSuccess("云仓地址未配置,未实际上报");
        }
        return cloudWmsErpFeignClient.reportInOutResult(param);
    }
 
    @Override
    public Map<String, Object> reportInventoryAdjust(InventoryAdjustReportParam param) {
        if (param == null) {
            return resultMap(400, "参数不能为空", null);
        }
        if (!isCloudWmsConfigured()) {
            log.warn("ErpApi(云仓WMS) 未配置 host,跳过 9.2 库存调整上报,物料:{}", param.getMatNr());
            return stubSuccess("云仓地址未配置,未实际上报");
        }
        return cloudWmsErpFeignClient.reportInventoryAdjust(param);
    }
 
    private boolean isCloudWmsConfigured() {
        String host = erpApi.getHost();
        return host != null && !host.trim().isEmpty();
    }
 
    private Map<String, Object> stubSuccess(String msg) {
        Map<String, Object> data = new HashMap<>();
        data.put("result", "SUCCESS");
        return resultMap(200, msg, data);
    }
 
    private Map<String, Object> resultMap(int code, String msg, Map<String, Object> data) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("msg", msg);
        map.put("data", data);
        return map;
    }
 
    // ========== 可选:HttpEntity(RestTemplate) 方式(当前未使用) ==========
    // 启用步骤:1)取消上方 restTemplate 的 @Autowired 注入;
    // 2)取消下面整段注释,恢复 buildUrl、postToCloudWms、parseResponse 方法及 OBJECT_MAPPER;
    // 3)在 syncMatnrsToCloud/reportInOutResult/reportInventoryAdjust 中改为:String url = buildUrl(erpApiInfo.getXxxPath()); if (url == null) return stubSuccess(...); return postToCloudWms(url, body);
    //
    // private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    // private String buildUrl(String path) { ... }
    // private Map<String, Object> postToCloudWms(String url, Object body) { HttpHeaders headers = ...; HttpEntity<Object> entity = new HttpEntity<>(body, headers); ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); return parseResponse(response.getBody()); }
    // private Map<String, Object> parseResponse(String json) { ... }
}