package com.gyee.wisdom.dao.sql.alarm;

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.alarm.GyeePowerStation;
import com.gyee.wisdom.common.data.windturbine.PowerStation;
import com.gyee.wisdom.dao.alarm.IGyeePowerStationDao;
import com.gyee.wisdom.dao.sql.Windturbine.entity.PowerStationEntity;
import com.gyee.wisdom.dao.sql.Windturbine.repsoitory.PowerStationRepository;
import com.gyee.wisdom.dao.sql.alarm.Entity.GyeePowerStationEntity;
import com.gyee.wisdom.dao.sql.alarm.repository.GyeePowerStationRepository;
import com.gyee.wisdom.dao.windturbine.IPowerStationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class JpaGyeePowerStationDao implements IGyeePowerStationDao {

    @Autowired
    public GyeePowerStationRepository gyeepowerStationRepository;

    @Override
    public List<GyeePowerStation> getAll() {
        List<GyeePowerStationEntity> pe=gyeepowerStationRepository.findAll();
        List<GyeePowerStation> stationList = DataUtil.convertDataList(gyeepowerStationRepository.findAll());

        return stationList;
    }

    @Override
    public List<GyeePowerStation> getListByPage(int pageIndex, int pageSize) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime"); //创建时间降序排序
        Pageable pageable = new PageRequest(pageIndex, pageSize, sort);
        Page<GyeePowerStationEntity> entityPage = gyeepowerStationRepository.findAll(pageable);
        List<GyeePowerStation> stationList = DataUtil.convertDataList(entityPage.getContent());
        return stationList;

    }

    @Override
    public GyeePowerStation getStationById(String id) {
        return gyeepowerStationRepository.findById(id).get().toData();
    }

    @Override
    public void add(GyeePowerStation newStation) {
        GyeePowerStationEntity stationEntity = new GyeePowerStationEntity(newStation);
        gyeepowerStationRepository.save(stationEntity);
    }

    @Override
    public void delete(String id) {
        gyeepowerStationRepository.deleteById(id);
    }

    @Override
    public void update(GyeePowerStation station) {
        gyeepowerStationRepository.save(new GyeePowerStationEntity(station));
    }
}
