liding
2 天以前 c0efb2e8358f4e7ee0774c340afd453c3d0c2471
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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;
 
/**
 * <p>
 * 正式库存表 服务实现类
 * </p>
 *
 * @author ruoyi
 * @since 2025-06-04
 */
@Service
@RequiredArgsConstructor
public class OfficialInventoryServiceImpl extends ServiceImpl<OfficialInventoryMapper, OfficialInventory> implements OfficialInventoryService {
 
    private final OfficialInventoryMapper officialInventoryMapper;
 
    private final CoalValueMapper coalValueMapper;
 
    private final CoalFieldMapper coalFieldMapper;
 
    private final PendingInventoryMapper pendingInventoryMapper;
 
    @Override
    public IPage<OfficialInventoryDto> selectOfficialInventoryList(Page page, OfficialInventoryDto officialInventoryDto) {
 
        //  先查出原始数据(OfficialInventory)
        LambdaQueryWrapper<OfficialInventory> queryWrapper = new LambdaQueryWrapper<>();
        IPage<OfficialInventory> entityPage = officialInventoryMapper.selectPage(page, queryWrapper);
 
        //  创建一个新的 Dto 分页结果
        IPage<OfficialInventoryDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(entityPage, dtoPage);
 
        List<OfficialInventoryDto> dtoList = new ArrayList<>();
 
        //  查询所有可用字段(CoalField)
        List<CoalField> coalFields = coalFieldMapper.selectList(null);
        List<String> 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<CoalValue> coalValues = coalValueMapper.selectList(
                    new LambdaQueryWrapper<CoalValue>().eq(CoalValue::getPlanId, pendingId)
            );
 
            //  构建 Map<fieldName, value>
            Map<String, String> fieldValueMap = coalValues.stream()
                    .collect(Collectors.toMap(
                            CoalValue::getFields,
                            CoalValue::getCoalValue,
                            (existing, replacement) -> existing // 重复字段保留第一个
                    ));
 
            // 构造最终 fields 列表,包含所有字段名,并设置默认值 "-"
            List<Map<String, String>> fields = new ArrayList<>();
            for (String field : allFieldNames) {
                Map<String, String> 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;
    }
}