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 deviceAppInsertBOList) { Boolean flag = false; if(CollectionUtils.isEmpty(deviceAppInsertBOList)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"请选择试验设备!"); } List deviceIdList = deviceAppInsertBOList.stream().map(ExperimentAndDeviceAppInsertBO::getDeviceId).collect(Collectors.toList()); List 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 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 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 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); } }