lyfO_o
2021-09-16 0dfab938c5116c8fa0191cadf36a47f5bb4f2936
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
package com.nanometer.smartlab.controller;
 
import com.nanometer.smartlab.entity.OpeApply;
import com.nanometer.smartlab.entity.SysProject;
import com.nanometer.smartlab.entity.SysReagent;
import com.nanometer.smartlab.entity.SysUser;
import com.nanometer.smartlab.entity.enumtype.ApplyStatus;
import com.nanometer.smartlab.entity.enumtype.ValidFlag;
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.service.*;
import com.nanometer.smartlab.util.Constants;
import com.nanometer.smartlab.util.FacesUtils;
import com.nanometer.smartlab.util.IDUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.TabChangeEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
 
/**
 * Created by johnny on 17/12/12.
 */
@Controller
@Scope("session")
public class ApplyMngController extends BaseController {
 
    private static Logger logger = Logger.getLogger(ApplyMngController.class);
 
    @Resource
    private OpeApplyService opeApplyService;
    @Resource
    private MenuController menuController;
    @Resource
    private SysReagentService sysReagentService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysSequenceService sysSequenceService;
    @Resource
    private SysProjectService sysProjectService;
 
    @Resource
    private BaseMetaService baseMetaService;
 
    private LazyDataModel<OpeApply> dataModel;
    private OpeApply opeApply;
    private List<OpeApply> selectedList;
    private String reagentName;
    private String queryCAS;
    private String applyUserName;
    private Timestamp startDeadline;
    private Timestamp endDeadline;
    private Integer status;
    private Integer favor;
    private List<ApplyStatus> statusSelectList;
    private String controlProduct;
 
 
    private LazyDataModel<SysReagent> reagentDataModel;
    private LazyDataModel<SysReagent> reagentFavorDataModel;
    private String applyNewReagentName;
    private String applyNewCas;
    private String applyNewSn;
    private SysReagent selectReagent;
    private int tabValue;
    private boolean isFlush = false;
 
 
 
 
    public SysReagent getSelectReagent() {
        return selectReagent;
    }
 
    public void setSelectReagent(SysReagent selectReagent) {
        try {
            sysUserService.updateUserFavor(selectReagent, getUser());
            this.isFlush = true;
        } catch (Exception e) {
            e.printStackTrace();
            FacesUtils.warn(e.getMessage());
        }
        this.selectReagent = selectReagent;
    }
 
    public String getApplyUserName() {
        return applyUserName;
    }
 
    public void setApplyUserName(String applyUserName) {
        this.applyUserName = applyUserName;
    }
 
    public String getApplyNewSn() {
        return applyNewSn;
    }
 
    public void setApplyNewSn(String applyNewSn) {
        this.applyNewSn = applyNewSn;
    }
 
    private List<SysUser> approveUserSelectList;
    private SysReagent sysReagent;
 
    private LazyDataModel<OpeApply> applyHistoryDataModel;
 
    // 新建申购List
    private List<OpeApply> applyNewList;
    // 新建申购model已选List
    private List<OpeApply> applyNewSelectedList;
    // 新建申购List编辑flag(1: 新建 2:修改)
    private int applyEditFlag = Constants.ACTION_ADD;
 
    private int action;
 
    private List<SysProject> sysProjectList;
 
    private String applyCode;
 
    public void initNewPage() {
        this.initApproveUserSelectList();
    }
 
    public void initApproveUserSelectList() {
        this.approveUserSelectList = this.sysUserService.getHasProjectSysUserList(this.getUserDepartment());
        if (!CollectionUtils.isEmpty(this.approveUserSelectList)){
            if(this.selectedList == null){
                this.sysProjectList = sysProjectService.getSysProjectList(this.approveUserSelectList.get(0).getId());
            }else{
                this.sysProjectList = sysProjectService.getSysProjectList(this.selectedList.get(0).getApproveUserId());
            }
        }
    }
 
