| | |
| | | } |
| | | |
| | | /** |
| | | * 全版出库完结:扣除库位数量,将库位状态设为空 |
| | | * |
| | | * @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; |
| | | } |
| | | |
| | | /** |
| | | * 修改任务优先级 |
| | | * |
| | | * @param task |
| | |
| | | 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("任务明细修改失败!!"); |
| | | } |
| | |
| | | if (Objects.isNull(loc)) { |
| | | throw new CoolException("库位不存在!!"); |
| | | } |
| | | if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type)) { |
| | | throw new CoolException("库位状态不处理于R.出库预约!!"); |
| | | } |
| | | |
| | | |
| | | List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId())); |
| | | if (taskItems.isEmpty()) { |
| | | throw new CoolException("任务明细不存在!!"); |
| | | } |
| | | |
| | | List<LocItem> locItems = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId())); |
| | | |
| | | // 如果库位状态不是R,检查是否已经处理过 |
| | | if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type)) { |
| | | // 如果库位明细为空,说明已经处理过了,直接更新任务状态为199 |
| | | if (locItems.isEmpty()) { |
| | | logger.warn("任务{}的库位{}状态为{},但库位明细为空,可能已经处理过,直接更新任务状态为199", |
| | | task.getId(), loc.getCode(), loc.getUseStatus()); |
| | | if (!this.update(new LambdaUpdateWrapper<Task>() |
| | | .eq(Task::getId, task.getId()) |
| | | .set(Task::getUpdateBy, loginUserId) |
| | | .set(Task::getUpdateTime, new Date()) |
| | | .set(Task::getTaskStatus, TaskStsType.WAVE_SEED.id))) { |
| | | throw new CoolException("任务状态更新失败!!"); |
| | | } |
| | | return; // 跳过后续处理 |
| | | } else { |
| | | // 库位明细不为空但状态不是R,记录错误但不抛出异常,让定时任务继续处理其他任务 |
| | | logger.error("任务{}的库位{}状态为{},不是R.出库预约状态,但库位明细不为空,跳过处理。任务编码:{},库位编码:{}", |
| | | task.getId(), loc.getCode(), loc.getUseStatus(), task.getTaskCode(), loc.getCode()); |
| | | return; // 跳过处理,避免异常中断定时任务 |
| | | } |
| | | } |
| | | |
| | | // 如果库位明细为空,可能是已经被处理过了,允许继续执行 |
| | | if (!locItems.isEmpty()) { |
| | | List<LocItemWorking> workings = new ArrayList<>(); |
| | |
| | | } |
| | | |
| | | try { |
| | | //更新库位明细 |
| | | subtractLocItem(loc); |
| | | // 根据任务类型更新库位明细 |
| | | 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()); |
| | |
| | | // 为每个不同的库位创建一个TaskItemParam |
| | | for (String locCode : locCodes) { |
| | | TaskItemParam outItemParam = new TaskItemParam(); |
| | | outItemParam.setTaskNo(task.getTaskCode()); |
| | | String taskNo = locCodes.size() > 1 |
| | | ? task.getTaskCode() + "_" + locCode |
| | | : task.getTaskCode(); |
| | | outItemParam.setTaskNo(taskNo); |
| | | outItemParam.setPriority(1); |
| | | outItemParam.setOriLoc(locCode); |
| | | outItemParam.setDestSta(task.getTargSite()); |
| | |
| | | /** |
| | | * @author Ryan |
| | | * @date 2025/5/20 |
| | | * @description: 扣减库存明细 |
| | | * @description: 扣减库存明细(全版出库:删除所有库位明细) |
| | | * @version 1.0 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void subtractLocItem(Loc loc) throws Exception { |
| | | // 删除库位明细,如果没有记录则忽略(可能已经被删除过了) |
| | | 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("库位明细数量扣减失败!!"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | .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("库位明细数量更新失败!!"); |
| | | } |
| | | } |
| | | }); |
| | | |