package com.jiayue.ipfcst.console.service;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.data.entity.WeatherStationInfo;
import com.jiayue.ipfcst.common.data.entity.WindTowerInfo;
import com.jiayue.ipfcst.common.data.repository.WindTowerInfoRepository;
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 WindTowerInfoService extends BaseService {
    private final WindTowerInfoRepository windTowerInfoRepository;

    @Autowired
    public WindTowerInfoService(WindTowerInfoRepository windTowerInfoRepository) {
        this.windTowerInfoRepository = windTowerInfoRepository;
    }

    /**
     * 新增测风塔
     *
     * @param windTowerInfo 测风塔
     * @throws BusinessException 业务异常
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public void add(WindTowerInfo windTowerInfo) throws BusinessException {

        try {
            this.windTowerInfoRepository.save(windTowerInfo);
        } catch (Exception e) {
            log.error("系统错误" + e);
        }

    }

    /**
     * 修改测风塔
     *
     * @param windTowerInfo 测风塔
     * @throws BusinessException 业务异常
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public void update(WindTowerInfo windTowerInfo)
            throws BusinessException {
        if (StringUtils.isEmpty(windTowerInfo.getId())) {
            throw new BusinessException("测风塔编号不能为空!");
        } else {
            this.windTowerInfoRepository.save(windTowerInfo);
        }
    }

    /**
     * 删除测风塔
     *
     * @param no 测风塔编号
     * @throws BusinessException 业务异常
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public void delete(final Integer no) throws BusinessException {
        if (StringUtils.isEmpty(no)) {
            throw new BusinessException("测风塔编号不能为空!");
        } else {
            this.windTowerInfoRepository.deleteById(no);
        }
    }

    /**
     * 查询测风塔
     *
     * @param no 测风塔编号
     * @return 逆变器
     * @throws BusinessException 业务异常
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public WindTowerInfo get(final Integer no)
            throws BusinessException {
        Optional<WindTowerInfo> optional = this.windTowerInfoRepository.findById(no);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            throw new BusinessException("测风塔不存在!");
        }
    }


    /**
     * 获取到所有的测风塔   create by xiuwei
     *
     * @return List<WindTowerInfo>  测风塔列表
     */
    public List<WindTowerInfo> getAll() {
        return windTowerInfoRepository.findAll();
    }


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

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCloud(WindTowerInfo bean) {
//        WindTowerInfo windTowerInfo = windTowerInfoRepository.findByName(bean.getName());
//        if (null != windTowerInfo) {
//            bean.setId(windTowerInfo.getId());
//        }
        windTowerInfoRepository.save(bean);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCloud(List<WindTowerInfo> beans) {
        if(beans != null && beans.size() > 0){
            windTowerInfoRepository.deleteAll();
            windTowerInfoRepository.saveAll(beans);
        }
    }
}
