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

org.zodiac.scaff.crud.service.CrudService Maven / Gradle / Ivy

package org.zodiac.scaff.crud.service;

import org.zodiac.fastorm.core.param.QueryParam;
import org.zodiac.fastorm.rdb.mapping.SyncDelete;
import org.zodiac.fastorm.rdb.mapping.SyncQuery;
import org.zodiac.fastorm.rdb.mapping.SyncRepository;
import org.zodiac.fastorm.rdb.mapping.SyncUpdate;
import org.zodiac.fastorm.rdb.mapping.defaults.SaveResult;
import org.zodiac.scaff.api.crud.entity.PagerResult;
import org.zodiac.scaff.api.crud.entity.QueryParamEntity;
import org.zodiac.scaff.api.crud.entity.TransactionManagers;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

public interface CrudService {

    SyncRepository getRepository();

    default SyncQuery createQuery() {
        return getRepository().createQuery();
    }

    default SyncUpdate createUpdate() {
        return getRepository().createUpdate();
    }

    default SyncDelete createDelete() {
        return getRepository().createDelete();
    }

    @Transactional(readOnly = true, transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default Optional findById(K id) {
        return getRepository()
                .findById(id);
    }

    @Transactional(readOnly = true, transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default List findById(Collection id) {
        if (CollUtil.isEmptyColl(id)) {
            return Collections.emptyList();
        }
        return this
                .getRepository()
                .findById(id);
    }

    @Transactional(transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default SaveResult save(Collection entityArr) {
        return getRepository()
                .save(entityArr);
    }

    @Transactional(transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default int insert(Collection entityArr) {
        return getRepository()
                .insertBatch(entityArr);
    }

    @Transactional(transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default void insert(E entityArr) {
        getRepository()
                .insert(entityArr);
    }

    @Transactional(transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default int updateById(K id, E entityArr) {
        return getRepository()
                .updateById(id, entityArr);
    }

    @Transactional(transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default SaveResult save(E entity) {
        return getRepository()
                .save(Collections.singletonList(entity));
    }

    @Transactional(transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default SaveResult save(List entities) {
        return getRepository()
                .save(entities);
    }

    @Transactional(transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default int deleteById(Collection idArr) {
        return getRepository().deleteById(idArr);
    }

    @Transactional(transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default int deleteById(K idArr) {
        return deleteById(Collections.singletonList(idArr));
    }

    @Transactional(readOnly = true, transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default List query(QueryParamEntity queryParam) {
        return createQuery().setParam(queryParam).fetch();
    }

    @Transactional(readOnly = true, transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default PagerResult queryPager(QueryParamEntity param) {
        int count = param.getTotal() == null ? count(param) : param.getTotal();
        if (count == 0) {
            return PagerResult.empty();
        }
        param.rePaging(count);
        return PagerResult.of(count, query(param), param);
    }

    @Transactional(readOnly = true, transactionManager = TransactionManagers.JDBC_TRANSACTION_MANAGER)
    default int count(QueryParam param) {
        return getRepository()
                .createQuery()
                .setParam(param)
                .count();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy