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

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.point.LevelTypeTestingPoint;
import com.gyee.wisdom.dao.point.ILevelTypeTestingPointDao;
import com.gyee.wisdom.dao.sql.point.entity.LevelTypeTestingPointEntity;
import com.gyee.wisdom.dao.sql.point.repository.LevelTypeTestingPointRepository;
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 JpaLevelTypeTestingPointDao implements ILevelTypeTestingPointDao {
    @Autowired
    private LevelTypeTestingPointRepository levelTypeTestingPointRepository;

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

        return pointList;
    }

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

    @Override
    public LevelTypeTestingPoint getPointById(String id) {
        LevelTypeTestingPointEntity pointEntity = levelTypeTestingPointRepository.findById(id).get();
        if (pointEntity != null)
            return levelTypeTestingPointRepository.findById(id).get().toData();
        else
            return null;
    }

    @Override
    public void save(LevelTypeTestingPoint newPoint) {
        LevelTypeTestingPointEntity pointEntity = new LevelTypeTestingPointEntity(newPoint);
        levelTypeTestingPointRepository.save(pointEntity);
    }

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

    @Override
    public void update(LevelTypeTestingPoint point) {
        levelTypeTestingPointRepository.save(new LevelTypeTestingPointEntity(point));
    }
}
