| | |
| | | package com.ruoyi.business.service.impl; |
| | | |
| | | 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.business.dto.EquipmentManagementDto; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.business.dto.EquipmentUsageDetailDto; |
| | | import com.ruoyi.business.entity.EquipmentManagement; |
| | | import com.ruoyi.business.entity.EquipmentUsageDetail; |
| | |
| | | import com.ruoyi.business.mapper.EquipmentManagementMapper; |
| | | import com.ruoyi.business.mapper.EquipmentUsageDetailMapper; |
| | | import com.ruoyi.business.mapper.EquipmentUsageRecordMapper; |
| | | import com.ruoyi.business.service.EquipmentManagementService; |
| | | import com.ruoyi.business.service.EquipmentUsageDetailService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.business.service.EquipmentUsageRecordService; |
| | | import com.ruoyi.common.exception.base.BaseException; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | |
| | | |
| | | |
| | | @Override |
| | | public IPage<EquipmentUsageDetailDto> selectEquipmentUsageDetailList(Page<EquipmentUsageDetail> page,EquipmentUsageDetailDto equipmentUsageDetailDto) { |
| | | public List<EquipmentUsageDetailDto> selectEquipmentUsageDetailList(EquipmentUsageDetailDto equipmentUsageDetailDto) { |
| | | // 必须传递usageId参数 |
| | | if (equipmentUsageDetailDto.getUsageId() == null) { |
| | | throw new BaseException("请选择使用记录"); |
| | | } |
| | | |
| | | // 查询明细列表(不分页) |
| | | LambdaQueryWrapper<EquipmentUsageDetail> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(EquipmentUsageDetail::getUsageId, equipmentUsageDetailDto.getUsageId()); |
| | | List<EquipmentUsageDetail> details = equipmentUsageDetailMapper.selectList(queryWrapper); |
| | | |
| | | Page<EquipmentUsageDetail> entityPage = equipmentUsageDetailMapper.selectPage(page, queryWrapper); |
| | | IPage<EquipmentUsageDetailDto> dtoPage = new Page<>(); |
| | | BeanUtils.copyProperties(entityPage, dtoPage); |
| | | // 先获取主表记录 |
| | | List<Long> usageIds = entityPage.getRecords().stream() |
| | | // 如果没有数据,直接返回空列表 |
| | | if (details.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | // 获取所有关联的 usageId |
| | | List<Long> usageIds = details.stream() |
| | | .map(EquipmentUsageDetail::getUsageId) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | |
| | | if (!usageIds.isEmpty()) { |
| | | // 查询关联的使用记录 |
| | | List<EquipmentUsageRecord> usageRecords = equipmentUsageRecordMapper.selectList( |
| | | new LambdaQueryWrapper<EquipmentUsageRecord>() |
| | |
| | | equipmentMap = new HashMap<>(); |
| | | } |
| | | |
| | | // 构建使用记录ID到设备ID的映射 |
| | | // 构建 usageId → equipmentId 的映射 |
| | | Map<Long, Long> usageIdToEquipmentIdMap = usageRecords.stream() |
| | | .collect(Collectors.toMap(EquipmentUsageRecord::getId, EquipmentUsageRecord::getEquipmentId)); |
| | | |
| | | // 转换DTO并填充设备信息 |
| | | List<EquipmentUsageDetailDto> dtoList = entityPage.getRecords().stream().map(detail -> { |
| | | // 转换为 DTO 并填充设备信息 |
| | | return details.stream().map(detail -> { |
| | | EquipmentUsageDetailDto detailDto = new EquipmentUsageDetailDto(); |
| | | BeanUtils.copyProperties(detail, detailDto); |
| | | |
| | | // 获取关联的设备ID |
| | | // 获取关联的设备信息 |
| | | Long equipmentId = usageIdToEquipmentIdMap.get(detail.getUsageId()); |
| | | if (equipmentId != null && equipmentMap.containsKey(equipmentId)) { |
| | | EquipmentManagement equipment = equipmentMap.get(equipmentId); |
| | |
| | | |
| | | return detailDto; |
| | | }).collect(Collectors.toList()); |
| | | |
| | | dtoPage.setRecords(dtoList); |
| | | } else { |
| | | dtoPage.setRecords(Collections.emptyList()); |
| | | } |
| | | return dtoPage; |
| | | } |
| | | } |