gdg
2021-01-29 f675672004af0ff9071d21546a0eee0a0c8f3092
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package com.nanometer.smartlab.controller;
 
import com.nanometer.smartlab.entity.*;
import com.nanometer.smartlab.entity.enumtype.ApplyStatus;
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.service.*;
import com.nanometer.smartlab.util.Constants;
import com.nanometer.smartlab.util.FacesUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
 
/**
 * Created by johnny on 17/12/15.
 */
@Controller
@Scope("session")
public class OrderMngController extends BaseController {
 
    private static Logger logger = Logger.getLogger(OrderMngController.class);
 
    @Resource
    private OpeOrderService opeOrderService;
    @Resource
    private MenuController menuController;
    @Resource
    private SysWarehouseService sysWarehouseService;
    @Resource
    private SysWarehouseContainerService sysWarehouseContainerService;
    @Resource
    private BaseMetaService baseMetaService;
    @Resource
    private SysUserService sysUserService;
 
    private LazyDataModel<OpeOrder> dataModel;
    private OpeOrder opeOrder;
    private List<OpeOrder> selectedList;
    private String orderCode;
    private String orderName;
    private Timestamp startTime;
    private Timestamp endTime;
    private String orderUserName;
    private Integer orderStatus;
    private List<ApplyStatus> orderStatusSelectList;
    private List<SysUser> userSelectList;
 
    private List<OpeApply> opeOrderApplyList;
 
    private List<SysWarehouse> warehouseList;
    private List<SysWarehouseContainer> warehouseContainerList;
    private List<BaseMeta> warehouseContainerPlaceList;
    private Map<String, String> warehouseNameMap;
    private Map<String, SysWarehouseContainer> warehouseContainerMap;
    private Map<String, List<SysWarehouseContainer>> warehouseIdContainerMap;
 
    public void initRukuPage() {
        this.userSelectList = this.sysUserService.getSysUserList(null, null, null, null, null,null,null);
        //将admin置于首位
        for (int i = 0; i < userSelectList.size(); i++) {
            if ("admin".equals(userSelectList.get(i).getAccount())){
                userSelectList.add(0, userSelectList.remove(i));
            }
        }
        this.warehouseList = this.sysWarehouseService.getSysWarehouseList(null, null, null, null);
        this.warehouseNameMap = new HashMap<String, String>();
        if (this.warehouseList != null && this.warehouseList.size() > 0) {
            for (SysWarehouse house : this.warehouseList) {
                this.warehouseNameMap.put(house.getId(), house.getName());
            }
        }
 
        this.warehouseContainerMap = new HashMap<String, SysWarehouseContainer>();
        this.warehouseIdContainerMap = new HashMap<String, List<SysWarehouseContainer>>();
        List<SysWarehouseContainer> warehouseContainerList = this.sysWarehouseContainerService.getSysWarehouseContainerList(null, null, null, null, null);
        if (warehouseContainerList != null && warehouseContainerList.size() > 0) {
            for (SysWarehouseContainer container : warehouseContainerList) {
                warehouseContainerMap.put(container.getId(), container);
 
                if (!warehouseIdContainerMap.containsKey(container.getWarehouseId())
                        || warehouseIdContainerMap.get(container.getWarehouseId()) == null) {
                    warehouseIdContainerMap.put(container.getWarehouseId(), new ArrayList<SysWarehouseContainer>());
                }
                warehouseIdContainerMap.get(container.getWarehouseId()).add(container);
            }
        }
 
        this.onWarehouseSelectChange(null);
    }
 
 
    public void initChangeCodePage() {
        initRukuPage();
    }
 
    public void onRukuBtnClick() {
        if (this.selectedList == null
                || this.selectedList.size() == 0) {
            FacesUtils.warn("请选择数据。");
            return;
        }
        if (this.selectedList.size() > 1) {
            FacesUtils.warn("只能选择一个数据进行入库。");
            return;
        }
 
        this.opeOrder = this.opeOrderService.getOpeOrder(this.selectedList.get(0).getId());
        if (this.opeOrder.getStatus().getKey() != ApplyStatus.PENDING_PURCHASE.getKey()
                && this.opeOrder.getStatus().getKey() != ApplyStatus.SUPPLIER_CONFIRM.getKey() && this.opeOrder.getStatus().getKey() != ApplyStatus.UNCOMPLETED.getKey()) {
            FacesUtils.warn("只有待采购,已确认和未完成状态的订单才能进行入库。");
            return;
        }
 
        this.opeOrderApplyList = this.opeOrderService.getOpeApplyListByOrder(this.selectedList.get(0).getId());
        this.menuController.goToPage(Constants.PAGE_ORDER_MNG_RUKU, Constants.PAGE_ORDER_MNG);
    }
 
//    public void onEditBtnClick() {
//        if (this.selectedList == null
//                || this.selectedList.size() == 0) {
//            FacesUtils.warn("请选择数据。");
//            return;
//        }
//        if (this.selectedList.size() > 1) {
//            FacesUtils.warn("只能选择一个数据进行修改。");
//            return;
//        }
//
//        this.opeOrder = this.opeOrderService.getOpeOrder(this.selectedList.get(0).getId());
//        /*if (this.opeOrder.getStatus().getKey() != ApplyStatus.PENDING_PURCHASE.getKey()) {
//            FacesUtils.warn("只有待采购状态的订单才能进行入库。");
//            return;
//        }*/
//        /*this.opeApply = this.opeApplyService.getOpeApply(this.selectedList.get(0).getId());
//        this.opeApply = this.opeApplyService.getOpeApply(this.selectedList.get(0).getId());
//        this.action = Constants.ACTION_EDIT;*/
//        this.menuController.goToPage(Constants.PAGE_ORDER_MNG_EDIT, Constants.PAGE_APPLY_MNG);
//    }
 
    public void onViewBtnClick() {
        if (this.selectedList == null
                || this.selectedList.size() == 0) {
            FacesUtils.warn("请选择数据。");
            return;
        }
        if (this.selectedList.size() > 1) {
            FacesUtils.warn("只能选择一个数据进行查看。");
            return;
        }
 
        this.opeOrder = this.opeOrderService.getOpeOrder(this.selectedList.get(0).getId());
        this.opeOrderApplyList = this.opeOrderService.getOpeApplyListByOrder(this.selectedList.get(0).getId());
        RequestContext.getCurrentInstance().execute("PF('dialog').show()");
    }
 
    public void onDeleteBtnClick() {
        try {
            if (this.selectedList == null
                    || this.selectedList.size() == 0) {
                FacesUtils.warn("请选择数据。");
                return;
            }
 
            for (OpeOrder oo : this.selectedList) {
                if (!this.opeOrderService.isOrderPendingPurchase(oo)) {
                    FacesUtils.warn("只有待采购状态的数据可以删除。");
                    return;
                }
            }
 
            this.opeOrderService.deleteOpeOrder(this.selectedList);
 
            FacesUtils.info("删除成功。");
        } catch (Exception e) {
            logger.error("操作失败。", e);
            FacesUtils.warn("操作失败。");
        }
    }
 
