|  |  | 
 |  |  |      * @description: 自动获取已生成盘点任务的盘点列表 | 
 |  |  |      * @version 1.0 | 
 |  |  |      */ | 
 |  |  |     @Scheduled(cron = "0/25 * * * * ?") | 
 |  |  |     @Scheduled(cron = "0/15 * * * * ?") | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     public void genReCheck() { | 
 |  |  |         List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>() | 
 |  |  |                 .notExists("SELECT 1 FROM man_check_diff_item cdi WHERE id != cdi.task_item_id")); | 
 |  |  |         List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>() | 
 |  |  |                 .select(Task::getId) | 
 |  |  |                 .eq(Task::getTaskStatus, TaskStsType.GENERATE_OUT.id) | 
 |  |  |                 .eq(Task::getTaskType, TaskType.TASK_TYPE_CHECK_OUT.type)); | 
 |  |  |         if (tasks.isEmpty()) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         List<CheckDiffItem> list = checkDiffItemService | 
 |  |  |                 .list(new LambdaQueryWrapper<CheckDiffItem>() | 
 |  |  |                         .select(CheckDiffItem::getTaskItemId)); | 
 |  |  |         Set<Long> taskIds = tasks.stream().map(Task::getId).collect(Collectors.toSet()); | 
 |  |  |         List<Long> itemIds = list.stream().map(CheckDiffItem::getTaskItemId).collect(Collectors.toList()); | 
 |  |  |         List<TaskItem> taskItems = taskItemService | 
 |  |  |                 .list(new LambdaQueryWrapper<TaskItem>() | 
 |  |  |                         .in(TaskItem::getTaskId, taskIds) | 
 |  |  |                         .notIn(!itemIds.isEmpty(), TaskItem::getId, itemIds)); | 
 |  |  |         if (taskItems.isEmpty()) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  | 
 |  |  |         taskMps.keySet().forEach(orderId -> { | 
 |  |  |             WkOrder order = checkOrderService.getById(orderId); | 
 |  |  |             if (Objects.isNull(order)) { | 
 |  |  |                throw new CoolException("盘点单据不存在!!"); | 
 |  |  |                 throw new CoolException("盘点单据不存在!!"); | 
 |  |  |             } | 
 |  |  |             CheckDiff checkDiff = checkDiffService.getOne(new LambdaQueryWrapper<CheckDiff>().eq(CheckDiff::getOrderId, orderId)); | 
 |  |  |             if (Objects.isNull(checkDiff)) { | 
 |  |  |                 checkDiff = new CheckDiff(); | 
 |  |  |                 checkDiff.setAnfme(order.getAnfme()) | 
 |  |  |                         .setCheckType(order.getCheckType()) | 
 |  |  |                         .setId(null) | 
 |  |  |                         .setOrderCode(order.getCode()) | 
 |  |  |                         .setExceStatus(CheckDiffExceStatus.CHECK_DIFF_EXCE_STATUS_ING.val) | 
 |  |  |                         .setExceStatus(CheckDiffExceStatus.CHECK_DIFF_EXCE_STATUS_INIT.val) | 
 |  |  |                         .setCreateBy(loginUserId) | 
 |  |  |                         .setCreateTime(new Date()) | 
 |  |  |                         .setUpdateBy(loginUserId) | 
 |  |  |                         .setUpdateTime(new Date()) | 
 |  |  |                         .setOrderId(order.getId()); | 
 |  |  |                 if (!checkDiffService.save(checkDiff)) { | 
 |  |  |                 if (!checkDiffService.saveOrUpdate(checkDiff)) { | 
 |  |  |                     throw new CoolException("盘点差异单保存失败!!"); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  | 
 |  |  |             List<TaskItem> items = taskMps.get(orderId); | 
 |  |  |             List<CheckDiffItem> diffItems = new ArrayList<>(); | 
 |  |  |             CheckDiff finalCheckDiff = checkDiff; | 
 |  |  |             items.forEach(item -> { | 
 |  |  |             for (TaskItem item : items) { | 
 |  |  |                 Task task = taskService.getById(item.getTaskId()); | 
 |  |  |                 if (Objects.isNull(task)) { | 
 |  |  |                     throw new CoolException("数据错误:任务不存在!!"); | 
 |  |  |                 } | 
 |  |  |                 CheckDiffItem checkDiffItem = new CheckDiffItem(); | 
 |  |  |                 BeanUtils.copyProperties(item, checkDiffItem); | 
 |  |  |                 checkDiffItem.setCheckId(finalCheckDiff.getId()) | 
 |  |  |                 checkDiffItem.setCheckId(checkDiff.getId()) | 
 |  |  |                         .setTaskItemId(item.getId()) | 
 |  |  |                         .setTaskId(item.getTaskId()) | 
 |  |  |                         .setCreateBy(loginUserId) | 
 |  |  | 
 |  |  |                         .setBarcode(task.getBarcode()) | 
 |  |  |                         .setUpdateBy(loginUserId) | 
 |  |  |                         .setUpdateTime(new Date()) | 
 |  |  |                         .setOrderCode(finalCheckDiff.getOrderCode()); | 
 |  |  |                         .setOrderCode(checkDiff.getOrderCode()); | 
 |  |  |                 diffItems.add(checkDiffItem); | 
 |  |  |  | 
 |  |  |                 if (Objects.isNull(finalCheckDiff.getAreaId())) { | 
 |  |  |                 if (Objects.isNull(checkDiff.getAreaId())) { | 
 |  |  |                     Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>() | 
 |  |  |                             .eq(Loc::getCode, task.getOrgLoc()) | 
 |  |  |                             .eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_R.type)); | 
 |  |  | 
 |  |  |                         throw new CoolException("盘点库位不存在!!"); | 
 |  |  |                     } | 
 |  |  |                     finalCheckDiff.setAreaId(loc.getAreaId()).setAreaName(loc.getAreaId$()); | 
 |  |  |  | 
 |  |  |                     if (!checkDiffService.updateById(finalCheckDiff)) { | 
 |  |  |                         throw new CoolException("库区信息修改失败!!"); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             }); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |            if (!checkDiffItemService.saveBatch(diffItems)) { | 
 |  |  |                throw new CoolException("盘点差异单保存失败!!"); | 
 |  |  |            } | 
 |  |  |             checkDiff.setAreaId(finalCheckDiff.getAreaId()).setAreaName(finalCheckDiff.getAreaName()); | 
 |  |  |  | 
 |  |  |             if (!checkDiffService.updateById(checkDiff)) { | 
 |  |  |                 throw new CoolException("库区信息修改失败!!"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             if (!checkDiffItemService.saveBatch(diffItems)) { | 
 |  |  |                 throw new CoolException("盘点差异单保存失败!!"); | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |     } | 
 |  |  | } |