自动化立体仓库 - WMS系统
zhang
2025-05-11 a2887a248fdf3d67175529e9e687d8feef965466
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
package com.zy.asrs.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.annotations.ManagerAuth;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.StockOutParam;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.LocDto;
import com.zy.common.model.OrderDto;
import com.zy.common.model.OrderMergeVo;
import com.zy.common.model.TaskDto;
import com.zy.common.web.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * Created by vincent on 2022/3/26
 */
@Slf4j
@RestController
public class OutController extends BaseController {
 
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private StaDescService staDescService;
    @Autowired
    private WorkService workService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private MatService matService;
 
    @PostMapping("/out/pakout/orderDetlIds/auth")
    @ManagerAuth
    public R pakoutOrderDetlIds(@RequestParam Long orderId) throws InterruptedException {
        Thread.sleep(200);
        return R.ok().add(orderDetlService.selectByOrderId(orderId).stream().map(OrderDetl::getId).distinct().collect(Collectors.toList()));
    }
 
    @PostMapping("/out/pakout/preview/auth")
    @ManagerAuth
    public R pakoutPreview(@RequestBody List<Long> ids) {
        if (Cools.isEmpty(ids)) {
            return R.parse(BaseRes.PARAM);
        }
        List<OrderDetl> orderDetls = orderDetlService.selectBatchIds(ids);
        List<LocDto> locDtos = new ArrayList<>();
 
        Set<String> exist = new HashSet<>();
 
        for (OrderDetl orderDetl : orderDetls) {
            double issued = Optional.of(orderDetl.getAnfme() - orderDetl.getQty()).orElse(0.0D);
            if (issued <= 0.0D) { continue; }
            List<LocDetl> locDetls = locDetlService.queryStock(orderDetl.getMatnr(), orderDetl.getBatch(), null, exist);
            for (LocDetl locDetl : locDetls) {
                if (issued > 0) {
                    LocDto locDto = new LocDto(locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getMaktx(), locDetl.getBatch(), orderDetl.getOrderNo(),
                            issued >= locDetl.getAnfme() ? locDetl.getAnfme() : issued);
                    int ioType = (issued >= locDetl.getAnfme() && locDetlService.selectCount(new EntityWrapper<LocDetl>().eq("loc_no", locDto.getLocNo())) == 1) ? 101 : 103;
                    List<Integer> staNos = staDescService.queryOutStaNosByLocNo(locDetl.getLocNo(), ioType);
                    locDto.setStaNos(staNos);
                    locDtos.add(locDto);
                    exist.add(locDetl.getLocNo());
                    // 剩余待出数量递减
                    issued = issued - locDetl.getAnfme();
                } else {
                    break;
                }
            }
            if (issued > 0) {
                LocDto locDto = new LocDto(null, orderDetl.getMatnr(), orderDetl.getMaktx(), orderDetl.getBatch(), orderDetl.getOrderNo(), issued);
                locDto.setLack(Boolean.TRUE);
                locDtos.add(locDto);
            }
        }
        return R.ok().add(locDtos);
    }
 
