gdg
2021-01-18 12714c71c7737e21c3268b44a39f8f02befc5cb5
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
package com.nanometer.smartlab.service;
 
import com.nanometer.smartlab.dao.OpeApplyDao;
import com.nanometer.smartlab.entity.OpeApply;
import com.nanometer.smartlab.entity.OpeApplyReserve;
import com.nanometer.smartlab.entity.OpeOrder;
import com.nanometer.smartlab.entity.SysUser;
import com.nanometer.smartlab.entity.enumtype.ApplyStatus;
import com.nanometer.smartlab.entity.enumtype.SeeFlag;
import com.nanometer.smartlab.exception.AlarmCode;
import com.nanometer.smartlab.exception.AlarmException;
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.exception.ExceptionEnumCode;
import com.nanometer.smartlab.util.IDUtils;
import com.nanometer.smartlab.util.MessageUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * Created by johnny on 17/12/12.
 */
@Service("opeApplyService")
public class OpeApplyServiceImpl implements OpeApplyService {
 
    private static Logger logger = Logger.getLogger(OpeApplyService.class);
 
    @Resource(name = "opeApplyDao")
    OpeApplyDao opeApplyDao;
    @Resource
    OpeOrderService opeOrderService;
    @Resource
    OpeReagentStatusService opeReagentStatusService;
    @Resource
    OpeWarehouseReserveService opeWarehouseReserveService;
 
