自动化立体仓库 - WMS系统
zwl
19 小时以前 a1b99c6eb0e36c36f7696eff9bc23ecf57206661
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
package com.zy.asrs.task.handler;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.zy.asrs.entity.BasCrnp;
import com.zy.asrs.entity.LocDetl;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.BasCrnpService;
import com.zy.asrs.service.LocDetlService;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.service.WorkService;
import com.zy.asrs.service.WrkMastService;
import com.zy.asrs.task.AbstractHandler;
import com.zy.asrs.task.core.ReturnT;
import com.zy.asrs.task.support.AutoFrontLocMoveSettings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
@Slf4j
@Service
public class AutoFrontLocMoveHandler extends AbstractHandler<String> {
 
    @Autowired
    private BasCrnpService basCrnpService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private WorkService workService;
 
    public synchronized ReturnT<String> start(AutoFrontLocMoveSettings settings) {
        if (settings == null || !settings.isEnabled() || Cools.isEmpty(settings.getRules())) {
            return SUCCESS;
        }
        try {
            // 每次调度按配置逐台堆垛机尝试,单台只成功下发一笔移库,避免瞬时堆积。
            for (AutoFrontLocMoveSettings.Rule rule : settings.getRules()) {
                dispatch(rule, settings.getUserId());
            }
        } catch (Exception e) {
            log.error("前排补货移库执行失败", e);
            return FAIL.setMsg(e.getMessage());
        }
        return SUCCESS;
    }
 
    void dispatch(AutoFrontLocMoveSettings.Rule rule, Long userId) {
        if (rule == null || rule.getCrnNo() == null || Cools.isEmpty(rule.getFrontRowList())) {
            return;
        }
        if (!isCraneAvailable(rule.getCrnNo())) {
            return;
        }
 
        List<LocMast> targetLocs = loadTargetLocs(rule);
        if (targetLocs.isEmpty()) {
            return;
        }
 
        List<LocMast> sourceLocs = loadSourceLocs(rule);
        if (sourceLocs.isEmpty()) {
            return;
        }
 
        // 目标库位按前排优先顺序遍历,来源库位按配置顺序或“离前排更远”优先遍历。
        for (LocMast targetLoc : targetLocs) {
            for (LocMast sourceLoc : sourceLocs) {
                if (!isCompatible(sourceLoc, targetLoc)) {
                    continue;
                }
                try {
                    workService.locMove(sourceLoc.getLocNo(), targetLoc.getLocNo(), userId);
                    log.info("前排补货移库已下发,堆垛机{},源库位{},目标库位{}",
                            rule.getCrnNo(), sourceLoc.getLocNo(), targetLoc.getLocNo());
                    return;
                } catch (Exception e) {
                    log.warn("前排补货移库下发失败,堆垛机{},源库位{},目标库位{},原因:{}",
                            rule.getCrnNo(), sourceLoc.getLocNo(), targetLoc.getLocNo(), e.getMessage());
                }
            }
        }
    }
 
    private boolean isCraneAvailable(Integer crnNo) {
        BasCrnp crnp = basCrnpService.selectById(crnNo);
        if (Cools.isEmpty(crnp)) {
            return false;
        }
        if ("N".equalsIgnoreCase(crnp.getInEnable()) || "N".equalsIgnoreCase(crnp.getOutEnable())) {
            return false;
        }
        if (crnp.getCrnSts() == null || crnp.getCrnSts() != 3) {
            return false;
        }
        if (crnp.getCrnErr() != null && crnp.getCrnErr() != 0L) {
            return false;
        }
        if (crnp.getTankQty() != null && crnp.getTankQty() == 0) {
            return false;
        }
        EntityWrapper<WrkMast> wrapper = new EntityWrapper<>();
        wrapper.eq("crn_no", crnNo);
        wrapper.last(" and wrk_sts in (2,3,4,11,12)");
        List<WrkMast> activeWorks = wrkMastService.selectList(wrapper);
        // 有执行中/待执行工作时不再追加自动移库,避免与人工或业务任务抢设备。
        return Cools.isEmpty(activeWorks);
    }
 
    private List<LocMast> loadTargetLocs(AutoFrontLocMoveSettings.Rule rule) {
        EntityWrapper<LocMast> wrapper = new EntityWrapper<>();
        wrapper.eq("crn_no", rule.getCrnNo());
        wrapper.eq("loc_sts", "O");
        wrapper.in("row1", rule.getFrontRowList());
        List<LocMast> candidates = locMastService.selectList(wrapper);
        List<LocMast> targetLocs = new ArrayList<>();
        if (!Cools.isEmpty(candidates)) {
            for (LocMast candidate : candidates) {
                if (isUsableLoc(candidate)) {
                    targetLocs.add(candidate);
                }
            }
        }
        targetLocs.sort(buildRowComparator(rule.getFrontRowList()));
        return targetLocs;
    }
 
