liding
10 小时以前 cb966ac5c02835eab5a99b7b93a5a9a063cf3201
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.ruoyi.business.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.CoalInfo;
import com.ruoyi.basic.entity.CoalValue;
import com.ruoyi.basic.mapper.CoalFieldMapper;
import com.ruoyi.basic.mapper.CoalInfoMapper;
import com.ruoyi.basic.mapper.CoalValueMapper;
import com.ruoyi.business.dto.PendingInventoryDto;
import com.ruoyi.business.entity.OfficialInventory;
import com.ruoyi.business.entity.PendingInventory;
import com.ruoyi.business.mapper.OfficialInventoryMapper;
import com.ruoyi.business.mapper.PendingInventoryMapper;
import com.ruoyi.business.service.InputInventoryRecordService;
import com.ruoyi.business.service.InventorySummaryService;
import com.ruoyi.business.service.PendingInventoryService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 待入库表 服务实现类
 * </p>
 *
 * @author ruoyi
 * @since 2025-06-04
 */
@Service
@RequiredArgsConstructor
public class PendingInventoryServiceImpl extends ServiceImpl<PendingInventoryMapper, PendingInventory> implements PendingInventoryService {
 
    private final PendingInventoryMapper pendingInventoryMapper;
 
    private final OfficialInventoryMapper officialInventoryMapper;
 
    private final CoalValueMapper coalValueMapper;
 
    private final CoalFieldMapper coalFieldMapper;
 
    private final CoalInfoMapper coalInfoMapper;
 
    private final SysUserMapper sysUserMapper;
 
    private final InputInventoryRecordService inputInventoryRecordService;
 
    private final InventorySummaryService inventorySummaryService;
 
    @Override
    public IPage<PendingInventoryDto> selectPendingInventoryList(Page page, PendingInventoryDto pendingInventoryDto) {
        // 1. 构建主查询
        LambdaQueryWrapper<PendingInventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(PendingInventory::getCreateTime);
 
        // 2. 执行主表分页查询
        IPage<PendingInventory> pendingInventoryPage = pendingInventoryMapper.selectPage(page, queryWrapper);
 
        // 3. 无数据快速返回
        if (CollectionUtils.isEmpty(pendingInventoryPage.getRecords())) {
            return new Page<>(page.getCurrent(), page.getSize(), pendingInventoryPage.getTotal());
        }
 
        // 4. 提取所有待处理库存ID
        List<Long> pendingIds = pendingInventoryPage.getRecords().stream()
                .map(PendingInventory::getId)
                .collect(Collectors.toList());
 
        // 5. 批量查询关联的煤炭信息和正式库存信息
        List<Long> coalIds = pendingInventoryPage.getRecords().stream()
                .map(PendingInventory::getCoalId)
                .distinct()
                .collect(Collectors.toList());
 
        // 批量查询CoalInfo
        Map<Long, CoalInfo> coalInfoMap;
        if (!coalIds.isEmpty()) {
            List<CoalInfo> coalInfos = coalInfoMapper.selectList(new LambdaQueryWrapper<CoalInfo>().in(CoalInfo::getId, coalIds));
            coalInfoMap = coalInfos.stream().collect(Collectors.toMap(CoalInfo::getId, Function.identity()));
        } else {
            coalInfoMap = new HashMap<>();
        }
 
        // 5. 批量查询登记人id
        List<Long> registrantIds = pendingInventoryPage.getRecords().stream()
                .map(PendingInventory::getRegistrantId)
                .distinct()
                .toList();
        // 批量查询登记人
        Map<Long, SysUser> sysUserMap;
        if (!registrantIds.isEmpty()) {
            List<SysUser> sysUsers = sysUserMapper.selectList(registrantIds);
            sysUserMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
        } else {
            sysUserMap = new HashMap<>();
        }
 
        // 批量查询正式库存信息
        Map<Long, Long> pendingToOfficialMap = getOfficialInventoryMap(pendingIds);
 
        // 6. 转换DTO并设置相关字段
        return pendingInventoryPage.convert(record -> {
            PendingInventoryDto dto = new PendingInventoryDto();
            BeanUtils.copyProperties(record, dto);
 
            // 设置Coal信息
            CoalInfo coalInfo = coalInfoMap.get(record.getCoalId());
            if (coalInfo != null) {
                dto.setCoal(coalInfo.getCoal());
            }
 
            // 设置登记人
            SysUser sysUser = sysUserMap.get(record.getRegistrantId());
            if (sysUser != null) {
                dto.setRegistrant(sysUser.getNickName());
            }
 
            // 从预加载的Map中获取officialId
            dto.setOfficialId(pendingToOfficialMap.getOrDefault(record.getId(), null));
 
            return dto;
        });
    }
 
 
    // 批量获取待处理库存与正式库存的映射关系
    private Map<Long, Long> getOfficialInventoryMap(List<Long> pendingIds) {
        if (CollectionUtils.isEmpty(pendingIds)) {
            return Collections.emptyMap();
        }
 
        // 查询关联的正式库存数据
        LambdaQueryWrapper<OfficialInventory> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(OfficialInventory::getId, OfficialInventory::getPendingId)
                .in(OfficialInventory::getPendingId, pendingIds);
 
        return officialInventoryMapper.selectList(wrapper)
                .stream()
                .collect(Collectors.toMap(
                        OfficialInventory::getPendingId,
                        OfficialInventory::getId,
                        (existing, replacement) -> existing // 如果有重复,保留第一个
                ));
    }
 