    public void onSaveBtnClick() {
        System.out.println("++++++testIsIn ++++++++++++++++");
        try {
            if (this.opeOrder == null) {
                FacesUtils.warn("订单数据为空。");
                return;
            }
 
            if (this.opeOrderApplyList == null
                    || this.opeOrderApplyList.size() == 0) {
                FacesUtils.warn("入库产品数据为空。");
                return;
            }
 
            BigDecimal startReagentCode = null;
            BigDecimal endReagentCode = null;
            for (OpeApply opeApply : this.opeOrderApplyList) {
                // 检查到货数量
                if (opeApply.getArrivalNum() == null || opeApply.getArrivalNum() <= 0) {
                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的到货数量不能为空,且要大于0。");
                    return;
                }
//                // 检查货号
//                if (StringUtils.isBlank(opeApply.getArticleNumber())) {
//                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的货号不能为空。");
//                    return;
//                }
//                // 检查条形码开始
//                if (StringUtils.isBlank(opeApply.getStartReagentCode())) {
//                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码开始不能为空。");
//                    return;
//                }
//                // 检查条形码结束
//                if (StringUtils.isBlank(opeApply.getEndReagentCode())) {
//                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码结束不能为空。");
//                    return;
//                }
                if (StringUtils.isNotBlank(opeApply.getStartReagentCode()) && StringUtils.isNotBlank(opeApply.getEndReagentCode())) {
                    startReagentCode = new BigDecimal(opeApply.getStartReagentCode());
                    endReagentCode = new BigDecimal(opeApply.getEndReagentCode());
                    // 检查条形码结束要大于条形码开始
                    if (endReagentCode.compareTo(startReagentCode) == -1) {
                        FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码开始必须小于条形码结束。");
                        return;
                    }
 
                    // 检查条形码和到货数量是否匹配
                    if (endReagentCode.subtract(startReagentCode).intValue() != (opeApply.getArrivalNum() - 1)) {
                        FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码数量和到货数量不一致。");
                        return;
                    }
                }
 
                // 检查仓库名
                if (StringUtils.isBlank(opeApply.getHouseId())) {
                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的仓库名不能为空。");
                    return;
                }
                // 检查货柜名
                if (StringUtils.isBlank(opeApply.getContainerId())) {
                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的货柜名不能为空。");
                    return;
                }
                // 检查货柜位置
                if (StringUtils.isBlank(opeApply.getPlaceId())) {
                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的货柜位置不能为空。");
                    return;
                }
            }
 
            this.opeOrderService.rukuOpeOrder(this.opeOrder, this.opeOrderApplyList);
 
            // 待发邮件保存
            this.opeOrderService.emailStatusSave(this.opeOrder, this.opeOrderApplyList);
 
            FacesUtils.info("入库成功。");
            this.onCancelBtnClick();
        } catch (BusinessException e) {
            logger.warn("操作失败。", e);
            FacesUtils.warn(e.getMessage());
        } catch (Exception e) {
            logger.error("操作失败。", e);
            FacesUtils.warn("操作失败。");
        }
    }
 
    public void onCancelBtnClick() {
        this.menuController.backToPage();
    }
 
    public LazyDataModel<OpeOrder> getDataModel() {
        if (this.dataModel == null) {
            this.dataModel = new LazyDataModel<OpeOrder>() {
                @Override
                public List<OpeOrder> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
                    List<OpeOrder> list = null;
                    try {
                        int count = opeOrderService.getOpeOrderTotalCount(orderCode, orderName, startTime, endTime, orderStatus, orderUserName);
                        this.setRowCount(count);
                        if (count > 0) {
                            list = opeOrderService.getOpeOrderList(orderCode, orderName, startTime, endTime, orderStatus, orderUserName, first, pageSize);
                        }
                    } catch (Exception e) {
                        logger.error(e);
                    }
                    return list;
                }
 
                @Override
                public OpeOrder getRowData(String rowKey) {
                    Iterator<OpeOrder> iterator = this.iterator();
                    if (iterator != null) {
                        OpeOrder oo = null;
                        while (iterator.hasNext()) {
                            oo = iterator.next();
                            if (rowKey.equals(oo.getId())) {
                                return oo;
                            }
                        }
                    }
                    return null;
                }
            };
        }
 
