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 syncMatnrsToCloud(Object body) { if (!isCloudWmsConfigured()) { log.warn("ErpApi(云仓WMS) 未配置 host,跳过物料基础信息同步"); return stubSuccess("云仓地址未配置,未实际同步"); } return cloudWmsErpFeignClient.syncMatnrs(body != null ? body : new HashMap<>()); } @Override public Map 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 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 stubSuccess(String msg) { Map data = new HashMap<>(); data.put("result", "SUCCESS"); return resultMap(200, msg, data); } private Map resultMap(int code, String msg, Map data) { Map 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 postToCloudWms(String url, Object body) { HttpHeaders headers = ...; HttpEntity entity = new HttpEntity<>(body, headers); ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); return parseResponse(response.getBody()); } // private Map parseResponse(String json) { ... } }