    @PostMapping("/out/pakout/auth")
    @ManagerAuth(memo = "订单出库")
    @Transactional
    public synchronized R pakout(@RequestBody List<LocDto> locDtos) throws InterruptedException {
        if (Cools.isEmpty(locDtos)) {
            return R.parse(BaseRes.PARAM);
        }
        boolean lack = true;
        for (LocDto locDto : locDtos) {
            if (!locDto.isLack()) {
                lack = false;
                break;
            }
        }
        if (lack) {
            return R.error("库存不足");
        }
 
        Thread.sleep(500L);
 
        // 订单预校验  ===>> 1.订单状态; 2.订单带出数量
        List<OrderDto> orderDtos = new ArrayList<>();
        for (LocDto locDto : locDtos) {
            if (!isJSON(locDto.getOrderNo())) {
                if (Cools.isEmpty(locDto.getOrderNo())) { continue; }
                OrderDto orderDto = new OrderDto(locDto.getOrderNo(), locDto.getMatnr(), locDto.getAnfme());
                if (OrderDto.has(orderDtos, orderDto)) {
                    OrderDto dto = OrderDto.find(orderDtos, orderDto);
                    assert dto != null;
                    dto.setAnfme(dto.getAnfme() + orderDto.getAnfme());
                } else {
                    orderDtos.add(orderDto);
                }
            } else {
                // 订单合并出库
//                List<OrderDto> orderDtoList = JSON.parseArray(locDto.getOrderNo(), OrderDto.class);
 
                List<OrderDto> orderDtoList = JSON.parseArray(locDto.getOrderNo(), OrderDto.class);
 
                for (OrderDto one : orderDtoList) {
                    OrderDto orderDto = new OrderDto(one.getOrderNo(), locDto.getMatnr(), one.getAnfme());
                    if (OrderDto.has(orderDtos, orderDto)) {
                        OrderDto dto = OrderDto.find(orderDtos, orderDto);
                        assert dto != null;
                        dto.setAnfme(dto.getAnfme() + orderDto.getAnfme());
                    } else {
                        orderDtos.add(orderDto);
                    }
                }
            }
        }
        for (OrderDto orderDto : orderDtos) {
            Order order = orderService.selectByNo(orderDto.getOrderNo());
            if (!Cools.isEmpty(order)){
                if (order.getSettle() > 2) {
                    return R.error(orderDto.getOrderNo() + "订单已失效,请及时刷新页面");
                }
            }
            OrderDetl orderDetl = orderDetlService.selectItem(orderDto.getOrderNo(), orderDto.getMatnr(), null);
            if (!Cools.isEmpty(orderDetl)){
                if (orderDetl.getAnfme() - orderDetl.getQty() < orderDto.getAnfme()) {
                    return R.ok(orderDto.getOrderNo() + "订单已作业,请及时刷新页面");
                }
            }
        }
 
        List<TaskDto> taskDtos = new ArrayList<>();
        // 根据 (库位 & 出库站) 分组; 理想状态:一组为一次出库任务
        for (LocDto locDto : locDtos) {
            if (locDto.isLack()) { continue; }
            // 防止前端页面提取库位信息后,在其他地方对该库位生成了出库任务(库位状态非F状态)
            LocMast locMast = locMastService.selectById(locDto.getLocNo());
            if(!Cools.isEmpty(locMast) && !locMast.getLocSts().equals("F")){
                return R.error("库位号非在库状态,请重新选择出库库位===>>" + locDto.getLocNo());
            }
 
            TaskDto taskDto = new TaskDto(locDto.getLocNo(), locDto.getStaNo(), locDto);
            if (TaskDto.has(taskDtos, taskDto)) {
                TaskDto dto = TaskDto.find(taskDtos, taskDto);
                assert dto != null;
                dto.getLocDtos().addAll(taskDto.getLocDtos());
            } else {
                taskDtos.add(taskDto);
            }
        }
        List<String> locNos = new ArrayList<>();
        for (TaskDto taskDto : taskDtos) {
            if (!locNos.contains(taskDto.getLocNo())){
                locNos.add(taskDto.getLocNo());
            }
        }
        List<TaskDto> taskDtos1 = new ArrayList<>();
        for (TaskDto taskDto : taskDtos) {
            String locNo = taskDto.getLocNo();
            List<String> groupOuterSingleLoc = Utils.getGroupOuterSingleLoc(locNo);
            LocMast locMast1 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", locNo));
            if (locMast1.getLocType2().equals((short)3)){
                groupOuterSingleLoc = Utils.getGroupOuterSingleLocLowFrequency(locNo);
            }
            for (String locNo1 : groupOuterSingleLoc){
                if (!locNos.contains(locNo1)){
                    locNos.add(locNo1);
                    LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", locNo1));
                    if (locMast.getLocSts().equals("F")){
                        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", locNo1));
                        if (Cools.isEmpty(locDetls) || locDetls.size()==0) {
                            TaskDto taskDto1 = new TaskDto(locNo1, taskDto.getStaNo());
                            taskDtos1.add(taskDto1);
                        }else {
                            List<LocDto> locDtos1 = new ArrayList<>();
                            for (LocDetl locDetl:locDetls){
                                LocDto locDto = new LocDto(locDetl,taskDto.getLocDtos().get(0),"伴生出库");
                                locDtos1.add(locDto);
                            }
                            TaskDto taskDto1 = new TaskDto(locNo1, taskDto.getStaNo(),locDtos1);
                            taskDtos1.add(taskDto1);
                        }
                    }else if (locMast.getLocSts().equals("D")){
                        TaskDto taskDto1 = new TaskDto(locNo1, taskDto.getStaNo());
                        taskDtos1.add(taskDto1);
                    }
                }
            }
        }
        taskDtos.addAll(taskDtos1);
        // -----------------------------------------------------------------------------------------------
        List<String> excludeLocNos = taskDtos.stream().map(TaskDto::getLocNo).distinct().collect(Collectors.toList());
        for (TaskDto taskDto : taskDtos) {
            BasDevp staNo = basDevpService.checkSiteStatus(taskDto.getStaNo());
            workService.stockOut(staNo, taskDto, getUserId());
//            locMastService.breakUp(taskDto.getLocNo(), excludeLocNos);
        }
        return R.ok();
    }
 
 
    /**
     * 合并订单汇总预览
     */
    @RequestMapping(value = "/order/merge/preview/auth")
    @ManagerAuth
    public R mergePreview(@RequestParam(value = "orderIds[]") List<Long> orderIds){
        return R.ok().add(orderService.mergePreview(orderIds));
    }
 
