李宇
2021-01-14 5ef5c096c23869fff7872cf498077d6924c679b2
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
package com.nanometer.smartlab.service;
 
import java.util.*;
 
import javax.annotation.Resource;
 
import com.google.common.collect.ImmutableMap;
import com.nanometer.smartlab.dao.*;
import com.nanometer.smartlab.entity.*;
import com.nanometer.smartlab.entity.enumtype.ValidFlag;
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 com.nanometer.smartlab.entity.enumtype.ArrivalStatus;
import com.nanometer.smartlab.entity.enumtype.OperateStatus;
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.springframework.util.Assert;
 
/**
 * Created by maweiqing on 17/12/12.
 */
@Service("opeReagentStatusService")
public class OpeReagentStatusServiceImpl implements OpeReagentStatusService {
 
    private static Logger logger = Logger.getLogger(OpeReagentStatusService.class);
 
    @Resource(name = "opeReagentStatusDao")
    OpeReagentStatusDao opeReagentStatusDao;
 
    @Resource
    private OpeWarehouseReserveService opeWarehouseReserveService;
    @Resource
    private OpeUseFlowService opeUseFlowService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysLaboratoryService sysLaboratoryService;
    @Resource
    private SysLaboratoryContainerDao sysLaboratoryContainerDao;
    @Resource
    private SysWarehouseContainerDao sysWarehouseContainerDao;
    @Resource
    private SysLaboratoryContainerService sysLaboratoryContainerService;
    @Resource
    private OpeUseFlowDao opeUseFlowDao;
    @Resource
    private OpeWarehouseReserveDao opeWarehouseReserveDao;
 
    @Resource(name="baseMetaDao")
    BaseMetaDao baseMetaDao;
 
    /*
     * (non-Javadoc)
     *
     * @see com.nanometer.smartlab.service.OpeReagentStatusService#
     * getOpeReagentStatusList(java.lang.String, java.lang.String,
     * java.lang.Integer, java.lang.String, java.lang.String, java.lang.Integer,
     * java.lang.Integer)
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public List<OpeReagentStatus> getOpeReagentStatusList(String reagentId, String articleNumber, Integer status,
            String reagentCode, String userId, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
 
            params.put("reagentId", reagentId);
            params.put("articleNumber", articleNumber);
            params.put("status", status);
            addParamByUserId(userId, params);
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.opeReagentStatusDao.getOpeReagentStatusList(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<OpeReagentStatus> getOpeReagentStatusListForLab(String name, String articleNumber, Integer status,
                                                          String reagentCode, String userId, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
 
            params.put("name", "%" + name + "%");
            params.put("articleNumber", articleNumber);
            params.put("status", status);
            addParamByUserId(userId, params);
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.opeReagentStatusDao.getOpeReagentStatusListForLab(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 getOpeReagentStatusTotalCountForLab(String name, String articleNumber, Integer status, String reagentCode,
                                             String userId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("name", "%" + name + "%");
 
            addParamByUserId(userId, params);
            params.put("articleNumber", articleNumber);
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
            params.put("status", status);
            return this.opeReagentStatusDao.getOpeReagentStatusTotalCountForLab(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 getOpeReagentStatusTotalCount(String reagentId, String articleNumber, Integer status, String reagentCode,
            String userId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("reagentId", reagentId);
 
            addParamByUserId(userId, params);
            params.put("articleNumber", articleNumber);
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
            params.put("status", status);
            return this.opeReagentStatusDao.getOpeReagentStatusTotalCount(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<OpeReagentStatus> getOpeReagentStatusListByName(String reagentId, String articleNumber, Integer status,
            String reagentCode, String userId, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
 
            params.put("reagentName", reagentId);
            params.put("articleNumber", articleNumber);
            params.put("status", status);
            addParamByUserId(userId, params);
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.opeReagentStatusDao.getOpeReagentStatusList(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 getOpeReagentStatusTotalCountByName(String reagentId, String articleNumber, Integer status, String reagentCode,
            String userId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("reagentName", reagentId);
 
            addParamByUserId(userId, params);
            params.put("articleNumber", articleNumber);
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
            params.put("status", status);
            return this.opeReagentStatusDao.getOpeReagentStatusTotalCount(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 getPersonReagentStatusTotalCountByName(String reagentId, String articleNumber, String reagentCode,
            String userId,String applyPerson,Date startTime,Date endTime) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("reagentName", reagentId);
 
            addParamByUserId(userId, params);
            params.put("articleNumber", articleNumber);
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
            params.put("applyPerson", applyPerson);
            params.put("startTime", startTime);
            params.put("endTime", endTime);
            return this.opeReagentStatusDao.getPersonReagentStatusTotalCount(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<OpeReagentStatus> getPersonReagentStatusListByName(String reagentId, String articleNumber,
            String reagentCode, String userId,String applyPerson,Date startTime,Date endTime, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("reagentName", reagentId);
            params.put("articleNumber", articleNumber);
            addParamByUserId(userId, params);
            if (StringUtils.isNotBlank(reagentCode)) {
                params.put("reagentCode", "%" + reagentCode + "%");
            }
            params.put("applyPerson", applyPerson);
            params.put("startTime", startTime);
            params.put("endTime", endTime);
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.opeReagentStatusDao.getPersonReagentStatusList(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<OpeReagentStatus> getLogOpeReagentStatusList(String reagentId, Integer status, String userId,
            String loginId, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
 
            params.put("reagentId", reagentId);
            addParamByLoginId(userId, loginId, params);
            params.put("status", status);
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.opeReagentStatusDao.getOpeReagentStatusList(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 getLogOpeReagentStatusTotalCount(String reagentId, Integer status, String userId, String loginId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("reagentId", reagentId);
            addParamByLoginId(userId, loginId, params);
            params.put("status", status);
            return this.opeReagentStatusDao.getOpeReagentStatusTotalCount(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    /**
     * 实验室库存管理用
     *
     * @param userId
     * @param params
     */
    private void addParamByUserId(String userId, Map<String, Object> params) {
        // 领用操作对话框的userid为空
        // 只有实验室管理时userid才不为空
        if (StringUtils.isNotBlank(userId)) {
            SysUser sysUser = sysUserService.getSysUser(userId);
 
//            // 不是管理员时,加入部门
//            if (sysUser.getSeeFlag().getKey() != SeeFlag.MANAGE.getKey()) {
//                params.put("department", sysUser.getDepartment());
//            }
 
            // 不是管理员时,加入userid
            //if (sysUser.getSeeFlag().getKey() == SeeFlag.MANAGE.getKey()||sysUser.getSeeFlag().getKey() == SeeFlag.LEADING.getKey()) {
            if (sysUser.getSeeFlag().getKey() == SeeFlag.MANAGE.getKey()) {
 
            } else {
                params.put("userId", userId);
            }
        }
    }
 
    /**
     * 个人管理、仓库库存管理用
     *
     * @param userId
     * @param loginId
     * @param params
     */
    private void addParamByLoginId(String userId, String loginId, Map<String, Object> params) {
        if (StringUtils.isNotBlank(loginId)) {
            SysUser sysUser = sysUserService.getSysUser(loginId);
 
            // 负责人,需要把部门的userid传递到SQL
            if (sysUser.getSeeFlag().getKey() == SeeFlag.LEADING.getKey()) {
                List<String> departmentUserIds = new ArrayList<String>();
                List<SysUser> departmentUsers = sysUserService.getSeeUserList(sysUser.getDepartment());
                for (SysUser departmentUser : departmentUsers) {
                    departmentUserIds.add(departmentUser.getId());
                }
 
                if (departmentUserIds.size() > 0) {
                    params.put("departmentUserIds", departmentUserIds);
                }
 
                if (StringUtils.isNotBlank(userId)) {
                    params.put("userId", userId);
                }
 
            } else if (sysUser.getSeeFlag().getKey() == SeeFlag.NORMAL.getKey()) {// 管理员,部门的userid和个人id都不需要
                params.put("userId", userId);
            } else {
                if (StringUtils.isNotBlank(userId)) {
                    params.put("userId", userId);
                }
            }
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public OpeReagentStatus insertOpeReagentStatus(OpeReagentStatus opeReagentStatus) {
        try {
            opeReagentStatus.setId(IDUtils.uuid());
            this.opeReagentStatusDao.insertOpeReagentStatus(opeReagentStatus);
            return opeReagentStatus;
        } 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
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean isOpeReagentStatusExist(String reagentCode) {
        try {
            OpeReagentStatus status = this.opeReagentStatusDao.getOpeReagentStatusByReagentCode(reagentCode);
            return status != null;
 
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public OpeReagentStatus getOpeReagentStatusByCode(String reagentCode){
        try {
            return this.opeReagentStatusDao.getOpeReagentStatusByReagentCode(reagentCode);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public boolean isAllowWarehouseUse(OpeReagentStatus opeReagentStatus) {
        return opeReagentStatus != null && opeReagentStatus.getStatus() != null
                && opeReagentStatus.getStatus().getKey() == ArrivalStatus.WAREHOUSE.getKey();
    }
 
    public boolean isAllowWarehouseUseByIds(List<String> list) {
        boolean flag = true;
        if (list != null && list.size() > 0) {
            for (String opeReagentStatusId : list) {
                OpeReagentStatus opeReagentStatus = getOpeReagentStatus(opeReagentStatusId);
                if (!isAllowWarehouseUse(opeReagentStatus)) {
                    flag = false;
                    break;
                }
            }
        } else {
            flag = false;
        }
 
        return flag;
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateReagentStatus2(OpeReagentStatus opeReagentStatus){
        try {
 
            if (isAllowWarehouseUse(opeReagentStatus)) {
                // 减少库存
                OpeWarehouseReserve opeWarehouseReserve = this.opeWarehouseReserveService.getOpeWarehouseReserve(
                        opeReagentStatus.getReagent().getId(), opeReagentStatus.getArticleNumber());
                opeWarehouseReserve.setReserve(opeWarehouseReserve.getReserve() - 1);
                this.opeWarehouseReserveService.updateOpeWarehouseReserve(opeWarehouseReserve);
            }
 
            //opeReagentStatus.setStatus(ArrivalStatus.NOREGISTER);
 
//            opeReagentStatus.setHouseId(null);
//            opeReagentStatus.setContainerId(null);
            int row = this.opeReagentStatusDao.updateOpeReagentStatusDao(opeReagentStatus);
 
            if (row == 0) {
                return false;
            }
 
            OpeUseFlow opeUseFlow = new OpeUseFlow();
            opeUseFlow.setReagentCode(opeReagentStatus.getReagentCode());
            opeUseFlow.setStatus(opeReagentStatus.getStatus());
            opeUseFlow.setHouseId(opeReagentStatus.getHouseId());
            opeUseFlow.setContainerId(opeReagentStatus.getContainerId());
            opeUseFlow.setUserId(opeReagentStatus.getUserId());
            opeUseFlow.setPlace(opeReagentStatus.getPlace());
            opeUseFlow.setRemainder(opeReagentStatus.getRemainder());
 
            Map<String, String> metaMap = new HashMap<>();
            metaMap.put("groupId", "operate_status");
            metaMap.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey()));
            List<BaseMeta> baseMetaList = baseMetaDao.getBaseMetaList(metaMap);
            opeUseFlow.setOperateState(baseMetaList.get(0).getId());
 
            this.opeUseFlowService.insertOpeUseFlow(opeUseFlow);
 
            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 updateReagentStatus(OpeReagentStatus opeReagentStatus) {
        try {
 
            if (isAllowWarehouseUse(opeReagentStatus)) {
                // 减少库存
                OpeWarehouseReserve opeWarehouseReserve = this.opeWarehouseReserveService.getOpeWarehouseReserve(
                        opeReagentStatus.getReagent().getId(), opeReagentStatus.getArticleNumber());
                opeWarehouseReserve.setReserve(opeWarehouseReserve.getReserve() - 1);
                this.opeWarehouseReserveService.updateOpeWarehouseReserve(opeWarehouseReserve);
            }
 
            opeReagentStatus.setStatus(ArrivalStatus.NOREGISTER);
 
//            opeReagentStatus.setHouseId(null);
//            opeReagentStatus.setContainerId(null);
            int row = this.opeReagentStatusDao.updateOpeReagentStatusDao(opeReagentStatus);
 
            if (row == 0) {
                return false;
            }
 
            OpeUseFlow opeUseFlow = new OpeUseFlow();
            opeUseFlow.setReagentCode(opeReagentStatus.getReagentCode());
            opeUseFlow.setStatus(opeReagentStatus.getStatus());
            opeUseFlow.setHouseId(opeReagentStatus.getHouseId());
            opeUseFlow.setContainerId(opeReagentStatus.getContainerId());
            opeUseFlow.setUserId(opeReagentStatus.getUserId());
            opeUseFlow.setPlace(opeReagentStatus.getPlace());
            opeUseFlow.setRemainder(opeReagentStatus.getRemainder());
 
            Map<String, String> metaMap = new HashMap<>();
            metaMap.put("groupId", "operate_status");
            metaMap.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey()));
            List<BaseMeta> baseMetaList = baseMetaDao.getBaseMetaList(metaMap);
            opeUseFlow.setOperateState(baseMetaList.get(0).getId());
 
            this.opeUseFlowService.insertOpeUseFlow(opeUseFlow);
 
            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 OpeReagentStatus getOpeReagentStatus(String id) {
        try {
            return this.opeReagentStatusDao.getOpeReagentStatus(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 OpeReagentStatus getOpeReagentStatusByReagentCode2(OpeReagentStatus opeReagentStatus) {
        try {
            OpeReagentStatus status = this.opeReagentStatusDao.getOpeReagentStatusByReagentCode(opeReagentStatus.getReagentCode());
            return status;
 
        } 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 OpeReagentStatus getOpeReagentStatusByReagentCode(String reagentCode) {
        try {
            OpeReagentStatus status = this.opeReagentStatusDao.getOpeReagentStatusByReagentCode(reagentCode);
            return status;
 
        } 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 void putReagentInLaboratory(String reagentCode, String barCode, String containerCode, SysUser sysUser) {
        try {
            // 检查试剂条码是否存在
            OpeReagentStatus opeReagentStatus = this.getOpeReagentStatusByReagentCode(reagentCode);
            if (opeReagentStatus == null) {
                throw new BusinessException(ExceptionEnumCode.PARAM_NO_EXIST,
                        MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_NO_EXIST.getCode(), "试剂条码", reagentCode));
            }
 
            // 检查地点条码是否存在
            SysLaboratory sysLaboratory = this.sysLaboratoryService.getSysLaboratoryByBarCode(barCode);
            if (sysLaboratory == null) {
                throw new BusinessException(ExceptionEnumCode.PARAM_NO_EXIST,
                        MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_NO_EXIST.getCode(), "地点条码", barCode));
            }
 
            // 检查货柜条码是否存在
            SysLaboratoryContainer sysLaboratoryContainer = this.sysLaboratoryContainerService
                    .getSysLaboratoryContainerByContainerCode(containerCode);
            if (sysLaboratoryContainer == null) {
                throw new BusinessException(ExceptionEnumCode.PARAM_NO_EXIST, MessageUtil
                        .getMessageByCode(ExceptionEnumCode.PARAM_NO_EXIST.getCode(), "货柜条码", containerCode));
            }
 
            // 检查货柜是否属于该地点
            if (!sysLaboratory.getId().equals(sysLaboratoryContainer.getLaboratoryId())) {
                throw new BusinessException(ExceptionEnumCode.PARAM_ERR, "货柜不属于该地点");
            }
 
            opeReagentStatus.setStatus(ArrivalStatus.LABORATORY);
            opeReagentStatus.setHouseId(sysLaboratory.getId());
            opeReagentStatus.setContainerId(sysLaboratoryContainer.getId());
            opeReagentStatus.setUserId(sysUser.getId());
            opeReagentStatus.setPlace(null);
            this.opeReagentStatusDao.updateOpeReagentStatusDao(opeReagentStatus);
 
            OpeUseFlow opeUseFlow = new OpeUseFlow();
            opeUseFlow.setReagentCode(opeReagentStatus.getReagentCode());
            opeUseFlow.setStatus(opeReagentStatus.getStatus());
            opeUseFlow.setHouseId(opeReagentStatus.getHouseId());
            opeUseFlow.setContainerId(opeReagentStatus.getContainerId());
            opeUseFlow.setUserId(opeReagentStatus.getUserId());
            opeUseFlow.setPlace(opeReagentStatus.getPlace());
            opeUseFlow.setRemainder(opeReagentStatus.getRemainder());
            // 接口应新追加操作状态和试剂状态两个参数。
 
            this.opeUseFlowService.insertOpeUseFlow(opeUseFlow);
 
        } 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 store(List<OpeReagentStatus> selectedStoreList, String laboratoryId, String laboratoryContainerId) {
        try {
            for (OpeReagentStatus opeReagentStatus : selectedStoreList) {
                opeReagentStatus.setStatus(ArrivalStatus.LABORATORY);
                opeReagentStatus.setHouseId(laboratoryId);
                opeReagentStatus.setContainerId(laboratoryContainerId);
                int row = this.opeReagentStatusDao.updateOpeReagentStatusDao(opeReagentStatus);
                if (row == 0) {
                    return false;
                }
 
                OpeUseFlow opeUseFlow = new OpeUseFlow();
                opeUseFlow.setReagentCode(opeReagentStatus.getReagentCode());
                opeUseFlow.setStatus(opeReagentStatus.getStatus());
                opeUseFlow.setHouseId(opeReagentStatus.getHouseId());
                opeUseFlow.setContainerId(opeReagentStatus.getContainerId());
                opeUseFlow.setUserId(opeReagentStatus.getUserId());
                opeUseFlow.setPlace(opeReagentStatus.getPlace());
                opeUseFlow.setRemainder(opeReagentStatus.getRemainder());
 
                Map<String, String> metaMap = new HashMap<>();
                metaMap.put("groupId", "operate_status");
                metaMap.put("metaKey", String.valueOf(OperateStatus.LABORATORYIN.getKey()));
                List<BaseMeta> baseMetaList = baseMetaDao.getBaseMetaList(metaMap);
                opeUseFlow.setOperateState(baseMetaList.get(0).getId());
 
                this.opeUseFlowService.insertOpeUseFlow(opeUseFlow);
            }
            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
    @Transactional(readOnly = true)
    public List<OpeReagentStatus> getOpeReagentStatusInfoList(String startTime,String endTime, String containerid, String status){
        Map params = ImmutableMap.of("startTime",startTime,"endTime",endTime,"containerid",containerid,"status",status);
        return opeReagentStatusDao.getOpeReagentStatusInfoList(params);
    }
 
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void syncOpeReagentStatus(List<LinkedHashMap> reagentStatusList){
        Assert.notNull(reagentStatusList,"请求参数不能为空!");
        for (Map params : reagentStatusList) {
            //<!--0:个人领用 1:在仓库sys_warehouse_container 2:在实验室sys_laboratory_container 3:报废-->
            if(params.get("idcard") != null){
                SysUser sysUser = sysUserService.getSysUserByIdCard((String)params.get("idcard"));
                if(Objects.isNull(sysUser)){
                    logger.error("The idcard:" + (String) params.get("idcard") + " has not been exists!");
                    continue;
                }
                params.put("userId",sysUser.getId());
            } else {
                logger.error("The idcard is empty!");
                continue;
            }
            //0:个人领用 1:在库 3:报废
            if(params.get("status").toString().equals("1")){
                SysWarehouseContainer container = sysWarehouseContainerDao.getSysWarehouseContainerByContainerCode((String)params.get("containerCode"));
                if(Objects.isNull(container)==false){
                    params.put("status",1);
                    params.put("containerId",container.getId());
                    params.put("houseId",container.getWarehouseId());
                }else{
                    SysLaboratoryContainer sysLaboratoryContainer = sysLaboratoryContainerDao.getSysLaboratoryContainerByContainerCode((String)params.get("containerCode"));
                    if(Objects.isNull(sysLaboratoryContainer)){
                        logger.error("The containerCode has not been exists!");
                        continue;
                    }
                    params.put("status",2);
                    params.put("containerId",sysLaboratoryContainer.getId());
                    params.put("houseId",sysLaboratoryContainer.getLaboratoryId());
                }
            }
            opeReagentStatusDao.syncOpeReagentStatus(params);
        }
    }
 
    @Override
    public void deleteByReagentCode(String reagentCode) {
        this.opeReagentStatusDao.deleteByReagentCode(reagentCode);
    }
 
    @Override
    public List<OpeReagentStatus> getReagentStatusByContainerCode(String containerCode) {
        try{
            SysLaboratoryContainer sysLaboratoryContainer = sysLaboratoryContainerDao.getSysLaboratoryContainerByContainerCode(containerCode);
            if(sysLaboratoryContainer != null){
                List<OpeReagentStatus> list = opeReagentStatusDao.getReagentStatusByContainerId(sysLaboratoryContainer.getId());
                return list;
            }
 
 
            SysWarehouseContainer sysWarehouseContainer = sysWarehouseContainerDao.getSysWarehouseContainerByContainerCode(containerCode);
            if(sysWarehouseContainer != null){
                List<OpeReagentStatus> list = opeReagentStatusDao.getReagentStatusByContainerId(sysWarehouseContainer.getId());
                return list;
            }
 
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR,
                    MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
        return null;
    }
 
    @Override
    public void updateTimeByCode(String code) {
        Map<String, String> params = new HashMap<>();
        params.put("code", code);
        opeReagentStatusDao.updateTimeByCode(params);
    }
 
    /**
    *@description  -1为入库,1为认领
    */
    @Override
    @Transactional
    public void batchUpdateStatusByCode(List<OpeReagentStatus> list) throws Exception {
        if(list == null || list.size() <1){
            throw new Exception("没有选择试剂");
        }
 
        for (OpeReagentStatus opeReagentStatus : list) {
            //变更试剂状态为入库
            Map<String, Object> params1 = new HashMap<>();
            String reagentCode = opeReagentStatus.getReagentCode();
            params1.put("code", reagentCode);
            params1.put("status",ArrivalStatus.WAREHOUSE);
            opeReagentStatusDao.updateStatusByCode(params1);
 
            //删除试剂流向记录
            Map<String, Object> params2 = new HashMap<>();
            params2.put("validFlag", ValidFlag.INVALID);
            params2.put("reagentCode", reagentCode);
            params2.put("status", ArrivalStatus.NOREGISTER);
            opeUseFlowDao.deleteByReagentCodeAndStatus(params2);
 
            //增加数据库存
            Map<String, Object> params3 = new HashMap<>();
            String reagentId = opeReagentStatus.getReagentId();
            String articleNumber = opeReagentStatus.getArticleNumber();
            params3.put("reagentId", reagentId);
            params3.put("articleNumber", articleNumber);
            opeWarehouseReserveDao.updateCount(params3);
 
        }
 
 
    }
 
    @Override
    public List<OpeReagentStatus> selectByReId(String id) {
        return  this.opeReagentStatusDao.selectByReId(id);
    }
 
    @Override
    public void updateByReId(String newReId, String oldReId) {
        Map<String, Object> params=new HashMap();
        params.put("newReId",newReId);
        params.put("oldReId",oldReId);
        this.opeReagentStatusDao.updateByReId(params);
    }
 
    @Override
    public List<String> getReagentCodes(String reagentId) {
        return opeReagentStatusDao.selectReagentCodesByReId(reagentId);
    }
 
    @Override
    public void updateOpeReagentStatus(OpeReagentStatus opeReagentStatus) {
        opeReagentStatusDao.updateOpeReagentStatusDao(opeReagentStatus);
    }
 
 
}