| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Task operateComplete(Long id, Long loginUserId) { |
| | | List<Integer> longs = Arrays.asList(TaskStsType.GENERATE_IN.id, TaskStsType.GENERATE_OUT.id); |
| | | // 先查询任务,不限制状态 |
| | | Task task = taskService.getOne(new LambdaQueryWrapper<Task>() |
| | | .eq(Task::getId, id) |
| | | .in(Task::getTaskStatus, longs)); |
| | | .eq(Task::getId, id)); |
| | | |
| | | if (Objects.isNull(task)) { |
| | | throw new CoolException("数据错误:当前任务不可执行完结操作!!"); |
| | | throw new CoolException("任务不存在!!"); |
| | | } |
| | | |
| | | // 根据前端逻辑判断是否允许完成: |
| | | // 前端条件:((taskStatus < 98) || (taskType >= 101 && taskStatus < 198)) || (taskType == 11 && taskStatus == 101) |
| | | // 1. 任何任务状态 < 98 都可以完成 |
| | | // 2. 出库任务(taskType >= 101)且状态 < 198 可以完成 |
| | | // 3. 库格移载(taskType == 11)且状态 == 101 可以完成 |
| | | boolean canComplete = false; |
| | | if (task.getTaskStatus() < TaskStsType.COMPLETE_IN.id) { |
| | | // 任何任务状态 < 98 都可以完成 |
| | | canComplete = true; |
| | | } else if (task.getTaskType() >= 101 && task.getTaskStatus() < TaskStsType.COMPLETE_OUT.id) { |
| | | // 出库任务且状态 < 198 |
| | | canComplete = true; |
| | | } else if (task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type) |
| | | && task.getTaskStatus().equals(TaskStsType.GENERATE_OUT.id)) { |
| | | // 库格移载且状态 == 101 |
| | | canComplete = true; |
| | | } |
| | | |
| | | if (!canComplete) { |
| | | throw new CoolException("数据错误:当前任务不可执行完结操作!!任务类型:" + task.getTaskType() + ",任务状态:" + task.getTaskStatus()); |
| | | } |
| | | |
| | | modiftyTaskSort(task, loginUserId); |
| | | // |
| | | // if (task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) { |
| | | // task.setTaskStatus(TaskStsType.COMPLETE_OUT.id); |
| | | // } else { |
| | | task.setTaskStatus(task.getTaskType() < 100 ? TaskStsType.COMPLETE_IN.id : TaskStsType.AWAIT.id); |
| | | // } |
| | | |
| | | // 如果任务状态已经是AWAIT (196),再次点击完结时,直接完成 |
| | | if (task.getTaskStatus().equals(TaskStsType.AWAIT.id)) { |
| | | // AWAIT状态的任务再次完结,直接设置为出库完成 |
| | | task.setTaskStatus(TaskStsType.COMPLETE_OUT.id); |
| | | |
| | | // 更新出库站点状态(与RCS通知完结保持一致) |
| | | if (task.getTaskType() >= TaskType.TASK_TYPE_OUT.type && StringUtils.isNotBlank(task.getTargSite())) { |
| | | BasStation station = basStationService.getOne(new LambdaQueryWrapper<BasStation>() |
| | | .eq(BasStation::getStationName, task.getTargSite())); |
| | | if (Objects.nonNull(station) && station.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) { |
| | | station.setUseStatus(LocStsType.LOC_STS_TYPE_F.type); |
| | | if (!basStationService.updateById(station)) { |
| | | throw new CoolException("出库站点状态修改失败!!"); |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | // 其他情况按原有逻辑处理 |
| | | // 入库任务(taskType < 100):设置为入库完成 |
| | | // 出库任务(taskType >= 100):设置为等待确认 |
| | | Integer newStatus = task.getTaskType() < 100 ? TaskStsType.COMPLETE_IN.id : TaskStsType.AWAIT.id; |
| | | task.setTaskStatus(newStatus); |
| | | |
| | | // 如果是入库任务完成,更新入库站点状态(与RCS通知完结保持一致) |
| | | if (newStatus.equals(TaskStsType.COMPLETE_IN.id) && StringUtils.isNotBlank(task.getOrgSite())) { |
| | | BasStation station = basStationService.getOne(new LambdaQueryWrapper<BasStation>() |
| | | .eq(BasStation::getStationName, task.getOrgSite())); |
| | | if (Objects.nonNull(station) && station.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) { |
| | | station.setUseStatus(LocStsType.LOC_STS_TYPE_O.type); |
| | | if (!basStationService.updateById(station)) { |
| | | throw new CoolException("入库站点状态修改失败!!"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (!this.updateById(task)) { |
| | | throw new CoolException("完成任务失败"); |
| | | } |
| | | return task; |
| | | } |
| | | |
| | | /** |
| | | * 全版出库完结:扣除库位数量,将库位状态设为空 |
| | | * |
| | | * @param id 任务ID |
| | | * @param loginUserId 登录用户ID |
| | | * @return 任务对象 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Task completeFullOutStock(Long id, Long loginUserId) { |
| | | // 查询任务 |
| | | Task task = taskService.getOne(new LambdaQueryWrapper<Task>() |
| | | .eq(Task::getId, id)); |
| | | |
| | | if (Objects.isNull(task)) { |
| | | throw new CoolException("任务不存在!!"); |
| | | } |
| | | |
| | | // 检查任务类型是否为全版出库 |
| | | if (!task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) { |
| | | throw new CoolException("当前任务不是全版出库任务,无法执行此操作!!"); |
| | | } |
| | | |
| | | // 检查任务状态:必须是199(WAVE_SEED)状态才能手动完结 |
| | | if (!task.getTaskStatus().equals(TaskStsType.WAVE_SEED.id)) { |
| | | throw new CoolException("任务状态不是等待确认状态(199),无法执行此操作!!当前状态:" + task.getTaskStatus()); |
| | | } |
| | | |
| | | // 查询库位 |
| | | Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getOrgLoc())); |
| | | if (Objects.isNull(loc)) { |
| | | throw new CoolException("库位不存在!!"); |
| | | } |
| | | |
| | | // 删除库位明细(扣除数量) |
| | | try { |
| | | subtractLocItem(loc); |
| | | } catch (Exception e) { |
| | | logger.error("删除库位明细失败", e); |
| | | throw new CoolException("删除库位明细失败:" + e.getMessage()); |
| | | } |
| | | |
| | | // 删除作业中库存记录(LocItemWorking) |
| | | locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>() |
| | | .eq(LocItemWorking::getTaskId, task.getId())); |
| | | |
| | | // 将库位状态设为空(O状态) |
| | | if (!locService.update(new LambdaUpdateWrapper<Loc>() |
| | | .set(Loc::getBarcode, null) |
| | | .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type) |
| | | .set(Loc::getUpdateBy, loginUserId) |
| | | .set(Loc::getUpdateTime, new Date()) |
| | | .eq(Loc::getCode, task.getOrgLoc()))) { |
| | | throw new CoolException("库位状态修改失败!!"); |
| | | } |
| | | |
| | | // 更新出库站点状态(如果有目标站点) |
| | | if (StringUtils.isNotBlank(task.getTargSite())) { |
| | | BasStation station = basStationService.getOne(new LambdaQueryWrapper<BasStation>() |
| | | .eq(BasStation::getStationName, task.getTargSite())); |
| | | if (Objects.nonNull(station) && station.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) { |
| | | station.setUseStatus(LocStsType.LOC_STS_TYPE_F.type); |
| | | if (!basStationService.updateById(station)) { |
| | | throw new CoolException("出库站点状态修改失败!!"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 更新任务状态为库存更新完成(200) |
| | | task.setTaskStatus(TaskStsType.UPDATED_OUT.id) |
| | | .setUpdateBy(loginUserId) |
| | | .setUpdateTime(new Date()); |
| | | |
| | | if (!this.updateById(task)) { |
| | | throw new CoolException("任务状态更新失败!!"); |
| | | } |
| | | |
| | | return task; |
| | | } |
| | | |
| | |
| | | LocItem locItem = new LocItem(); |
| | | LocItemWorking locWorking = locItemWorkingService.getOne(new LambdaQueryWrapper<LocItemWorking>() |
| | | .eq(LocItemWorking::getTaskId, taskItem.getTaskId()) |
| | | .eq(LocItemWorking::getFieldsIndex, taskItem.getFieldsIndex()) |
| | | .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItemWorking::getFieldsIndex, taskItem.getFieldsIndex()) |
| | | .eq(StringUtils.isNotEmpty(taskItem.getBatch()), LocItemWorking::getBatch, taskItem.getBatch()) |
| | | .eq(LocItemWorking::getMatnrId, taskItem.getMatnrId())); |
| | | if (Objects.isNull(locWorking)) { |
| | |
| | | |
| | | tempLocs.forEach(working -> { |
| | | taskItems.forEach(taskItem -> { |
| | | if (taskItem.getFieldsIndex().equals(working.getFieldsIndex())) { |
| | | if (Objects.equals(taskItem.getFieldsIndex(), working.getFieldsIndex())) { |
| | | Double minQty = taskItem.getAnfme(); |
| | | if (!task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) { |
| | | // 计算剩余数量:从LocItemWorking中减去TaskItem的拣料数量 |
| | | minQty = Math.round((working.getAnfme() - taskItem.getQty()) * 1000000) / 1000000.0; |
| | | } |
| | | if (minQty.compareTo(0.0) >= 0) { |
| | | // 更新TaskItem的剩余数量 |
| | | taskItem.setAnfme(minQty); |
| | | if (!taskItemService.updateById(taskItem)) { |
| | | throw new CoolException("任务明细修改失败!!"); |
| | | } |
| | | // 更新LocItemWorking的剩余数量(非盘点入库时需要更新) |
| | | if (!task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) { |
| | | working.setAnfme(minQty); |
| | | if (!locItemWorkingService.updateById(working)) { |
| | | throw new CoolException("作业库存数量更新失败!!"); |
| | | } |
| | | } |
| | | } else { |
| | | // 剩余数量小于0,删除任务明细 |
| | | if (!taskItemService.removeById(taskItem)) { |
| | | log.error("任务明细修改失败!!"); |
| | | } |
| | |
| | | } |
| | | }); |
| | | }); |
| | | List<String> matnrIds = taskItems.stream().map(TaskItem::getFieldsIndex).collect(Collectors.toList()); |
| | | List<String> matnrIds = taskItems.stream() |
| | | .map(TaskItem::getFieldsIndex) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toList()); |
| | | //删除与任务明细重复的库存信息,以任务明细为准 |
| | | List<LocItemWorking> itemWorkings = tempLocs.stream() |
| | | .filter(working -> !matnrIds.contains(working.getFieldsIndex())) |
| | | .filter(working -> working.getFieldsIndex() != null && !matnrIds.contains(working.getFieldsIndex())) |
| | | .collect(Collectors.toList()); |
| | | itemWorkings.forEach(working -> { |
| | | TaskItem taskItem = taskItems.stream().findFirst().get(); |
| | |
| | | } |
| | | |
| | | List<LocItem> locItems = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId())); |
| | | if (locItems.isEmpty()) { |
| | | throw new CoolException("库位明细不存在!!"); |
| | | } |
| | | // 如果库位明细为空,可能是已经被处理过了,允许继续执行 |
| | | if (!locItems.isEmpty()) { |
| | | List<LocItemWorking> workings = new ArrayList<>(); |
| | | for (LocItem item : locItems) { |
| | | LocItemWorking working = new LocItemWorking(); |
| | | BeanUtils.copyProperties(item, working); |
| | | working.setId(null) |
| | | .setTaskId(task.getId()) |
| | | .setLocItemId(item.getId()) |
| | | .setUpdateBy(loginUserId) |
| | | .setUpdateTime(new Date()); |
| | | workings.add(working); |
| | | } |
| | | |
| | | List<LocItemWorking> workings = new ArrayList<>(); |
| | | for (LocItem item : locItems) { |
| | | LocItemWorking working = new LocItemWorking(); |
| | | BeanUtils.copyProperties(item, working); |
| | | working.setId(null) |
| | | .setTaskId(task.getId()) |
| | | .setLocItemId(item.getId()) |
| | | .setUpdateBy(loginUserId) |
| | | .setUpdateTime(new Date()); |
| | | workings.add(working); |
| | | } |
| | | if (!workings.isEmpty() && !locItemWorkingService.saveBatch(workings)) { |
| | | throw new CoolException("临时库存保存失败!!"); |
| | | } |
| | | |
| | | if (!locItemWorkingService.saveBatch(workings)) { |
| | | throw new CoolException("临时库存保存失败!!"); |
| | | } |
| | | |
| | | try { |
| | | //更新库位明细 |
| | | subtractLocItem(loc); |
| | | } catch (Exception e) { |
| | | logger.error("<UNK>", e); |
| | | throw new CoolException(e.getMessage()); |
| | | try { |
| | | // 根据任务类型更新库位明细 |
| | | if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) { |
| | | // 全版出库:删除所有库位明细 |
| | | subtractLocItem(loc); |
| | | } else { |
| | | // 部分出库(如拣料出库):根据TaskItem数量扣减库位明细 |
| | | subtractLocItemByTaskItems(loc, taskItems, loginUserId); |
| | | } |
| | | } catch (Exception e) { |
| | | logger.error("<UNK>", e); |
| | | throw new CoolException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | //添加出入库记录信息 |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void pubTaskToWcs(List<Task> tasks) { |
| | | WcsTaskParams taskParams = new WcsTaskParams(); |
| | | List<TaskItemParam> items = new ArrayList<>(); |
| | | /**任务下发接口*/ |
| | | String pubTakUrl = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.pubTask; |
| | | |
| | | // 设置批次编号(使用第一个任务的barcode,如果为null则使用任务编码) |
| | | String batchNo = null; |
| | | if (!tasks.isEmpty()) { |
| | | Task firstTask = tasks.get(0); |
| | | batchNo = StringUtils.isNotBlank(firstTask.getBarcode()) |
| | | ? firstTask.getBarcode() |
| | | : firstTask.getTaskCode(); |
| | | } |
| | | if (StringUtils.isBlank(batchNo)) { |
| | | // 如果批次编号仍为空,生成一个默认值 |
| | | batchNo = "BATCH_" + System.currentTimeMillis(); |
| | | } |
| | | |
| | | taskParams.setBatchNo(batchNo); |
| | | log.info("设置批次编号:{}", batchNo); |
| | | |
| | | tasks.forEach(task -> { |
| | | for (Task task : tasks) { |
| | | WcsTaskParams taskParams = new WcsTaskParams(); |
| | | List<TaskItemParam> items = new ArrayList<>(); |
| | | |
| | | // 设置批次编号(使用当前任务的任务编码) |
| | | String batchNo = task.getTaskCode(); |
| | | if (StringUtils.isBlank(batchNo)) { |
| | | // 如果任务编号为空,生成一个默认值 |
| | | batchNo = "BATCH_" + System.currentTimeMillis(); |
| | | } |
| | | taskParams.setBatchNo(batchNo); |
| | | log.info("任务批次编号:{}", batchNo); |
| | | |
| | | // 构建当前任务的参数 |
| | | TaskItemParam itemParam = new TaskItemParam(); |
| | | //任务编码(对应seqNum) |
| | | itemParam.setTaskNo(task.getTaskCode()); |
| | |
| | | if (!task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) { |
| | | station = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getTargSite())); |
| | | if (Objects.isNull(station)) { |
| | | throw new CoolException("站点不存在!!"); |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("站点不存在!!任务编码:{},目标站点:{}", task.getTaskCode(), task.getTargSite()); |
| | | continue; |
| | | } |
| | | } |
| | | |
| | |
| | | if (task.getTaskType() <= TaskType.TASK_TYPE_CHECK_IN.type && !task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) { |
| | | station.setUseStatus(LocStsType.LOC_STS_TYPE_R.type); |
| | | if (!basStationService.updateById(station)) { |
| | | throw new CoolException("站点状态更新失败!!"); |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("站点状态更新失败!!任务编码:{},站点:{}", task.getTaskCode(), station.getStationName()); |
| | | continue; |
| | | } |
| | | } else if (task.getTaskType() >= TaskType.TASK_TYPE_OUT.type) { |
| | | station.setUseStatus(LocStsType.LOC_STS_TYPE_S.type); |
| | | if (!basStationService.updateById(station)) { |
| | | throw new CoolException("站点状态更新失败!!"); |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("站点状态更新失败!!任务编码:{},站点:{}", task.getTaskCode(), station.getStationName()); |
| | | continue; |
| | | } |
| | | } |
| | | } |
| | |
| | | || task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_OUT.type) |
| | | || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type) |
| | | || task.getTaskType().equals(TaskType.TASK_TYPE_MERGE_OUT.type)) { |
| | | /**出库参数*/ |
| | | itemParam.setOriLoc(task.getOrgLoc()) |
| | | .setDestSta(task.getTargSite()); |
| | | /**出库参数 - 需要根据任务明细中的多个库位进行组装*/ |
| | | // 查询任务明细 |
| | | List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId())); |
| | | if (!taskItems.isEmpty()) { |
| | | // 通过TaskItem的source字段查询LocItem,获取库位信息 |
| | | Set<String> locCodes = new HashSet<>(); |
| | | Map<String, LocItem> locItemMap = new HashMap<>(); |
| | | |
| | | for (TaskItem taskItem : taskItems) { |
| | | if (taskItem.getSource() != null) { |
| | | // source字段对应LocItem的id |
| | | LocItem locItem = locItemService.getById(taskItem.getSource()); |
| | | if (locItem != null && StringUtils.isNotBlank(locItem.getLocCode())) { |
| | | locCodes.add(locItem.getLocCode()); |
| | | locItemMap.put(locItem.getLocCode(), locItem); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 如果通过source没有找到库位,使用Task的orgLoc作为默认值 |
| | | if (locCodes.isEmpty() && StringUtils.isNotBlank(task.getOrgLoc())) { |
| | | locCodes.add(task.getOrgLoc()); |
| | | } |
| | | |
| | | // 为每个不同的库位创建一个TaskItemParam |
| | | for (String locCode : locCodes) { |
| | | TaskItemParam outItemParam = new TaskItemParam(); |
| | | outItemParam.setTaskNo(task.getTaskCode()); |
| | | outItemParam.setPriority(1); |
| | | outItemParam.setOriLoc(locCode); |
| | | outItemParam.setDestSta(task.getTargSite()); |
| | | if (task.getBarcode() != null) { |
| | | outItemParam.setZpallet(task.getBarcode()); |
| | | } |
| | | items.add(outItemParam); |
| | | } |
| | | |
| | | log.info("出库任务包含{}个库位:{}", locCodes.size(), locCodes); |
| | | } else { |
| | | // 如果没有任务明细,使用Task的orgLoc |
| | | itemParam.setOriLoc(task.getOrgLoc()) |
| | | .setDestSta(task.getTargSite()); |
| | | items.add(itemParam); |
| | | } |
| | | } else { |
| | | /**站点间移库参数*/ |
| | | itemParam.setOriSta(task.getOrgSite()).setDestSta(task.getTargSite()); |
| | | |
| | | BasStation curSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getOrgSite())); |
| | | if (Objects.isNull(curSta)) { |
| | | throw new CoolException("站点不存在!!"); |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("站点不存在!!任务编码:{},源站点:{}", task.getTaskCode(), task.getOrgSite()); |
| | | continue; |
| | | } |
| | | if (curSta.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) { |
| | | if (!curSta.getUseStatus().equals(LocStsType.LOC_STS_TYPE_F.type)) { |
| | | throw new CoolException("当前站点不是F.在库状态!!"); |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("当前站点不是F.在库状态!!任务编码:{},站点:{},当前状态:{}", task.getTaskCode(), curSta.getStationName(), curSta.getUseStatus()); |
| | | continue; |
| | | } |
| | | } |
| | | if (station.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) { |
| | | if (!station.getUseStatus().equals(LocStsType.LOC_STS_TYPE_O.type)) { |
| | | throw new CoolException("目标站点不是O.空闲状态!!"); |
| | | // 站点间移库需要获取目标站点 |
| | | BasStation targetStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getTargSite())); |
| | | if (Objects.isNull(targetStation)) { |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("目标站点不存在!!任务编码:{},目标站点:{}", task.getTaskCode(), task.getTargSite()); |
| | | continue; |
| | | } |
| | | if (targetStation.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) { |
| | | if (!targetStation.getUseStatus().equals(LocStsType.LOC_STS_TYPE_O.type)) { |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("目标站点不是O.空闲状态!!任务编码:{},站点:{},当前状态:{}", task.getTaskCode(), targetStation.getStationName(), targetStation.getUseStatus()); |
| | | continue; |
| | | } |
| | | } |
| | | } |
| | | items.add(itemParam); |
| | | }); |
| | | taskParams.setTasks(items); |
| | | /**任务下发接口*/ |
| | | String pubTakUrl = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.pubTask; |
| | | /**RCS基础配置链接*/ |
| | | log.info("========== 开始下发任务到RCS =========="); |
| | | log.info("RCS请求地址:{}", pubTakUrl); |
| | | log.info("批次编号:{}", batchNo); |
| | | log.info("任务数量:{}", tasks.size()); |
| | | log.info("任务列表详情:"); |
| | | tasks.forEach(task -> { |
| | | log.info(" - 任务编码:{},任务类型:{},源库位:{},目标库位:{},源站点:{},目标站点:{}", |
| | | |
| | | // 对于非出库任务,添加单个itemParam |
| | | if (!(task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type) |
| | | || task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type) |
| | | || task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_OUT.type) |
| | | || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type) |
| | | || task.getTaskType().equals(TaskType.TASK_TYPE_MERGE_OUT.type))) { |
| | | items.add(itemParam); |
| | | } |
| | | |
| | | taskParams.setTasks(items); |
| | | |
| | | // 记录当前任务信息 |
| | | log.info(" RCS- 任务编码:{},任务类型:{},源库位:{},目标库位:{},源站点:{},目标站点:{}", |
| | | task.getTaskCode(), task.getTaskType(), task.getOrgLoc(), |
| | | task.getTargLoc(), task.getOrgSite(), task.getTargSite()); |
| | | }); |
| | | log.info("请求参数:{}", JSONObject.toJSONString(taskParams)); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.add("Content-Type", "application/json"); |
| | | headers.add("api-version", "v2.0"); |
| | | HttpEntity httpEntity = new HttpEntity(taskParams, headers); |
| | | long startTime = System.currentTimeMillis(); |
| | | ResponseEntity<String> exchange = restTemplate.exchange(pubTakUrl, HttpMethod.POST, httpEntity, String.class); |
| | | long endTime = System.currentTimeMillis(); |
| | | log.info("RCS响应耗时:{}ms", (endTime - startTime)); |
| | | log.info("RCS响应状态码:{}", exchange.getStatusCode()); |
| | | log.info("RCS响应头:{}", exchange.getHeaders()); |
| | | log.info("RCS响应体:{}", exchange.getBody()); |
| | | if (Objects.isNull(exchange.getBody())) { |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("RCS响应体为空,无法解析响应结果"); |
| | | log.error("请求地址:{}", pubTakUrl); |
| | | log.error("请求参数:{}", JSONObject.toJSONString(taskParams)); |
| | | throw new CoolException("任务下发失败,RCS响应体为空!!"); |
| | | } else { |
| | | log.info("RCS-请求参数:{}", JSONObject.toJSONString(taskParams)); |
| | | |
| | | // 发送当前任务 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.add("Content-Type", "application/json"); |
| | | headers.add("api-version", "v2.0"); |
| | | HttpEntity httpEntity = new HttpEntity(taskParams, headers); |
| | | |
| | | /**RCS基础配置链接*/ |
| | | log.info("========== 开始下发任务到RCS =========="); |
| | | log.info("请求RCS-地址:{}", pubTakUrl); |
| | | log.info("请求RCS-参数:{}", JSONObject.toJSONString(taskParams)); |
| | | long startTime = System.currentTimeMillis(); |
| | | ResponseEntity<String> exchange = null; |
| | | try { |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | objectMapper.coercionConfigDefaults() |
| | | .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty); |
| | | CommonResponse result = objectMapper.readValue(exchange.getBody(), CommonResponse.class); |
| | | log.info("RCS响应解析结果 - code:{},msg:{},data:{}", |
| | | result.getCode(), result.getMsg(), result.getData()); |
| | | if (result.getCode() == 200) { |
| | | log.info("RCS任务下发成功,开始更新任务状态"); |
| | | tasks.forEach(task -> { |
| | | log.info("更新任务状态 - 任务编码:{},任务类型:{}", task.getTaskCode(), task.getTaskType()); |
| | | exchange = restTemplate.exchange(pubTakUrl, HttpMethod.POST, httpEntity, String.class); |
| | | long endTime = System.currentTimeMillis(); |
| | | log.info("RCS响应耗时:{}ms", (endTime - startTime)); |
| | | log.info("RCS响应状态码:{}", exchange.getStatusCode()); |
| | | log.info("RCS响应头:{}", exchange.getHeaders()); |
| | | log.info("RCS响应体:{}", exchange.getBody()); |
| | | } catch (org.springframework.web.client.ResourceAccessException e) { |
| | | long endTime = System.currentTimeMillis(); |
| | | log.error("========== RCS任务下发资源访问异常 =========="); |
| | | log.error("请求RCS-资源访问异常(可能包含连接超时),耗时:{}ms,任务编码:{}", (endTime - startTime), task.getTaskCode(), e); |
| | | log.error("请求RCS-请求地址:{}", pubTakUrl); |
| | | log.error("请求RCS-请求参数:{}", JSONObject.toJSONString(taskParams)); |
| | | // 检查是否是连接超时异常 |
| | | Throwable cause = e.getCause(); |
| | | String errorMsg = e.getMessage(); |
| | | if (cause instanceof java.net.SocketTimeoutException || |
| | | (cause instanceof java.net.ConnectException && cause.getMessage() != null && cause.getMessage().contains("timed out")) || |
| | | (errorMsg != null && (errorMsg.contains("Connection timed out") || errorMsg.contains("timed out") || errorMsg.contains("timeout")))) { |
| | | log.error("RCS连接超时,任务下发失败!任务编码:{},错误信息:{}", task.getTaskCode(), errorMsg); |
| | | } else { |
| | | log.error("RCS资源访问异常,任务下发失败!任务编码:{},错误信息:{}", task.getTaskCode(), errorMsg); |
| | | } |
| | | continue; |
| | | } catch (Exception e) { |
| | | long endTime = System.currentTimeMillis(); |
| | | log.error("========== RCS任务下发异常 =========="); |
| | | log.error("请求RCS-异常,耗时:{}ms,任务编码:{}", (endTime - startTime), task.getTaskCode(), e); |
| | | log.error("请求RCS-地址:{}", pubTakUrl); |
| | | log.error("请求RCS-参数:{}", JSONObject.toJSONString(taskParams)); |
| | | String errorMsg = e.getMessage(); |
| | | // 检查是否是连接超时相关的异常 |
| | | if (errorMsg != null && (errorMsg.contains("Connection timed out") || errorMsg.contains("timed out") || errorMsg.contains("timeout"))) { |
| | | log.error("RCS连接超时,任务下发失败!任务编码:{},错误信息:{}", task.getTaskCode(), errorMsg); |
| | | } else { |
| | | log.error("RCS任务下发异常!任务编码:{},错误信息:{}", task.getTaskCode(), errorMsg); |
| | | } |
| | | continue; |
| | | } |
| | | |
| | | if (Objects.isNull(exchange) || Objects.isNull(exchange.getBody())) { |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("请求RCS-RCS响应体为空,无法解析响应结果"); |
| | | log.error("请求RCS-地址:{}", pubTakUrl); |
| | | log.error("请求RCS-参数:{}", JSONObject.toJSONString(taskParams)); |
| | | log.error("请求RCS-失败的任务编码:{}", task.getTaskCode()); |
| | | log.error("任务下发失败,RCS响应体为空!!任务编码:{}", task.getTaskCode()); |
| | | continue; |
| | | } else { |
| | | try { |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | objectMapper.coercionConfigDefaults() |
| | | .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty); |
| | | CommonResponse result = objectMapper.readValue(exchange.getBody(), CommonResponse.class); |
| | | log.info("RCS响应解析结果 - code:{},msg:{},data:{}", |
| | | result.getCode(), result.getMsg(), result.getData()); |
| | | if (result.getCode() == 200) { |
| | | log.info("RCS任务下发成功,开始更新任务状态 - 任务编码:{}", task.getTaskCode()); |
| | | if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type) |
| | | || task.getTaskType().equals(TaskType.TASK_TYPE_PICK_IN.type) |
| | | || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type) |
| | |
| | | |
| | | BasStation curSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getOrgSite())); |
| | | if (Objects.isNull(curSta)) { |
| | | throw new CoolException("站点不存在!!"); |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("站点不存在!!任务编码:{},源站点:{}", task.getTaskCode(), task.getOrgSite()); |
| | | continue; |
| | | } |
| | | |
| | | log.info("更新入库任务状态 - 任务编码:{},新状态:{}", task.getTaskCode(), TaskStsType.WCS_EXECUTE_IN.id); |
| | | if (!taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getTaskCode, task.getTaskCode()) |
| | | .set(Task::getTaskStatus, TaskStsType.WCS_EXECUTE_IN.id))) { |
| | | throw new CoolException("任务状态修改失败!!"); |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("任务状态修改失败!!任务编码:{}", task.getTaskCode()); |
| | | continue; |
| | | } |
| | | log.info("入库任务状态更新成功 - 任务编码:{}", task.getTaskCode()); |
| | | /**排除移库功能*/ |
| | |
| | | if (curSta.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) { |
| | | curSta.setUseStatus(LocStsType.LOC_STS_TYPE_R.type); |
| | | if (!basStationService.updateById(curSta)) { |
| | | throw new CoolException("站点预约失败!!"); |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("站点预约失败!!任务编码:{},站点:{}", task.getTaskCode(), curSta.getStationName()); |
| | | continue; |
| | | } |
| | | } |
| | | } |
| | |
| | | || task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type)) { |
| | | BasStation curSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getTargSite())); |
| | | if (Objects.isNull(curSta)) { |
| | | throw new CoolException("站点不存在!!"); |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("站点不存在!!任务编码:{},目标站点:{}", task.getTaskCode(), task.getTargSite()); |
| | | continue; |
| | | } |
| | | |
| | | log.info("更新出库任务状态 - 任务编码:{},新状态:{}", task.getTaskCode(), TaskStsType.WCS_EXECUTE_OUT.id); |
| | | if (!taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getTaskCode, task.getTaskCode()) |
| | | .set(Task::getTaskStatus, TaskStsType.WCS_EXECUTE_OUT.id))) { |
| | | throw new CoolException("任务状态修改失败!!"); |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("任务状态修改失败!!任务编码:{}", task.getTaskCode()); |
| | | continue; |
| | | } |
| | | log.info("出库任务状态更新成功 - 任务编码:{}", task.getTaskCode()); |
| | | /**如果是普通站点,修改站点状态为入库预约*/ |
| | | if (curSta.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) { |
| | | curSta.setUseStatus(LocStsType.LOC_STS_TYPE_S.type); |
| | | if (!basStationService.updateById(curSta)) { |
| | | throw new CoolException("站点预约失败!!"); |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("站点预约失败!!任务编码:{},站点:{}", task.getTaskCode(), curSta.getStationName()); |
| | | continue; |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | log.info("========== RCS任务下发完成,共{}个任务状态已更新 ==========", tasks.size()); |
| | | } else { |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("RCS返回错误 - code:{},msg:{},data:{}", |
| | | result.getCode(), result.getMsg(), result.getData()); |
| | | log.error("失败的任务列表:"); |
| | | tasks.forEach(task -> { |
| | | log.error(" - 任务编码:{},任务类型:{}", task.getTaskCode(), task.getTaskType()); |
| | | }); |
| | | // throw new CoolException("任务下发失败!!"); |
| | | } else { |
| | | log.error("========== RCS任务下发失败 =========="); |
| | | log.error("RCS返回错误 - code:{},msg:{},data:{}", |
| | | result.getCode(), result.getMsg(), result.getData()); |
| | | log.error("失败的任务编码:{},任务类型:{}", task.getTaskCode(), task.getTaskType()); |
| | | log.error("任务下发失败!!任务编码:{}", task.getTaskCode()); |
| | | continue; |
| | | } |
| | | } catch (JsonProcessingException e) { |
| | | log.error("========== RCS任务下发异常 =========="); |
| | | log.error("解析RCS响应失败,响应体:{},任务编码:{}", exchange.getBody(), task.getTaskCode(), e); |
| | | log.error("解析RCS响应失败:{},任务编码:{}", e.getMessage(), task.getTaskCode()); |
| | | continue; |
| | | } catch (Exception e) { |
| | | log.error("========== RCS任务下发异常 =========="); |
| | | log.error("任务下发过程中发生异常,任务编码:{}", task.getTaskCode(), e); |
| | | log.error("任务下发异常:{},任务编码:{}", e.getMessage(), task.getTaskCode()); |
| | | continue; |
| | | } |
| | | } catch (JsonProcessingException e) { |
| | | log.error("========== RCS任务下发异常 =========="); |
| | | log.error("解析RCS响应失败,响应体:{}", exchange.getBody(), e); |
| | | throw new CoolException("解析RCS响应失败:" + e.getMessage()); |
| | | } catch (Exception e) { |
| | | log.error("========== RCS任务下发异常 =========="); |
| | | log.error("任务下发过程中发生异常", e); |
| | | throw e; |
| | | } |
| | | } |
| | | log.info("========== RCS任务下发完成,共{}个任务已处理 ==========", tasks.size()); |
| | | }/** |
| | | |
| | | |
| | | /** |
| | | * @author Ryan |
| | | * @date 2025/5/20 |
| | | * @description: 扣减库存明细 |
| | | * @description: 扣减库存明细(全版出库:删除所有库位明细) |
| | | * @version 1.0 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void subtractLocItem(Loc loc) throws Exception { |
| | | if (!locItemService.remove(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()))) { |
| | | throw new CoolException("库存明细删除失败!!"); |
| | | // 删除库位明细,如果没有记录则忽略(可能已经被删除过了) |
| | | locItemService.remove(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId())); |
| | | } |
| | | |
| | | /** |
| | | * 根据任务明细扣减库位明细数量(部分出库) |
| | | * |
| | | * @param loc 库位 |
| | | * @param taskItems 任务明细列表 |
| | | * @param loginUserId 登录用户ID |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void subtractLocItemByTaskItems(Loc loc, List<TaskItem> taskItems, Long loginUserId) { |
| | | for (TaskItem taskItem : taskItems) { |
| | | // 查询对应的库位明细 |
| | | LocItem locItem = locItemService.getOne(new LambdaQueryWrapper<LocItem>() |
| | | .eq(LocItem::getLocId, loc.getId()) |
| | | .eq(LocItem::getMatnrId, taskItem.getMatnrId()) |
| | | .eq(StringUtils.isNotBlank(taskItem.getBatch()), LocItem::getBatch, taskItem.getBatch()) |
| | | .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItem::getFieldsIndex, taskItem.getFieldsIndex())); |
| | | |
| | | if (Objects.nonNull(locItem)) { |
| | | // 计算扣减后的数量 |
| | | Double newAnfme = Math.round((locItem.getAnfme() - taskItem.getQty()) * 1000000) / 1000000.0; |
| | | |
| | | if (newAnfme.compareTo(0.0) <= 0) { |
| | | // 数量小于等于0,删除库位明细 |
| | | locItemService.removeById(locItem.getId()); |
| | | } else { |
| | | // 更新库位明细数量 |
| | | locItem.setAnfme(newAnfme) |
| | | .setUpdateBy(loginUserId) |
| | | .setUpdateTime(new Date()); |
| | | if (!locItemService.updateById(locItem)) { |
| | | throw new CoolException("库位明细数量扣减失败!!"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | List<TaskItem> items = orderMap.get(key); |
| | | //保存入出库明细 |
| | | saveStockItems(items, task, pakinItem.getId(), pakinItem.getAsnCode(), pakinItem.getWkType(), pakinItem.getType(), loginUserId); |
| | | //移出收货区库存, 修改组托状态 |
| | | removeReceiptStock(pakinItem, loginUserId); |
| | | //移出收货区库存, 修改组托状态(只有当source不为null时才需要移除收货区库存) |
| | | if (Objects.nonNull(pakinItem.getSource())) { |
| | | removeReceiptStock(pakinItem, loginUserId); |
| | | } |
| | | }); |
| | | |
| | | Set<Long> pkinItemIds = taskItems.stream().map(TaskItem::getSource).collect(Collectors.toSet()); |
| | |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public synchronized void removeReceiptStock(WaitPakinItem pakinItem, Long loginUserId) { |
| | | // 如果source为null,说明组托明细不在收货区,无需移除收货区库存 |
| | | if (Objects.isNull(pakinItem.getSource())) { |
| | | return; |
| | | } |
| | | WarehouseAreasItem itemServiceOne = warehouseAreasItemService.getOne(new LambdaQueryWrapper<WarehouseAreasItem>() |
| | | .eq(WarehouseAreasItem::getId, pakinItem.getSource())); |
| | | if (Objects.isNull(itemServiceOne)) { |
| | |
| | | .eq(LocItem::getMatnrId, taskItem.getMatnrId()) |
| | | .eq(LocItem::getLocId, loc.getId()) |
| | | .eq(StringUtils.isNotBlank(taskItem.getBatch()), LocItem::getBatch, taskItem.getBatch()) |
| | | .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItem::getFieldsIndex, taskItem.getFieldsIndex())); |
| | | .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItem::getFieldsIndex, taskItem.getFieldsIndex()) |
| | | ); |
| | | if (Objects.isNull(locItem)) { |
| | | // 库位明细不存在,创建新的库位明细 |
| | | BeanUtils.copyProperties(taskItem, item); |
| | | item.setLocCode(loc.getCode()) |
| | | .setId(null) |
| | |
| | | throw new CoolException("库位明细更新失败!!"); |
| | | } |
| | | } else { |
| | | logger.error("当前票号:" + locItem.getFieldsIndex() + " 已在库内,请检查后再操作!!"); |
| | | // throw new CoolException("当前票号已在库内,请检查后再操作!!"); |
| | | // locItem.setAnfme(Math.round((locItem.getAnfme() + taskItem.getAnfme()) * 1000000) / 1000000.0) |
| | | // .setUpdateTime(new Date()); |
| | | // if (!locItemService.saveOrUpdate(locItem)) { |
| | | // throw new CoolException("库位明细更新失败!!"); |
| | | // } |
| | | // 库位明细已存在,累加数量 |
| | | Double newAnfme = Math.round((locItem.getAnfme() + taskItem.getAnfme()) * 1000000) / 1000000.0; |
| | | locItem.setAnfme(newAnfme) |
| | | .setUpdateBy(loginUserId) |
| | | .setUpdateTime(new Date()); |
| | | if (!locItemService.updateById(locItem)) { |
| | | throw new CoolException("库位明细数量更新失败!!"); |
| | | } |
| | | } |
| | | }); |
| | | |