All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.net.vidyo.yd.common.data.service.EntityService Maven / Gradle / Ivy

package cn.net.vidyo.yd.common.data.service;

import cn.hutool.core.bean.BeanUtil;
import cn.net.vidyo.yd.common.data.dao.CommonJpaRepository;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import java.util.List;
import java.util.Optional;

public class EntityService,ENTITY,KEY>  {
    DAO entityDao;

    public EntityService() {
    }

    //
    public DAO getEntityDao() {
        return entityDao;
    }

    public void setEntityDao(DAO entityDao) {
        this.entityDao = entityDao;
    }
    //


    public Object getFieldValueById(KEY id, String fieldName,Object defaultValue){
        ENTITY entity = getById(id);
        if(entity==null){
            return defaultValue;
        }
         return BeanUtil.getFieldValue(entity, fieldName);
    }
    public int setStatusById(KEY id,int status){
        ENTITY entity = getById(id);
        if(entity==null){
            return -1;
        }
        return this.getEntityDao().updateByWhere("status=?","id=?",status,id);
    }
    public int deleteWithLogic(KEY id){
        ENTITY entity = getById(id);
        if(entity==null){
            return -1;
        }
        return this.getEntityDao().updateByWhere("hidden=true","id=?",id);
    }
    public ENTITY insert(ENTITY entity){
        return getEntityDao().insert(entity);
    }
    public ENTITY update(ENTITY entity){
        return getEntityDao().update(entity);
    }


    //
    public Class getEntityClass() {
        return getEntityDao().getEntityClass();
    }

    public Optional findOne(Specification spec) {
        return getEntityDao().findOne(spec);
    }

    public List findAll(Specification spec) {
        return getEntityDao().findAll(spec);
    }

    public Page findAll(Specification spec, Pageable pageable) {
        return getEntityDao().findAll(spec, pageable);
    }

    public List findAll(Specification spec, Sort sort) {
        return getEntityDao().findAll(spec, sort);
    }

    public long count(Specification spec) {
        return getEntityDao().count(spec);
    }

    public List findAll() {
        return getEntityDao().findAll();
    }

    public List findAll(Sort sort) {
        return getEntityDao().findAll(sort);
    }

    public List findByIds(Iterable keys) {
        return getEntityDao().findAllById(keys);
    }

    public  List saveAll(Iterable entities) {
        return getEntityDao().saveAll(entities);
    }

    public void flush() {
        getEntityDao().flush();
    }

    public  S saveAndFlush(S entity) {
        return getEntityDao().saveAndFlush(entity);
    }
//
//    public  List saveAllAndFlush(Iterable entities) {
//        return getEntityDao().saveAndFlush(entities);
//    }

    @Deprecated
    public void deleteInBatch(Iterable entities) {
        getEntityDao().deleteInBatch(entities);
    }
//
//    public void deleteAllInBatch(Iterable entities) {
//        getEntityDao().deleteAllInBatch(entities);
//    }
//
//    public void deleteAllByIdInBatch(Iterable keys) {
//        getEntityDao().deleteAllByIdInBatch(keys);
//    }

    public void deleteAllInBatch() {
        getEntityDao().deleteAllInBatch();
    }

    @Deprecated
    public ENTITY getOne(KEY key) {
        return getEntityDao().getOne(key);
    }

    public ENTITY getById(KEY key) {
        return getEntityDao().getByWhere("id=?1",key);
    }

    public  List findAll(Example example) {
        return getEntityDao().findAll(example);
    }

    public  List findAll(Example example, Sort sort) {
        return getEntityDao().findAll(example, sort);
    }

    public Page findAll(Pageable pageable) {
        return getEntityDao().findAll(pageable);
    }

    public  S save(S entity) {
        return getEntityDao().save(entity);
    }

    public Optional findById(KEY key) {
        return getEntityDao().findById(key);
    }

    public boolean existsById(KEY key) {
        return getEntityDao().existsById(key);
    }

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

    public void deleteById(KEY key) {
        getEntityDao().deleteById(key);
    }

    public void delete(ENTITY entity) {
        getEntityDao().delete(entity);
    }
//
//    public void deleteAllById(Iterable keys) {
//        getEntityDao().deleteAllById(keys);
//    }

    public void deleteAll(Iterable entities) {
        getEntityDao().deleteAll(entities);
    }

    public void deleteAll() {
        getEntityDao().deleteAll();
    }

    public  Optional findOne(Example example) {
        return getEntityDao().findOne(example);
    }

    public  Page findAll(Example example, Pageable pageable) {
        return getEntityDao().findAll(example, pageable);
    }

    public  long count(Example example) {
        return getEntityDao().count(example);
    }

    public  boolean exists(Example example) {
        return getEntityDao().exists(example);
    }

//    public  R findBy(Example example, Function, R> queryFunction) {
//        return getEntityDao().findBy(example, queryFunction);
//    }

    public int dropTable(Class... entityClass){
        return getEntityDao().dropTable(entityClass);
    }
    public int truncateTable(Class... entityClass){
        return getEntityDao().truncateTable(entityClass);
    }
    //
}