    @Override
    public int addOrEditPending(PendingInventoryDto pendingInventoryDto) {
        PendingInventory pendingInventory = new PendingInventory();
        BeanUtils.copyProperties(pendingInventoryDto, pendingInventory);
        if (Objects.isNull(pendingInventoryDto.getId())) {
            return pendingInventoryMapper.insert(pendingInventory);
        } else {
            return pendingInventoryMapper.updateById(pendingInventory);
        }
    }
 
    @Override
    public int delByIds(Long[] ids) {
        // 检查参数
        if (ids == null || ids.length == 0) {
            return 0;
        }
        // 构造更新条件
        UpdateWrapper<PendingInventory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids)
                .set("deleted", 1);  // 设置 deleted 为 1 表示已删除
        // 执行批量逻辑删除
        return pendingInventoryMapper.update(null, updateWrapper);
    }
 
    @Transactional
    @Override
    public int addOrEditCoalValue(PendingInventoryDto pendingInventoryDto) {
        // Step 1: 删除原有 planId 相关数据
        coalValueMapper.delete(new LambdaQueryWrapper<CoalValue>()
                .eq(CoalValue::getPlanId, pendingInventoryDto.getPId()));
        // Step 2: 构建基础 CoalValue 对象
        CoalValue coalValue = new CoalValue();
        BeanUtils.copyProperties(coalValue, pendingInventoryDto);
        coalValue.setPlanId(pendingInventoryDto.getPId());
        List<Map<String, String>> fieldValue = pendingInventoryDto.getFieldValue();
        if (CollectionUtils.isEmpty(fieldValue)) {
            throw new BaseException("字段值不能为空");
        }
        // Step 3: 提前获取所有 field -> CoalField 映射,避免重复查库
        Set<String> allFields = fieldValue.stream()
                .flatMap(map -> map.keySet().stream())
                .collect(Collectors.toSet());
        List<CoalField> coalFields = coalFieldMapper.selectList(
                new LambdaQueryWrapper<CoalField>().in(CoalField::getFields, allFields));
        Map<String, String> fieldMap = coalFields.stream()
                .collect(Collectors.toMap(
                        CoalField::getFields,
                        CoalField::getFieldName));
        // Step 4: 批量插入
        int i = 0;
        for (Map<String, String> fieldMapEntry : fieldValue) {
            for (Map.Entry<String, String> entry : fieldMapEntry.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                String fieldName = fieldMap.get(key);
                if (fieldName == null) {
                    throw new BaseException("字段名不存在");
                }
                coalValue.setId(null); // 清空 id,确保每次都是新记录
                coalValue.setCoalValue(value);
                coalValue.setFields(key);
                coalValue.setFieldName(fieldName);
                coalValue.setType(String.valueOf(1));
                i = coalValueMapper.insert(coalValue);
            }
        }
        if (i > 0) {
            BigDecimal quantity = pendingInventoryDto.getInventoryQuantity();
            PendingInventory pendingInventory = pendingInventoryMapper.selectById(pendingInventoryDto.getPId());
            if (pendingInventory == null) {
                throw new BaseException("待入库记录不存在");
            }
            BigDecimal left = pendingInventory.getInventoryQuantity().subtract(quantity);
            if (left.compareTo(BigDecimal.ZERO) > 0) {
                pendingInventory.setInventoryQuantity(left);
                pendingInventoryMapper.updateById(pendingInventory);
            } else {
                pendingInventoryMapper.deleteById(pendingInventoryDto.getPId());
            }
            //正式库
            if (pendingInventoryDto.getOfficialId() == null) {
                OfficialInventory officialInventory = new OfficialInventory();
                BeanUtils.copyProperties(pendingInventory, officialInventory);
                officialInventory.setId(null);
                officialInventory.setPendingId(pendingInventoryDto.getPId());
                officialInventory.setInventoryQuantity(quantity);
                officialInventoryMapper.insert(officialInventory);
            } else {
                OfficialInventory officialInventory = officialInventoryMapper.selectById(pendingInventoryDto.getOfficialId());
                officialInventory.setInventoryQuantity(quantity.add(officialInventory.getInventoryQuantity()));
                officialInventoryMapper.updateById(officialInventory);
            }
        }
        return i;
    }
}