| | |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.transaction.interceptor.TransactionAspectSupport; |
| | | import org.springframework.transaction.support.TransactionSynchronizationManager; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.util.*; |
| | |
| | | * 以 8.3 文档字段为主,转发立库时映射为服务端 SyncOrderParams 字段。 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public CommonResponse addOrUpdateOrder(ErpOpParams params) { |
| | | if (Objects.isNull(params.getOrderNo()) || params.getOrderNo().isEmpty()) { |
| | | throw new CoolException("订单号不能为空!!"); |
| | | } |
| | | assertValidForSync(params); |
| | | if (Integer.valueOf(3).equals(params.getOperateType())) { |
| | | log.info("order/add 收到 operateType=3,走统一取消逻辑: {}", params.getOrderNo()); |
| | | return doCancel(params); |
| | |
| | | return r; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public CommonResponse addOrUpdateOrders(List<ErpOpParams> paramsList) { |
| | | if (paramsList == null || paramsList.isEmpty()) { |
| | | throw new CoolException("参数不能为空!!"); |
| | | } |
| | | List<Map<String, Object>> errorRows = new ArrayList<>(); |
| | | for (ErpOpParams params : paramsList) { |
| | | String orderNo = params != null ? params.getOrderNo() : null; |
| | | try { |
| | | addOrUpdateOrder(params); |
| | | } catch (CoolException e) { |
| | | Map<String, Object> row = new LinkedHashMap<>(); |
| | | row.put("orderNo", orderNo); |
| | | row.put("msg", e.getMessage()); |
| | | errorRows.add(row); |
| | | log.warn("order/addAll 单条失败 orderNo={} : {}", orderNo, e.getMessage()); |
| | | } catch (Exception e) { |
| | | Map<String, Object> row = new LinkedHashMap<>(); |
| | | row.put("orderNo", orderNo); |
| | | row.put("msg", e.getMessage() != null ? e.getMessage() : "请求异常"); |
| | | errorRows.add(row); |
| | | log.warn("order/addAll 单条异常 orderNo={}", orderNo, e); |
| | | } |
| | | } |
| | | if (!errorRows.isEmpty()) { |
| | | if (TransactionSynchronizationManager.isActualTransactionActive()) { |
| | | TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); |
| | | } |
| | | Map<String, Object> data = new LinkedHashMap<>(); |
| | | data.put("result", ResultData.FAIL); |
| | | data.put("failCount", errorRows.size()); |
| | | data.put("items", errorRows); |
| | | CommonResponse r = new CommonResponse(); |
| | | r.setCode(500); |
| | | r.setMsg("共 " + errorRows.size() + " 条失败,本地事务已回滚,详见 data.items"); |
| | | r.setData(data); |
| | | return r; |
| | | } |
| | | CommonResponse r = new CommonResponse(); |
| | | r.setCode(200); |
| | | r.setMsg("操作成功"); |
| | | r.setData(ResultData.success()); |
| | | return r; |
| | | } |
| | | |
| | | /** 与单条 addOrUpdateOrder 入口一致:不通过则抛 CoolException */ |
| | | private void assertValidForSync(ErpOpParams params) { |
| | | String e = validateForSync(params); |
| | | if (e != null) { |
| | | throw new CoolException(e); |
| | | } |
| | | } |
| | | |
| | | /** 与单条下发相同的入参校验;通过返回 null */ |
| | | private String validateForSync(ErpOpParams params) { |
| | | if (params == null) { |
| | | return "单据参数不能为空!!"; |
| | | } |
| | | if (Objects.isNull(params.getOrderNo()) || params.getOrderNo().isEmpty()) { |
| | | return "订单号不能为空!!"; |
| | | } |
| | | if (!Integer.valueOf(3).equals(params.getOperateType())) { |
| | | if (params.getOrderInternalCode() == null || params.getOrderInternalCode().trim().isEmpty()) { |
| | | return "单据内码不能为空!!"; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** 8.3 参数转为立库 SyncOrderParams 结构(orderNo/type/wkType/anfme/arrTime/orderItems 等) */ |
| | | private Map<String, Object> toServerOrderMap(ErpOpParams params) { |
| | | Map<String, Object> m = new HashMap<>(); |
| | | m.put("orderNo", params.getOrderNo()); |
| | | m.put("orderInternalCode", params.getOrderInternalCode()); |
| | | m.put("stationId", params.getStationId()); |
| | | m.put("wkType", params.getWkType()); |
| | | m.put("type", params.getOrderType() != null ? String.valueOf(params.getOrderType()) : null); |
| | | m.put("orderId", params.getOrderId()); |
| | | m.put("operateType", params.getOperateType()); |
| | | double anfmeSum = 0; |
| | | if (params.getOrderItems() != null) { |
| | | List<Map<String, Object>> items = params.getOrderItems().stream() |
| | |
| | | } |
| | | m.put("anfme", anfmeSum); |
| | | if (params.getBusinessTime() != null) { |
| | | m.put("arrTime", new Date(params.getBusinessTime() * 1000)); |
| | | m.put("arrTime", params.getBusinessTime()); |
| | | } else if (params.getCreateTime() != null) { |
| | | m.put("arrTime", new Date(params.getCreateTime() * 1000)); |
| | | m.put("arrTime", params.getCreateTime()); |
| | | } |
| | | if (params.getCreateTime() != null) { |
| | | m.put("createTime", params.getCreateTime()); |
| | | } |
| | | return m; |
| | | } |
| | |
| | | m.put("model", item.getModel()); |
| | | m.put("unit", item.getUnit()); |
| | | m.put("batch", item.getBatch()); |
| | | m.put("planNo", item.getPlanNo()); |
| | | m.put("palletId", item.getPalletId()); |
| | | m.put("targetWareHouseId", item.getTargetWareHouseId()); |
| | | m.put("sourceWareHouseId", item.getSourceWareHouseId()); |
| | | return m; |
| | | } |
| | | |
| | |
| | | * 取消订单/取消单据。与 /order/add(operateType=3)共用同一套取消逻辑,转发立库 sync/orders/delete。 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public CommonResponse orderCancel(ErpOpParams params) { |
| | | if (Objects.isNull(params.getOrderNo()) || params.getOrderNo().isEmpty()) { |
| | | throw new CoolException("订单号不能为空!!"); |
| | |
| | | /** 统一取消逻辑:/order/add(operateType=3) 与 /order/cancel、/order/del 均走此方法;8.3.3 data 仅含 result */ |
| | | private CommonResponse doCancel(ErpOpParams params) { |
| | | log.info("取消单据,请求参数: {}", JSONObject.toJSONString(params)); |
| | | // Map<String, Object> one = new HashMap<>(); |
| | | // one.put("orderNo", params.getOrderNo()); |
| | | // one.put("orderItems", params.getOrderItems() != null ? params.getOrderItems().stream() |
| | | // .map(this::toServerOrderItemMap) |
| | | // .collect(Collectors.toList()) : Collections.emptyList()); |
| | | // Map<String, Object> res = wmsServerFeignClient.orderDel(Collections.singletonList(one)); |
| | | Map<String, Object> res = wmsServerFeignClient.orderDel(Collections.singletonList(toCancelOrderMap(params))); |
| | | CommonResponse r = mapToCommonResponse(res); |
| | | r.setData(ResultData.success()); |
| | | return r; |
| | | } |
| | | |
| | | private Map<String, Object> toCancelOrderMap(ErpOpParams params) { |
| | | Map<String, Object> one = new HashMap<>(); |
| | | one.put("orderNo", params.getOrderNo()); |
| | | one.put("orderItems", params.getOrderItems() != null ? params.getOrderItems().stream() |
| | | .map(this::toServerOrderItemMap) |
| | | .collect(Collectors.toList()) : Collections.emptyList()); |
| | | Map<String, Object> res = wmsServerFeignClient.orderDel(Collections.singletonList(one)); |
| | | CommonResponse r = mapToCommonResponse(res); |
| | | r.setData(ResultData.success()); |
| | | return r; |
| | | return one; |
| | | } |
| | | |
| | | /** |