| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | 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.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.domain.entity.TDept; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.system.dto.asset.AddInventoryTaskDTO; |
| | | import com.ruoyi.system.mapper.AssetInventoryTaskMapper; |
| | | import com.ruoyi.system.mapper.TDeptMapper; |
| | | import com.ruoyi.system.model.AssetInventoryTask; |
| | | import com.ruoyi.system.model.AssetInventoryTaskItem; |
| | | import com.ruoyi.system.query.AssertInventoryQuery; |
| | | import com.ruoyi.system.service.AssetInventoryTaskItemService; |
| | | import com.ruoyi.system.service.AssetInventoryTaskService; |
| | | import com.ruoyi.system.vo.asset.AssetInventoryTaskVO; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @since 2025-09-15 |
| | | */ |
| | | @Service |
| | | @RequiredArgsConstructor(onConstructor_ = {@Lazy}) |
| | | public class AssetInventoryTaskServiceImpl extends ServiceImpl<AssetInventoryTaskMapper, AssetInventoryTask> implements AssetInventoryTaskService { |
| | | |
| | | private final TDeptMapper tDeptMapper; |
| | | |
| | | private final AssetInventoryTaskItemService assetInventoryTaskItemService; |
| | | |
| | | @Override |
| | | public IPage<AssetInventoryTaskVO> getInventoryTaskPage(AssertInventoryQuery query) { |
| | | // 1. 构建Lambda查询条件 |
| | | LambdaQueryWrapper<AssetInventoryTask> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.like(StringUtils.isNotBlank(query.getTaskName()), |
| | | AssetInventoryTask::getTaskName, query.getTaskName()) |
| | | .eq(AssetInventoryTask::getDisabled, false) |
| | | .orderByDesc(AssetInventoryTask::getCreateTime); |
| | | |
| | | // 2. 构建分页对象 |
| | | Page<AssetInventoryTask> page = new Page<>(query.getPageNum(), query.getPageSize()); |
| | | |
| | | // 3. 执行分页查询 |
| | | IPage<AssetInventoryTask> taskPage = this.page(page, wrapper); |
| | | |
| | | // 4. 收集部门ID并去重 |
| | | List<Integer> deptIds = taskPage.getRecords().stream() |
| | | .map(AssetInventoryTask::getDeptId) |
| | | .filter(Objects::nonNull) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 5. 批量查询部门信息 |
| | | List<TDept> depts = deptIds.isEmpty() ? |
| | | Collections.emptyList() : |
| | | tDeptMapper.selectBatchIds(deptIds); |
| | | |
| | | // 6. 构建部门ID->名称的映射Map |
| | | Map<Integer, String> deptNameMap = depts.stream() |
| | | .collect(Collectors.toMap(TDept::getId, TDept::getDeptName)); |
| | | |
| | | // 7. 使用BeanUtil.copyToList转换VO并填充部门名称 |
| | | List<AssetInventoryTaskVO> voList = BeanUtil.copyToList(taskPage.getRecords(), AssetInventoryTaskVO.class); |
| | | voList.forEach(vo -> { |
| | | if (vo.getDeptId() != null) { |
| | | vo.setDeptName(deptNameMap.get(vo.getDeptId())); |
| | | } |
| | | }); |
| | | |
| | | // 8. 构建返回的分页结果 |
| | | IPage<AssetInventoryTaskVO> resultPage = new Page<>(taskPage.getCurrent(), taskPage.getSize(), taskPage.getTotal()); |
| | | resultPage.setRecords(voList); |
| | | |
| | | return resultPage; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean createInventoryTask(AddInventoryTaskDTO dto) { |
| | | // 1. DTO转Entity |
| | | AssetInventoryTask task = new AssetInventoryTask(); |
| | | BeanUtil.copyProperties(dto, task); |
| | | |
| | | // 2. 设置默认值 |
| | | SysUser currentUser = SecurityUtils.getLoginUser().getUser(); |
| | | task.setUserId(Math.toIntExact(currentUser.getUserId())); |
| | | task.setStatus(0); // 未开始状态 |
| | | task.setCreateTime(LocalDateTime.now()); |
| | | task.setCreateBy(currentUser.getUserName()); |
| | | task.setDisabled(false); |
| | | |
| | | // 3. 保存主任务 |
| | | boolean saved = this.save(task); |
| | | if (!saved) { |
| | | throw new RuntimeException("保存盘点任务失败"); |
| | | } |
| | | |
| | | // 4. 批量创建资产明细 |
| | | List<AssetInventoryTaskItem> items = buildTaskItems(task.getId(), dto.getAssetMainIds()); |
| | | return assetInventoryTaskItemService.saveBatch(items); |
| | | } |
| | | |
| | | /** |
| | | * 构建盘点任务资产明细列表 |
| | | * |
| | | * @param taskId 盘点任务ID |
| | | * @param assetMainIds 资产ID列表 |
| | | * @return 资产明细列表 |
| | | */ |
| | | private List<AssetInventoryTaskItem> buildTaskItems(Integer taskId, List<Integer> assetMainIds) { |
| | | SysUser currentUser = SecurityUtils.getLoginUser().getUser(); |
| | | Integer currentUserId = Math.toIntExact(currentUser.getUserId()); |
| | | |
| | | return assetMainIds.stream() |
| | | .map(assetId -> { |
| | | AssetInventoryTaskItem item = new AssetInventoryTaskItem(); |
| | | item.setInventoryTaskId(taskId); |
| | | item.setAssetMainId(assetId); |
| | | item.setUserId(currentUserId); |
| | | item.setResultStatus(0); // 未盘点状态 |
| | | return item; |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | } |