自动化立体仓库 - WMS系统
chen.llin
2 天以前 2816415f539ef54839e331657edae7055c243ad8
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package com.zy.asrs.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.core.common.Cools;
import com.core.common.R;
import com.core.common.SnowflakeIdWorker;
import com.core.exception.CoolException;
import com.zy.asrs.entity.BasAreas;
import com.zy.asrs.entity.LocCache;
import com.zy.asrs.entity.param.LocMastInitParam;
import com.zy.asrs.mapper.LocCacheMapper;
import com.zy.asrs.service.BasAreasService;
import com.zy.asrs.service.LocCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
@Slf4j
@Service("locCacheService")
public class LocCacheServiceImpl extends ServiceImpl<LocCacheMapper, LocCache> implements LocCacheService {
 
 
    @Autowired
    private BasAreasService basAreasService;
    
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
 
    /**
     * @author Ryan
     * @date 2025/9/18
     * @description: 初始化库区库位信息
     * @version 1.0
     */
    @Override
    public R initLocCache(LocMastInitParam param, Long userId) {
        try {
            List<LocCache> list = new ArrayList<>();
            BasAreas areas = basAreasService.selectById(param.getIdentifying());
            if (Cools.isEmpty(areas)) {
                return R.error("库区不存在!!!");
            }
            for (int r = param.getStartRow(); r <= param.getEndRow(); r++) {
                for (int b = param.getStartBay(); b <= param.getEndBay(); b++) {
                    for (int l = param.getStartLev(); l <= param.getEndLev(); l++) {
                        // 获取库位号
                        String locNo = areas.getAreaNo() + String.format("%02d", r) + String.format("%03d", b) + String.format("%02d", l);
                        Date now = new Date();
                        LocCache locMast = new LocCache();
                        // 手动生成ID
                        locMast.setId(snowflakeIdWorker.nextId());
                        locMast.setLocNo(locNo);
                        locMast.setLocSts("O");
                        locMast.setRow1(r); // 排
                        locMast.setBay1(b); // 列
                        locMast.setLev1(l); // 层
                        locMast.setLocType1(!Cools.isEmpty(param.getLocType1()) ? param.getLocType1() : 1);
                        locMast.setLocType2(param.getLocType2());
                        locMast.setLocType3(param.getLocType3());
                        locMast.setAppeUser(userId);
                        locMast.setAppeTime(now);
                        locMast.setModiUser(userId);
                        locMast.setModiTime(now);
                        locMast.setAreaId(areas.getId());
                        locMast.setAreaName(areas.getName());
                        list.add(locMast);
                    }
                }
            }
            if (!Cools.isEmpty(param.getEnable()) && param.getEnable() == 1) {
                if (!this.delete(new EntityWrapper<>())) {
                    throw new CoolException("删除失败!!");
                }
            }
 
            // SQL Server 批量插入获取生成键可能失败,改用循环单条插入确保所有记录都能成功插入
            int successCount = 0;
            for (LocCache locCache : list) {
                if (this.insert(locCache)) {
                    successCount++;
                } else {
                    log.error("插入库位失败: {}", locCache.getLocNo());
                }
            }
            
            if (successCount != list.size()) {
                throw new CoolException(String.format("添加失败!成功插入 %d 条,应插入 %d 条", successCount, list.size()));
            }
            
            return R.ok("初始化成功,共插入 " + successCount + " 条记录");
        } catch (Exception e) {
            log.error("初始化库位失败", e);
            return R.error("初始化失败===>" + e.getMessage());
        }
    }
 
    /**
     * 锁定/解锁缓存库位(复用AGV搬货后的库位有货逻辑)
     * 锁定:满托设置为 "F"(在库),空托设置为 "D"(空桶/空栈板)
     * 解锁:统一设置为 "O"(空库位)
     * @param locNo 库位号
     * @param lock 是否锁定,true-锁定,false-解锁
     * @param fullPlt 是否满托,true-满托(设置为F),false-空托(设置为D),解锁时忽略此参数
     * @param userId 用户ID
     * @return 操作结果
     */
    @Override
    public R lockOrUnlockLocCache(String locNo, Boolean lock, Boolean fullPlt, Long userId) {
        try {
            if (Cools.isEmpty(locNo)) {
                return R.error("库位号不能为空");
            }
 
            // 根据库位号查询库位
            EntityWrapper<LocCache> wrapper = new EntityWrapper<>();
            wrapper.eq("loc_no", locNo);
            LocCache locCache = this.selectOne(wrapper);
 
            if (Cools.isEmpty(locCache)) {
                return R.error("库位不存在:" + locNo);
            }
 
            // 更新库位状态
            String locSts;
            if (lock) {
                // 锁定:满托设置为"F"(在库),空托设置为"D"(空桶/空栈板)
                if (fullPlt == null) {
                    return R.error("锁定操作时,满托参数不能为空");
                }
                locSts = fullPlt ? "F" : "D";
                locCache.setFullPlt(fullPlt ? "Y" : "N");
            } else {
                // 解锁:统一设置为"O"(空库位)
                locSts = "O";
                locCache.setFullPlt("N");
            }
            
            locCache.setLocSts(locSts);
            locCache.setModiUser(userId);
            locCache.setModiTime(new Date());
 
            boolean success = this.updateById(locCache);
            if (success) {
                String action = lock ? "锁定" : "解锁";
                String detail = lock ? (fullPlt ? "满托" : "空托") : "";
                log.info("{}库位成功:{},库位状态:{},{},操作人:{}", action, locNo, locCache.getLocSts(), detail, userId);
                return R.ok(action + "成功");
            } else {
                return R.error("操作失败");
            }
        } catch (Exception e) {
            log.error("锁定/解锁库位失败", e);
            return R.error("操作失败:" + e.getMessage());
        }
    }
 
    /**
     * 锁定/解锁当前排的所有库位
     * @param locNo 库位号(用于获取排号)
     * @param lock 是否锁定,true-锁定,false-解锁
     * @param userId 用户ID
     * @return 操作结果
     */
    @Override
    public R lockOrUnlockRowLocCache(String locNo, Boolean lock, Boolean fullPlt, Long userId) {
        try {
            if (Cools.isEmpty(locNo)) {
                return R.error("库位号不能为空");
            }
 
            // 根据库位号查询库位,获取排号
            EntityWrapper<LocCache> wrapper = new EntityWrapper<>();
            wrapper.eq("loc_no", locNo);
            LocCache locCache = this.selectOne(wrapper);
 
            if (Cools.isEmpty(locCache)) {
                return R.error("库位不存在:" + locNo);
            }
 
            Integer row = locCache.getRow1();
            if (Cools.isEmpty(row)) {
                return R.error("库位排号为空:" + locNo);
            }
 
            // 查询当前排的1、2、3列的所有库位(解锁整排时只清空1、2、3列)
            EntityWrapper<LocCache> rowWrapper = new EntityWrapper<>();
            rowWrapper.eq("row1", row)
                    .in("bay1", Arrays.asList(1, 2, 3)); // 只查询1、2、3列
            // 如果库位有库区ID,也加上库区条件
            if (!Cools.isEmpty(locCache.getAreaId())) {
                rowWrapper.eq("area_id", locCache.getAreaId());
            }
            List<LocCache> locCacheList = this.selectList(rowWrapper);
 
            if (Cools.isEmpty(locCacheList)) {
                return R.error("当前排没有找到库位");
            }
 
            // 批量更新库位状态
            Date now = new Date();
            int successCount = 0;
            String locSts;
            if (lock) {
                // 锁定:满托设置为"F"(在库),空托设置为"D"(空桶/空栈板)
                if (fullPlt == null) {
                    return R.error("锁定操作时,满托参数不能为空");
                }
                locSts = fullPlt ? "F" : "D";
            } else {
                // 解锁:统一设置为"O"(空库位)
                locSts = "O";
            }
            
            for (LocCache cache : locCacheList) {
                cache.setLocSts(locSts);
                if (lock) {
                    cache.setFullPlt(fullPlt ? "Y" : "N");
                } else {
                    cache.setFullPlt("N");
                }
                cache.setModiUser(userId);
                cache.setModiTime(now);
                if (this.updateById(cache)) {
                    successCount++;
                }
            }
 
            String action = lock ? "锁定" : "解锁";
            log.info("{}排{}所有库位成功,共{}个库位,操作人:{}", action, row, successCount, userId);
            return R.ok(String.format("%s成功,共处理 %d 个库位", action, successCount));
        } catch (Exception e) {
            log.error("锁定/解锁排库位失败", e);
            return R.error("操作失败:" + e.getMessage());
        }
    }
 
    /**
     * 锁定/解锁当前列的所有库位
     * @param locNo 库位号(用于获取列号bay1)
     * @param lock 是否锁定,true-锁定,false-解锁
     * @param fullPlt 是否满托,true-满托(设置为F),false-空托(设置为D),解锁时忽略此参数
     * @param userId 用户ID
     * @return 操作结果
     */
    @Override
    public R lockOrUnlockBayLocCache(String locNo, Boolean lock, Boolean fullPlt, Long userId) {
        try {
            if (Cools.isEmpty(locNo)) {
                return R.error("库位号不能为空");
            }
 
            // 根据库位号查询库位,获取列号
            EntityWrapper<LocCache> wrapper = new EntityWrapper<>();
            wrapper.eq("loc_no", locNo);
            LocCache locCache = this.selectOne(wrapper);
 
            if (Cools.isEmpty(locCache)) {
                return R.error("库位不存在:" + locNo);
            }
 
            Integer bay = locCache.getBay1();
            if (Cools.isEmpty(bay)) {
                return R.error("库位列号为空:" + locNo);
            }
 
            // 查询当前列的所有库位
            EntityWrapper<LocCache> bayWrapper = new EntityWrapper<>();
            bayWrapper.eq("bay1", bay);
            // 如果库位有库区ID,也加上库区条件
            if (!Cools.isEmpty(locCache.getAreaId())) {
                bayWrapper.eq("area_id", locCache.getAreaId());
            }
            List<LocCache> locCacheList = this.selectList(bayWrapper);
 
            if (Cools.isEmpty(locCacheList)) {
                return R.error("当前列没有找到库位");
            }
 
            // 批量更新库位状态
            Date now = new Date();
            int successCount = 0;
            String locSts;
            if (lock) {
                // 锁定:满托设置为"F"(在库),空托设置为"D"(空桶/空栈板)
                if (fullPlt == null) {
                    return R.error("锁定操作时,满托参数不能为空");
                }
                locSts = fullPlt ? "F" : "D";
            } else {
                // 解锁:统一设置为"O"(空库位)
                locSts = "O";
            }
            
            for (LocCache cache : locCacheList) {
                cache.setLocSts(locSts);
                if (lock) {
                    cache.setFullPlt(fullPlt ? "Y" : "N");
                } else {
                    cache.setFullPlt("N");
                }
                cache.setModiUser(userId);
                cache.setModiTime(now);
                if (this.updateById(cache)) {
                    successCount++;
                }
            }
 
            String action = lock ? "锁定" : "解锁";
            log.info("{}列{}所有库位成功,共{}个库位,操作人:{}", action, bay, successCount, userId);
            return R.ok(String.format("%s成功,共处理 %d 个库位", action, successCount));
        } catch (Exception e) {
            log.error("锁定/解锁列库位失败", e);
            return R.error("操作失败:" + e.getMessage());
        }
    }
 
    /**
     * 清空整排的所有库位(所有列)
     * @param locNo 库位号(用于获取排号row1)
     * @param lock 是否锁定,true-锁定,false-解锁(清空)
     * @param fullPlt 是否满托,true-满托(设置为F),false-空托(设置为D),解锁时忽略此参数
     * @param userId 用户ID
     * @return 操作结果
     */
    @Override
    public R clearAllColumnsInRow(String locNo, Boolean lock, Boolean fullPlt, Long userId) {
        try {
            if (Cools.isEmpty(locNo)) {
                return R.error("库位号不能为空");
            }
 
            // 根据库位号查询库位,获取排号
            EntityWrapper<LocCache> wrapper = new EntityWrapper<>();
            wrapper.eq("loc_no", locNo);
            LocCache locCache = this.selectOne(wrapper);
 
            if (Cools.isEmpty(locCache)) {
                return R.error("库位不存在:" + locNo);
            }
 
            Integer row = locCache.getRow1();
            if (Cools.isEmpty(row)) {
                return R.error("库位排号为空:" + locNo);
            }
 
            // 查询当前排的所有库位(所有列,不限制bay1)
            EntityWrapper<LocCache> rowWrapper = new EntityWrapper<>();
            rowWrapper.eq("row1", row);
            // 如果库位有库区ID,也加上库区条件
            if (!Cools.isEmpty(locCache.getAreaId())) {
                rowWrapper.eq("area_id", locCache.getAreaId());
            }
            List<LocCache> locCacheList = this.selectList(rowWrapper);
 
            if (Cools.isEmpty(locCacheList)) {
                return R.error("当前排没有找到库位");
            }
 
            // 批量更新库位状态
            Date now = new Date();
            int successCount = 0;
            String locSts;
            if (lock) {
                // 锁定:满托设置为"F"(在库),空托设置为"D"(空桶/空栈板)
                if (fullPlt == null) {
                    return R.error("锁定操作时,满托参数不能为空");
                }
                locSts = fullPlt ? "F" : "D";
            } else {
                // 解锁:统一设置为"O"(空库位)
                locSts = "O";
            }
            
            for (LocCache cache : locCacheList) {
                cache.setLocSts(locSts);
                if (lock) {
                    cache.setFullPlt(fullPlt ? "Y" : "N");
                } else {
                    cache.setFullPlt("N");
                }
                cache.setModiUser(userId);
                cache.setModiTime(now);
                if (this.updateById(cache)) {
                    successCount++;
                }
            }
 
            String action = lock ? "锁定" : "清空";
            log.info("{}排{}所有库位(所有列)成功,共{}个库位,操作人:{}", action, row, successCount, userId);
            return R.ok(String.format("%s成功,共处理 %d 个库位", action, successCount));
        } catch (Exception e) {
            log.error("清空整排库位失败", e);
            return R.error("操作失败:" + e.getMessage());
        }
    }
}