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

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.point.ElectricalTestingPoint;
import com.gyee.wisdom.dao.point.IElectricalTestingPointDao;
import com.gyee.wisdom.dao.sql.point.entity.ElectricalTestingPointEntity;
import com.gyee.wisdom.dao.sql.point.repository.ElectricalTestingPointRepository;
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 JpaElectricalTestingPointDao implements IElectricalTestingPointDao {

    @Autowired
    private ElectricalTestingPointRepository electricalTestingPointRepository;

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

        return pointList;
    }

    @Override
    public List<ElectricalTestingPoint> getListByPage(int pageIndex, int pageSize) {
        // Sort sort = new Sort(Sort.Direction.DESC, "createTime"); //创建时间降序排序
        Pageable pageable = new PageRequest(pageIndex, pageSize);
        Page<ElectricalTestingPointEntity> entityPage = electricalTestingPointRepository.findAll(pageable);
        List<ElectricalTestingPoint> pointList = DataUtil.convertDataList(entityPage.getContent());
        return pointList;
    }

    @Override
    public ElectricalTestingPoint getPointById(String id) {
        ElectricalTestingPointEntity entity = electricalTestingPointRepository.findById(id).get();
        if (entity != null)
            return entity.toData();
        else
            return null;

    }

    @Override
    public void save(ElectricalTestingPoint newPoint) {
        ElectricalTestingPointEntity pointEntity = new ElectricalTestingPointEntity(newPoint);
        electricalTestingPointRepository.save(pointEntity);
    }

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

    @Override
    public void update(ElectricalTestingPoint point) {
        electricalTestingPointRepository.save(new ElectricalTestingPointEntity(point));
    }
}
