自动化立体仓库 - WMS系统
luxiaotao1123
2022-03-26 580a85c33e14a7d83d63de555bafc943547015f8
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
package com.zy.asrs.controller;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.annotations.ManagerAuth;
import com.core.common.Cools;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.entity.LocDetl;
import com.zy.asrs.entity.MatCode;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.entity.param.OpenApiStockOutParam;
import com.zy.asrs.entity.result.PakoutVo;
import com.zy.asrs.entity.result.StoPreTab;
import com.zy.asrs.service.*;
import com.zy.common.model.LocDetlDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
 
/**
 * Created by vincent on 2021/3/19
 */
@RestController
@RequestMapping("/open/api")
public class OpenController {
 
    private boolean pickSite = false;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private MatCodeService matCodeService;
    @Autowired
    private WorkService workService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    @PostMapping("/stockOut/prew")
    @Transactional
    public R stockOutPrew(@RequestBody List<OpenApiStockOutParam> params){
        List<StoPreTab> result = new ArrayList<>();
        StringBuilder errorMsg = new StringBuilder();
        boolean error = false;
        for (OpenApiStockOutParam param : params) {
            Double sumAnfme = Optional.ofNullable(locDetlService.getSumAnfme(param.getMatnr())).orElse(0.0D);
            double lack = 0.0D;
            if (sumAnfme < param.getAnfme()) {
                lack = param.getAnfme() - sumAnfme;
                if (!error) {
                    error = true;
                }
                MatCode mat = matCodeService.selectById(param.getMatnr());
                // 视图对象
                StoPreTab tab = new StoPreTab();
                tab.setTitle(param.getMatnr() + (mat==null?"": "(" + mat.getMatName() + ")"));
                tab.setMatnr(param.getMatnr());
                tab.setMaktx((mat==null?"": "(" + mat.getMatName() + ")"));
                tab.setAnfme(param.getTotal());
                tab.setLocNo("缺货");
                tab.setTotal(lack);
                tab.setReduce(lack);
                tab.setRemQty(0.0D);
                tab.setPrior(false);
                tab.setPrior$("×");
                tab.setType(0);
                result.add(tab);
 
                errorMsg.append(mat == null ? param.getMatnr() : mat.getMatName()).append("库存不足,缺货数量:").append(param.getAnfme() - sumAnfme).append("</br>");
//                continue;
            }
            List<LocDetl> locDetls = locDetlService.selectPakoutByRule(param.getMatnr());
            double issued = Optional.of(param.getAnfme() - lack).orElse(0.0D) ;
            for (LocDetl locDetl : locDetls) {
                if (issued > 0) {
                    // 视图对象
                    StoPreTab tab = new StoPreTab();
                    tab.setTitle(locDetl.getMatnr() + "(" + locDetl.getMaktx() + ")");
                    tab.setMatnr(locDetl.getMatnr());
                    tab.setMaktx(locDetl.getMaktx());
                    tab.setAnfme(param.getTotal());
 
                    tab.setLocNo(locDetl.getLocNo());
//                    tab.setNodeId(locDetl.getNodeId());
                    tab.setTotal(locDetl.getAnfme());
                    tab.setReduce(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
                    tab.setRemQty(tab.getTotal() - tab.getReduce());
                    tab.setPrior(false);
                    tab.setPrior$("×");
                    tab.setType(2);
                    result.add(tab);
                    // 剩余待出数量递减
                    issued = issued - locDetl.getAnfme();
                }
            }
 
        }
//        if (error) {
//            return R.error(errorMsg.toString());
//        }
        return R.ok(errorMsg).add(result);
    }
 
    @PostMapping("/stockOut")
    @Transactional
    public R stockOut(@RequestBody List<OpenApiStockOutParam> params){
        List<PakoutVo> result = new ArrayList<>();
        if (!Cools.isEmpty(params)) {
            for (OpenApiStockOutParam param : params) {
                // 推荐货位补仓工作档防重
                if (wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("packed", param.getAllotNo()).le("wrk_sts", 14)) > 0) {
                    throw new CoolException("当前补仓任务正在执行,请耐心等待");
                }
                Double sumAnfme = Optional.ofNullable(locDetlService.getSumAnfme(param.getMatnr())).orElse(0.0D);
                if (!Cools.isEmpty(param.getAllotNo()) && sumAnfme < param.getAnfme()) {
                    throw new CoolException("立库库存不足,缺货数量:" + (param.getAnfme() - sumAnfme));
                }
                double lack = 0.0D;
                if (sumAnfme < param.getAnfme()) {
                    lack = param.getAnfme() - sumAnfme;
                    PakoutVo pakoutVo = new PakoutVo();
                    pakoutVo.setLocNo("缺货");
                    pakoutVo.setAnfme(lack);
                    pakoutVo.setMatnr(param.getMatnr());
//                    pakoutVo.setZpallet("param.getZpallet()");
                    result.add(pakoutVo);
 
//                    throw new CoolException(param.getMatnr() + "物料数量不足,缺货数量:" + (param.getAnfme() - sumAnfme));
                }
 
                List<LocDetl> locDetls = locDetlService.selectPakoutByRule(param.getMatnr());
                double issued = Optional.of(param.getAnfme()-lack).orElse(0.0D) ;
                for (LocDetl locDetl : locDetls) {
                    if (issued > 0) {
                        // 生成出库工作档
 
                        int priorCount = jdbcTemplate.queryForObject("select isnull(count(*),0) from man_prior where 1=1 and matnr = '" + param.getMatnr() + "'", Integer.class);
 
                        // 全板
                        if (issued>=locDetl.getAnfme()) {
                            BasDevp staNo = basDevpService.checkSiteStatus(103);
                            List<LocDetlDto> detlDtos = new ArrayList<>();
                            LocDetlDto dto = new LocDetlDto();
                            dto.setLocDetl(locDetl);
                            dto.setCount(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
                            detlDtos.add(dto);
                            workService.stockOut(staNo, detlDtos, 101, 9527L, param.getAllotNo(), Cools.isEmpty(param.getAllotNo()), null);
                        // 拣料
                        } else {
 
                            if (priorCount > 0) {
                                BasDevp staNo = basDevpService.checkSiteStatus(103);
                                List<LocDetlDto> detlDtos = new ArrayList<>();
                                LocDetlDto dto = new LocDetlDto();
                                dto.setLocDetl(locDetl);
                                dto.setCount(locDetl.getAnfme());
                                detlDtos.add(dto);
                                workService.stockOut(staNo, detlDtos, 101, 9527L, param.getAllotNo(), Cools.isEmpty(param.getAllotNo()), locDetl.getAnfme()-issued);
                            } else {
                                BasDevp staNo = basDevpService.checkSiteStatus(pickSite?113:109);
                                List<LocDetlDto> detlDtos = new ArrayList<>();
                                LocDetlDto dto = new LocDetlDto();
                                dto.setLocDetl(locDetl);
                                dto.setCount(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
                                detlDtos.add(dto);
                                workService.stockOut(staNo, detlDtos, 103, 9527L, param.getAllotNo(), Cools.isEmpty(param.getAllotNo()), null);
                            }
 
                        }
 
                        PakoutVo pakoutVo = new PakoutVo();
                        pakoutVo.setLocNo(locDetl.getLocNo());
                        pakoutVo.setAnfme(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
                        pakoutVo.setMatnr(locDetl.getMatnr());
                        pakoutVo.setZpallet(locDetl.getZpallet());
                        result.add(pakoutVo);
 
                        // 剩余待出数量递减
                        issued = issued - locDetl.getAnfme();
                    }
                }
 
            }
        }
        pickSite = !pickSite;
        return R.ok().add(result);
    }
 
    @PostMapping("/available/take/site")
    @Transactional
    public R availableTakeSite(){
        List<Map<String, Object>> result = new ArrayList<>();
        List<Integer> outSite = basDevpService.getAvailableOutSite(101);
        for (Integer siteId : outSite) {
            Map<String, Object> map = new HashMap<>();
            map.put("siteId", siteId);
            map.put("desc", siteId + "(全板出库口)");
            result.add(map);
        }
        List<Integer> pickOutSite = basDevpService.getAvailableOutSite(103);
        for (Integer siteId : pickOutSite) {
            Map<String, Object> map = new HashMap<>();
            map.put("siteId", siteId);
            map.put("desc", siteId + "(拣料出库口)");
            result.add(map);
        }
        return R.ok().add(result);
    }
 
    @PostMapping("/get/asrsLocDetl")
    @Transactional
    public R queryLocDetl(String matnr) {
        List<LocDetl> locDetls = locDetlService.getAsrsLocDetl(matnr);
        return R.ok().add(locDetls);
    }
 
    @PostMapping("/stockOut/locMode")
    @Transactional
    public R stockOutLocMode(@RequestBody List<OpenApiStockOutParam> params){
        List<PakoutVo> result = new ArrayList<>();
        if (!Cools.isEmpty(params)) {
            for (OpenApiStockOutParam param : params) {
                // 推荐货位补仓工作档防重
                if (wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("packed", param.getAllotNo()).le("wrk_sts", 14)) > 0) {
                    throw new CoolException("当前补仓任务正在执行,请耐心等待");
                }
                Double sumAnfme = Optional.ofNullable(locDetlService.getSumAnfme(param.getMatnr())).orElse(0.0D);
                if (!Cools.isEmpty(param.getAllotNo()) && sumAnfme < param.getAnfme()) {
                    throw new CoolException("立库库存不足,缺货数量:" + (param.getAnfme() - sumAnfme));
                }
                double lack = 0.0D;
                if (sumAnfme < param.getAnfme()) {
                    lack = param.getAnfme() - sumAnfme;
                    PakoutVo pakoutVo = new PakoutVo();
                    pakoutVo.setLocNo("缺货");
                    pakoutVo.setAnfme(lack);
                    pakoutVo.setMatnr(param.getMatnr());
//                    pakoutVo.setZpallet("param.getZpallet()");
                    result.add(pakoutVo);
 
//                    throw new CoolException(param.getMatnr() + "物料数量不足,缺货数量:" + (param.getAnfme() - sumAnfme));
                }
 
                // 根据立库库位码去获取立库信息
                List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", param.getLocNo()));
                if (locDetls.size() == 0) {
                    throw new CoolException("未查询到库位信息!");
                }
                double issued = Optional.of(param.getAnfme()-lack).orElse(0.0D) ;
                for (LocDetl locDetl : locDetls) {
                    // 生成出库工作档
 
                    Integer locNoNum = locDetlService.countLocNoNum(locDetl.getLocNo());
                    // 全板
                    if (locNoNum == 1) {
                        BasDevp staNo = basDevpService.checkSiteStatus(103);
                        List<LocDetlDto> detlDtos = new ArrayList<>();
                        LocDetlDto dto = new LocDetlDto();
                        dto.setLocDetl(locDetl);
                        dto.setCount(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
                        detlDtos.add(dto);
                        workService.stockOut(staNo, detlDtos, 101, 9527L, param.getAllotNo(), Cools.isEmpty(param.getAllotNo()), null);
                    } else { // 拣料
                        BasDevp staNo = basDevpService.checkSiteStatus(107);
                        List<LocDetlDto> detlDtos = new ArrayList<>();
                        LocDetlDto dto = new LocDetlDto();
                        dto.setLocDetl(locDetl);
                        dto.setCount(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
                        detlDtos.add(dto);
                        workService.stockOut(staNo, detlDtos, 103, 9527L, param.getAllotNo(), Cools.isEmpty(param.getAllotNo()), null);
                    }
                    PakoutVo pakoutVo = new PakoutVo();
                    pakoutVo.setLocNo(locDetl.getLocNo());
                    pakoutVo.setAnfme(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
                    pakoutVo.setMatnr(locDetl.getMatnr());
                    pakoutVo.setZpallet(locDetl.getZpallet());
                    result.add(pakoutVo);
 
                    // 剩余待出数量递减
                    issued = issued - locDetl.getAnfme();
                }
 
            }
        }
 
        return R.ok().add(result);
    }
}