yaowanxin
10 天以前 3df6baa96b6761673f32b8e4fc5b8d6318ea0477
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
package com.ruoyi.warehouse.service.impl;
 
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
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.warehouse.mapper.WarehouseGoodsShelvesMapper;
import com.ruoyi.warehouse.mapper.WarehouseGoodsShelvesRowcolMapper;
import com.ruoyi.warehouse.pojo.WarehouseGoodsShelvesRowcol;
import com.ruoyi.warehouse.service.WarehouseGoodsShelvesRowcolService;
import com.ruoyi.warehouse.service.WarehouseGoodsShelvesService;
import lombok.extern.slf4j.Slf4j;
import com.ruoyi.warehouse.pojo.WarehouseGoodsShelves;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
/**
* @author 86151
* @description 针对表【warehouse_goods_shelves(仓库货架表)】的数据库操作Service实现
* @createDate 2025-08-13 11:49:02
*/
@Service
@Slf4j
public class WarehouseGoodsShelvesServiceImpl extends ServiceImpl<WarehouseGoodsShelvesMapper, WarehouseGoodsShelves>
    implements WarehouseGoodsShelvesService {
    @Autowired
    private WarehouseGoodsShelvesRowcolService warehouseGoodsShelvesRowcolService;
    @Autowired
    private WarehouseGoodsShelvesRowcolMapper warehouseGoodsShelvesRowcolMapper;
    @Autowired
    private WarehouseGoodsShelvesMapper warehouseGoodsShelvesMapper;
    @Override
    public IPage<WarehouseGoodsShelves> listPage(Page page, WarehouseGoodsShelves warehouseGoodsShelves) {
        return warehouseGoodsShelvesMapper.listPage(page,warehouseGoodsShelves);
    }
 
    @Override
    public boolean add(WarehouseGoodsShelves warehouseGoodsShelves) {
        int insert = warehouseGoodsShelvesMapper.insert(warehouseGoodsShelves);
        if (insert <= 0) {
            log.error("货架主记录添加失败");
            return false;
        }
        Long shelvesId = warehouseGoodsShelves.getId();
        Long warehouseId = warehouseGoodsShelves.getWarehouseId();
        Long createUser = warehouseGoodsShelves.getCreateUser();
        Long updateUser = warehouseGoodsShelves.getUpdateUser();
        // 3. 批量创建行列记录
        List<WarehouseGoodsShelvesRowcol> rowcolList = new ArrayList<>();
        for (long i = 1; i <= warehouseGoodsShelves.getStorey(); i++) {
            for (long j = 1; j <= warehouseGoodsShelves.getArrange(); j++) {
                WarehouseGoodsShelvesRowcol rowcol = new WarehouseGoodsShelvesRowcol();
                rowcol.setStorey(i);
                rowcol.setArrange(j);
                rowcol.setWarehouseGoodsShelvesId(shelvesId);
                rowcol.setWarehouseId(warehouseId);
                rowcol.setCreateUser(createUser);
                rowcol.setUpdateUser(updateUser);
                rowcolList.add(rowcol);
            }
        }
        // 4. 批量插入行列记录
        if (!rowcolList.isEmpty()) {
            try {
                // 使用批量插入方法替代循环单条插入
                boolean saveBatch = warehouseGoodsShelvesRowcolService.saveBatch(rowcolList);
 
                if (!saveBatch) {
                    log.warn("货架[{}]的行列记录部分添加失败", shelvesId);
                }
            } catch (Exception e) {
                log.error("货架[{}]的行列记录批量添加失败", shelvesId, e);
                // 抛出异常触发事务回滚
                throw e;
            }
        }
 
        log.info("货架[{}]添加成功,层数:{},排数:{}",
                shelvesId,
                warehouseGoodsShelves.getStorey(),
                warehouseGoodsShelves.getArrange());
        return true;
    }
    /**
     * 根据ID更新货架及其行列信息
     * @param warehouseGoodsShelves 货架信息
     * @return 是否更新成功
     */
    @Override
    public boolean updateRowcolById(WarehouseGoodsShelves warehouseGoodsShelves) {
        // 提取关键ID,减少重复调用
        Long shelvesId = warehouseGoodsShelves.getId();
        Long warehouseId = warehouseGoodsShelves.getWarehouseId();
 
        // 先更新货架主信息
        boolean isShelvesUpdated = updateById(warehouseGoodsShelves);
        if (!isShelvesUpdated) {
            log.warn("货架[{}]主信息更新失败", shelvesId);
            return false;
        }
 
        // 构建查询条件:查询该货架下的所有行列记录
        WarehouseGoodsShelvesRowcol queryRowcol = new WarehouseGoodsShelvesRowcol();
        queryRowcol.setWarehouseGoodsShelvesId(shelvesId);
        queryRowcol.setWarehouseId(warehouseId);
 
        IPage<WarehouseGoodsShelvesRowcol> rowcolPage = warehouseGoodsShelvesRowcolService.listPage(new Page<>(), queryRowcol);
        List<WarehouseGoodsShelvesRowcol> oldRowcolList = rowcolPage.getRecords();
 
        // 如果没有旧的行列记录,直接添加新记录
        if (oldRowcolList.isEmpty()) {
            add(warehouseGoodsShelves);
            return true;
        }
 
        // 检查货架上是否有商品(有商品则不允许更新)
        boolean hasGoodsOnShelves = oldRowcolList.stream()
                .anyMatch(rowcol -> rowcol.getDocumentationId() != null);
        if (hasGoodsOnShelves) {
            log.error("货架[{}]上存在商品,不允许更新行列信息", shelvesId);
            return false;
        }
 
        // 提取旧行列记录的ID集合,用于删除操作
        List<Long> oldRowcolIds = oldRowcolList.stream()
                .map(WarehouseGoodsShelvesRowcol::getId)
                .collect(Collectors.toList());
 
        // 删除旧的行列记录
        boolean isOldRowcolRemoved = warehouseGoodsShelvesRowcolService.removeByIds(oldRowcolIds);
        if (!isOldRowcolRemoved) {
            log.error("货架[{}]的旧行列记录删除失败", shelvesId);
            return false;
        }
 
        // 添加新的行列记录
        add(warehouseGoodsShelves);
        log.info("货架[{}]的行列信息更新成功", shelvesId);
        return true;
    }
    /**
     * 批量删除货架及其关联的行列记录
     * @param ids 货架ID列表
     * @return 是否删除成功
     */
    @Override
    public boolean deleteByIds(List<Long> ids) {
        // 1. 先查询所有要删除的货架信息
        List<WarehouseGoodsShelves> shelvesList = warehouseGoodsShelvesMapper.selectBatchIds(ids);
        if (CollectionUtils.isEmpty(shelvesList)) {
            log.info("未查询到需删除的货架,ID列表:{}", ids);
            return true; // 无数据可删,视为成功
        }
 
        // 2. 检查所有货架是否存在商品(有商品则不允许删除)
        for (WarehouseGoodsShelves shelves : shelvesList) {
            Long shelvesId = shelves.getId();
            Long warehouseId = shelves.getWarehouseId();
 
            // 构建查询条件:查询当前货架的所有行列记录
            Wrapper<WarehouseGoodsShelvesRowcol> queryWrapper = new LambdaQueryWrapper<WarehouseGoodsShelvesRowcol>()
                    .eq(WarehouseGoodsShelvesRowcol::getWarehouseGoodsShelvesId, shelvesId)
                    .eq(WarehouseGoodsShelvesRowcol::getWarehouseId, warehouseId);
 
            // 查询行列记录(仅需判断是否有商品,无需分页)
            List<WarehouseGoodsShelvesRowcol> rowcolList = warehouseGoodsShelvesRowcolService.list(queryWrapper);
 
            // 检查是否有商品
            boolean hasGoods = rowcolList.stream()
                    .anyMatch(rowcol -> rowcol.getDocumentationId() != null && rowcol.getDocumentationId()!=0);
 
            if (hasGoods) {
                log.error("货架[ID:{}]上存在商品,禁止删除", shelvesId);
                return false; // 任一货架有商品则终止删除
            }
        }
 
        // 3. 先删除货架主记录
        boolean isShelvesDeleted = removeByIds(ids);
        if (!isShelvesDeleted) {
            log.error("货架主记录删除失败,ID列表:{}", ids);
            return false;
        }
 
        // 4. 批量删除所有关联的行列记录
        List<Long> allShelvesIds = shelvesList.stream()
                .map(WarehouseGoodsShelves::getId)
                .collect(Collectors.toList());
 
        // 构建批量删除条件
        Wrapper<WarehouseGoodsShelvesRowcol> deleteWrapper = new LambdaQueryWrapper<WarehouseGoodsShelvesRowcol>()
                .in(WarehouseGoodsShelvesRowcol::getWarehouseGoodsShelvesId, allShelvesIds);
 
        boolean isRowcolDeleted = warehouseGoodsShelvesRowcolService.remove(deleteWrapper);
        if (!isRowcolDeleted) {
            log.warn("货架关联的行列记录删除失败,货架ID列表:{}", allShelvesIds);
            // 此处可根据业务需求决定是否回滚货架删除操作
        }
 
        log.info("货架批量删除成功,ID列表:{}", ids);
        return true;
    }
 
}