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

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.point.TestingPoint;
import com.gyee.wisdom.dao.point.ITestingPointDao;
import com.gyee.wisdom.dao.sql.point.entity.PowerStationTestingPointEntity;
import com.gyee.wisdom.dao.sql.point.entity.TestingPointEntity;
import com.gyee.wisdom.dao.sql.point.repository.TestingPointRepository;
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 JpaTestingPointDao implements ITestingPointDao {
 @Autowired
 private TestingPointRepository testingPointRepository;

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

        return pointList;
    }

    @Override
    public List<TestingPoint> getListByPage(int pageIndex, int pageSize) {
        Pageable pageable =  PageRequest.of(pageIndex, pageSize);
        Page<TestingPointEntity> entityPage = testingPointRepository.findAll(pageable);
        List<TestingPoint> pointList = DataUtil.convertDataList(entityPage.getContent());
        return pointList;
    }

    @Override
    public TestingPoint getPointById(String id) {

        TestingPointEntity entity = testingPointRepository.findById(id).get();
        if (entity != null)
            return entity.toData();
        else
            return null;
    }

    @Override
    public void save(TestingPoint newPoint) {
        TestingPointEntity pointEntity = new TestingPointEntity(newPoint);
        testingPointRepository.save(pointEntity);
    }

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

    @Override
    public void update(TestingPoint point) {
        testingPointRepository.save(new TestingPointEntity(point));
    }
}
