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

com.github.wzc789376152.springboot.utils.PageUtils Maven / Gradle / Ivy

The newest version!
package com.github.wzc789376152.springboot.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReflectUtil;
import com.alibaba.ttl.threadpool.TtlExecutors;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

public class PageUtils {
    private final Integer pageNum;
    private final Integer pageSize;

    private final ThreadPoolTaskExecutor executor;

    private Boolean count = true;
    private Page page;

    @FunctionalInterface
    public interface PageFunction {
        void list();
    }

    @FunctionalInterface
    public interface PageConvertTwoFunction {
        R convert(T1 t1, T2 t2);
    }

    @FunctionalInterface
    public interface PageConvertFunction {
        R convert(T object);
    }

    public PageUtils(Integer pageNum, Integer pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        if (pageSize == 0) {
            this.count = false;
        }
        executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(4);
        executor.setMaxPoolSize(8);
        executor.setQueueCapacity(10000);
        executor.setThreadNamePrefix("pageResult-handle-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
    }

    public PageUtils count(Boolean count) {
        this.count = count;
        return this;
    }

    public static PageUtils page(Integer pageNum, Integer pageSize) {
        return new PageUtils(pageNum, pageSize);
    }

    public PageUtils start(PageFunction pageFunction) {
        page = PageHelper.startPage(pageNum, pageSize, count, null, pageSize == 0);
        try {
            pageFunction.list();
        } finally {
            PageHelper.clearPage();
        }
        return this;
    }

    public  PageInfo result(Class tClass) {
        Page toPage = page(tClass);
        return PageInfo.of(toPage);
    }

    private  Page page(Class tClass) {
        Page toPage = new Page<>(page.getPageNum(), page.getPageSize());
        toPage.setPages(page.getPages());
        toPage.setTotal(page.getTotal());
        for (Object from : page.getResult()) {
            if (from.getClass().getName().equals(tClass.getName())) {
                toPage.add((T) from);
            } else {
                T toObj = ReflectUtil.newInstance(tClass);
                Map stringBeanMap = BeanUtil.beanToMap(from);
                if (stringBeanMap != null) {
                    T toInstance = BeanUtil.fillBeanWithMapIgnoreCase(stringBeanMap, toObj, true);
                    if (toInstance != null) {
                        toPage.add(toInstance);
                    }
                }
            }
        }
        return toPage;
    }

    public  PageUtils resultThen(Class tClass) {
        page = page(tClass);
        return this;
    }

    public  PageInfo result(PageConvertFunction pageConvertFunction) {
        ExecutorService executorService = TtlExecutors.getTtlExecutorService(executor.getThreadPoolExecutor());
        try {
            Page toPage = new Page<>(page.getPageNum(), page.getPageSize());
            toPage.setPages(page.getPages());
            toPage.setTotal(page.getTotal());
            List> futureList = new ArrayList<>();
            for (Object from : page.getResult()) {
                futureList.add(executorService.submit(() -> pageConvertFunction.convert((S) from)));
            }
            for (Future future : futureList) {
                try {
                    toPage.add(future.get());
                } catch (InterruptedException | ExecutionException e) {
                    throw new RuntimeException(e);
                }
            }
            return PageInfo.of(toPage);
        } finally {
            executor.shutdown();
        }
    }

    public  PageInfo result(PageConvertTwoFunction pageConvertFunction, P param) {
        ExecutorService executorService = TtlExecutors.getTtlExecutorService(executor.getThreadPoolExecutor());
        try {
            Page toPage = new Page<>(page.getPageNum(), page.getPageSize());
            toPage.setPages(page.getPages());
            toPage.setTotal(page.getTotal());
            List> futureList = new ArrayList<>();
            for (Object from : page.getResult()) {
                futureList.add(executorService.submit(() -> pageConvertFunction.convert((S) from, param)));
            }
            for (Future future : futureList) {
                try {
                    toPage.add(future.get());
                } catch (InterruptedException | ExecutionException e) {
                    throw new RuntimeException(e);
                }
            }
            return PageInfo.of(toPage);
        } finally {
            executor.shutdown();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy