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.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.GasCategoryConfiguration; import com.gkhy.testFourierSpecialGasMonitor.entity.req.GasCategoryConfigurationReqDTO; import com.gkhy.testFourierSpecialGasMonitor.repository.GasCategoryConfigurationRepository; import com.gkhy.testFourierSpecialGasMonitor.service.GasCategoryConfigurationService; import com.gkhy.testFourierSpecialGasMonitor.utils.ThreadLocalUtil; import io.micrometer.core.instrument.util.StringUtils; import org.redisson.api.RedissonClient; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.Optional; @Service public class GasCategoryConfigurationServiceImpl implements GasCategoryConfigurationService { @Autowired private UserRepository userRepository; @Autowired private RedissonClient redissonClient; @Autowired private GasCategoryConfigurationRepository gasCategoryConfigurationRepository; 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 updateById(GasCategoryConfigurationReqDTO reqDTO) { if (reqDTO==null) throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); if (reqDTO.getOrientation()==null) throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"方位不能为空"); if (reqDTO.getConcentration()==null) throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"本底浓度不能为空"); if (reqDTO.getMultiplication()==null) throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"倍增系数不能为空"); GasCategoryConfiguration byOrientation = gasCategoryConfigurationRepository.findByGasCategoryIdAndOrientation(reqDTO.getGasCategoryId(), reqDTO.getOrientation()); if (byOrientation!=null && byOrientation.getId() != reqDTO.getId()) throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR.getCode(),"同一方位不能重复添加"); User user = getCurrentUser(); Optional byId = gasCategoryConfigurationRepository.findById(reqDTO.getId()); if (byId.isPresent()){ GasCategoryConfiguration gasCategoryConfiguration = byId.get(); BeanUtils.copyProperties(reqDTO,gasCategoryConfiguration); gasCategoryConfiguration.setUpdateBy(user.getRealName()); gasCategoryConfiguration.setUpdateTime(LocalDateTime.now()); GasCategoryConfiguration save = gasCategoryConfigurationRepository.save(gasCategoryConfiguration); if (save==null) throw new BusinessException(this.getClass(),ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"更新气体参数失败"); } return Result.success(); } @Override public Result deletedById(Integer id) { Optional byId = gasCategoryConfigurationRepository.findById(id); if (byId.isPresent()){ GasCategoryConfiguration gasCategoryConfiguration = byId.get(); gasCategoryConfiguration.setDelFlag(1); GasCategoryConfiguration save = gasCategoryConfigurationRepository.save(gasCategoryConfiguration); if (save==null) throw new BusinessException(this.getClass(),ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"删除气体参数失败"); } return Result.success(); } @Override public Result createGasCategoryConfiguration(GasCategoryConfigurationReqDTO reqDTO) { if (reqDTO==null) throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); if (reqDTO.getOrientation()==null) throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"方位不能为空"); if (reqDTO.getConcentration()==null) throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"本底浓度不能为空"); if (reqDTO.getMultiplication()==null) throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_NULL.getCode(),"倍增系数不能为空"); GasCategoryConfiguration byOrientation = gasCategoryConfigurationRepository.findByGasCategoryIdAndOrientation(reqDTO.getGasCategoryId(), reqDTO.getOrientation()); if (byOrientation!=null) throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR.getCode(),"同一方位不能重复添加"); User currentUser = getCurrentUser(); GasCategoryConfiguration gasCategoryConfiguration = new GasCategoryConfiguration(); BeanUtils.copyProperties(reqDTO,gasCategoryConfiguration); gasCategoryConfiguration.setCreateBy(currentUser.getRealName()); gasCategoryConfiguration.setCreateTime(LocalDateTime.now()); gasCategoryConfiguration.setUpdateTime(LocalDateTime.now()); gasCategoryConfiguration.setUpdateBy(currentUser.getRealName()); gasCategoryConfiguration.setDelFlag(0); GasCategoryConfiguration save = gasCategoryConfigurationRepository.save(gasCategoryConfiguration); if (save==null) throw new BusinessException(this.getClass(),ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"新增气体参数失败"); return Result.success(); } }