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

com.github.wz2cool.dynamic.mybatis.mapper.SelectRowBoundsByDynamicQueryMapper Maven / Gradle / Ivy

There is a newer version: 3.2.32
Show newest version
package com.github.wz2cool.dynamic.mybatis.mapper;

import com.github.wz2cool.dynamic.*;
import com.github.wz2cool.dynamic.helper.CommonsHelper;
import com.github.wz2cool.dynamic.model.LogicPagingResult;
import com.github.wz2cool.dynamic.mybatis.mapper.constant.MapperConstants;
import com.github.wz2cool.dynamic.mybatis.mapper.helper.LogicPagingHelper;
import com.github.wz2cool.dynamic.mybatis.mapper.provider.DynamicQueryProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.session.RowBounds;
import tk.mybatis.mapper.annotation.RegisterMapper;

import java.util.*;

/**
 * @author Frank
 */
@RegisterMapper
public interface SelectRowBoundsByDynamicQueryMapper {

    /**
     * select row rounds by dynamic query.
     *
     * @param dynamicQuery dynamic query
     * @param rowBounds    row bounds
     * @return the list of items
     */
    @SelectProvider(type = DynamicQueryProvider.class, method = "dynamicSQL")
    List selectRowBoundsByDynamicQuery(
            @Param(MapperConstants.DYNAMIC_QUERY) DynamicQuery dynamicQuery,
            RowBounds rowBounds);

    /**
     * select first record by dynamic query
     *
     * @param dynamicQuery dynamic query
     * @return matched first record
     */
    default Optional selectFirstByDynamicQuery(DynamicQuery dynamicQuery) {
        RowBounds rowBounds = new RowBounds(0, 1);
        List result = selectRowBoundsByDynamicQuery(dynamicQuery, rowBounds);
        if (result == null || result.isEmpty()) {
            return Optional.empty();
        } else {
            return Optional.ofNullable(result.get(0));
        }
    }

    /**
     * select by logic paging
     *
     * @param logicPagingQuery logic paging query
     * @return logic paging result
     */
    default LogicPagingResult selectByLogicPaging(LogicPagingQuery logicPagingQuery) {
        int pageSize = logicPagingQuery.getPageSize();
        int queryPageSize = pageSize + 1;
        DynamicQuery dynamicQuery = DynamicQuery.createQuery(logicPagingQuery.getClazz());
        dynamicQuery.addFilters(logicPagingQuery.getFilters());
        dynamicQuery.setDistinct(logicPagingQuery.isDistinct());
        dynamicQuery.setSelectedProperties(logicPagingQuery.getSelectedProperties());
        dynamicQuery.setIgnoredProperties(logicPagingQuery.getIgnoredProperties());
        Map.Entry mapEntry = LogicPagingHelper.getPagingSortFilterMap(
                logicPagingQuery.getPagingPropertyFunc(),
                logicPagingQuery.getSortDescriptor().getDirection(),
                logicPagingQuery.getLastStartPageId(),
                logicPagingQuery.getLastEndPageId(),
                logicPagingQuery.getUpDown());
        // 加上主要排序
        dynamicQuery.addSorts(mapEntry.getKey());
        // 紧跟上次要排序
        dynamicQuery.addSorts(logicPagingQuery.getSorts());
        if (Objects.nonNull(mapEntry.getValue())) {
            dynamicQuery.addFilters(mapEntry.getValue());
        }
        List dataList = selectRowBoundsByDynamicQuery(dynamicQuery, new RowBounds(0, queryPageSize));
        if (!logicPagingQuery.getSortDirection().equals(mapEntry.getKey().getDirection())) {
            Collections.reverse(dataList);
        }
        Optional> logicPagingResultOptional = LogicPagingHelper.getPagingResult(
                logicPagingQuery.getPagingPropertyFunc(),
                dataList, logicPagingQuery.getPageSize(), logicPagingQuery.getUpDown());
        if (logicPagingResultOptional.isPresent()) {
            return logicPagingResultOptional.get();
        }
        LogicPagingQuery resetPagingQuery = LogicPagingQuery.createQuery(
                logicPagingQuery.getClazz(),
                logicPagingQuery.getPagingPropertyFunc(),
                logicPagingQuery.getSortDirection(),
                UpDown.NONE);
        resetPagingQuery.setPageSize(logicPagingQuery.getPageSize());
        resetPagingQuery.setFilters(logicPagingQuery.getFilters());
        return selectByLogicPaging(resetPagingQuery);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy