heheng
2025-01-13 a27162cb82ef0cabf9b43cbfd1f3eb8c177d1e14
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
package com.gkhy.labRiskManage.domain.experiment.service.impl;
 
import com.gkhy.labRiskManage.application.experiment.dto.bo.ExperimentAndDeviceAppInsertBO;
import com.gkhy.labRiskManage.commons.enums.ResultCode;
import com.gkhy.labRiskManage.commons.enums.StatusEnum;
import com.gkhy.labRiskManage.commons.exception.BusinessException;
import com.gkhy.labRiskManage.domain.basic.model.dto.DeviceQueryDTO;
import com.gkhy.labRiskManage.domain.basic.service.BasicExperimentDeviceService;
import com.gkhy.labRiskManage.domain.experiment.entity.ExperimentAndDevice;
import com.gkhy.labRiskManage.domain.experiment.repository.jpa.ExperimentAndDeviceRepository;
import com.gkhy.labRiskManage.domain.experiment.service.ExperimentAndDeviceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
 
/**
 * 实验与设备
 */
@Service
public class ExperimentAndDeviceServiceImpl implements ExperimentAndDeviceService {
    @Autowired
    private ExperimentAndDeviceRepository repository;
    @Autowired
    private BasicExperimentDeviceService basicExperimentDeviceService;
 
 
 
    @Override
    public boolean saveBatch(Long currentUserId, List<ExperimentAndDeviceAppInsertBO> deviceAppInsertBOList) {
        Boolean flag = false;
        if(CollectionUtils.isEmpty(deviceAppInsertBOList)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请选择试验设备!");
        }
        List<Long> deviceIdList = deviceAppInsertBOList.stream().map(ExperimentAndDeviceAppInsertBO::getDeviceId).collect(Collectors.toList());
        List<DeviceQueryDTO> basicExperimentDeviceByIdList = basicExperimentDeviceService.getBasicExperimentDeviceByIdList(deviceIdList);
 
        for(ExperimentAndDeviceAppInsertBO experimentAndDeviceAppInsertBO:deviceAppInsertBOList){
            if(ObjectUtils.isEmpty(experimentAndDeviceAppInsertBO.getDeviceId())){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"设备主键不可为空!");
            }
            if(ObjectUtils.isEmpty(experimentAndDeviceAppInsertBO.getDeviceUseCount())){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"设备数量不可为空!");
            }
            if(ObjectUtils.isEmpty(experimentAndDeviceAppInsertBO.getExperimentId())){
                throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"实验主键不可为空!");
            }
            List<DeviceQueryDTO> selectList = basicExperimentDeviceByIdList.stream().filter(item -> item.getId().equals(experimentAndDeviceAppInsertBO.getDeviceId())).collect(Collectors.toList());
            if(selectList.size() == 0){
                throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(),"未查询到设备信息!");
            }
 
        }
        List<ExperimentAndDevice> experimentAndDeviceInsertList = new ArrayList<>();
        for (ExperimentAndDeviceAppInsertBO deviceInsertBO:deviceAppInsertBOList){
            ExperimentAndDevice device = new ExperimentAndDevice();
            BeanUtils.copyProperties(deviceInsertBO,device);
            device.setDeleteStatus(StatusEnum.DELETE_NOT.getCode().byteValue());
            device.setCreateByUserId(currentUserId);
            device.setUpdateByUserId(currentUserId);
            experimentAndDeviceInsertList.add(device);
        }
        List<ExperimentAndDevice> experimentAndDeviceList = repository.saveAll(experimentAndDeviceInsertList);
        if(!CollectionUtils.isEmpty(experimentAndDeviceList)){
            flag = true;
        }
        return flag;
    }
 
    @Override
    public void deleteByExperimentId(Long experimentId,Long currentUserId) {
        if(ObjectUtils.isEmpty(experimentId)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"实验主键不可为空!");
        }
        repository.deleteByExperimentId(experimentId);
 
    }
}