|  |  | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | 
 |  |  | import com.fasterxml.jackson.core.JsonProcessingException; | 
 |  |  | import com.fasterxml.jackson.databind.ObjectMapper; | 
 |  |  | import com.fasterxml.jackson.databind.cfg.CoercionAction; | 
 |  |  | import com.fasterxml.jackson.databind.cfg.CoercionInputShape; | 
 |  |  | import com.vincent.rsf.common.domain.CommonReponse; | 
 |  |  | import com.vincent.rsf.framework.common.R; | 
 |  |  | import com.vincent.rsf.framework.exception.CoolException; | 
 |  |  | import com.vincent.rsf.server.api.config.RemotesInfoProperties; | 
 |  |  | import com.vincent.rsf.server.api.controller.erp.params.ReportParams; | 
 |  |  | import com.vincent.rsf.server.api.entity.CommonResponse; | 
 |  |  | import com.vincent.rsf.server.api.entity.constant.RcsConstant; | 
 |  |  | import com.vincent.rsf.server.api.entity.dto.PoItemsDto; | 
 |  |  | import com.vincent.rsf.server.api.entity.params.ErpInspectItem; | 
 |  |  | import com.vincent.rsf.server.api.entity.params.ErpInspectParams; | 
 |  |  | import com.vincent.rsf.server.api.service.ReportMsgService; | 
 |  |  | import com.vincent.rsf.server.manager.entity.AsnOrder; | 
 |  |  | import com.vincent.rsf.server.manager.entity.AsnOrderItem; | 
 |  |  | import com.vincent.rsf.server.manager.entity.WkOrder; | 
 |  |  | import com.vincent.rsf.server.manager.entity.WkOrderItem; | 
 |  |  | import com.vincent.rsf.server.manager.entity.Purchase; | 
 |  |  | import com.vincent.rsf.server.manager.service.AsnOrderItemService; | 
 |  |  | import com.vincent.rsf.server.manager.service.AsnOrderService; | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private PurchaseService purchaseService; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private RemotesInfoProperties.ApiInfo apiInfo; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private RestTemplate restTemplate; | 
 |  |  |     @Autowired | 
 |  |  |     private RemotesInfoProperties remotesInfoProperties; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private AsnOrderService asnOrderService; | 
 |  |  |     @Autowired | 
 |  |  |     private AsnOrderItemService asnOrderItemService; | 
 |  |  |     @Autowired | 
 |  |  |     private RemotesInfoProperties.RcsApi rcsApi; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * @author Ryan | 
 |  |  |      * @description 上报质检列表 | 
 |  |  | 
 |  |  |             inspectParams.add(params); | 
 |  |  |  | 
 |  |  |         }); | 
 |  |  |         CommonReponse reponse = new CommonReponse(); | 
 |  |  |         if (!remotesInfoProperties.getHost().equals("http://127.0.0.1")){ | 
 |  |  |             MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); | 
 |  |  |             //获取上报质检接口 | 
 |  |  |             String url = remotesInfoProperties.getBaseUrl() + apiInfo.getNotifyInspect(); | 
 |  |  |             // 设置请求参数 | 
 |  |  |             params.add("params", JSONObject.toJSONString(inspectParams)); | 
 |  |  |             log.info("请求地址:{},请求参数:{}", url, JSONObject.toJSONString(inspectParams)); | 
 |  |  |             HttpHeaders headers = new HttpHeaders(); | 
 |  |  |             headers.add("Content-Type", "application/json"); | 
 |  |  |             HttpEntity httpEntity = new HttpEntity<>(params, headers); | 
 |  |  |             // 请求 | 
 |  |  |             ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class); | 
 |  |  |             log.info("下发任务 返回结果:{}", exchange); | 
 |  |  |             if (Objects.isNull(exchange.getBody()) || exchange.getBody() == null) { | 
 |  |  |                 throw new CoolException("下发任务失败!!"); | 
 |  |  |             } | 
 |  |  |             reponse = (CommonReponse) JSON.parse(exchange.getBody()); | 
 |  |  |         }else { | 
 |  |  |             reponse.setCode(0); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); | 
 |  |  |         //获取上报质检接口 | 
 |  |  |         String url = remotesInfoProperties.getBaseUrl() + apiInfo.getNotifyInspect(); | 
 |  |  |         // 设置请求参数 | 
 |  |  |         params.add("params", JSONObject.toJSONString(inspectParams)); | 
 |  |  |         log.info("请求地址:{},请求参数:{}", url, JSONObject.toJSONString(inspectParams)); | 
 |  |  |  | 
 |  |  |         if (reponse.getCode() == 0) { | 
 |  |  |             //修改asn上报状态 | 
 |  |  |             Set<String> itemSets = items.stream().map(PoItemsDto::getId).collect(Collectors.toSet()); | 
 |  |  |             Set<Long> longSet = new HashSet<>(); | 
 |  |  |             itemSets.forEach(set -> { | 
 |  |  |                 longSet.add(Long.parseLong(set)); | 
 |  |  |             }); | 
 |  |  |  | 
 |  |  |             List<WkOrderItem> wkOrderItems = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>().in(WkOrderItem::getPoDetlId, longSet)); | 
 |  |  |             if (wkOrderItems.isEmpty()) { | 
 |  |  |                 throw new CoolException("ASN单据不存在!!"); | 
 |  |  |             } | 
 |  |  |             List<Long> list = wkOrderItems.stream().map(WkOrderItem::getId).collect(Collectors.toList()); | 
 |  |  |  | 
 |  |  |             /**************************     修改ASN明细单状态         ******************************/ | 
 |  |  |  | 
 |  |  |  | 
 |  |  |             if (!asnOrderItemService.update(new LambdaUpdateWrapper<WkOrderItem>().in(WkOrderItem::getId, list).set(WkOrderItem::getNtyStatus, 1))) { | 
 |  |  |                 throw new CoolException("ASN明细单据状态修改失败!!"); | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             /**************************     修改ASN主单数据         ******************************/ | 
 |  |  |             //获取ASN明细订单标识 | 
 |  |  |             List<Long> ids = wkOrderItems.stream().map(WkOrderItem::getOrderId).collect(Collectors.toList()); | 
 |  |  |             //ASN明细单据分组 | 
 |  |  |             Map<Long, List<WkOrderItem>> asnIds = wkOrderItems.stream().collect(Collectors.groupingBy(WkOrderItem::getOrderId)); | 
 |  |  |             ids.forEach(id -> { | 
 |  |  |                 int count = asnOrderService.count(new LambdaQueryWrapper<WkOrder>().in(WkOrder::getId, id)); | 
 |  |  |                 if (count == asnIds.get(id).size()) { | 
 |  |  |                     if (!asnOrderService.update(new LambdaUpdateWrapper<WkOrder>().eq(WkOrder::getId, id).set(WkOrder::getNtyStatus, 1))) { | 
 |  |  |                         throw new CoolException("ASN主单状态修改失败!!"); | 
 |  |  |                     } | 
 |  |  |                 } else { | 
 |  |  |                     if (!asnOrderService.update(new LambdaUpdateWrapper<WkOrder>().eq(WkOrder::getId, id).set(WkOrder::getNtyStatus, 2))) { | 
 |  |  |                         throw new CoolException("ASN主单状态修改失败!!"); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             }); | 
 |  |  |             return true; | 
 |  |  |         } else { | 
 |  |  |             throw new CoolException(reponse.getMsg()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 上报已完成订单明细 | 
 |  |  |      * @author Ryan | 
 |  |  |      * @date 2025/10/27 21:36 | 
 |  |  |      * @param params | 
 |  |  |      * @return com.vincent.rsf.framework.common.R | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     public R uploadReportOrders(ReportParams params) { | 
 |  |  |         if (Objects.isNull(params)) { | 
 |  |  |             throw new CoolException("参数不能为空!!"); | 
 |  |  |         } | 
 |  |  |         /**WMS基础配置链接*/ | 
 |  |  |         String rcsUrl = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.REPORT_ORDERS; | 
 |  |  |         log.info("上报已完成订单:{}, 请求参数: {}", rcsUrl, JSONObject.toJSONString(params)); | 
 |  |  |         HttpHeaders headers = new HttpHeaders(); | 
 |  |  |         headers.add("Content-Type", "application/json"); | 
 |  |  |         HttpEntity httpEntity = new HttpEntity<>(params, headers); | 
 |  |  |         // 请求 | 
 |  |  |         ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class); | 
 |  |  |         log.info("下发任务 返回结果:{}", exchange); | 
 |  |  |         if (Objects.isNull(exchange.getBody()) || exchange.getBody() == null) { | 
 |  |  |             throw new CoolException("下发任务失败!!"); | 
 |  |  |         headers.add("api-version", "v2.0"); | 
 |  |  |  | 
 |  |  |         HttpEntity httpEntity = new HttpEntity(params, headers); | 
 |  |  |         ResponseEntity<String> exchange = restTemplate.exchange(rcsUrl, HttpMethod.POST, httpEntity, String.class); | 
 |  |  |         log.info("上报已完成订单,返回结果: {}", exchange); | 
 |  |  |         if (Objects.isNull(exchange.getBody())) { | 
 |  |  |             throw new CoolException("修改失败!!"); | 
 |  |  |         } else { | 
 |  |  |             CommonReponse reponse = (CommonReponse) JSON.parse(exchange.getBody()); | 
 |  |  |             if (reponse.getCode() == 0) { | 
 |  |  |                 //修改asn上报状态 | 
 |  |  |                 Set<String> itemSets = items.stream().map(PoItemsDto::getId).collect(Collectors.toSet()); | 
 |  |  |                 Set<Long> longSet = new HashSet<>(); | 
 |  |  |                 itemSets.forEach(set -> { | 
 |  |  |                     longSet.add(Long.parseLong(set)); | 
 |  |  |                 }); | 
 |  |  |  | 
 |  |  |                 List<AsnOrderItem> asnOrderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>().in(AsnOrderItem::getPoDetlId, longSet)); | 
 |  |  |                 if (asnOrderItems.isEmpty()) { | 
 |  |  |                     throw new CoolException("ASN单据不存在!!"); | 
 |  |  |             ObjectMapper objectMapper = new ObjectMapper(); | 
 |  |  |             objectMapper.coercionConfigDefaults().setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty); | 
 |  |  |             try { | 
 |  |  |                 CommonResponse result = objectMapper.readValue(exchange.getBody(), CommonResponse.class); | 
 |  |  |                 if (result.getCode() == 200) { | 
 |  |  |                     return R.ok().add(result.getData()); | 
 |  |  |                 } else { | 
 |  |  |                     throw new CoolException("修改失败!!"); | 
 |  |  |                 } | 
 |  |  |                 List<Long> list = asnOrderItems.stream().map(AsnOrderItem::getId).collect(Collectors.toList()); | 
 |  |  |  | 
 |  |  |                 /**************************     修改ASN明细单状态         ******************************/ | 
 |  |  |  | 
 |  |  |  | 
 |  |  |                 if (!asnOrderItemService.update(new LambdaUpdateWrapper<AsnOrderItem>().in(AsnOrderItem::getId, list).set(AsnOrderItem::getStatus, 1))) { | 
 |  |  |                     throw new CoolException("ASN明细单据状态修改失败!!"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 /**************************     修改ASN主单数据         ******************************/ | 
 |  |  |                 //获取ASN明细订单标识 | 
 |  |  |                 List<Long> ids = asnOrderItems.stream().map(AsnOrderItem::getAsnId).collect(Collectors.toList()); | 
 |  |  |                 //ASN明细单据分组 | 
 |  |  |                 Map<Long, List<AsnOrderItem>> asnIds = asnOrderItems.stream().collect(Collectors.groupingBy(AsnOrderItem::getAsnId)); | 
 |  |  |                 ids.forEach(id -> { | 
 |  |  |                     int count = asnOrderService.count(new LambdaQueryWrapper<AsnOrder>().in(AsnOrder::getId, id)); | 
 |  |  |                     if (count == asnIds.get(id).size()) { | 
 |  |  |                         if (!asnOrderService.update(new LambdaUpdateWrapper<AsnOrder>().eq(AsnOrder::getId, id).set(AsnOrder::getStatus, 1))) { | 
 |  |  |                             throw new CoolException("ASN主单状态修改失败!!"); | 
 |  |  |                         } | 
 |  |  |                     } else { | 
 |  |  |                         if (!asnOrderService.update(new LambdaUpdateWrapper<AsnOrder>().eq(AsnOrder::getId, id).set(AsnOrder::getStatus, 2))) { | 
 |  |  |                             throw new CoolException("ASN主单状态修改失败!!"); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 }); | 
 |  |  |                 return true; | 
 |  |  |             } else { | 
 |  |  |                 throw new CoolException(reponse.getMsg()); | 
 |  |  |             } catch (JsonProcessingException e) { | 
 |  |  |                 throw new CoolException(e.getMessage()); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } |