自动化立体仓库 - WMS系统
zwl
3 天以前 2acfc2d2a0e956910c51bd996f443b3cb9bd3dc9
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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.exception.CoolException;
import com.zy.asrs.entity.BasCrnDepthRule;
import com.zy.asrs.entity.RowLastno;
import com.zy.asrs.entity.param.BasCrnDepthRuleTemplateParam;
import com.zy.asrs.mapper.BasCrnDepthRuleMapper;
import com.zy.asrs.service.BasCrnDepthRuleService;
import com.zy.asrs.service.RowLastnoService;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.CrnDepthRuleProfile;
import com.zy.common.properties.SlaveProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
 
@Service("basCrnDepthRuleService")
public class BasCrnDepthRuleServiceImpl extends ServiceImpl<BasCrnDepthRuleMapper, BasCrnDepthRule> implements BasCrnDepthRuleService {
 
    private static final String PROFILE_SOURCE_CONFIG = "CONFIG";
    private static final String PROFILE_SOURCE_LEGACY = "LEGACY";
 
    @Autowired
    private RowLastnoService rowLastnoService;
    @Autowired
    private SlaveProperties slaveProperties;
 
    /**
     * 校验并标准化单条堆垛机深浅规则。
     */
    @Override
    public void validateRule(BasCrnDepthRule rule) {
        if (rule == null || rule.getWhsType() == null || rule.getCrnNo() == null) {
            throw new CoolException("仓库和堆垛机不能为空");
        }
        if (!Integer.valueOf(1).equals(rule.getLayoutType()) && !Integer.valueOf(2).equals(rule.getLayoutType())) {
            throw new CoolException("布局类型只允许 1=单伸 或 2=双伸");
        }
        List<Integer> searchRows = parseRows(rule.getSearchRowsCsv());
        List<Integer> shallowRows = parseRows(rule.getShallowRowsCsv());
        List<Integer> deepRows = parseRows(rule.getDeepRowsCsv());
        if (searchRows.isEmpty()) {
            throw new CoolException("搜索排顺序不能为空");
        }
        if (Integer.valueOf(1).equals(rule.getLayoutType())) {
            if (shallowRows.isEmpty()) {
                shallowRows = new ArrayList<Integer>(searchRows);
            }
            if (!deepRows.isEmpty()) {
                throw new CoolException("单伸规则不允许填写深库位排");
            }
        } else {
            if (shallowRows.isEmpty()) {
                throw new CoolException("双伸规则必须填写浅库位排");
            }
            if (deepRows.isEmpty()) {
                throw new CoolException("双伸规则必须填写深库位排");
            }
        }
        rule.setSearchRowsCsv(joinRows(searchRows));
        rule.setShallowRowsCsv(joinRows(shallowRows));
        rule.setDeepRowsCsv(joinRows(deepRows));
        if (rule.getEnabled() == null) {
            rule.setEnabled(1);
        }
    }
 
    /**
     * 查询某仓库某堆垛机当前启用的深浅规则。
     */
    @Override
    public BasCrnDepthRule findEnabledRule(Integer whsType, Integer crnNo) {
        if (whsType == null || crnNo == null) {
            return null;
        }
        return this.selectOne(new EntityWrapper<BasCrnDepthRule>()
                .eq("whs_type", whsType)
                .eq("crn_no", crnNo)
                .eq("enabled", 1));
    }
 
    /**
     * 解析运行时使用的深浅排画像,优先走配置,缺失时回退旧逻辑。
     */
    @Override
    public CrnDepthRuleProfile resolveProfile(RowLastno rowLastno, Integer crnNo, Integer preferredNearRow) {
        if (rowLastno == null || crnNo == null) {
            return new CrnDepthRuleProfile();
        }
        BasCrnDepthRule rule = findEnabledRule(rowLastno.getWhsType(), crnNo);
        if (!Cools.isEmpty(rule)) {
            return buildProfileFromRule(rule);
        }
        return buildLegacyProfile(rowLastno, crnNo, preferredNearRow);
    }
 
    /**
     * 按旧逻辑预览一批堆垛机的默认深浅规则模板。
     */
    @Override
    public List<BasCrnDepthRule> previewTemplate(BasCrnDepthRuleTemplateParam param) {
        if (param == null || param.getWhsType() == null) {
            throw new CoolException("仓库不能为空");
        }
        RowLastno rowLastno = rowLastnoService.selectById(param.getWhsType());
        if (Cools.isEmpty(rowLastno)) {
            throw new CoolException("未找到仓库对应的轮询规则");
        }
        int startCrnNo = param.getStartCrnNo() == null ? defaultStartCrnNo(rowLastno) : param.getStartCrnNo();
        int endCrnNo = param.getEndCrnNo() == null ? defaultEndCrnNo(rowLastno) : param.getEndCrnNo();
        if (startCrnNo <= 0 || endCrnNo < startCrnNo) {
            throw new CoolException("堆垛机范围错误");
        }
        List<BasCrnDepthRule> rules = new ArrayList<BasCrnDepthRule>();
        for (int crnNo = startCrnNo; crnNo <= endCrnNo; crnNo++) {
            CrnDepthRuleProfile profile = buildLegacyProfile(rowLastno, crnNo, null);
            BasCrnDepthRule rule = new BasCrnDepthRule();
            rule.setWhsType(param.getWhsType());
            rule.setCrnNo(crnNo);
            rule.setLayoutType(profile.getLayoutType());
            rule.setSearchRowsCsv(joinRows(profile.getSearchRows()));
            rule.setShallowRowsCsv(joinRows(profile.getShallowRows()));
            rule.setDeepRowsCsv(joinRows(profile.getDeepRows()));
            rule.setEnabled(param.getEnabled() == null ? 1 : param.getEnabled());
            rule.setMemo(param.getMemo());
            rules.add(rule);
        }
        return rules;
    }
 
    /**
     * 按模板批量保存规则,已存在则覆盖,不存在则新增。
     */
    @Override
    public void saveTemplate(BasCrnDepthRuleTemplateParam param, Long userId) {
        List<BasCrnDepthRule> previewRules = previewTemplate(param);
        Date now = new Date();
        for (BasCrnDepthRule previewRule : previewRules) {
            validateRule(previewRule);
            BasCrnDepthRule exists = this.selectOne(new EntityWrapper<BasCrnDepthRule>()
                    .eq("whs_type", previewRule.getWhsType())
                    .eq("crn_no", previewRule.getCrnNo()));
            if (exists == null) {
                previewRule.setCreateBy(userId);
                previewRule.setCreateTime(now);
                previewRule.setUpdateBy(userId);
                previewRule.setUpdateTime(now);
                this.insert(previewRule);
                continue;
            }
            previewRule.setId(exists.getId());
            previewRule.setCreateBy(exists.getCreateBy());
            previewRule.setCreateTime(exists.getCreateTime());
            previewRule.setUpdateBy(userId);
            previewRule.setUpdateTime(now);
            this.updateById(previewRule);
        }
    }
 
    /**
     * 把数据库规则转换成统一的运行时画像对象。
     */
    private CrnDepthRuleProfile buildProfileFromRule(BasCrnDepthRule rule) {
        CrnDepthRuleProfile profile = new CrnDepthRuleProfile();
        profile.setWhsType(rule.getWhsType());
        profile.setCrnNo(rule.getCrnNo());
        profile.setLayoutType(rule.getLayoutType());
        profile.setSource(PROFILE_SOURCE_CONFIG);
        profile.setSearchRows(normalizeRows(parseRows(rule.getSearchRowsCsv())));
        profile.setShallowRows(normalizeRows(parseRows(rule.getShallowRowsCsv())));
        profile.setDeepRows(normalizeRows(parseRows(rule.getDeepRowsCsv())));
        if (profile.getSearchRows().isEmpty()) {
            if (!profile.getShallowRows().isEmpty()) {
                profile.setSearchRows(new ArrayList<Integer>(profile.getShallowRows()));
            } else {
                profile.setSearchRows(new ArrayList<Integer>(profile.getDeepRows()));
            }
        }
        if (profile.isSingleExtension() && profile.getShallowRows().isEmpty()) {
            profile.setShallowRows(new ArrayList<Integer>(profile.getSearchRows()));
        }
        buildPairRows(profile);
        return profile;
    }
 
