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

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.point.WindturbineTestingPoint;
import com.gyee.wisdom.dao.point.IWindturbineTestingPointDao;
import com.gyee.wisdom.dao.sql.point.entity.TestingPointEntity;
import com.gyee.wisdom.dao.sql.point.entity.WindturbineTestingPointEntity;
import com.gyee.wisdom.dao.sql.point.repository.WindturbineTestingPointRepository;
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 JpaWindturbineTestingPointDao implements IWindturbineTestingPointDao {

    @Autowired
    private WindturbineTestingPointRepository windturbineTestingPointRepository;

    @Override
    public List<WindturbineTestingPoint> getAll() {
        List<WindturbineTestingPoint> pointList = DataUtil.convertDataList(windturbineTestingPointRepository.findAll());
        return pointList;
    }

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

    @Override
    public WindturbineTestingPoint getPointById(String id) {
        WindturbineTestingPointEntity entity = windturbineTestingPointRepository.findById(id).get();
        if (entity != null)
            return entity.toData();
        else
            return null;
    }

    @Override
    public void save(WindturbineTestingPoint newPoint) {
        WindturbineTestingPointEntity pointEntity = new WindturbineTestingPointEntity(newPoint);
        windturbineTestingPointRepository.save(pointEntity);
    }

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

    @Override
    public void update(WindturbineTestingPoint point) {
        windturbineTestingPointRepository.save(new WindturbineTestingPointEntity(point));
    }



    @Override
    public List<WindturbineTestingPoint> findBycategory(Integer category){
        List<WindturbineTestingPointEntity> windturbineTestingPointEntityList=windturbineTestingPointRepository.findBycategory(category);
        return DataUtil.convertDataList(windturbineTestingPointEntityList);
    }


}
