16639036659
2023-08-28 7a9a4f4764b0a402e3d0b4a1dafc7ecf96cb1583
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
package com.gkhy.labRiskManage.domain.experiment.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.gkhy.labRiskManage.application.experiment.dto.bo.ExperimentAppInsertBO;
import com.gkhy.labRiskManage.commons.domain.SearchResult;
import com.gkhy.labRiskManage.commons.enums.ResultCode;
import com.gkhy.labRiskManage.commons.enums.StatusEnum;
import com.gkhy.labRiskManage.commons.enums.UserRoleEnum;
import com.gkhy.labRiskManage.commons.enums.UserTagEnum;
import com.gkhy.labRiskManage.commons.exception.BusinessException;
import com.gkhy.labRiskManage.commons.model.PageQuery;
import com.gkhy.labRiskManage.commons.utils.BeanCopyUtils;
import com.gkhy.labRiskManage.domain.account.entity.Role;
import com.gkhy.labRiskManage.domain.account.entity.SysUserRoleBind;
import com.gkhy.labRiskManage.domain.account.entity.User;
import com.gkhy.labRiskManage.domain.account.model.dto.SysUserRoleBindDomainDTO;
import com.gkhy.labRiskManage.domain.account.model.dto.UserInfoDomainDTO;
import com.gkhy.labRiskManage.domain.account.service.UserDomainService;
import com.gkhy.labRiskManage.domain.experiment.converter.ExperimentInfoDomainConverter;
import com.gkhy.labRiskManage.domain.experiment.entity.*;
import com.gkhy.labRiskManage.domain.experiment.model.bo.ExperimentDevelopUpdateBO;
import com.gkhy.labRiskManage.domain.basic.model.dto.PersonQueryDTO;
import com.gkhy.labRiskManage.domain.basic.model.dto.SiteQueryDTO;
import com.gkhy.labRiskManage.domain.basic.service.BasicExperimentPersonService;
import com.gkhy.labRiskManage.domain.basic.service.BasicExperimentSiteService;
import com.gkhy.labRiskManage.domain.experiment.enums.*;
import com.gkhy.labRiskManage.domain.experiment.model.bo.ExperimentInfoQueryBO;
import com.gkhy.labRiskManage.domain.experiment.model.dto.*;
import com.gkhy.labRiskManage.domain.experiment.repository.jpa.ExperimentInfoRepository;
import com.gkhy.labRiskManage.domain.experiment.service.ExperimentInfoService;
import com.gkhy.labRiskManage.domain.riskReport.entity.ReportRiskAssessInfo;
import com.gkhy.labRiskManage.domain.riskReport.entity.RiskAssessPlan;
import com.gkhy.labRiskManage.domain.riskReport.model.bo.UpdateExperimentInfoBO;
import com.gkhy.labRiskManage.domain.riskReport.repository.jpa.ReportRiskAssessInfoRepository;
import com.gkhy.labRiskManage.domain.riskReport.service.ReportRiskAssessInfoService;
import com.gkhy.labRiskManage.domain.riskReport.utils.GetRoleTagUtils;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import javax.persistence.criteria.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
 
/**
 * 实验信息
 */
@Service
public class ExperimentInfoServiceImpl implements ExperimentInfoService {
    @Autowired
    private ExperimentInfoRepository repository;
 
    @Autowired
    private BasicExperimentPersonService basicExperimentPersonService;
    @Autowired
    private UserDomainService userDomainService;
    @Autowired
    private ExperimentInfoDomainConverter converter;
 
    @Autowired
    private ReportRiskAssessInfoRepository reportRiskAssessInfoRepository;
 
    @Override
    public ExperimentInfoDTO save(Long currentUserId, ExperimentAppInsertBO experimentAppInsertBO) {
        UserInfoDomainDTO operator = userDomainService.getUserInfoById(currentUserId);
        //验证权限
        List<SysUserRoleBindDomainDTO> roles = operator.getRoles();
        boolean flag = false;
        if(roles != null && roles.size() > 0){
            for (SysUserRoleBindDomainDTO role : roles){
                if (role.getRoleName().equals(UserRoleEnum.USER_ROLE_1.getDesc())) {
                    flag = true;
                    break;
                }
            }
        }
        if (!flag){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"该账号无权限录入实验信息!");
        }
 
