| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | |
| | | import com.ruoyi.system.constants.AssetDeptConstant; |
| | | import com.ruoyi.system.dto.asset.AssetInventoryTaskDTO; |
| | | import com.ruoyi.system.dto.asset.AssetInventoryTaskItemDTO; |
| | | import com.ruoyi.system.dto.asset.AssetInventoryTaskItemUpdateDTO; |
| | | import com.ruoyi.system.dto.asset.AssetInventoryUserUpdateDTO; |
| | | import com.ruoyi.system.emums.AssetInventoryTaskStatusEnum; |
| | | import com.ruoyi.system.mapper.AssetInventoryTaskMapper; |
| | | import com.ruoyi.system.model.AssetInventoryRecord; |
| | | import com.ruoyi.system.model.AssetInventoryTask; |
| | |
| | | // 1. 查询所有有效的盘点任务,支持筛选条件 |
| | | LambdaQueryWrapper<AssetInventoryTask> taskWrapper = new LambdaQueryWrapper<>(); |
| | | taskWrapper.eq(AssetInventoryTask::getDisabled, false) |
| | | .ne(AssetInventoryTask::getStatus, AssetInventoryTaskStatusEnum.CANCELED.getCode()) |
| | | .orderByDesc(AssetInventoryTask::getCreateTime); |
| | | // 按部门筛选 |
| | | if (hasNoPermission) { |
| | |
| | | // 3. 查询并更新对应的资产盘点项 |
| | | LambdaQueryWrapper<AssetInventoryTaskItem> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(AssetInventoryTaskItem::getInventoryTaskId, dto.getTaskId()) |
| | | .eq(AssetInventoryTaskItem::getAssetMainId, dto.getAssetId()); |
| | | .in(AssetInventoryTaskItem::getAssetMainId, dto.getAssetIds()); |
| | | |
| | | AssetInventoryTaskItem taskItem = assetInventoryTaskItemService.getOne(wrapper); |
| | | if (taskItem == null) { |
| | | List<AssetInventoryTaskItem> taskItems = assetInventoryTaskItemService.list(wrapper); |
| | | if (CollUtil.isEmpty(taskItems)) { |
| | | throw new ServiceException("未找到对应的盘点资产项"); |
| | | } |
| | | |
| | | // 4. 更新盘点人 |
| | | String currentUserName = SecurityUtils.getLoginUser().getUser().getNickName(); |
| | | taskItem.setUserId(dto.getUserId()); |
| | | boolean updated = assetInventoryTaskItemService.updateById(taskItem); |
| | | taskItems.forEach(item->item.setUserId(dto.getUserId())); |
| | | boolean updated = assetInventoryTaskItemService.updateBatchById(taskItems); |
| | | |
| | | if (!updated) { |
| | | throw new ServiceException("修改盘点人失败"); |
| | | } |
| | | |
| | | log.info("成功修改盘点人,任务ID:{},资产ID:{},新盘点人ID:{},操作人:{}", |
| | | dto.getTaskId(), dto.getAssetId(), dto.getUserId(), currentUserName); |
| | | dto.getTaskId(), dto.getAssetIds(), dto.getUserId(), currentUserName); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void handleResult(List<AssetInventoryTaskItemDTO> dtoList) { |
| | | Map<Integer, Integer> taskItemMap = dtoList.stream() |
| | | .collect(Collectors.toMap(AssetInventoryTaskItemDTO::getAssetInventoryTaskItemId, AssetInventoryTaskItemDTO::getDeal)); |
| | |
| | | } |
| | | } |
| | | assetInventoryTaskItemService.updateBatchById(assetInventoryTaskItems); |
| | | |
| | | // 更新任务状态为已完成(盘点结果处理完成) |
| | | if (CollUtil.isNotEmpty(dtoList) && CollUtil.isNotEmpty(assetInventoryTaskItems)) { |
| | | // 获取任务ID(从第一个盘点项获取) |
| | | Integer taskId = assetInventoryTaskItems.get(0).getInventoryTaskId(); |
| | | updateTaskStatusToCompleted(taskId); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 直接更新任务状态为已完成 |
| | | * |
| | | * @param taskId 盘点任务ID |
| | | */ |
| | | private void updateTaskStatusToCompleted(Integer taskId) { |
| | | // 参数验证 |
| | | if (taskId == null) { |
| | | log.warn("任务ID为空,无法更新任务状态"); |
| | | return; |
| | | } |
| | | |
| | | // 检查任务是否存在 |
| | | AssetInventoryTask task = this.getById(taskId); |
| | | if (task == null) { |
| | | log.warn("任务不存在,任务ID:{}", taskId); |
| | | return; |
| | | } |
| | | |
| | | // 检查任务是否已经是已完成状态 |
| | | if (task.getStatus() != null && task.getStatus().equals(AssetInventoryTaskStatusEnum.COMPLETED.getCode())) { |
| | | log.debug("任务已经是已完成状态,任务ID:{}", taskId); |
| | | return; |
| | | } |
| | | |
| | | // 更新任务状态为已完成 |
| | | task.setStatus(AssetInventoryTaskStatusEnum.COMPLETED.getCode()); |
| | | task.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName()); |
| | | this.updateById(task); |
| | | log.info("盘点任务状态更新为已完成,任务ID:{},任务名称:{},操作人:{}", |
| | | taskId, task.getTaskName(), task.getUpdateBy()); |
| | | } |
| | | |
| | | @Override |
| | | public boolean cancel(Integer id) { |
| | | AssetInventoryTask assetInventoryTask = this.getById(id); |
| | | if (Objects.nonNull(assetInventoryTask) && !assetInventoryTask.getStatus() .equals(AssetInventoryTaskStatusEnum.COMPLETED.getCode())) { |
| | | assetInventoryTask.setStatus(AssetInventoryTaskStatusEnum.CANCELED.getCode()); //已取消 |
| | | assetInventoryTask.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName()); |
| | | return this.updateById(assetInventoryTask); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void saveInventory(List<AssetInventoryTaskItemUpdateDTO> dtoList) { |
| | | Map<Integer, AssetInventoryTaskItemUpdateDTO> dtoMap = dtoList.stream() |
| | | .collect(Collectors.toMap(AssetInventoryTaskItemUpdateDTO::getAssetInventoryTaskItemId, dto -> dto)); |
| | | List<AssetInventoryTaskItem> assetInventoryTaskItems = assetInventoryTaskItemService.listByIds(dtoMap.keySet()); |
| | | |
| | | for (AssetInventoryTaskItem assetInventoryTaskItem : assetInventoryTaskItems) { |
| | | AssetInventoryTaskItemUpdateDTO dto = dtoMap.get(assetInventoryTaskItem.getId()); |
| | | assetInventoryTaskItem.setResultStatus(dto.getResultStatus()); |
| | | assetInventoryTaskItem.setRealDeptName(dto.getRealDeptName()); |
| | | assetInventoryTaskItem.setRealUserName(dto.getRealUserName()); |
| | | assetInventoryTaskItem.setExceptionExplain(dto.getExceptionExplain()); |
| | | } |
| | | assetInventoryTaskItemService.updateBatchById(assetInventoryTaskItems); |
| | | |
| | | // 检查并更新任务状态(保存盘点数据完成) |
| | | if (CollUtil.isNotEmpty(dtoList) && CollUtil.isNotEmpty(assetInventoryTaskItems)) { |
| | | // 获取任务ID(从第一个盘点项获取) |
| | | Integer taskId = assetInventoryTaskItems.get(0).getInventoryTaskId(); |
| | | checkAndUpdateTaskCompletionStatus(taskId); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 检查并更新任务完成状态 |
| | | * 如果任务的所有盘点项都为正常状态,则将任务状态更新为已完成 |
| | | * |
| | | * @param taskId 盘点任务ID |
| | | */ |
| | | private void checkAndUpdateTaskCompletionStatus(Integer taskId) { |
| | | // 参数验证 |
| | | if (taskId == null) { |
| | | log.warn("任务ID为空,无法检查任务完成状态"); |
| | | return; |
| | | } |
| | | |
| | | // 1. 检查任务是否存在 |
| | | AssetInventoryTask task = this.getById(taskId); |
| | | if (task == null) { |
| | | log.warn("任务不存在,任务ID:{}", taskId); |
| | | return; |
| | | } |
| | | |
| | | // 2. 查询任务的所有盘点项 |
| | | LambdaQueryWrapper<AssetInventoryTaskItem> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(AssetInventoryTaskItem::getInventoryTaskId, taskId); |
| | | List<AssetInventoryTaskItem> allTaskItems = assetInventoryTaskItemService.list(wrapper); |
| | | |
| | | // 3. 检查是否所有项都已完成且为正常状态(resultStatus = 1) |
| | | boolean allNormal = !allTaskItems.isEmpty() && |
| | | allTaskItems.stream() |
| | | .allMatch(item -> item.getResultStatus() != null && item.getResultStatus() == 1); |
| | | |
| | | // 4. 如果都正常,更新任务状态为已完成 |
| | | if (allNormal) { |
| | | // 检查任务是否已经是已完成状态 |
| | | if (task.getStatus() != null && !task.getStatus().equals(AssetInventoryTaskStatusEnum.COMPLETED.getCode())) { |
| | | task.setStatus(AssetInventoryTaskStatusEnum.COMPLETED.getCode()); |
| | | task.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName()); |
| | | this.updateById(task); |
| | | log.info("盘点任务已完成,任务ID:{},任务名称:{},操作人:{}", |
| | | taskId, task.getTaskName(), task.getUpdateBy()); |
| | | } else { |
| | | log.debug("任务已经是已完成状态,任务ID:{}", taskId); |
| | | } |
| | | } else { |
| | | log.debug("任务未完全完成,跳过状态更新,任务ID:{},盘点项总数:{}", |
| | | taskId, allTaskItems.size()); |
| | | } |
| | | } |
| | | } |