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.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.basic.entity.CoalField; import com.ruoyi.basic.entity.CoalValue; import com.ruoyi.basic.mapper.CoalFieldMapper; import com.ruoyi.basic.mapper.CoalValueMapper; import com.ruoyi.business.dto.OfficialInventoryDto; import com.ruoyi.business.entity.OfficialInventory; import com.ruoyi.business.mapper.OfficialInventoryMapper; import com.ruoyi.business.mapper.PendingInventoryMapper; import com.ruoyi.business.service.OfficialInventoryService; import com.ruoyi.common.utils.bean.BeanUtils; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** *

* 正式库存表 服务实现类 *

* * @author ruoyi * @since 2025-06-04 */ @Service @RequiredArgsConstructor public class OfficialInventoryServiceImpl extends ServiceImpl implements OfficialInventoryService { private final OfficialInventoryMapper officialInventoryMapper; private final CoalValueMapper coalValueMapper; private final CoalFieldMapper coalFieldMapper; private final PendingInventoryMapper pendingInventoryMapper; @Override public IPage selectOfficialInventoryList(Page page, OfficialInventoryDto officialInventoryDto) { // 先查出原始数据(OfficialInventory) LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); IPage entityPage = officialInventoryMapper.selectPage(page, queryWrapper); // 创建一个新的 Dto 分页结果 IPage dtoPage = new Page<>(); BeanUtils.copyProperties(entityPage, dtoPage); List dtoList = new ArrayList<>(); // 查询所有可用字段(CoalField) List coalFields = coalFieldMapper.selectList(null); List allFieldNames = coalFields.stream() .map(CoalField::getFields) .distinct() .collect(Collectors.toList()); // 遍历每条记录,进行转换并填充 fields for (OfficialInventory entity : entityPage.getRecords()) { OfficialInventoryDto dto = new OfficialInventoryDto(); BeanUtils.copyProperties(entity, dto); Long pendingId = entity.getPendingId(); // 查询该 pendingId 对应的 CoalValue 数据 List coalValues = coalValueMapper.selectList( new LambdaQueryWrapper().eq(CoalValue::getPlanId, pendingId) ); // 构建 Map Map fieldValueMap = coalValues.stream() .collect(Collectors.toMap( CoalValue::getFields, CoalValue::getCoalValue, (existing, replacement) -> existing // 重复字段保留第一个 )); // 构造最终 fields 列表,包含所有字段名,并设置默认值 "-" List> fields = new ArrayList<>(); for (String field : allFieldNames) { Map fieldMap = new HashMap<>(); fieldMap.put(field, fieldValueMap.getOrDefault(field, "-")); fields.add(fieldMap); } // 设置到 DTO 中 dto.setFields(fields); dtoList.add(dto); } dtoPage.setRecords(dtoList); // 设置转换后的 DtoList return dtoPage; } }