package com.gkhy.testFourierSpecialGasMonitor.service.impl; import com.gkhy.testFourierSpecialGasMonitor.commons.domain.Result; import com.gkhy.testFourierSpecialGasMonitor.commons.enums.ResultCode; import com.gkhy.testFourierSpecialGasMonitor.commons.exception.BusinessException; import com.gkhy.testFourierSpecialGasMonitor.decorator.WarningThresholdUpdateEvent; import com.gkhy.testFourierSpecialGasMonitor.domain.account.entity.User; import com.gkhy.testFourierSpecialGasMonitor.domain.account.enums.UserStatusEnum; import com.gkhy.testFourierSpecialGasMonitor.domain.account.repository.jpa.UserRepository; import com.gkhy.testFourierSpecialGasMonitor.entity.GasThreshold; import com.gkhy.testFourierSpecialGasMonitor.entity.req.UpdateGasThresholdReqDTO; import com.gkhy.testFourierSpecialGasMonitor.entity.resp.GasThresholdListRespDTO; import com.gkhy.testFourierSpecialGasMonitor.repository.GasThresholdRepository; import com.gkhy.testFourierSpecialGasMonitor.service.GasThresholdService; import com.gkhy.testFourierSpecialGasMonitor.utils.ThreadLocalUtil; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.time.LocalDateTime; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; /** * @author Mr.huang * @decription * @date 2023/8/9 14:33 */ @Service public class GasThresholdServiceImpl implements GasThresholdService { @Autowired private GasThresholdRepository gasThresholdRepository; @Autowired private ApplicationEventPublisher applicationEventPublisher; @Autowired private UserRepository userRepository; private User getCurrentUser(){ Long userId = ThreadLocalUtil.get().getId(); User user = userRepository.findUserByIdAndStatus(userId, UserStatusEnum.STATUS_ACTIVE.getStatus()); if (user == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"未成功获取用户信息"); return user; } @Override public Result gasThresholdList() { Result success = Result.success(); List gasThresholds = gasThresholdRepository.findAll(); if (!CollectionUtils.isEmpty(gasThresholds)){ List collect = gasThresholds.stream().map(gasThreshold -> { GasThresholdListRespDTO dto = new GasThresholdListRespDTO(); BeanUtils.copyProperties(gasThreshold, dto); return dto; }).collect(Collectors.toList()); success.setData(collect); } return success; } @Override public synchronized Result updateGasThreshold(UpdateGasThresholdReqDTO reqDto) { User currentUser = getCurrentUser(); if (reqDto == null || reqDto.getId() == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); if (reqDto.getThreshold() <= 0) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"连续超过阈值点数不能小于0"); Optional thresholdRepositoryById = gasThresholdRepository.findById(reqDto.getId()); if (!thresholdRepositoryById.isPresent()) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"预警该配置不存在"); GasThreshold threshold = thresholdRepositoryById.get(); threshold.setThreshold(reqDto.getThreshold()); threshold.setGmtModified(LocalDateTime.now()); threshold.setLastmodifiedby(currentUser.getRealName()); GasThreshold save = gasThresholdRepository.save(threshold); if (save == null) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"预警设置修改失败"); applicationEventPublisher.publishEvent(new WarningThresholdUpdateEvent("事件源--预警阈值发生变更")); return Result.success(); } @Override public List findAll() { return gasThresholdRepository.findAll(); } }