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
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
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.CoalValue;
import com.ruoyi.basic.mapper.CoalFieldMapper;
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.PendingInventoryService;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
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;
 
    @Override
    public IPage<PendingInventory> selectPendingInventoryList(Page page, PendingInventoryDto pendingInventoryDto) {
        LambdaQueryWrapper<PendingInventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(PendingInventory::getCreateTime);
        return pendingInventoryMapper.selectPage(page, queryWrapper);
    }
 
    @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);
                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());
            }
            officialInventoryMapper.delete(new LambdaQueryWrapper<OfficialInventory>().eq(OfficialInventory::getPendingId, pendingInventoryDto.getPId()));
            OfficialInventory officialInventory = new OfficialInventory();
            BeanUtils.copyProperties(pendingInventory, officialInventory);
            officialInventory.setId(null);
            officialInventory.setPendingId(pendingInventoryDto.getPId());
            officialInventory.setInventoryQuantity(quantity);
            officialInventoryMapper.insert(officialInventory);
        }
        return i;
    }
}