自动化立体仓库 - WMS系统
#
Administrator
18 小时以前 16bd33b89e43278a1080a5f322356675522c84ee
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
package com.zy.asrs.task.handler;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.core.common.Cools;
import com.zy.asrs.entity.LocDetl;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.entity.WrkDetl;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.entity.param.EmptyPlateOutParam;
import com.zy.asrs.mapper.LocDetlMapper;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.service.StaDescService;
import com.zy.asrs.service.WorkService;
import com.zy.asrs.service.WrkDetlService;
import com.zy.asrs.service.WrkMastService;
import com.zy.asrs.task.AbstractHandler;
import com.zy.asrs.task.core.ReturnT;
import com.zy.common.service.CommonService;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import java.util.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
@Slf4j
@Service
public class AutoEmptyOutHandler extends AbstractHandler<String> {
    @Autowired
    private ConfigService configService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private WorkService workService;
    @Autowired
    private LocDetlMapper locDetlMapper;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private StaDescService staDescService;
 
    @Transactional
    public ReturnT<String> moveOldestInventory(Integer crnNo, Integer max, Long userId) {
        try {
            Long operatorUserId = userId == null ? 1L : userId;
            Integer threshold = max;
            if (threshold == null) {
                Config config = configService.selectConfigByCode("EmptyMax");
                if (Cools.isEmpty(config)) {
                    threshold = 10;
                } else {
                    threshold = Integer.valueOf(config.getValue());
                }
            }
 
            Config configInventory = configService.selectConfigByCode("InventoryAutoOut");
            if (Cools.isEmpty(configInventory) || "false".equalsIgnoreCase(configInventory.getValue())) {
                return new ReturnT<>(ReturnT.SUCCESS_CODE, "InventoryAutoOut=false");
            }
 
            List<Integer> crnNos = new ArrayList<>();
            if (crnNo == null) {
                for (int i = 1; i < 5; i++) {
                    crnNos.add(i);
                }
            } else {
                crnNos.add(crnNo);
            }
 
            List<String> moved = new ArrayList<>();
            for (Integer currentCrnNo : crnNos) {
                if (currentCrnNo == null) {
                    continue;
                }
                List<WrkMast> wrkMastList = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                        .eq("crn_no", currentCrnNo)
                        .eq("io_type", 110));
                if (!wrkMastList.isEmpty()) {
                    continue;
                }
 
                Integer emptyLocCount = locMastService.selectCount(new EntityWrapper<LocMast>()
                        .eq("crn_no", currentCrnNo)
                        .eq("loc_sts", "O")
                        .eq("frozen", 0)
                        .eq("deleted", 0)
                        .eq("whs_type", 1));
                if (emptyLocCount == null || emptyLocCount >= threshold) {
                    continue;
                }
 
                String moveResult = moveOldestInventoryInner(currentCrnNo, operatorUserId);
                if (!Cools.isEmpty(moveResult)) {
                    moved.add(moveResult);
                }
            }
 
            ReturnT<String> res = new ReturnT<>(ReturnT.SUCCESS_CODE, "OK");
            if (!moved.isEmpty()) {
                res.setContent(String.join(";", moved));
            }
            return res;
        } catch (Exception e) {
            log.error("fail", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
        }
    }
 
    public String moveOldestInventoryInner(Integer crnNo, Long userId) {
        LocDetl oldest = locDetlMapper.selectOldestLocDetl(crnNo);
        return moveInventoryFromLocDetl(oldest, userId);
    }
 
    public String moveMostMatnrInventoryInner(Integer crnNo, Long userId) {
        String matnr = locDetlMapper.selectMostMatnrByCrnNo(crnNo);
        if (Cools.isEmpty(matnr)) {
            return null;
        }
        LocDetl detl = locDetlMapper.selectOldestLocDetlByCrnNoAndMatnr(crnNo, matnr);
        return moveInventoryFromLocDetl(detl, userId);
    }
 
