package com.sysu.admin.support.base;

import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQuery;
import com.xiesx.fastboot.core.jpa.JpaPlusRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.List;

/**
 * @author shu hao
 * @description:
 * @date 2022/5/6 8:38
 */
public abstract class BaseService<T,ID> extends RepositoryContext {

    public abstract JpaPlusRepository<T, ID> r();

    public T findOne(ID id){
        if(id == null){
            return null;
        }
        return r().findOne(id);
    }

    public List<T> findAll(Predicate predicate){
        return r().findAll(predicate);
    }

    public Page<T> findAll(Predicate predicate,  Pageable pageable){
        return r().findAll(predicate, pageable);
    }

    public List<T> findAll(Predicate predicate, Sort sort){
        return r().findAll(predicate,sort);
    }

    public <S> Page<S> findAll(JPAQuery<S> query, Pageable pageable){
        return r().findAll(query,pageable);
    }

    public <S> Page<S> findAllNoCount(JPAQuery<S> query, Pageable pageable, long count){
        return r().findAllNoCount(query, pageable, count);
    }


    public int insertOrUpdate(T entity){
        return r().insertOrUpdate(entity);
    }

    public int delete(ID... ids){
        return r().delete(ids);
    }

    public  void deleteAll(Iterable<? extends T> var1){
        r().deleteAll(var1);
    }

    public List<T> findAll(){
        return r().findAll();
    }

    public List<T> findAll(Sort sort){
        return r().findAll(sort);
    }


    public List<T> findAllById(List<ID> ids){
        return r().findAllById(ids);
    }


    public <S extends T> List<S> saveAll(List<S> entities){
        return r().saveAll(entities);
    }


    public <S extends T> S saveAndFlush(S entity){
        return r().saveAndFlush(entity);
    }

    public <S extends T> S save(S entity){
        return r().save(entity);
    }

    public boolean existsById(ID id){
        return r().existsById(id);
    }

    public long count(){
        return r().count();
    }

    @Autowired
    public ServiceContext serviceContext;

}
