package com.jiayue.ipfcst.console.service; import com.jiayue.ipfcst.common.core.exception.BusinessException; import com.jiayue.ipfcst.common.data.entity.InverterInfo; import com.jiayue.ipfcst.common.data.entity.WeatherStationInfo; import com.jiayue.ipfcst.common.data.repository.WeatherStationInfoRepository; import com.jiayue.ipfcst.common.data.service.BaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.*; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; /** * 环境监测仪业务层 * * @author zzy * @version 1.0 * @since 2019/8/7 10:58 */ @Service @Slf4j public class WeatherStationInfoService extends BaseService { private final WeatherStationInfoRepository weatherStationInfoRepository; @Autowired public WeatherStationInfoService(WeatherStationInfoRepository weatherStationInfoRepository) { this.weatherStationInfoRepository = weatherStationInfoRepository; } /** * 新增环境监测仪 * * @param weatherStationInfo 气象站 * @throws BusinessException 业务异常 */ @Transactional(propagation = Propagation.SUPPORTS) public void add(WeatherStationInfo weatherStationInfo) throws BusinessException { this.weatherStationInfoRepository.save(weatherStationInfo); } /** * 修改环境监测仪 * * @param weatherStationInfo 监测仪 * @throws BusinessException 业务异常 */ @Transactional(propagation = Propagation.SUPPORTS) public void update(WeatherStationInfo weatherStationInfo) throws BusinessException { if (StringUtils.isEmpty(weatherStationInfo.getId())) { throw new BusinessException("环境监测仪编号不能为空!"); } else { this.weatherStationInfoRepository.save(weatherStationInfo); } } /** * 删除环境监测仪 * * @param id 监测仪编号 * @throws BusinessException 业务异常 */ @Transactional(propagation = Propagation.SUPPORTS) public void delete(final Integer id) throws BusinessException { if (StringUtils.isEmpty(id)) { throw new BusinessException("环境监测仪编号不能为空!"); } else { this.weatherStationInfoRepository.deleteById(id); } } /** * 查询环境监测仪 * * @param no 测风塔编号 * @return 逆变器 * @throws BusinessException 业务异常 */ @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public WeatherStationInfo get(final Integer no) throws BusinessException { Optional optional = this.weatherStationInfoRepository.findById(no); if (optional.isPresent()) { return optional.get(); } else { throw new BusinessException("环境监测仪不存在!"); } } /** * 查询环境监测仪【分页查询】 * * @param stationCode 场站编号 * @return 分页结果 */ @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public List get(String stationCode) { List resultList = new ArrayList<>(); resultList = this.weatherStationInfoRepository.findAll(); if (!"ALL".equals(stationCode)){ resultList = resultList.stream().filter(s->s.getStationCode().equals(stationCode)).collect(Collectors.toList()); } return resultList.stream().sorted(Comparator.comparing(WeatherStationInfo::getStationCode)).collect(Collectors.toList()); } /** * 查询环境监测仪 * * @return 分页结果 */ @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public List getAll() { return this.weatherStationInfoRepository.findAll(); } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public void saveCloud(WeatherStationInfo bean) { // WeatherStationInfo weatherStationInfo = weatherStationInfoRepository.findByName(bean.getName()); // if (null != weatherStationInfo) { // bean.setId(weatherStationInfo.getId()); // } weatherStationInfoRepository.save(bean); } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public void saveCloud(List beans) { if(beans != null && beans.size() > 0){ weatherStationInfoRepository.deleteAll(); weatherStationInfoRepository.saveAll(beans); } } }