        //验证
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExperimentName())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验名称!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExperimentType())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"请选择实验类型!");
        }
        if(ObjectUtils.isEmpty(ExperimentTypeEnum.prase(experimentAppInsertBO.getExperimentType()))){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"实验类型非法!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getLiabilityUserId())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请选择实验负责人!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getLiabilityUserPhone())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验负责人手机号!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getSafeLiabilityUser())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写安全负责人!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getSafeLiabilityUserPhone())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写安全负责人手机号!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getDep())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请输入部门!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExperimentStep())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验步骤!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExperimentMethod())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验操作方法!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getProcess())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写工艺过程!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getKeyProcess())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写关键流程!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getTimeout())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请选择是否存在过夜、老化实验!");
        }
        if(ObjectUtils.isEmpty(TimeoutEnum.prase(experimentAppInsertBO.getTimeout()))){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"是否存在过夜、老化实验数据校验非法!");
        }
 
        if(experimentAppInsertBO.getTimeout().equals(TimeoutEnum.EXISTENT.getValue()) && ObjectUtils.isEmpty(experimentAppInsertBO.getTimeoutManager())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写过夜、老化实验保障管理措施!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getClosed())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请选择是否在密闭条件下!");
        }
        if(ObjectUtils.isEmpty(CloseConditionEnum.prase(experimentAppInsertBO.getClosed()))){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"是否在密闭条件下数据校验非法!");
        }
        if(experimentAppInsertBO.getClosed().equals(CloseConditionEnum.CLOSE_YES.getValue()) && ObjectUtils.isEmpty(experimentAppInsertBO.getUnclosedManager())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写密闭实验保障管理措施!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExplosionProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防爆措施条件和设施情况!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getFireProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防火措施条件和设施情况!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getPoisonProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防爆措施条件和设施情况!");
        }
 
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getSafeManagerMethod())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写安全管理制度!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExplosionProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防爆措施条件和设施情况!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExplosionProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防爆措施条件和设施情况!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExplosionProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防爆措施条件和设施情况!");
        }
        if(ObjectUtils.isEmpty(PertitionConditionEnum.prase(experimentAppInsertBO.getPartitionCondition()))){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"是否需要分区隔断参数非法!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getCreateExperimentTime())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请选择实验立项时间!");
        }
        ExperimentInfo experimentInfo = new ExperimentInfo();
        BeanUtils.copyProperties(experimentAppInsertBO, experimentInfo);
        experimentInfo.setDeleteStatus(StatusEnum.DELETE_NOT.getCode().byteValue());
        experimentInfo.setLiabilityUser(operator.getName());
        experimentInfo.setLiabilityUserId(currentUserId);
        experimentInfo.setStage(ExperimentStageEnum.NOT_EVALUATION.getValue());
        experimentInfo.setStatus(ExperimentStatusEnum.NOT_APPLY.getValue());
        experimentInfo.setExperimentTag(ExperimentTagEnum.NEW_CREATE.getValue());
        experimentInfo.setRectifyStatus(ExperimentRectifyStatusEnum.NOT_RECTIFY.getValue());
        experimentInfo.setExperimentCode(generateTestNumber());
        experimentInfo.setCreateByUserId(currentUserId);
        experimentInfo.setInformant(operator == null?"":operator.getName());
        experimentInfo.setUpdateByUserId(currentUserId);
        experimentInfo.setApprovalStatus(ExperimentApprovalStatusEnum.NOT_APPROVAL.getValue());
        ExperimentInfo experiment = repository.save(experimentInfo);
 
        return converter.getExperimentInfoDTO(experiment);
    }
    @Override
    public ExperimentInfoDTO developSave(Long currentUserId, ExperimentAppInsertBO experimentAppInsertBO) {
        UserInfoDomainDTO operator = userDomainService.getUserInfoById(currentUserId);
        //验证权限
        List<SysUserRoleBindDomainDTO> roles = operator.getRoles();
        boolean flag = false;
        if(roles != null && roles.size() > 0){
            for (SysUserRoleBindDomainDTO role : roles){
                if (role.getRoleName().equals(UserRoleEnum.USER_ROLE_1.getDesc())) {
                    flag = true;
                    break;
                }
            }
        }
        if (!flag){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"该账号无权限录入实验信息!");
        }
        //验证
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExperimentName())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验名称!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExperimentType())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"请选择实验类型!");
        }
        if(ObjectUtils.isEmpty(ExperimentTypeEnum.prase(experimentAppInsertBO.getExperimentType()))){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"实验类型非法!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getLiabilityUserPhone())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验负责人手机号!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getSafeLiabilityUser())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写安全负责人!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getSafeLiabilityUserPhone())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写安全负责人手机号!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getDep())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请输入部门!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExperimentStep())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验步骤!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExperimentMethod())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验操作方法!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getProcess())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写工艺过程!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getKeyProcess())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请填写关键流程!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getTimeout())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请选择是否存在过夜、老化实验!");
        }
        if(ObjectUtils.isEmpty(TimeoutEnum.prase(experimentAppInsertBO.getTimeout()))){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"是否存在过夜、老化实验数据校验非法!");
        }
        if(experimentAppInsertBO.getTimeout().equals(TimeoutEnum.EXISTENT.getValue()) && ObjectUtils.isEmpty(experimentAppInsertBO.getTimeoutManager())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写过夜、老化实验保障管理措施!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getClosed())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请选择是否在密闭条件下!");
        }
        if(ObjectUtils.isEmpty(CloseConditionEnum.prase(experimentAppInsertBO.getClosed()))){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"是否在密闭条件下数据校验非法!");
        }
        if(experimentAppInsertBO.getClosed().equals(CloseConditionEnum.CLOSE_YES.getValue()) && ObjectUtils.isEmpty(experimentAppInsertBO.getUnclosedManager())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写密闭实验保障管理措施!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExplosionProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防爆措施条件和设施情况!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getFireProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防火措施条件和设施情况!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getPoisonProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防爆措施条件和设施情况!");
        }
 
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getSafeManagerMethod())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写安全管理制度!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExplosionProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防爆措施条件和设施情况!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExplosionProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防爆措施条件和设施情况!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getExplosionProof())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请填写实验场所防爆措施条件和设施情况!");
        }
        if(ObjectUtils.isEmpty(PertitionConditionEnum.prase(experimentAppInsertBO.getPartitionCondition()))){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"是否需要分区隔断参数非法!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getCreateExperimentTime())){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"请选择实验立项时间!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getSisStatus())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请选择是否是安全信息系统!");
        }
        if(ObjectUtils.isEmpty(SafeInformationSystemEnum.prase(experimentAppInsertBO.getSisStatus()))){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_ILLEGAL.getCode(), "安全信息系统数据校验错误!");
        }
        if(SafeInformationSystemEnum.YES.getValue().equals(experimentAppInsertBO.getSisStatus()) && ObjectUtils.isEmpty(experimentAppInsertBO.getSafeInformationSystem())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请填写安全信息系统名称!");
        }
        if(ObjectUtils.isEmpty(experimentAppInsertBO.getStartTime())){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请填写请实验开展时间!");
        }
        ExperimentInfo experimentInfo = new ExperimentInfo();
        BeanUtils.copyProperties(experimentAppInsertBO, experimentInfo);
        experimentInfo.setDeleteStatus(StatusEnum.DELETE_NOT.getCode().byteValue());
        experimentInfo.setLiabilityUserId(currentUserId);
        experimentInfo.setLiabilityUser(operator.getName());
        experimentInfo.setStage(ExperimentStageEnum.NOT_EVALUATION.getValue());
        experimentInfo.setStatus(ExperimentStatusEnum.NOT_APPLY.getValue());
        experimentInfo.setExperimentTag(ExperimentTagEnum.AREADLY_DEVELOP.getValue());
        experimentInfo.setRectifyStatus(ExperimentRectifyStatusEnum.NOT_RECTIFY.getValue());
        experimentInfo.setExperimentCode(generateTestNumber());
        experimentInfo.setCreateByUserId(currentUserId);
        experimentInfo.setInformant(operator == null?"":operator.getName());
        experimentInfo.setUpdateByUserId(currentUserId);
        experimentInfo.setApprovalStatus(ExperimentApprovalStatusEnum.NOT_APPROVAL.getValue());
        ExperimentInfo experiment = repository.save(experimentInfo);
 
        return converter.getExperimentInfoDTO(experiment);
    }
 
    @Override
    public boolean deleteById(Long id,Long currentUserId) {
        boolean flag = false;
        if(ObjectUtils.isEmpty(id)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"实验主键不可为空!");
        }
        //获取该条实现信息
        ExperimentInfo experimentInfo = repository.getExperimentInfoById(id);
        if(ObjectUtils.isEmpty(experimentInfo)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "   该实验信息不存在,或已被删除");
        }
        if(experimentInfo.getStatus().equals(ExperimentStatusEnum.YES_APPLY.getValue())){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(),"该条实验信息已申请评估,不可删除!");
        }
        LocalDateTime date = LocalDateTime.now();
        //设置数据为删除
        experimentInfo.setDeleteStatus(StatusEnum.DELETED.getCode().byteValue());
        experimentInfo.setUpdateByUserId(currentUserId); // todo 没有用户信息
        experimentInfo.setUpdateTime(date);
        ExperimentInfo deleteExperiment = repository.save(experimentInfo);
        if(!ObjectUtils.isEmpty(deleteExperiment)){
            flag = true;
        }
        return flag;
    }
    @Override
    public ExperimentInfoDTO getExperimentById(Long id) {
        if(ObjectUtils.isEmpty(id)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"实验主键不可为空!");
        }
        //获取该条实现信息
        ExperimentInfo experimentInfo = repository.getExperimentInfoById(id);
        if(ObjectUtils.isEmpty(experimentInfo)){ //todo 暂时抛出100
            throw new BusinessException(this.getClass(), ResultCode.OK.getCode(),"实验信息不存在或已被删除");
        }
        return converter.getExperimentInfoDTO(experimentInfo);
    }
    @Override
    public ExperimentInfoDTO getExperimentByName(String experimentName) {
        if(StringUtil.isBlank(experimentName)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"实验名称不可为空!");
        }
        //获取该条实现信息
        ExperimentInfo experimentInfo = repository.getExperimentInfoByName(experimentName);
        if(ObjectUtils.isEmpty(experimentInfo)){  //todo 暂时抛出100
            throw new BusinessException(this.getClass(), ResultCode.OK.getCode(),"实验信息不存在或已被删除");
        }
        return converter.getExperimentInfoDTO(experimentInfo);
    }
 
    @Override
    public List<ExperimentInfoDTO> getExperimentInfoByIds(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "该实验信息主键不可为空!");
        }
        List<ExperimentInfo> experimentInfoByIdList = repository.getExperimentInfoByIds(idList);
 
        return converter.getExperimentInfoDTOSimpleList(experimentInfoByIdList);
    }
 
    @Override
    public boolean updateDevelop(Long currentUserId, List<ExperimentDevelopUpdateBO> developUpdateBOList) {
         boolean flag = false;
        if (CollectionUtils.isEmpty(developUpdateBOList)) {
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请选择需要开展实验信息!");
        }
        List<Long> idList = developUpdateBOList.stream().map(ExperimentDevelopUpdateBO::getId).collect(Collectors.toList());
        //获取实验信息
        List<ExperimentInfo> experimentInfoByIdList = repository.getExperimentInfoByIds(idList);
        List<ExperimentInfo> updateExoerimentInfoList = new ArrayList<>();
        for(ExperimentDevelopUpdateBO experimentDevelopUpdateBO:developUpdateBOList){
            if(ObjectUtils.isEmpty(experimentDevelopUpdateBO.getSisStatus())){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请选择是否是安全信息系统!");
            }
            if(ObjectUtils.isEmpty(SafeInformationSystemEnum.prase(experimentDevelopUpdateBO.getSisStatus()))){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_ILLEGAL.getCode(), "安全信息系统数据校验错误!");
            }
            if(SafeInformationSystemEnum.YES.getValue().equals(experimentDevelopUpdateBO.getSisStatus()) && ObjectUtils.isEmpty(experimentDevelopUpdateBO.getSafeInformationSystem())){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请填写安全信息系统名称!");
            }
            if(ObjectUtils.isEmpty(experimentDevelopUpdateBO.getStartTime())){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请填写请实验开展时间!");
            }
            List<ExperimentInfo> selectList = experimentInfoByIdList.stream().filter(experimentInfo -> experimentInfo.getId().equals(experimentDevelopUpdateBO.getId())).collect(Collectors.toList());
            if(selectList.size() == 0){
                throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(), "实验信息不存在或已删除!");
            }
            ExperimentInfo experimentInfo = selectList.get(0);
            if(experimentInfo.getExperimentTag().equals(ExperimentTagEnum.AREADLY_DEVELOP.getValue())){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "实验已开展不可再次开展!");
            }
            experimentInfo.setExperimentTag(ExperimentTagEnum.AREADLY_DEVELOP.getValue());
            experimentInfo.setSisStatus(experimentDevelopUpdateBO.getSisStatus());
            experimentInfo.setSafeInformationSystem(experimentDevelopUpdateBO.getSafeInformationSystem());
            experimentInfo.setUpdateTime(LocalDateTime.now());
            experimentInfo.setUpdateByUserId(currentUserId);
            experimentInfo.setStartTime(experimentDevelopUpdateBO.getStartTime());
            updateExoerimentInfoList.add(experimentInfo);
        }
        List<ExperimentInfo> experimentInfos = repository.saveAll(updateExoerimentInfoList);
        if (!CollectionUtils.isEmpty(experimentInfos)) {
            flag = true;
        }
        return flag;
    }
 
    @Override
    public boolean applyEvaluation(Long currentUserId, List<Long> ids) {
        boolean flag = false;
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "实验信息主键不可为空!");
        }
        //获取实验信息
        List<ExperimentInfo> experimentInfoByIdList = repository.getExperimentInfoByIds(ids);
        List<ExperimentInfo> updateExperimentInfoList = new ArrayList<>();
        for(Long id:ids){
            List<ExperimentInfo> selectList = experimentInfoByIdList.stream().filter(experimentInfo -> experimentInfo.getId().equals(id)).collect(Collectors.toList());
            if(selectList.size() == 0){
                throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(), "实验信息不存在或已删除!");
            }
            ExperimentInfo experimentInfo = selectList.get(0);
            if(experimentInfo.getStatus().equals(ExperimentStatusEnum.YES_APPLY.getValue())){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "实验已申请评估不可再次申请!");
            }
            experimentInfo.setStatus(ExperimentStatusEnum.YES_APPLY.getValue());
            experimentInfo.setAssessApplyTime(LocalDateTime.now());
            experimentInfo.setUpdateTime(LocalDateTime.now());
            experimentInfo.setUpdateByUserId(currentUserId);
            updateExperimentInfoList.add(experimentInfo);
        }
        List<ExperimentInfo> experimentInfos = repository.saveAll(updateExperimentInfoList);
        if(!CollectionUtils.isEmpty(experimentInfos)){
            flag = true;
        }
 
 
 
        return flag;
    }
 
    /**
     * 修改申请评估状态
     * @param currentUserId
     * @param ids
     * @return
     */
    @Override
    public boolean revokeApplyEvaluation(Long currentUserId, List<Long> ids) {
        boolean flag = false;
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "实验信息主键不可为空!");
        }
        //获取实验信息
        List<ExperimentInfo> experimentInfoByIdList = repository.getExperimentInfoByIds(ids);
        List<ExperimentInfo> updateExperimentInfoList = new ArrayList<>();
        for(Long id:ids){
            List<ExperimentInfo> selectList = experimentInfoByIdList.stream().filter(experimentInfo -> experimentInfo.getId().equals(id)).collect(Collectors.toList());
            if(selectList.size() == 0){
                throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(), "实验信息不存在或已删除!");
            }
            ExperimentInfo experimentInfo = selectList.get(0);
            if(experimentInfo.getStatus().equals(ExperimentStatusEnum.NOT_APPLY.getValue()) || !experimentInfo.getStage().equals(ExperimentStageEnum.NOT_EVALUATION.getValue())){
                //未申请评估或者评估中评估完成
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "实验未申请评估,评估中,评估完成,不可执行撤销操作!");
            }
            experimentInfo.setStatus(ExperimentStatusEnum.NOT_APPLY.getValue());
            experimentInfo.setUpdateTime(LocalDateTime.now());
            experimentInfo.setUpdateByUserId(currentUserId);
            updateExperimentInfoList.add(experimentInfo);
        }
        List<ExperimentInfo> experimentInfos = repository.saveAll(updateExperimentInfoList);
        if(!CollectionUtils.isEmpty(experimentInfos)){
            flag = true;
        }
        return flag;
    }
 
    @Override
    public List<ExperimentInfoDTO> getExperimentInfoList(Long currentUserId) {
        return converter.getExperimentInfoDTOSimpleList(repository.getExperimentInfoList(currentUserId));
    }
 
    @Override
    public boolean updateRectify(Long experimentId) {
        if(ObjectUtils.isEmpty(experimentId)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "实验信息主键不可为空!");
        }
        ExperimentInfo experimentInfo = repository.getExperimentInfoById(experimentId);
        if (ObjectUtils.isEmpty(experimentInfo)) {
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(), "实验信息不存在或已删除!");
        }
        if (!experimentInfo.getRectifyStatus().equals(ExperimentRectifyStatusEnum.WAIT_RECTIFY.getValue())) {
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), "非待整改实验,不可申请整改!");
        }
        experimentInfo.setRectifyStatus(ExperimentRectifyStatusEnum.YES_RECTIFY.getValue());
        ExperimentInfo save = repository.save(experimentInfo);
        if(ObjectUtils.isEmpty(save)){
            return false;
        }
        return true;
 
    }
 
    @Override
    public SearchResult<List<ExperimentInfoDTO>> listByPage(PageQuery<ExperimentInfoQueryBO> pageQuery,Long currentUserId) {
        SearchResult searchResult = new SearchResult<>();
        searchResult.setPageIndex(pageQuery.getPageIndex());
        searchResult.setPageSize(pageQuery.getPageSize());
        ExperimentInfoQueryBO queryBO = pageQuery.getSearchParams();
        UserInfoDomainDTO user = userDomainService.getUserById(currentUserId);
        int roleTag = GetRoleTagUtils.GetRoleTagUtils(user);
        Specification<ExperimentInfo> specification = new Specification<ExperimentInfo>() {
            @Override
            public Predicate toPredicate(Root<ExperimentInfo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                predicateList.add(criteriaBuilder.equal(root.get("deleteStatus"),StatusEnum.DELETE_NOT.getCode()));
                if(!ObjectUtils.isEmpty(ExperimentStatusEnum.prase(queryBO.getStatus()))){
                    predicateList.add(criteriaBuilder.equal(root.get("status"),queryBO.getStatus()));
                }
                if(!ObjectUtils.isEmpty(ExperimentTagEnum.prase(queryBO.getExperimentTag()))){
                    predicateList.add(criteriaBuilder.equal(root.get("experimentTag"),queryBO.getExperimentTag()));
                }
                if (!ObjectUtils.isEmpty(ExperimentTypeEnum.prase(queryBO.getExperimentType()))){
                    predicateList.add(criteriaBuilder.equal(root.get("experimentType"), queryBO.getExperimentType()));
                }
                if (StrUtil.isNotBlank(queryBO.getExperimentName())){
                    predicateList.add(criteriaBuilder.like(root.get("experimentName"), '%'+ queryBO.getExperimentName()+'%'));
                }
                if (!ObjectUtils.isEmpty(queryBO.getStartTime())){
                    predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"), queryBO.getStartTime()));
                }
                if (!ObjectUtils.isEmpty(queryBO.getEndTime())){
                    predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime"), queryBO.getEndTime()));
                }
                if(roleTag == UserTagEnum.USER_TAG_0.getCode()){  // todo
                    Join<ExperimentInfo, RiskAssessPlan> experimentJion = root.join("riskAssessPlans", JoinType.LEFT);
                    predicateList.add(criteriaBuilder.or(
                            criteriaBuilder.equal(root.get("liabilityUserId"), currentUserId),
                            criteriaBuilder.equal(experimentJion.get("identificationUserId"), currentUserId),
                            criteriaBuilder.equal(experimentJion.get("evaluateUserId"), currentUserId))
                    );
                    query.groupBy(root.get("id"));
                }
 
                //返回组装的条件
                return criteriaBuilder.and(predicateList.toArray(predicateList.toArray(new Predicate[0])));
            }
        };
        Pageable pageable = PageRequest.of(pageQuery.getPageIndex()-1, pageQuery.getPageSize(), Sort.Direction.DESC, "createTime");
        Page<ExperimentInfo> pageResult = repository.findAll(specification, pageable);
 
        List<ExperimentInfoDTO> experimentInfoDTODetailList = converter.getExperimentInfoDTODetailList(pageResult.getContent());
        for (ExperimentInfoDTO experimentInfoDTO : experimentInfoDTODetailList) {
            ReportRiskAssessInfo report = reportRiskAssessInfoRepository.getByExperimentId(experimentInfoDTO.getId());
            if (!ObjectUtils.isEmpty(report) && report.getCreateTime().isBefore(report.getUpdateTime())){
                experimentInfoDTO.setAssessPerson(report.getAssessPerson());
            }
        }
 
        searchResult.setTotal(pageResult.getTotalElements());
        searchResult.setPages(pageResult.getTotalPages());
        searchResult.setData(experimentInfoDTODetailList);
        return searchResult;
    }
 
 
    /**
     * 生成实验编号
     */
    private String generateTestNumber(){
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd" );
        SimpleDateFormat sdf2 =new SimpleDateFormat("yyyyMMdd" );
        String time = sdf.format(new Date());
        //获取当天新立项数量
        Integer count = repository.countByCreateTime(time);
 
        String serialCode = null;
        if(count >= 1000){
            serialCode = "" + (count+1);
        }else if(count >=0){
            String countStr = String.valueOf(count+1);
            serialCode = "000".substring(0,(3- countStr.length()))+countStr;
        }
        return sdf2.format(new Date()) + serialCode;
    }
 
    /**
     * 实验信息 - 修改实验状态
     */
    @Override
    public ExperimentInfoDTO updateExperimentStage(Long id, Byte stage) {
 
//        ExperimentInfo experimentInfo = repository.updateExperimentStage(id, StatusEnum.EXPERIMENT_STATUS_FINISH.getCode());
        ExperimentInfo experimentInfoById = repository.getExperimentInfoById(id);
 
        experimentInfoById.setStage(stage);
 
        ExperimentInfo experimentInfo = repository.save(experimentInfoById);
 
        if (ObjectUtils.isEmpty(experimentInfo)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "实验评估状态修改失败");
        }
 
        return BeanCopyUtils.copyBean(experimentInfo, ExperimentInfoDTO.class);
    }
 
    @Override
    public List<ExperimentInfoDTO> getExperimentByUser(Long currentUserId) {
 
        List<ExperimentInfo> experimentList = repository.getExperimentByUser(currentUserId);
        if (ObjectUtils.isEmpty(experimentList)){
            List<ExperimentInfoDTO> experiment = new ArrayList<>();
            return experiment;
        }
        return BeanCopyUtils.copyBeanList(experimentList, ExperimentInfoDTO.class);
    }
 
    @Override
    public ExperimentInfo updateExperimentStatus(UpdateExperimentInfoBO updateBO) {
 
        ExperimentInfo experimentInfoById = repository.getExperimentInfoById(updateBO.getExperimentId());
        if (ObjectUtils.isEmpty(experimentInfoById)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "实验不存在");
        }
 
        experimentInfoById.setApprovalStatus(updateBO.getApprovalStatus());
        if (updateBO.getRectifyStatus() == ExperimentRectifyStatusEnum.WAIT_RECTIFY.getValue()){
            experimentInfoById.setRectifyStatus(updateBO.getRectifyStatus());
        }
        return repository.save(experimentInfoById);
    }
}