    private String moveInventoryFromLocDetl(LocDetl sourceDetl, Long userId) {
        if (Cools.isEmpty(sourceDetl) || Cools.isEmpty(sourceDetl.getLocNo())) {
            return null;
        }
 
        LocMast sourceLoc = locMastService.selectById(sourceDetl.getLocNo());
        if (Cools.isEmpty(sourceLoc) || Cools.isEmpty(sourceLoc.getBarcode())) {
            return null;
        }
        Integer sourceCrnNo = sourceLoc.getCrnNo();
        if (sourceCrnNo == null) {
            return null;
        }
 
        Integer runningCount = wrkMastService.selectCount(new EntityWrapper<WrkMast>()
                .eq("barcode", sourceLoc.getBarcode())
                .in("wrk_sts", Arrays.asList(2L, 3L, 4L, 11L, 12L)));
        if (runningCount != null && runningCount > 0) {
            return null;
        }
 
        Integer targetCrnNo = null;
        Integer targetEmptyCount = null;
        for (int candidate = 1; candidate < 5; candidate++) {
            if (candidate == sourceCrnNo) {
                continue;
            }
//            Integer runingCount = wrkMastService.selectCount(new EntityWrapper<WrkMast>()
//                    .eq("io_type", 101).eq("crn_no",candidate)
//                    .in("wrk_sts", Arrays.asList(2L, 3L, 4L, 11L, 12L)));
//            if(runingCount != null && runingCount > 3) {continue;}
            Integer existCount = wrkMastService.selectCount(new EntityWrapper<WrkMast>()
                    .eq("io_type", 101)
                    .eq("log_mk", "Y")
                    .eq("ove_mk", "N"));
            int allow = 3 - (existCount == null ? 0 : existCount);
            if (allow <= 0) {
                continue;
            }
            Integer candidateEmpty = locMastService.selectCount(new EntityWrapper<LocMast>()
                    .eq("crn_no", candidate)
                    .eq("loc_sts", "O")
                    .eq("frozen", 0)
                    .eq("deleted", 0)
                    .eq("whs_type", 1));
            if (candidateEmpty == null || candidateEmpty <= 0) {
                continue;
            }
            if (targetCrnNo == null || candidateEmpty > targetEmptyCount) {
                targetCrnNo = candidate;
                targetEmptyCount = candidateEmpty;
            }
        }
        if (targetCrnNo == null) {
            return null;
        }
 
        LocMast targetLoc = locMastService.selectOne(new EntityWrapper<LocMast>()
                .eq("crn_no", targetCrnNo)
                .eq("loc_sts", "O")
                .eq("frozen", 0)
                .eq("deleted", 0)
                .eq("whs_type", 1)
                .orderBy("lev1,bay1"));
        if (Cools.isEmpty(targetLoc)) {
            return null;
        }
        if (sourceCrnNo.equals(targetLoc.getCrnNo())) {
            return null;
        }
 
        List<Integer> sourceStaNos = staDescService.queryOutStaNosByLocNo(sourceLoc.getLocNo(), 101);
        if (Cools.isEmpty(sourceStaNos)) {
            return null;
        }
        Integer sourceStaNo = sourceStaNos.get(0);
 
        List<Integer> targetStaNos = staDescService.queryOutStaNosByLocNo(targetLoc.getLocNo(), 1);
        if (Cools.isEmpty(targetStaNos)) {
            return null;
        }
        Integer targetStaNo = targetStaNos.get(0);
 
        List<LocDetl> sourceDetls = locDetlMapper.selectList(new EntityWrapper<LocDetl>().eq("loc_no", sourceLoc.getLocNo()));
        if (Cools.isEmpty(sourceDetls)) {
            return null;
        }
 
        Date now = new Date();
        Integer workNo = commonService.getWorkNo(0);
        WrkMast wrkMast = new WrkMast();
        wrkMast.setWrkNo(workNo);
        wrkMast.setIoTime(now);
        wrkMast.setWrkSts(11L);
        wrkMast.setIoType(101);
        wrkMast.setIoPri(12D);
        wrkMast.setCrnNo(sourceLoc.getCrnNo());
        wrkMast.setSourceLocNo(sourceLoc.getLocNo());
        wrkMast.setLocNo(targetLoc.getLocNo());
        wrkMast.setBarcode(sourceLoc.getBarcode());
        wrkMast.setSourceStaNo(String.valueOf(sourceStaNo));
        wrkMast.setStaNo(String.valueOf(targetStaNo));
        wrkMast.setFullPlt("Y");
        wrkMast.setExitMk("N");
        wrkMast.setLogMk("Y");
        wrkMast.setEmptyMk("N");
        wrkMast.setPacked(null);
        wrkMast.setOveMk("N");
        wrkMast.setAppeUser(userId);
        wrkMast.setAppeTime(now);
        wrkMast.setModiUser(userId);
        wrkMast.setModiTime(now);
        if (!wrkMastService.insert(wrkMast)) {
            throw new RuntimeException("自动跨巷道移库生成任务失败,workNo=" + workNo + ", sourceLocNo=" + sourceLoc.getLocNo());
        }
 
        for (LocDetl sourceDetl1 : sourceDetls) {
            WrkDetl wrkDetl = new WrkDetl();
            wrkDetl.sync(sourceDetl1);
            wrkDetl.setWrkNo(workNo);
            wrkDetl.setIoTime(now);
            wrkDetl.setAnfme(sourceDetl1.getAnfme());
            wrkDetl.setAppeTime(now);
            wrkDetl.setAppeUser(userId);
            wrkDetl.setModiTime(now);
            wrkDetl.setModiUser(userId);
            if (!wrkDetlService.insert(wrkDetl)) {
                throw new RuntimeException("自动跨巷道移库生成任务明细失败,workNo=" + workNo + ", sourceLocNo=" + sourceLoc.getLocNo());
            }
        }
 
        sourceLoc.setLocSts("R");
        sourceLoc.setModiUser(userId);
        sourceLoc.setModiTime(now);
        if (!locMastService.updateById(sourceLoc)) {
            throw new RuntimeException("自动跨巷道移库更新源库位失败,workNo=" + workNo + ", sourceLocNo=" + sourceLoc.getLocNo());
        }
 
        targetLoc.setLocSts("S");
        targetLoc.setModiUser(userId);
        targetLoc.setModiTime(now);
        if (!locMastService.updateById(targetLoc)) {
            throw new RuntimeException("自动跨巷道移库更新目标库位失败,workNo=" + workNo + ", targetLocNo=" + targetLoc.getLocNo());
        }
 
        return "workNo=" + workNo + ",sourceLocNo=" + sourceLoc.getLocNo() + ",targetLocNo=" + targetLoc.getLocNo();
    }
    @Transactional
    public ReturnT<String> start() {
        try {
            Config config = configService.selectConfigByCode("EmptyMax");
            Integer max;
            if (Cools.isEmpty(config)) {
                max = 10;
            }else{
                max = Integer.valueOf(config.getValue());
            }
            for(Integer crnNo = 1; crnNo < 5; crnNo++){
                List<WrkMast> wrkMastList = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                        .eq("crn_no", crnNo)
                        .eq("io_type", 110)
                );
                if(!wrkMastList.isEmpty()){continue;}
                Integer emptyLocCount = locMastService.selectCount(new EntityWrapper<LocMast>()
                        .eq("crn_no", crnNo)
                        .eq("loc_sts", "O")
                        .eq("frozen", 0)
                        .eq("deleted", 0)
                        .eq("whs_type", 1));
                if (emptyLocCount == null || emptyLocCount >= max) {
                    continue;
                }
 
                List<LocMast> locMastDList = locMastService.selectPage(
                        new Page<>(1, 3), //选三个空轴库位
                        new EntityWrapper<LocMast>()
                                .eq("crn_no", crnNo)
                                .eq("loc_sts", "D")
                                .eq("frozen", 0)
                                .eq("deleted", 0)
                                .eq("whs_type", 1)
                                .orderBy("lev1,bay1")
                ).getRecords();
                if (Cools.isEmpty(locMastDList)) {
                    Config configInventory = configService.selectConfigByCode("InventoryAutoOut");
                    if (Cools.isEmpty(configInventory) || "false".equalsIgnoreCase(configInventory.getValue())) {
                        continue;
                    }
                    moveOldestInventoryInner(crnNo, 1L);
                    continue;
                }
 
                List<String> locNos = new ArrayList<>();
                for (LocMast locMast : locMastDList) {
                    locNos.add(locMast.getLocNo());
                }
                EmptyPlateOutParam emptyPlateOutParam = new EmptyPlateOutParam();
                emptyPlateOutParam.setOutSite(1076);
                emptyPlateOutParam.setLocNos(locNos);
                workService.emptyPlateOut(emptyPlateOutParam, 1L);
 
            }
 
        } catch (Exception e) {
            log.error("fail", e);
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return FAIL.setMsg(e.getMessage());
        }
        return SUCCESS;
    }
 
 
}