        return dataModel;
    }
 
    public void onChangeCodeBtnClick() {
        if (this.selectedList == null
                || this.selectedList.size() == 0) {
            FacesUtils.warn("请选择数据。");
            return;
        }
        if (this.selectedList.size() > 1) {
            FacesUtils.warn("只能选择一个数据进行入库。");
            return;
        }
 
        this.opeOrder = this.opeOrderService.getOpeOrder(this.selectedList.get(0).getId());
        if (!(this.opeOrder.getStatus().getKey() == ApplyStatus.STORAGE.getKey() || this.opeOrder.getStatus().getKey() == ApplyStatus.UNCOMPLETED.getKey())) {
            FacesUtils.warn("只有已入库和未完成状态的订单才能进行变更。");
            return;
        }
 
        this.opeOrderApplyList = this.opeOrderService.getOpeApplyListByOrder(this.selectedList.get(0).getId());
 
        this.menuController.goToPage(Constants.PAGE_ORDER_MNG_CHANEGE_CODE, Constants.PAGE_ORDER_MNG);
    }
 
 
    public void onChangeCodePageSaveBtnClick() {
        try {
            if (this.opeOrder == null) {
                FacesUtils.warn("订单数据为空。");
                return;
            }
 
            if (this.opeOrderApplyList == null
                    || this.opeOrderApplyList.size() == 0) {
                FacesUtils.warn("变更条形码产品数据为空。");
                return;
            }
 
            BigDecimal startReagentCode = null;
            BigDecimal endReagentCode = null;
            for (OpeApply opeApply : this.opeOrderApplyList) {
                // 检查到货数量
                if (opeApply.getArrivalNum() == null || opeApply.getArrivalNum() <= 0) {
                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的到货数量不能为空,且要大于0。");
                    return;
                }
                // 检查货号
                if (StringUtils.isBlank(opeApply.getArticleNumber())) {
                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的货号不能为空。");
                    return;
                }
                // 检查条形码开始
                if (StringUtils.isBlank(opeApply.getStartReagentCode())) {
                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码开始不能为空。");
                    return;
                }
                // 检查条形码结束
                if (StringUtils.isBlank(opeApply.getEndReagentCode())) {
                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码结束不能为空。");
                    return;
                }
                startReagentCode = new BigDecimal(opeApply.getStartReagentCode());
                endReagentCode = new BigDecimal(opeApply.getEndReagentCode());
                // 检查条形码结束要大于条形码开始
                if (endReagentCode.compareTo(startReagentCode) == -1) {
                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码开始必须小于条形码结束。");
                    return;
                }
                // 检查条形码和到货数量是否匹配
                if (endReagentCode.subtract(startReagentCode).intValue() != (opeApply.getArrivalNum() - 1)) {
                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码数量和到货数量不一致。");
                    return;
                }
//                // 检查仓库名
//                if (StringUtils.isBlank(opeApply.getHouseId())) {
//                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的仓库名不能为空。");
//                    return;
//                }
//                // 检查货柜名
//                if (StringUtils.isBlank(opeApply.getContainerId())) {
//                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的货柜名不能为空。");
//                    return;
//                }
//                // 检查货柜位置
//                if (StringUtils.isBlank(opeApply.getPlaceId())) {
//                    FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的货柜位置不能为空。");
//                    return;
//                }
            }
 
            //由于修改条形码重新入库
            this.opeOrderService.reRukuInfo(this.opeOrder, this.opeOrderApplyList);
 
            // 待发邮件保存
            this.opeOrderService.emailStatusSave(this.opeOrder, this.opeOrderApplyList);
 
            FacesUtils.info("条形码修改成功。");
            this.onCancelBtnClick();
        } catch (BusinessException e) {
            logger.warn("操作失败。", e);
            FacesUtils.warn(e.getMessage());
        } catch (Exception e) {
            logger.error("操作失败。", e);
            FacesUtils.warn("操作失败。");
        }
    }
 
    public OpeOrder getOpeOrder() {
        return opeOrder;
    }
 
    public void setOpeOrder(OpeOrder opeOrder) {
        this.opeOrder = opeOrder;
    }
 
    public List<OpeOrder> getSelectedList() {
        return selectedList;
    }
 
    public void setSelectedList(List<OpeOrder> selectedList) {
        this.selectedList = selectedList;
    }
 
    public String getOrderCode() {
        return orderCode;
    }
 
    public void setOrderCode(String orderCode) {
        this.orderCode = orderCode;
    }
 
    public String getOrderName() {
        return orderName;
    }
 
    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }
 
    public Timestamp getStartTime() {
        return startTime;
    }
 
    public void setStartTime(Timestamp startTime) {
        this.startTime = startTime;
    }
 
    public Timestamp getEndTime() {
        return endTime;
    }
 
    public void setEndTime(Timestamp endTime) {
        this.endTime = endTime;
    }
 
    public String getOrderUserName() {
        return orderUserName;
    }
 
    public void setOrderUserName(String orderUserName) {
        this.orderUserName = orderUserName;
    }
 
    public Integer getOrderStatus() {
        return orderStatus;
    }
 
    public void setOrderStatus(Integer orderStatus) {
        this.orderStatus = orderStatus;
    }
 
    public List<ApplyStatus> getOrderStatusSelectList() {
        if (this.orderStatusSelectList == null) {
                this.orderStatusSelectList = new ArrayList<ApplyStatus>();
                for(ApplyStatus d : ApplyStatus.values()){
                    orderStatusSelectList.add(d);
                }
        }
        return orderStatusSelectList;
    }
 
    public List<OpeApply> getOpeOrderApplyList() {
        return opeOrderApplyList;
    }
 
    public List<SysWarehouse> getWarehouseList() {
        return warehouseList;
    }
 
    public List<SysWarehouseContainer> getWarehouseContainerList() {
        return warehouseContainerList;
    }
 
    public List<BaseMeta> getWarehouseContainerPlaceList() {
        return warehouseContainerPlaceList;
    }
 
    public List<SysUser> getUserSelectList() {
        return userSelectList;
    }
 
    public String formatWarehouseName(String id) {
        if (StringUtils.isNotBlank(id)
                && this.warehouseNameMap != null) {
            return warehouseNameMap.get(id);
        }
 
        return "";
    }
    public String formatWarehouseContainerName(String id) {
        if (StringUtils.isNotBlank(id)
                && this.warehouseContainerMap != null) {
            if (this.warehouseContainerMap.get(id) != null) {
                return this.warehouseContainerMap.get(id).getContainerCode();
            }
        }
 
        return "";
    }
    public String formatWarehouseContainerPlaceName(String id) {
        if (StringUtils.isNotBlank(id)) {
            return this.baseMetaService.getBaseMetaValue(id);
        }
 
        return "";
    }
 
    public void onWarehouseSelectChange(String warehouseId) {
        this.warehouseContainerList = null;
        this.warehouseContainerPlaceList = null;
        this.changeWarehouseContainerList(warehouseId);
        this.changeWarehouseContainerPlaceList(null);
    }
 
    public void onWarehouseContainerSelectChange(String warehouseContainerId) {
        this.warehouseContainerPlaceList = null;
        this.changeWarehouseContainerPlaceList(warehouseContainerId);
    }
 
    private void changeWarehouseContainerList(String warehouseId) {
        if (StringUtils.isBlank(warehouseId)) {
            if (this.warehouseList != null && this.warehouseList.size() > 0) {
                warehouseId = this.warehouseList.get(0).getId();
            }
        }
 
        if (StringUtils.isNotBlank(warehouseId)) {
            this.warehouseContainerList = warehouseIdContainerMap.get(warehouseId);
        }
    }
    private void changeWarehouseContainerPlaceList(String warehouseContainerId) {
        if (StringUtils.isBlank(warehouseContainerId)) {
            if (this.warehouseContainerList != null && this.warehouseContainerList.size() > 0) {
                warehouseContainerId = this.warehouseContainerList.get(0).getId();
            }
        }
 
        if (StringUtils.isNotBlank(warehouseContainerId)) {
            SysWarehouseContainer container = this.warehouseContainerMap.get(warehouseContainerId);
            BaseMeta baseMeta = null;
            if (container != null
                    && (baseMeta = this.baseMetaService.getBaseMeta(container.getStructure())) != null) {
                this.warehouseContainerPlaceList = this.baseMetaService.getBaseMetaList(baseMeta.getMetaKey());
            }
        }
    }
}