    /**
     * 用旧的 row_lastno + slaveProperties 规则构建运行时画像。
     */
    private CrnDepthRuleProfile buildLegacyProfile(RowLastno rowLastno, Integer crnNo, Integer preferredNearRow) {
        CrnDepthRuleProfile profile = new CrnDepthRuleProfile();
        profile.setWhsType(rowLastno.getWhsType());
        profile.setCrnNo(crnNo);
        profile.setLayoutType(resolveLegacyLayoutType(rowLastno));
        profile.setSource(PROFILE_SOURCE_LEGACY);
        profile.setSearchRows(getLegacySearchRows(rowLastno, crnNo, preferredNearRow));
        if (profile.isSingleExtension()) {
            profile.setShallowRows(new ArrayList<Integer>(profile.getSearchRows()));
            profile.setDeepRows(new ArrayList<Integer>());
            return profile;
        }
        List<Integer> shallowRows = new ArrayList<Integer>();
        List<Integer> deepRows = new ArrayList<Integer>();
        for (Integer searchRow : profile.getSearchRows()) {
            if (searchRow == null) {
                continue;
            }
            if (Utils.isShallowLoc(slaveProperties, searchRow)) {
                shallowRows.add(searchRow);
                Integer deepRow = safeGetLegacyDeepRow(rowLastno, searchRow);
                if (deepRow != null) {
                    deepRows.add(deepRow);
                }
            } else {
                deepRows.add(searchRow);
            }
        }
        profile.setShallowRows(normalizeRows(shallowRows));
        profile.setDeepRows(normalizeRows(deepRows));
        buildPairRows(profile);
        return profile;
    }
 
    /**
     * 建立浅排与深排之间的对应关系,供双伸选位时配对使用。
     */
    private void buildPairRows(CrnDepthRuleProfile profile) {
        if (profile == null || !profile.isDoubleExtension()) {
            return;
        }
        for (Integer shallowRow : profile.getShallowRows()) {
            Integer deepRow = findNearestAdjacentRow(shallowRow, profile.getDeepRows());
            if (deepRow == null) {
                continue;
            }
            profile.getShallowToDeepRow().put(shallowRow, deepRow);
            profile.getDeepToShallowRow().put(deepRow, shallowRow);
        }
    }
 
    /**
     * 在候选排中找到与当前排距离最近的配对排。
     */
    private Integer findNearestAdjacentRow(Integer currentRow, List<Integer> candidateRows) {
        if (currentRow == null || Cools.isEmpty(candidateRows)) {
            return null;
        }
        Integer best = null;
        int bestDistance = Integer.MAX_VALUE;
        for (Integer candidateRow : candidateRows) {
            if (candidateRow == null) {
                continue;
            }
            int distance = Math.abs(candidateRow - currentRow);
            if (distance < bestDistance) {
                bestDistance = distance;
                best = candidateRow;
            }
            if (distance == 1) {
                return candidateRow;
            }
        }
        return best;
    }
 
    /**
     * 根据旧库型规则推断单伸/双伸布局类型。
     */
    private Integer resolveLegacyLayoutType(RowLastno rowLastno) {
        if (rowLastno == null) {
            return 1;
        }
        if (rowLastno.getTypeId() != null && rowLastno.getTypeId() == 2) {
            return 1;
        }
        return 2;
    }
 
    /**
     * 按旧逻辑推导某台堆垛机的搜索排顺序。
     */
    private List<Integer> getLegacySearchRows(RowLastno rowLastno, Integer crnNo, Integer preferredNearRow) {
        List<Integer> searchRows = new ArrayList<Integer>();
        if (rowLastno == null || crnNo == null) {
            return searchRows;
        }
        addRow(searchRows, preferredNearRow, rowLastno);
        int rowSpan = getLegacyRowSpan(rowLastno.getTypeId());
        int startCrnNo = rowLastno.getsCrnNo() == null ? 1 : rowLastno.getsCrnNo();
        int startRow = rowLastno.getsRow() == null ? 1 : rowLastno.getsRow();
        int crnOffset = crnNo - startCrnNo;
        if (crnOffset < 0) {
            return searchRows;
        }
        int crnStartRow = startRow + crnOffset * rowSpan;
        if (rowLastno.getTypeId() != null && rowLastno.getTypeId() == 2) {
            addRow(searchRows, crnStartRow, rowLastno);
            addRow(searchRows, crnStartRow + 1, rowLastno);
            return searchRows;
        }
        addRow(searchRows, crnStartRow + 1, rowLastno);
        addRow(searchRows, crnStartRow + 2, rowLastno);
        if (searchRows.isEmpty()) {
            for (int row = crnStartRow; row < crnStartRow + rowSpan; row++) {
                addRow(searchRows, row, rowLastno);
            }
        }
        return searchRows;
    }
 