    public void onNewBtnClick() {
        /*List<OpeApply> list=opeApplyService.getOpeApplyList(null,null,null,null,null,null,null,null,null,null);
        for(int i=0;i<list.size();i++) {
            if(list.get(i).getBeforeApproveUserId()==null || list.get(i).getBeforeApproveUserId()==""){
                opeApplyService.updateFirst(list.get(i).getId());
            }else {
                opeApplyService.updateFirst2(list.get(i).getId());
            }
        }*/
        this.onResetBtnClick();
        this.onResetBtnClickForNewP();
        this.action = Constants.ACTION_ADD;
        this.selectedList = null;
        this.menuController.goToPage(Constants.PAGE_APPLY_MNG_NEW, Constants.PAGE_APPLY_MNG);
    }
 
    public void onEditBtnClick() {
        if (this.selectedList == null
                || this.selectedList.size() == 0) {
            FacesUtils.warn("请选择数据。");
            return;
        }
        if (this.selectedList.size() > 1) {
            FacesUtils.warn("只能选择一个数据进行修改。");
            return;
        }
 
        if (!this.opeApplyService.isApplyPendingApproval(this.selectedList.get(0))) {
            FacesUtils.warn("只有待审批状态的数据可以修改。");
            return;
        }
 
        this.opeApply = this.opeApplyService.getOpeApply(this.selectedList.get(0).getId());
        this.action = Constants.ACTION_EDIT;
        this.menuController.goToPage(Constants.PAGE_APPLY_MNG_NEW, Constants.PAGE_APPLY_MNG);
    }
 
 
    public void onDeleteBtnClick() {
        try {
            if (this.selectedList == null
                    || this.selectedList.size() == 0) {
                FacesUtils.warn("请选择数据。");
                return;
            }
 
            for (OpeApply oa : this.selectedList) {
                if (!this.opeApplyService.isApplyPendingApproval(oa)) {
                    FacesUtils.warn("只有待审批状态的数据可以删除。");
                    return;
                }
            }
 
            this.opeApplyService.deleteOpeApply(this.selectedList);
 
            FacesUtils.info("删除成功。");
        } catch (Exception e) {
            logger.error("操作失败。", e);
            FacesUtils.warn("操作失败。");
        }
    }
 
 
    public void onExportFileBtnClickNew(){
        ServletOutputStream out = null;
        InputStream is = null;
        try {
            FacesContext ctx = FacesContext.getCurrentInstance();
            ctx.responseComplete();
            String contentType = "application/x-download";
            HttpServletResponse response = (HttpServletResponse) ctx
                    .getExternalContext().getResponse();
            response.setContentType(contentType);
            StringBuffer contentDisposition = new StringBuffer();
            contentDisposition.append("attachment;");
            contentDisposition.append("filename=\"");
            contentDisposition.append("申购管理.xls");
            contentDisposition.append("\"");
            response.setHeader(
                    "Content-Disposition",
                    new String(contentDisposition.toString().getBytes(
                            System.getProperty("file.encoding")), "ISO8859-1"));
            out = response.getOutputStream();
            Map<String, Integer> tempMap = new HashMap<String,Integer>();
            List<OpeApply> realDataList = new ArrayList<>();
            //map=null;
            if(selectedList!=null&&selectedList.size()>0){
                realDataList=selectedList;
            }else {
                realDataList=opeApplyService.getOpeApplyList(reagentName, startDeadline,controlProduct, endDeadline, status, getUserId(), null, queryCAS, null, null,null,applyUserName,(byte)1);
 
            }
             List<String> headerList = new ArrayList<>();
            headerList.add("申购编号");
            headerList.add("产品编号");
            headerList.add("试剂名称");
            headerList.add("管制品");
            headerList.add("规格型号");
            headerList.add("包装");
            headerList.add("含税售价");
            headerList.add("CAS号");
            headerList.add("危险性质");
            headerList.add("厂家");
            headerList.add("试剂类型");
            headerList.add("申购数量");
            headerList.add("申购人");
            headerList.add("申购日期");
            headerList.add("一级审批者");
            headerList.add("二级审批者");
            headerList.add("订单状态");
            headerList.add("审批批注");
            HSSFWorkbook hssfWorkbook =exportExcelNew(headerList, realDataList);
            hssfWorkbook.write(out);
            out.flush();
            ctx.responseComplete();
        }catch (Exception e) {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
    }
 
 
    public void uploadApply(FileUploadEvent event){
        try {
            opeApplyService.importApply(event, getUser());
            FacesUtils.info("导入成功");
        } catch (BusinessException e) {
            FacesUtils.warn(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            FacesUtils.warn("导入失败,请联系管理员");
        }
 
    }
 
 
    public  HSSFWorkbook exportExcelNew(List<String> headerList, List<OpeApply> dataList) {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
 
 
        Integer total = null;
        HSSFSheet sheet = hssfWorkbook.createSheet("申购详情");
        HSSFRow titlerRow = sheet.createRow(0);
        for(int i = 0; i < headerList.size(); i++) {
            titlerRow.createCell(i).setCellValue(headerList.get(i));
        }
        for (int i = 0; i < dataList.size(); i++) {
            System.out.println(dataList.get(i));
            HSSFRow dataRow = sheet.createRow(i + 1);
            dataRow.createCell(0).setCellValue(dataList.get(i).getApplyCode()== null ? "": String.valueOf(dataList.get(i).getApplyCode()));
            dataRow.createCell(1).setCellValue(dataList.get(i).getReagent().getProductSn()== null ? "": String.valueOf(dataList.get(i).getReagent().getProductSn()));
            dataRow.createCell(2).setCellValue(dataList.get(i).getReagent().getName()== null ? "": String.valueOf(dataList.get(i).getReagent().getName()));
            dataRow.createCell(3).setCellValue(dataList.get(i).getReagent().getControlProducts()== null ? "": String.valueOf(baseMetaService.getBaseMetaValue(dataList.get(i).getReagent().getControlProducts())));
            dataRow.createCell(4).setCellValue(dataList.get(i).getReagent().getReagentFormat()== null ? "": String.valueOf(baseMetaService.getBaseMetaValue(dataList.get(i).getReagent().getReagentFormat())));
            dataRow.createCell(5).setCellValue(dataList.get(i).getReagent().getMainMetering()== null ? "": String.valueOf(dataList.get(i).getReagent().getMainMetering())+baseMetaService.getBaseMetaValue(dataList.get(i).getReagent().getReagentUnit()));
            dataRow.createCell(6).setCellValue(dataList.get(i).getReagent().getPrice()== null ? "": String.valueOf(dataList.get(i).getReagent().getPrice()));
            dataRow.createCell(7).setCellValue(dataList.get(i).getReagent().getCas()== null ? "": String.valueOf(dataList.get(i).getReagent().getCas()));
            dataRow.createCell(8).setCellValue(dataList.get(i).getReagent().getReagentCharacter()== null ? "": String.valueOf(baseMetaService.getBaseMetaValue(dataList.get(i).getReagent().getReagentCharacter())));
            dataRow.createCell(9).setCellValue(dataList.get(i).getReagent().getProductHomeName()== null ? "": String.valueOf(dataList.get(i).getReagent().getProductHomeName()));
            dataRow.createCell(10).setCellValue(dataList.get(i).getReagent().getReagentType()== null ? "": String.valueOf(baseMetaService.getBaseMetaValue(dataList.get(i).getReagent().getReagentType())));
            dataRow.createCell(11).setCellValue(String.valueOf(dataList.get(i).getNum()));
            dataRow.createCell(12).setCellValue(String.valueOf(dataList.get(i).getApplyUserName()));
            dataRow.createCell(13).setCellValue(String.valueOf(dataList.get(i).getCreateTime()));
           // SysUser u=sysUserService.getSysUser(dataList.get(i).getApproveUserId());
            dataRow.createCell(14).setCellValue(String.valueOf(dataList.get(i).getFirName()));
            if(dataList.get(i).getBeforeApproveUserId()!=null && !dataList.get(i).getBeforeApproveUserId().equals("")){
                //u=sysUserService.getSysUser(dataList.get(i).getBeforeApproveUserId());
                dataRow.createCell(15).setCellValue(String.valueOf(dataList.get(i).getApproveUserName()));
            }
            //dataRow.createCell(14).setCellValue(String.valueOf(u.getName()));
            dataRow.createCell(16).setCellValue(String.valueOf(dataList.get(i).getStatus().getText()));
            dataRow.createCell(17).setCellValue(dataList.get(i).getMemo()==null ?"":String.valueOf(dataList.get(i).getMemo()));
        }
 
        return hssfWorkbook;
    }
    public void handleCancel() {
        try {
            if (this.selectedList == null
                    || this.selectedList.size() == 0) {
                FacesUtils.warn("请选择数据。");
                return;
            }
 
            for (OpeApply oa : this.selectedList) {
                if (!this.opeApplyService.isApplyPendingApproval(oa)) {
                    FacesUtils.warn("只有待审批状态的数据可以取消。");
                    return;
                }
            }
 
            for (OpeApply ope:this.selectedList) {
                this.opeApplyService.cancelApply(ApplyStatus.CANCEL,ope.getId());
            }
 
            FacesUtils.info("取消成功。");
        } catch (Exception e) {
            logger.error("操作失败。", e);
            FacesUtils.warn("操作失败。");
        }
    }
 
    public void onCancelBtnClick() {
        this.menuController.backToPage();
    }
 
    public void onResetBtnClick() {
        this.opeApply = new OpeApply();
        this.opeApply.setReagent(new SysReagent());
    }
 
    public void onSaveBtnClick() {
        try {
            // 新建
            if (this.action == Constants.ACTION_ADD) {
                if (this.opeApply == null) {
                    FacesUtils.warn("新建对象为空。");
                    return;
                }
                if (this.opeApply.getReagent() == null
                        || StringUtils.isBlank(this.opeApply.getReagent().getId())) {
                    FacesUtils.warn("请选择申购产品。");
                    return;
                }
 
                SysUser applyUser = this.getUser();
                if (applyUser == null) {
                    FacesUtils.warn("当前登陆用户信息未能找到。");
                    return;
                }
 
                this.opeApply.setApplyCode(this.sysSequenceService.getApplyCode());
                this.opeApply.setApplyUserId(applyUser.getId());
                this.opeApply.setStatus(ApplyStatus.PENDING_APPROVAL);
                this.opeApplyService.insertOpeApply(this.opeApply);
 
                FacesUtils.info("新建成功。");
                this.menuController.backToPage();
            } else if (this.action == Constants.ACTION_EDIT) {
                if (this.opeApply == null) {
                    FacesUtils.warn("修改对象为空。");
                    return;
                }
                if (this.opeApply.getReagent() == null
                        || StringUtils.isBlank(this.opeApply.getReagent().getId())) {
                    FacesUtils.warn("请选择申购产品。");
                    return;
                }
 
                this.opeApplyService.updateOpeApply(this.opeApply);
 
                FacesUtils.info("修改成功。");
                this.menuController.backToPage();
            }
        } catch (Exception e) {
            logger.error("操作失败。", e);
            FacesUtils.warn("操作失败。");
        }
    }
 
    public void onTabChange(TabChangeEvent event) {
        if ("reagent".equals(event.getTab().getId())) {
            tabValue = 0;
            if (isFlush){
                RequestContext.getCurrentInstance().execute("document.querySelector('.reagentSearch').click()");
                isFlush = false;
            }
        }else if ("applyHistory".equals(event.getTab().getId())){
            tabValue = 1;
        }else{
            tabValue = 2;
        }
    }
    public void onReagentNewBtnClick() {
        this.sysReagent = new SysReagent();
    }
 
    public void onReagentSaveBtnClick() {
        try {
            if (this.sysReagent == null) {
                FacesUtils.warn("新建对象为空。");
                return;
            }
 
            this.sysReagent.setId(IDUtils.uuid());
            this.sysReagentService.insertSysReagent(this.sysReagent);
 
            FacesUtils.info("新建成功。");
            RequestContext.getCurrentInstance().execute("PF('dialog').hide()");
        } catch (Exception e) {
            logger.error("操作失败。", e);
            FacesUtils.warn("操作失败。");
        }
    }
 
    public void onReagentRowSelect(SelectEvent event) {
        this.opeApply = new OpeApply();
        this.opeApply.setReagent((SysReagent) event.getObject());
 
        if (this.action == Constants.ACTION_ADD) {
            this.applyEditFlag = Constants.ACTION_ADD;
            RequestContext.getCurrentInstance().execute("PF('applyDialog').show()");
        }
    }
 
    public void onApplyHistoryRowSelect(SelectEvent event) {
        this.opeApply = new OpeApply();
        this.opeApply = (OpeApply) event.getObject();
 
        if (this.action == Constants.ACTION_ADD) {
            this.applyEditFlag = Constants.ACTION_ADD;
            RequestContext.getCurrentInstance().execute("PF('applyDialog').show()");
        }
    }
 
    public LazyDataModel<OpeApply> getDataModel() {
        if (this.dataModel == null) {
            this.dataModel = new LazyDataModel<OpeApply>() {
                @Override
                public List<OpeApply> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
                    List<OpeApply> list = null;
                    try {
                        int count = opeApplyService.getOpeApplyTotalCount(reagentName, startDeadline,controlProduct, endDeadline, status, getUserId(), null, queryCAS,null,applyUserName,(byte)1);
                        this.setRowCount(count);
                        if (count > 0) {
                            list = opeApplyService.getOpeApplyList(reagentName, startDeadline,controlProduct, endDeadline, status, getUserId(), null, queryCAS, first, pageSize,null,applyUserName,(byte)1);
                            for(int i=0;i<list.size();i++){
                                if(list.get(i).getBeforeApproveUserId()!=null&&!list.get(i).getBeforeApproveUserId().equals("")){
                                    String name=list.get(i).getApproveUserName();
                                    list.get(i).setFirst(list.get(i).getFirName());
                                    list.get(i).setSecond(name);
                                }else {
                                    list.get(i).setFirst(list.get(i).getApproveUserName());
                                    list.get(i).setSecond(list.get(i).getSecondUserName());
                                }
                            }
                        }
                    } catch (Exception e) {
                        logger.error(e);
                    }
                    selectedList = null;
                    return list;
                }
 
                @Override
                public OpeApply getRowData(String rowKey) {
//                    Iterator<OpeApply> iterator = this.iterator();
//                    if (iterator != null) {
//                        OpeApply oa = null;
//                        while (iterator.hasNext()) {
//                            oa = iterator.next();
//                            if (rowKey.equals(oa.getId())) {
//                                return oa;
//                            }
//                        }
//                    }
                    return opeApplyService.getOpeApply(rowKey);
                }
            };
        }
 
        return dataModel;
    }
 
    public LazyDataModel<SysReagent> getReagentDataModel() {
        if (this.reagentDataModel == null) {
            this.reagentDataModel = new LazyDataModel<SysReagent>() {
                @Override
                public List<SysReagent> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
                    List<SysReagent> list = null;
                    try {
                        int count = sysReagentService.reagentCount(getUser(), applyNewReagentName, applyNewCas, null, applyNewSn);
                        this.setRowCount(count);
                        if (count > 0) {
                            list = sysReagentService.reagentList(getUser(), applyNewReagentName, applyNewCas, null, applyNewSn, first, pageSize,count);
                        }
                    } catch (Exception e) {
                        logger.error(e);
                    }
                    return list;
                }
 
                @Override
                public SysReagent getRowData(String rowKey) {
//                    Iterator<SysReagent> iterator = this.iterator();
//                    if (iterator != null) {
//                        SysReagent sr = null;
//                        while (iterator.hasNext()) {
//                            sr = iterator.next();
//                            if (rowKey.equals(sr.getId())) {
//                                return sr;
//                            }
//                        }
//                    }
                    return sysReagentService.getRowData(rowKey);
                }
            };
        }
 
        return reagentDataModel;
    }
 
 
    public LazyDataModel<SysReagent> getReagentFavorDataModel() {
        if (this.reagentFavorDataModel == null) {
            this.reagentFavorDataModel = new LazyDataModel<SysReagent>() {
                @Override
                public List<SysReagent> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
                    List<SysReagent> list = null;
                    try {
                        int count = sysReagentService.favorCount(getUser(), null, null, null,null,1);
                        this.setRowCount(count);
                        if (count > 0) {
                            list = sysReagentService.favorList(getUser(),null, null,null, null,1, first, pageSize);
                        }
                    } catch (Exception e) {
                        logger.error(e);
                    }
                    return list;
                }
 
                @Override
                public SysReagent getRowData(String rowKey) {
//                    Iterator<SysReagent> iterator = this.iterator();
//                    if (iterator != null) {
//                        SysReagent sr = null;
//                        while (iterator.hasNext()) {
//                            sr = iterator.next();
//                            if (rowKey.equals(sr.getId())) {
//                                return sr;
//                            }
//                        }
//                    }
                    return sysReagentService.getRowData(rowKey);
                }
            };
        }
 
