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

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.point.PowerStationTestingPoint;
import com.gyee.wisdom.dao.point.IPowerStationTestingPointDao;
import com.gyee.wisdom.dao.sql.point.entity.ElectricalTestingPointEntity;
import com.gyee.wisdom.dao.sql.point.entity.PowerStationTestingPointEntity;
import com.gyee.wisdom.dao.sql.point.repository.PowerStationTestingPointRepository;
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 JpaPowerStationTestingPointDao implements IPowerStationTestingPointDao {

    @Autowired
    private PowerStationTestingPointRepository powerStationTestingPointRepository;

    @Override
    public List<PowerStationTestingPoint> getAll() {
        List<PowerStationTestingPoint> pointList = DataUtil.convertDataList(powerStationTestingPointRepository.findAll());

        return pointList;
    }

    @Override
    public List<PowerStationTestingPoint> getListByPage(int pageIndex, int pageSize) {
        Pageable pageable = new PageRequest(pageIndex, pageSize);
        Page<PowerStationTestingPointEntity> entityPage = powerStationTestingPointRepository.findAll(pageable);
        List<PowerStationTestingPoint> pointList = DataUtil.convertDataList(entityPage.getContent());
        return pointList;
    }

    @Override
    public PowerStationTestingPoint getPointById(String id) {
        PowerStationTestingPointEntity entity = powerStationTestingPointRepository.findById(id).get();
        if (entity != null)
            return entity.toData();
        else
            return null;
    }

    @Override
    public void save(PowerStationTestingPoint newPoint) {
        PowerStationTestingPointEntity pointEntity = new PowerStationTestingPointEntity(newPoint);
        powerStationTestingPointRepository.save(pointEntity);
    }

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

    @Override
    public void update(PowerStationTestingPoint point) {
        powerStationTestingPointRepository.save(new PowerStationTestingPointEntity(point));
    }
}