    @Resource
    private SysUserService sysUserService;
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<OpeApply> getOpeApplyList(String reagentName, Timestamp startDeadline, Timestamp endDeadline, Integer status,
                                          String applyUserId, String approveUserId, String queryCas,
                                          Integer first, Integer pageSize,String applyCode,String applyUserName) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(reagentName)) {
                params.put("reagentName", "%" + reagentName + "%");
            }
            params.put("startDeadline", startDeadline);
            params.put("endDeadline", endDeadline);
            params.put("status", status);
 
            if (StringUtils.isNotBlank(applyUserId)) {
                SysUser sysUser = sysUserService.getSysUser(applyUserId);
                // 不是管理员时,加入applyUserId
                if (sysUser.getSeeFlag().getKey() != SeeFlag.MANAGE.getKey()) {
                     params.put("applyUserId", applyUserId);
                }
 
                if("连悦".equals(sysUser.getName())){
                    params.remove("applyUserId");
                }
            }
 
            params.put("approveUserId", approveUserId);
            if (StringUtils.isNotBlank(queryCas)) {
                params.put("cas", "%" + queryCas + "%");
            }
            params.put("first", first);
            params.put("pageSize", pageSize);
            params.put("applyCode", applyCode);
            params.put("applyUserName", applyUserName);
            return this.opeApplyDao.getOpeApplyList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<OpeApply> getOpeApplyListForApproval(String applyUserName,String approvaUserName,String reagentName, Timestamp startDeadline, Timestamp endDeadline, Integer status,
                                          String applyUserId, String approveUserId, String queryCas,
                                          Integer first, Integer pageSize,String applyCode,String searchApproval) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(reagentName)) {
                params.put("reagentName", "%" + reagentName + "%");
            }
            params.put("startDeadline", startDeadline);
            params.put("endDeadline", endDeadline);
            params.put("status", status);
            params.put("searchApproval",searchApproval);
 
            if (StringUtils.isNotBlank(approveUserId)) {
                SysUser sysUser = sysUserService.getSysUser(approveUserId);
                // 不是管理员时,加入applyUserId
                if (sysUser.getSeeFlag().getKey() != SeeFlag.MANAGE.getKey()) {
                    params.put("approveUserId", approveUserId);
                }
            }
 
            params.put("applyUserId", applyUserId);
            if (StringUtils.isNotBlank(queryCas)) {
                params.put("cas", "%" + queryCas + "%");
            }
            if (StringUtils.isNotBlank(approvaUserName)) {
                params.put("approvaUserName", "%" + approvaUserName + "%");
            }
            if (StringUtils.isNotBlank(applyUserName)) {
                params.put("applyUserName", "%" + applyUserName + "%");
            }
            params.put("first", first);
            params.put("pageSize", pageSize);
            params.put("applyCode", applyCode);
            return this.opeApplyDao.getOpeApplyList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public int getOpeApplyTotalCountForApproval(String applyUserName,String approvaUserName,String reagentName, Timestamp startDeadline, Timestamp endDeadline, Integer status,
                                     String applyUserId, String approveUserId, String queryCas,String applyCode,String searchApproval) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(reagentName)) {
                params.put("reagentName", "%" + reagentName + "%");
            }
            params.put("startDeadline", startDeadline);
            params.put("endDeadline", endDeadline);
            params.put("status", status);
            params.put("searchApproval",searchApproval);
            if (StringUtils.isNotBlank(approveUserId)) {
                SysUser sysUser = sysUserService.getSysUser(approveUserId);
                // 不是管理员时,加入applyUserId
                if (sysUser.getSeeFlag().getKey() != SeeFlag.MANAGE.getKey()) {
                    params.put("approveUserId", approveUserId);
                }
            }
            if (StringUtils.isNotBlank(approvaUserName)) {
                params.put("approvaUserName", "%" + approvaUserName + "%");
            }
            if (StringUtils.isNotBlank(applyUserName)) {
                params.put("applyUserName", "%" + applyUserName + "%");
            }
            params.put("applyUserId", applyUserId);
 
            if (StringUtils.isNotBlank(queryCas)) {
                params.put("cas", "%" + queryCas + "%");
            }
            params.put("applyCode", applyCode);
            return this.opeApplyDao.getOpeApplyTotalCount(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public int getOpeApplyTotalCount(String reagentName, Timestamp startDeadline, Timestamp endDeadline, Integer status,
                                     String applyUserId, String approveUserId, String queryCas,String applyCode,String applyUserName) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(reagentName)) {
                params.put("reagentName", "%" + reagentName + "%");
            }
            params.put("startDeadline", startDeadline);
            params.put("endDeadline", endDeadline);
            params.put("status", status);
 
            if (StringUtils.isNotBlank(applyUserId)) {
                SysUser sysUser = sysUserService.getSysUser(applyUserId);
                // 不是管理员时,加入applyUserId
                if (sysUser.getSeeFlag().getKey() != SeeFlag.MANAGE.getKey()) {
                     params.put("applyUserId", applyUserId);
                }
 
                if("连悦".equals(sysUser.getName())){
                    params.remove("applyUserId");
                }
            }
 
            params.put("approveUserId", approveUserId);
            params.put("applyUserName", applyUserName);
            if (StringUtils.isNotBlank(queryCas)) {
                params.put("cas", "%" + queryCas + "%");
            }
            params.put("applyCode", applyCode);
            return this.opeApplyDao.getOpeApplyTotalCount(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public OpeApply getOpeApply(String id) {
        try {
            return this.opeApplyDao.getOpeApply(id);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public OpeApply insertOpeApply(OpeApply opeApply) {
        try {
            opeApply.setId(IDUtils.uuid());
            this.opeApplyDao.insertOpeApply(opeApply);
            return opeApply;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<OpeApply> insertOpeApplyList(List<OpeApply> opeApplyList) {
        try {
            this.opeApplyDao.insertOpeApplyList(opeApplyList);
            return opeApplyList;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateOpeApply(OpeApply opeApply) {
        try {
            int row = this.opeApplyDao.updateOpeApply(opeApply);
 
            if (row == 0) {
                return false;
            }
            return true;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateFirst(String id) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("id",id);
            int row = this.opeApplyDao.updateFirst(params);
 
            if (row == 0) {
                return false;
            }
            return true;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateFirst2(String id) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("id",id);
            int row = this.opeApplyDao.updateFirst2(params);
 
            if (row == 0) {
                return false;
            }
            return true;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateOpeApplyProject(String newObjective,String oldObjective) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("newObjective", newObjective);
            params.put("oldObjective", oldObjective);
            int row = this.opeApplyDao.updateOpeApplyProject(params);
 
            return true;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Override
    public boolean updateSelectedOpeApplyStatus(String memo, List<String> applyIds, String adminUserId, List<String> adminApplyIds,List<String> firstUserIds) {
        boolean apply = false, adminApply = false;
        if (!CollectionUtils.isEmpty(applyIds)) {
            apply = this.updateOpeApplyStatus(ApplyStatus.APPROVED, memo, applyIds);
        }
        if (!CollectionUtils.isEmpty(adminApplyIds)) {
            adminApply = this.updateOpeApplyAdminStatus2(memo, adminApplyIds, adminUserId,firstUserIds);
        }
        return apply || adminApply;
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateOpeApplyStatus(ApplyStatus status, String memo, List<String> applyIds) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("status", status.getKey());
            params.put("memo", memo);
            params.put("applyIds", applyIds);
 
            int row = this.opeApplyDao.updateOpeApplyStatus(params);
 
            if (row == 0) {
                return false;
            }
 
            return true;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateOpeApplyAdminStatus2(String memo, List<String> applyIds, String adminUserId,List<String> firstUserIds) {
        try {
            for(int i=0;i<applyIds.size();i++){
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("userId", adminUserId);
                params.put("memo", memo);
                params.put("id", applyIds.get(i));
                params.put("beforeUserId",firstUserIds.get(i));
                int row = this.opeApplyDao.updateOpeApplyAdmin2(params);
                if (row == 0) {
                    return false;
                }
            }
 
 
            return true;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateOpeApplyAdminStatus(String memo, List<String> applyIds, String adminUserId,List<String> firstUserIds) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("userId", adminUserId);
            params.put("memo", memo);
            params.put("applyIds", applyIds);
            params.put("firstUserIds",firstUserIds);
            int row = this.opeApplyDao.updateOpeApplyAdmin(params);
 
            if (row == 0) {
                return false;
            }
 
            return true;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteOpeApply(List<OpeApply> opeApplyList) {
        try {
            if (opeApplyList == null || opeApplyList.size() == 0) {
                return false;
            }
 
            List<String> ids = new ArrayList<String>();
            for (OpeApply opeApply : opeApplyList) {
                ids.add(opeApply.getId());
            }
 
            int row = this.opeApplyDao.deleteOpeApply(ids);
            if (row == 0) {
                return false;
            }
 
            return true;
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    public boolean isApplyPendingApproval(OpeApply opeApply) {
        if (opeApply != null
                && opeApply.getStatus() != null
                && opeApply.getStatus().getKey() == ApplyStatus.PENDING_APPROVAL.getKey()) {
            return true;
        }
 
        return false;
    }
 
    public boolean isApplyApproved(OpeApply opeApply) {
        if (opeApply != null
                && opeApply.getStatus() != null
                && opeApply.getStatus().getKey() == ApplyStatus.APPROVED.getKey()) {
            return true;
        }
 
        return false;
    }
 
    public boolean isAllowApplyCancel(OpeApply opeApply) {
        if (this.isApplyPendingApproval(opeApply)
                || this.isApplyApproved(opeApply)) {
            return true;
        }
 
        return false;
    }
 
    @Override
    public OpeApply selectOpeApplyByPrimaryKey(String id) {
        try {
            OpeApply info = this.opeApplyDao.selectOpeApplyByPrimaryKey(id);
            return info;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Override
    public int getOpeApplyReserveTotalCountByName(String id,String reagentName, String personName) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("reagentName", reagentName);
            params.put("personName", personName);
            params.put("userId",id);
            return this.opeApplyDao.getOpeApplyReserveTotalCount(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public int getOpeApplyReserveTotalCountByNameFor(String id,String reagentName, String personName) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("reagentName", reagentName);
            params.put("personName", personName);
            params.put("userId",id);
            return this.opeApplyDao.getOpeApplyReserveTotalCountFor(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
    @Override
    public List<OpeApplyReserve> getOpeApplyReserveListByNameFor(String id,String reagentName, String personName, Integer first,
                                                              Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("reagentName", reagentName);
            params.put("personName", personName);
            params.put("userId",id);
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.opeApplyDao.getOpeApplyReserveListFor(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
    @Override
    public List<OpeApplyReserve> getOpeApplyReserveListByName(String id,String reagentName, String personName, Integer first,
            Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("reagentName", reagentName);
            params.put("personName", personName);
            params.put("userId",id);
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.opeApplyDao.getOpeApplyReserveList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updaetOpeApplyPrice(BigDecimal applyPrice, String id){
        try{
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("applyPrice", applyPrice);
            params.put("id", id);
            int row =  this.opeApplyDao.updaetOpeApplyPrice(params);
            if (row == 0) {
                return false;
            }
            return true;
        }catch (DataAccessException e){
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updaetOpeApplyStockFlag(int stockFlag, String id){
        try{
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("stockFlag", stockFlag);
            params.put("id", id);
            int row =  this.opeApplyDao.updaetOpeApplyStockFlag(params);
            if (row == 0) {
                return false;
            }
            return true;
        }catch (DataAccessException e){
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean cancelApply(ApplyStatus status,String id){
        try{
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("status", status.getKey());
            params.put("id", id);
            int row =  this.opeApplyDao.cancelApply(params);
            if (row == 0) {
                return false;
            }
            return true;
        }catch (DataAccessException e){
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<OpeApply>selectByReId(String id){
        return this.opeApplyDao.selectByReId(id);
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateByReId(String newReId,String oldReId){
        Map<String, Object> params=new HashMap();
        params.put("newReId",newReId);
        params.put("oldReId",oldReId);
        this.opeApplyDao.updateByReId(params);
    }
 
    @Override
    public OpeApplyReserve getOpeApplyDetail(String id) {
        return opeApplyDao.getOpeApplyDetail(id);
    }
 
    @Override
    public void updateOpeApplyInfo(ApplyStatus storage, String consigneeId, Timestamp arrivalTime, String applyId) {
        Map<String,Object> params = new HashMap();
        params.put("status", storage);
        params.put("consigneeId", consigneeId);
        params.put("arrivalTime", arrivalTime);
        params.put("applyId", applyId);
        opeApplyDao.updateOpeApplyInfo(params);
    }
 
    @Override
    @Transactional
    public void orderInputWarehouse(List<OpeApplyReserve> opeApplyList,String consigneeId,Timestamp arrivalTime) {
        for (OpeApplyReserve opeApplyReserve:opeApplyList) {
            //0.1获取该申购单的订单
            OpeOrder oo = opeOrderService.getOrder(opeApplyReserve.getId());
            if (oo == null) {
                throw new BusinessException(ExceptionEnumCode.ORDER_ERROR,"订单不存在");
            }
            //0.2检查条码->得到试剂条码列表
            List<String> codeList = opeReagentStatusService
                    .checkReagentCode(opeApplyReserve.getStartReagentCode2(), opeApplyReserve.getEndReagentCode2(), opeApplyReserve.getArrivalNum());
            //1.入库
            if (codeList == null){
                //无条码入库 不需要增加状态和流向
                //1.1库存表增加
                opeWarehouseReserveService.insertOpeWarehouseReserve2(opeApplyReserve, oo);
            }else{
                //条码入库
                assert codeList.size() > 0;
                //1.1入库(库存表增加)
                opeWarehouseReserveService.insertOpeWarehouseReserve2(opeApplyReserve, oo);
                //1.2试剂状态表增加,流向增加
                opeReagentStatusService.orderInputWarehouseReagentStatusAndUseFlow(opeApplyReserve,consigneeId,codeList,oo);
            }
            //2.更改申购单所属订单状态(判断子订单是否全部完成)
            //2.1更改该申购单的状态,收货人和到货时间
            this.updateOpeApplyInfo(ApplyStatus.STORAGE,consigneeId,arrivalTime, opeApplyReserve.getId());
            //2.2更改订单状态
            List<OpeApply> oas = opeOrderService.getOpeApplyListByOrder(oo.getId());
            assert oas.size() > 0;
            //子订单是否全部入库,是则父单为完成,否则未完成
            boolean flag = oas.stream().allMatch(oa -> oa.getStatus() == ApplyStatus.STORAGE);
            oo.setConsigneeUserId(consigneeId);
            oo.setArrivalTime(arrivalTime);
            if (flag){
                oo.setStatus(ApplyStatus.FINISHED);
                opeOrderService.updateOpeOrderStatus(oo);
            }else{
                oo.setStatus(ApplyStatus.UNCOMPLETED);
                opeOrderService.updateOpeOrder(oo);
            }
 
        }
    }
 
}