    @PostMapping("/out/pakout/preview/merge/auth")
    @ManagerAuth
    public R pakoutPreviewMerge(@RequestBody List<OrderMergeVo> list) {
        if (Cools.isEmpty(list)) {
            return R.parse(BaseRes.PARAM);
        }
        Set<String> exist = new HashSet<>();
        List<LocDto> locDtos = new ArrayList<>();
 
        for (OrderMergeVo vo : list) {
            double issued = Optional.of(vo.getAnfme()).orElse(0.0D);
            if (issued <= 0.0D) { continue; }
            List<LocDetl> locDetls = locDetlService.queryStock(vo.getMatnr(), vo.getBatch(), null, exist);
            for (LocDetl locDetl : locDetls) {
                if (issued > 0) {
                    LocDto locDto = new LocDto(locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getMaktx(), locDetl.getBatch(), JSON.toJSONString(vo.getOrderDtos()),
                            issued >= locDetl.getAnfme() ? locDetl.getAnfme() : issued);
                    int count = locDetlService.selectCount(new EntityWrapper<LocDetl>().eq("loc_no", locDto.getLocNo()));
                    int ioType = (issued >= locDetl.getAnfme() && count == 1 ? 101 : 103);
                    List<Integer> staNos = staDescService.queryOutStaNosByLocNo(locDetl.getLocNo(), ioType);
                    locDto.setStaNos(staNos);
                    locDtos.add(locDto);
                    exist.add(locDetl.getLocNo());
                    // 剩余待出数量递减
                    issued = issued - locDetl.getAnfme();
                } else {
                    break;
                }
            }
            if (issued > 0) {
                LocDto locDto = new LocDto(null, vo.getMatnr(), vo.getMaktx(), vo.getBatch(), JSON.toJSONString(vo.getOrderDtos()), issued);
                locDto.setLack(Boolean.TRUE);
                locDtos.add(locDto);
            }
        }
        for (LocDto locDto : locDtos) {
            Mat mat = matService.selectByMatnr(locDto.getMatnr());
            assert mat != null;
            locDto.setSpecs(mat.getSpecs());
        }
        return R.ok().add(locDtos);
    }
 
    /**
     * excel导入
     */
    @PostMapping(value = "/checkout/excel/import/auth")
    @ManagerAuth(memo = "盘点导入")
    @Transactional
    public R cstmrExcelImport(MultipartFile file) throws IOException {
        InputStream inStream = file.getInputStream();
        String fileMime = file.getContentType();
        int excelVersion = 2007;
        if ("application/vnd.ms-excel".equals(fileMime)) {
            excelVersion = 2003;
        }
        Workbook book = null;
        try {
            if (excelVersion == 2003) {
                book = new HSSFWorkbook(inStream);
            }
            else {  // 当 excel 是 2007 时
                book = new XSSFWorkbook(inStream);
            }
        } catch (Exception e) {
            log.error("fail", e);
            return R.error("导入文件格式错误,请使用xls后缀的文件!");
        }
 
        Sheet sheet = book.getSheetAt(0);
        int totalRows = sheet.getLastRowNum() + 1;    // 总
        Long userId = getUserId();
        Date now = new Date();
        DataFormatter dataFormatter = new DataFormatter();
        for (int i = 1; i < totalRows; i++) {
            Row row = sheet.getRow(i);
            // 库位号
            String locno = dataFormatter.formatCellValue(row.getCell(0));
            // 出库站
            Integer outSite = Integer.parseInt(dataFormatter.formatCellValue(row.getCell(1)));
 
            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", locno));
            StockOutParam param = new StockOutParam();
 
            if (!Cools.isEmpty(locMast) && locMast.getLocSts().equals("F")) {
                List<LocDetl> locDetls = locDetlService.selectByLocNo(locno);
                List<StockOutParam.LocDetl> locDes = new ArrayList<>();
                for (LocDetl locDetl : locDetls) {
                    StockOutParam.LocDetl SOlocDetl = new StockOutParam.LocDetl(locDetl.getLocNo(),locDetl.getMatnr(),locDetl.getBatch(),locDetl.getAnfme());
                    locDes.add(SOlocDetl);
                }
                param.setLocDetls(locDes);
                param.setOutSite(outSite);
                workService.locCheckOut(param, getUserId());
            } else {
                return R.error("出库失败");
            }
        }
        return R.ok("出库完成");
    }
 
}