    private List<LocMast> loadSourceLocs(AutoFrontLocMoveSettings.Rule rule) {
        EntityWrapper<LocMast> wrapper = new EntityWrapper<>();
        wrapper.eq("crn_no", rule.getCrnNo());
        wrapper.eq("loc_sts", "F");
        if (!Cools.isEmpty(rule.getSourceRowList())) {
            wrapper.in("row1", rule.getSourceRowList());
        }
        List<LocMast> candidates = locMastService.selectList(wrapper);
        if (Cools.isEmpty(candidates)) {
            return new ArrayList<>();
        }
 
        Set<Integer> frontRows = new HashSet<>(rule.getFrontRowList());
        List<LocMast> sourceLocs = new ArrayList<>();
        for (LocMast candidate : candidates) {
            if (!isUsableLoc(candidate)) {
                continue;
            }
            // 未显式配置来源排时,默认只从“非前排”搬货,避免刚补到前排又被搬走。
            if (Cools.isEmpty(rule.getSourceRowList()) && frontRows.contains(candidate.getRow1())) {
                continue;
            }
            if (!hasMovableStock(candidate.getLocNo())) {
                continue;
            }
            sourceLocs.add(candidate);
        }
 
        if (!Cools.isEmpty(rule.getSourceRowList())) {
            sourceLocs.sort(buildRowComparator(rule.getSourceRowList()));
        } else {
            sourceLocs.sort(buildDefaultSourceComparator(rule.getFrontRowList()));
        }
        return sourceLocs;
    }
 
    private Comparator<LocMast> buildRowComparator(List<Integer> rowOrder) {
        Map<Integer, Integer> sortIndex = new HashMap<>();
        for (int i = 0; i < rowOrder.size(); i++) {
            sortIndex.put(rowOrder.get(i), i);
        }
        return Comparator
                .comparingInt((LocMast loc) -> sortIndex.getOrDefault(loc.getRow1(), Integer.MAX_VALUE))
                .thenComparing(LocMast::getBay1, Comparator.nullsLast(Integer::compareTo))
                .thenComparing(LocMast::getLev1, Comparator.nullsLast(Integer::compareTo))
                .thenComparing(LocMast::getGro1, Comparator.nullsLast(Integer::compareTo))
                .thenComparing(LocMast::getLocNo, Comparator.nullsLast(String::compareTo));
    }
 
    private Comparator<LocMast> buildDefaultSourceComparator(List<Integer> frontRowOrder) {
        // 前排是升序时,来源默认从更后排开始;前排是降序时,来源默认从更前排开始。
        boolean descending = frontRowOrder.size() > 1 && frontRowOrder.get(0) > frontRowOrder.get(frontRowOrder.size() - 1);
        Comparator<Integer> rowComparator = descending ? Comparator.reverseOrder() : Integer::compareTo;
        return Comparator
                .comparing(LocMast::getRow1, Comparator.nullsLast(rowComparator))
                .thenComparing(LocMast::getBay1, Comparator.nullsLast(Integer::compareTo))
                .thenComparing(LocMast::getLev1, Comparator.nullsLast(Integer::compareTo))
                .thenComparing(LocMast::getGro1, Comparator.nullsLast(Integer::compareTo))
                .thenComparing(LocMast::getLocNo, Comparator.nullsLast(String::compareTo));
    }
 
    private boolean hasMovableStock(String locNo) {
        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", locNo));
        if (Cools.isEmpty(locDetls)) {
            return false;
        }
        // 任何一条库存明细被冻结,都视为该托盘不可被自动策略搬运。
        for (LocDetl locDetl : locDetls) {
            if (locDetl != null && locDetl.getFrozen() != null && locDetl.getFrozen() == 1) {
                return false;
            }
        }
        return true;
    }
 
    private boolean isUsableLoc(LocMast locMast) {
        return locMast != null
                && (locMast.getFrozen() == null || locMast.getFrozen() == 0)
                && !"N".equalsIgnoreCase(locMast.getOutEnable());
    }
 
    private boolean isCompatible(LocMast sourceLoc, LocMast targetLoc) {
        if (sourceLoc == null || targetLoc == null) {
            return false;
        }
        if (sourceLoc.getLocNo() != null && sourceLoc.getLocNo().equals(targetLoc.getLocNo())) {
            return false;
        }
        // 自动移库只做同类库位补位,减少因库位画像差异导致的后续作业风险。
        return sameOrBlank(sourceLoc.getWhsType(), targetLoc.getWhsType())
                && sameOrBlank(sourceLoc.getLocType1(), targetLoc.getLocType1())
                && sameOrBlank(sourceLoc.getLocType2(), targetLoc.getLocType2())
                && sameOrBlank(sourceLoc.getLocType3(), targetLoc.getLocType3());
    }
 
    private boolean sameOrBlank(Object source, Object target) {
        return source == null || target == null || source.equals(target);
    }
}