    /**
     * 由旧浅排规则推断对应的深排。
     */
    private Integer safeGetLegacyDeepRow(RowLastno rowLastno, Integer shallowRow) {
        if (rowLastno == null || shallowRow == null) {
            return null;
        }
        try {
            String shallowLocNo = Utils.zerofill(String.valueOf(shallowRow), 2) + "00101";
            Integer deepRow = Utils.getRow(Utils.getDeepLoc(slaveProperties, shallowLocNo));
            if (deepRow < rowLastno.getsRow() || deepRow > rowLastno.geteRow()) {
                return null;
            }
            return deepRow;
        } catch (Exception ignored) {
            return null;
        }
    }
 
    /**
     * 取模板生成时的默认起始堆垛机号。
     */
    private Integer defaultStartCrnNo(RowLastno rowLastno) {
        return rowLastno.getsCrnNo() == null ? 1 : rowLastno.getsCrnNo();
    }
 
    /**
     * 取模板生成时的默认结束堆垛机号。
     */
    private Integer defaultEndCrnNo(RowLastno rowLastno) {
        if (rowLastno.geteCrnNo() != null && rowLastno.geteCrnNo() >= defaultStartCrnNo(rowLastno)) {
            return rowLastno.geteCrnNo();
        }
        if (rowLastno.getCrnQty() != null && rowLastno.getCrnQty() > 0) {
            return defaultStartCrnNo(rowLastno) + rowLastno.getCrnQty() - 1;
        }
        return defaultStartCrnNo(rowLastno);
    }
 
    /**
     * 按旧库型规则返回每台堆垛机占用的排跨度。
     */
    private int getLegacyRowSpan(Integer typeId) {
        if (typeId != null && typeId == 2) {
            return 2;
        }
        return 4;
    }
 
    /**
     * 向搜索排列表追加一个合法且不重复的排号。
     */
    private void addRow(List<Integer> searchRows, Integer row, RowLastno rowLastno) {
        if (row == null || rowLastno == null) {
            return;
        }
        if (row < rowLastno.getsRow() || row > rowLastno.geteRow()) {
            return;
        }
        if (!searchRows.contains(row)) {
            searchRows.add(row);
        }
    }
 
    /**
     * 去重并过滤非法排号。
     */
    private List<Integer> normalizeRows(List<Integer> rows) {
        LinkedHashSet<Integer> normalizedRows = new LinkedHashSet<Integer>();
        if (Cools.isEmpty(rows)) {
            return new ArrayList<Integer>();
        }
        for (Integer row : rows) {
            if (row != null && row > 0) {
                normalizedRows.add(row);
            }
        }
        return new ArrayList<Integer>(normalizedRows);
    }
 
    /**
     * 把 CSV/范围表达式解析成排号列表。
     */
    private List<Integer> parseRows(String rowsCsv) {
        List<Integer> rows = new ArrayList<Integer>();
        if (Cools.isEmpty(rowsCsv)) {
            return rows;
        }
        String normalized = rowsCsv.replace(",", ",")
                .replace(";", ";")
                .replace("、", ",")
                .replaceAll("\\s+", "");
        if (normalized.isEmpty()) {
            return rows;
        }
        String[] segments = normalized.split("[,;]");
        for (String segment : segments) {
            if (Cools.isEmpty(segment)) {
                continue;
            }
            if (segment.contains("-")) {
                String[] rangeParts = segment.split("-", 2);
                Integer startRow = safeParseInt(rangeParts[0]);
                Integer endRow = safeParseInt(rangeParts[1]);
                if (startRow == null || endRow == null) {
                    continue;
                }
                int step = startRow <= endRow ? 1 : -1;
                for (int row = startRow; step > 0 ? row <= endRow : row >= endRow; row += step) {
                    rows.add(row);
                }
                continue;
            }
            Integer row = safeParseInt(segment);
            if (row != null) {
                rows.add(row);
            }
        }
        return normalizeRows(rows);
    }
 
    /**
     * 安全地把字符串转换为整数,失败时返回 null。
     */
    private Integer safeParseInt(String value) {
        if (Cools.isEmpty(value)) {
            return null;
        }
        try {
            return Integer.parseInt(value.trim());
        } catch (NumberFormatException ignored) {
            return null;
        }
    }
 
    /**
     * 把排号列表序列化成逗号分隔字符串。
     */
    private String joinRows(List<Integer> rows) {
        if (Cools.isEmpty(rows)) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        for (Integer row : rows) {
            if (row == null) {
                continue;
            }
            if (builder.length() > 0) {
                builder.append(",");
            }
            builder.append(row);
        }
        return builder.length() == 0 ? null : builder.toString();
    }
}