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

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.windturbine.PowerStation;

import com.gyee.wisdom.dao.sql.Windturbine.entity.PowerStationEntity;
import com.gyee.wisdom.dao.sql.Windturbine.repsoitory.PowerStationRepository;
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 JpaPowerStationDao implements IPowerStationDao {

    @Autowired
    public PowerStationRepository powerStationRepository;

    @Override
    public List<PowerStation> getAll() {
        powerStationRepository.findAll();
        List<PowerStation> stationList = DataUtil.convertDataList(powerStationRepository.findAll());

        return stationList;
    }

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

    }

    @Override
    public PowerStation getStationById(String id) {
        return powerStationRepository.findById(id).get().toData();
    }
    @Override
    public List<PowerStation> getStationByIdIn(String[] ids) {
        return DataUtil.convertDataList(powerStationRepository.findByIdIn(ids));
    }

    @Override
    public void add(PowerStation newStation) {
        PowerStationEntity stationEntity = new PowerStationEntity(newStation);
        powerStationRepository.save(stationEntity);
    }

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

    @Override
    public void update(PowerStation station) {
        powerStationRepository.save(new PowerStationEntity(station));
    }
}
