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.List;
import java.util.Optional;

/**
 * 环境监测仪业务层
 *
 * @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 no 监测仪编号
     * @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<WeatherStationInfo> optional = this.weatherStationInfoRepository.findById(no);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            throw new BusinessException("环境监测仪不存在!");
        }
    }

    /**
     * 查询环境监测仪【分页查询】
     *
     * @param weatherStationInfo 查询条件
     * @param page               页码
     * @param size               每页记录数
     * @return 分页结果
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Page<WeatherStationInfo> get(final WeatherStationInfo weatherStationInfo, final Integer page, final Integer size) {
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("modelNumber", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<WeatherStationInfo> example = Example.of(weatherStationInfo, matcher);
        Pageable pageable = PageRequest.of(page - 1, size);
        return this.weatherStationInfoRepository.findAll(example, pageable);
    }

    /**
     * 查询环境监测仪
     *
     * @return 分页结果
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<WeatherStationInfo> 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<WeatherStationInfo> beans) {
        if(beans != null && beans.size() > 0){
            weatherStationInfoRepository.deleteAll();
            weatherStationInfoRepository.saveAll(beans);
        }
    }
}