        return reagentFavorDataModel;
    }
 
    public LazyDataModel<OpeApply> getApplyHistoryDataModel() {
        if (this.applyHistoryDataModel == null) {
            this.applyHistoryDataModel = new LazyDataModel<OpeApply>() {
                @Override
                public List<OpeApply> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
                    List<OpeApply> list = null;
                    try {
                        String applyUserId = getUserId();
                        int count = 0;
                        if (StringUtils.isNotBlank(applyUserId)) {
                            count = opeApplyService.getOpeApplyTotalCount(null, null, null,null, null, applyUserId, null, null,applyCode,null,(byte)1);
                        }
                        this.setRowCount(count);
                        if (count > 0) {
                            list = opeApplyService.getOpeApplyList(null, null,null, null, null, applyUserId, null, null, first, pageSize,applyCode,null,(byte)1);
                        }
                        selectedList = null;
                    } catch (Exception e) {
                        logger.error(e);
                    }
                    return list;
                }
 
                @Override
                public OpeApply getRowData(String rowKey) {
//                    Iterator<OpeApply> iterator = this.iterator();
//                    if (iterator != null) {
//                        OpeApply oa = null;
//                        while (iterator.hasNext()) {
//                            oa = iterator.next();
//                            if (rowKey.equals(oa.getId())) {
//                                return oa;
//                            }
//                        }
//                    }
                    return opeApplyService.getOpeApply(rowKey);
                }
            };
        }
 
        return applyHistoryDataModel;
    }
 
    /**
     * 新建申购List_重置
     */
    public void onResetBtnClickForNewP() {
        this.applyNewList = new ArrayList<OpeApply>();
    }
 
    /**
     * 新建申购List_修改
     */
    public void onEditBtnClickForNewP() {
 
        if (this.applyNewSelectedList == null
                || this.applyNewSelectedList.size() == 0) {
            FacesUtils.warn("请选择数据。");
            return;
        }
        if (this.applyNewSelectedList.size() > 1) {
            FacesUtils.warn("只能选择一个数据进行修改。");
            return;
        }
 
        this.applyEditFlag = Constants.ACTION_EDIT;
        this.opeApply = this.applyNewSelectedList.get(0);
        RequestContext.getCurrentInstance().execute("PF('applyDialog').show()");
    }
 
    /**
     * 新建申购List_删除
     */
    public void onDeleteBtnClickForNewP() {
 
        if (this.applyNewSelectedList == null
                || this.applyNewSelectedList.size() == 0) {
            FacesUtils.warn("请选择数据。");
            return;
        }
 
        List<String> selectedIds = new ArrayList<String>();
 
        for (OpeApply oa : this.applyNewSelectedList) {
            selectedIds.add(oa.getReagent().getId());
        }
 
        Iterator<OpeApply> it = this.applyNewList.iterator();
 
        while (it.hasNext()) {
 
            OpeApply ope = it.next();
 
            if (selectedIds.contains(ope.getReagent().getId())) {
                it.remove();
            }
        }
 
        this.applyNewSelectedList = null;
 
        FacesUtils.info("删除成功。");
    }
 
    /**
     * 新建申购List_保存
     */
    public void onSaveBtnClickForNewP() {
 
        try {
            if (this.applyNewList == null || this.applyNewList.size() == 0) {
                FacesUtils.warn("新建对象为空。");
                return;
            }
 
            SysUser applyUser = this.getUser();
            if (applyUser == null) {
                FacesUtils.warn("当前登陆用户信息未能找到。");
                return;
            }
 
            //申请一个申购编号
            String applyCode = this.sysSequenceService.getApplyCode();
            for (OpeApply opeApply : this.applyNewList) {
                if (opeApply.getReagent().getValidFlag() == null
                        ||opeApply.getReagent().getValidFlag().equals(ValidFlag.INVALID)){
                    FacesUtils.warn(opeApply.getReagent().getName()+",该试剂已删除,请选择其他试剂");
                    return;
                }
                if (opeApply.getReagent().getType() == 1
                        && (opeApply.getReagent().getSupplierFlag() == null
                        || opeApply.getReagent().getSupplierFlag().equals(ValidFlag.INVALID))){
                    FacesUtils.warn(opeApply.getReagent().getName()+",该试剂供应商已删除,请选择其他试剂");
                    return;
                }
                opeApply.setId(IDUtils.uuid());
                opeApply.setApplyCode(applyCode);
                opeApply.setApplyUserId(applyUser.getId());
                opeApply.setStatus(ApplyStatus.PENDING_APPROVAL);
            }
 
            this.opeApplyService.insertOpeApplyList(this.applyNewList);
 
            FacesUtils.info("新建成功。");
            this.menuController.backToPage();
        } catch (Exception e) {
            logger.error("操作失败。", e);
            FacesUtils.warn("操作失败。");
        }
    }
 
    /**
     * 新建申购dialog_取消
     */
    public void onCancelBtnClickNew() {
 
        this.opeApply = new OpeApply();
        RequestContext.getCurrentInstance().execute("PF('applyDialog').hide()");
    }
 
    /**
     * 新建申购dialog_增加
     */
    public void onSaveBtnClickNew() {
 
        if (this.opeApply.getReagent() == null
                || StringUtils.isBlank(this.opeApply.getReagent().getId())) {
            FacesUtils.warn("请选择申购产品。");
            RequestContext.getCurrentInstance().execute("PF('applyDialog').hide()");
            return;
        }
 
        if (this.applyEditFlag == Constants.ACTION_ADD) {
            if (this.applyNewList.stream().filter(apply -> apply.getReagent().getId().equals(this.opeApply.getReagent().getId())).count() > 0) {
                FacesUtils.warn("该试剂已被增加!");
            } else {
 
                this.applyNewList.add(this.opeApply);
                FacesUtils.info("增加成功。");
            }
        }
 
        this.opeApply = new OpeApply();
        RequestContext.getCurrentInstance().execute("PF('applyDialog').hide()");
    }
 
 
    /**
     * 一键批量增加 按钮
     */
    public void onBatchSaveBtnClickNew() {
 
        if (this.selectedList.isEmpty()) {
            FacesUtils.warn("请选择申购产品。");
            return;
        }
 
        //检查是否试剂已经加入列表
        for(OpeApply opeapply:this.selectedList){
            if (this.applyNewList.stream().filter(apply -> apply.getReagent().getId().equals(opeapply.getReagent().getId())).count() > 0) {
                FacesUtils.warn("名为:"+opeapply.getReagent().getName()+"的试剂已被增加!");
                return;
            }
        }
 
        this.applyNewList.addAll(this.selectedList);
        FacesUtils.info("增加成功。");
    }
 
    /**
     * 审批者名称取得
     */
    public String getUserName(String approveUserId) {
 
        if (!StringUtils.isEmpty(approveUserId)) {
 
            for (SysUser user : this.approveUserSelectList) {
 
                if (user.getId().equals(approveUserId)) {
                    return user.getName();
                }
            }
        }
 
        return "";
    }
 
    public String getProjectName(String projectId) {
        String projectName = null;
        if (!CollectionUtils.isEmpty(this.sysProjectList)) {
            Optional<SysProject> first = this.sysProjectList.parallelStream().filter(sysProject -> StringUtils.isNotBlank(projectId) && StringUtils.isNotBlank(sysProject.getProjectId()) && projectId.equals(sysProject.getProjectId())).findFirst();
            if (first.isPresent()) {
                projectName = first.get().getProjectName();
            }
        }
        return projectName;
    }
 
    public void changeCharge(AjaxBehaviorEvent event) {
        this.sysProjectList = sysProjectService.getSysProjectList(this.opeApply.getApproveUserId());
    }
 
    public OpeApply getOpeApply() {
        return opeApply;
    }
 
    public void setOpeApply(OpeApply opeApply) {
        this.opeApply = opeApply;
    }
 
    public List<OpeApply> getSelectedList() {
        return selectedList;
    }
 
    public void setSelectedList(List<OpeApply> selectedList) {
        this.selectedList = selectedList;
    }
 
    public String getReagentName() {
        return reagentName;
    }
 
    public void setReagentName(String reagentName) {
        this.reagentName = reagentName;
    }
 
    public Timestamp getStartDeadline() {
        return startDeadline;
    }
 
    public void setStartDeadline(Timestamp startDeadline) {
        this.startDeadline = startDeadline;
    }
 
    public Timestamp getEndDeadline() {
        return endDeadline;
    }
 
    public void setEndDeadline(Timestamp endDeadline) {
        this.endDeadline = endDeadline;
    }
 
    public Integer getStatus() {
        return status;
    }
 
    public void setStatus(Integer status) {
        this.status = status;
    }
 
    public String getApplyNewReagentName() {
        return applyNewReagentName;
    }
 
    public void setApplyNewReagentName(String applyNewReagentName) {
        this.applyNewReagentName = applyNewReagentName;
    }
 
    public String getApplyNewCas() {
        return applyNewCas;
    }
 
    public void setApplyNewCas(String applyNewCas) {
        this.applyNewCas = applyNewCas;
    }
 
    public int getAction() {
        return action;
    }
 
    public SysReagent getSysReagent() {
        return sysReagent;
    }
 
    public void setSysReagent(SysReagent sysReagent) {
        this.sysReagent = sysReagent;
    }
 
    public List<ApplyStatus> getStatusSelectList() {
        if (this.statusSelectList == null) {
            this.statusSelectList = Arrays.asList(ApplyStatus.values());
        }
        return statusSelectList;
    }
 
    public List<SysUser> getApproveUserSelectList() {
        if (this.approveUserSelectList == null) {
            initApproveUserSelectList();
        }
        return approveUserSelectList;
    }
 
    public List<OpeApply> getApplyNewList() {
        return applyNewList;
    }
 
    public void setApplyNewList(List<OpeApply> applyNewList) {
        this.applyNewList = applyNewList;
    }
 
    public List<OpeApply> getApplyNewSelectedList() {
        return applyNewSelectedList;
    }
 
    public void setApplyNewSelectedList(List<OpeApply> applyNewSelectedList) {
        this.applyNewSelectedList = applyNewSelectedList;
    }
 
    public int getApplyEditFlag() {
        return applyEditFlag;
    }
 
    public String getQueryCAS() {
        return queryCAS;
    }
 
    public void setQueryCAS(String queryCAS) {
        this.queryCAS = queryCAS;
    }
 
    public List<SysProject> getSysProjectList() {
        return sysProjectList;
    }
 
    public void setSysProjectList(List<SysProject> sysProjectList) {
        this.sysProjectList = sysProjectList;
    }
 
    public String getApplyCode() {
        return applyCode;
    }
 
    public void setApplyCode(String applyCode) {
        this.applyCode = applyCode;
    }
 
 
    public int getTabValue() {
        return tabValue;
    }
 
    public void setTabValue(int tabValue) {
        this.tabValue = tabValue;
    }
 
    public String getControlProduct() {
        return controlProduct;
    }
 
    public void setControlProduct(String controlProduct) {
        this.controlProduct = controlProduct;
    }
}