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

cn.foxtech.common.entity.utils.PageUtils Maven / Gradle / Ivy

The newest version!
/* ----------------------------------------------------------------------------
 * Copyright (c) Guangzhou Fox-Tech Co., Ltd. 2020-2024. All rights reserved.
 * --------------------------------------------------------------------------- */

package cn.foxtech.common.entity.utils;

import cn.foxtech.common.entity.constant.BaseVOFieldConstant;
import cn.foxtech.common.entity.entity.BaseEntity;
import cn.foxtech.common.entity.service.mybatis.BaseEntityMapper;
import cn.foxtech.common.utils.method.MethodUtils;
import cn.foxtech.core.domain.AjaxResult;

import java.util.*;

public class PageUtils {
    public static Map getPageList(List entityList, int pageNum, int pageSize) {
        Map data = new HashMap<>();
        data.put("total", entityList.size());


        List resultList = new ArrayList<>();

        int pageStartId = pageSize * (pageNum - 1);
        int pageEndId = pageSize * pageNum;
        int index = 0;
        for (Object entity : entityList) {
            if (index < pageStartId) {
                index++;
                continue;
            }
            if (index >= pageEndId) {
                break;
            }

            resultList.add(entity);
            index++;
        }


        data.put("list", resultList);


        return data;
    }

    /**
     * 获得分页列表数据
     *
     * @param entityList 实体列表
     * @param body       包含pageNum和pageSize参数的用户参数
     * @return 分页后的数据
     */
    public static AjaxResult getPageList(List entityList, Map body) {
        try {
            Map data = new HashMap<>();
            data.put("total", entityList.size());


            // 根据ID排序
            Collections.sort(entityList, new Comparator() {
                public int compare(BaseEntity o1, BaseEntity o2) {
                    //降序
                    return o2.getId().compareTo(o1.getId());
                }
            });

            if (body.containsKey(BaseVOFieldConstant.field_page_num) && body.containsKey(BaseVOFieldConstant.field_page_size)) {
                List resultList = new ArrayList<>();

                int pageNum = Integer.parseInt(body.get(BaseVOFieldConstant.field_page_num).toString());
                int pageSize = Integer.parseInt(body.get(BaseVOFieldConstant.field_page_size).toString());
                int pageStartId = pageSize * (pageNum - 1);
                int pageEndId = pageSize * pageNum;
                int index = 0;
                for (BaseEntity entity : entityList) {
                    if (index < pageStartId) {
                        index++;
                        continue;
                    }
                    if (index >= pageEndId) {
                        break;
                    }

                    resultList.add(entity);
                    index++;
                }


                data.put("list", EntityVOBuilder.buildVOList(resultList));
            } else {
                data.put("list", EntityVOBuilder.buildVOList(entityList));
            }

            return AjaxResult.success(data);

        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获得分页列表数据
     *
     * @param entityList 实体列表
     * @param body       包含pageNum和pageSize参数的用户参数
     * @return 分页后的数据
     */
    public static AjaxResult getPageMapList(List> entityList, Map body) {
        try {
            Map data = new HashMap<>();
            data.put("total", entityList.size());

            if (body.containsKey(BaseVOFieldConstant.field_page_num) && body.containsKey(BaseVOFieldConstant.field_page_size)) {
                List> resultList = new ArrayList<>();

                int pageNum = Integer.parseInt(body.get(BaseVOFieldConstant.field_page_num).toString());
                int pageSize = Integer.parseInt(body.get(BaseVOFieldConstant.field_page_size).toString());
                int pageStartId = pageSize * (pageNum - 1);
                int pageEndId = pageSize * pageNum;
                int index = 0;
                for (Map entity : entityList) {
                    if (index < pageStartId) {
                        index++;
                        continue;
                    }
                    if (index >= pageEndId) {
                        break;
                    }

                    resultList.add(entity);
                    index++;
                }

                data.put("list", resultList);
            } else {
                data.put("list", entityList);
            }

            return AjaxResult.success(data);

        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 生成分页查询语句
     *
     * @param tableName 表名称
     * @param filter    过滤条件
     * @param order     ID按ASC/DESC排序
     * @param pageNmu   分页号
     * @param pageSize  分页大小
     * @return
     */
    public static String makeSelectSQLPage(String tableName, String filter, String order, Integer total, Long pageNmu, Long pageSize) {
        return makeSelectSQLPage(tableName, null, null, filter, order, total, pageNmu, pageSize);
    }

    /**
     * 生成分页查询语句
     *
     * @param tableName 表名称
     * @param vFields   字段列表
     * @param filter    过滤条件
     * @param order     ID按ASC/DESC排序
     * @param pageNmu   分页号
     * @param pageSize  分页大小
     * @return
     */
    public static String makeSelectSQLPage(String tableName, List vFields, List cFields, String filter, String order, Integer total, Long pageNmu, Long pageSize) {
        StringBuilder sb = new StringBuilder();


        Long pageIndex = (pageNmu - 1) * pageSize;

        String where = "";
        if (filter != null && !filter.isEmpty()) {
            where = "WHERE (" + filter + ")";
        }
        String ASC = "ASC";
        String DESC = "DESC";
        if (order.equals("DESC".toUpperCase())) {
            ASC = "DESC";
            DESC = "ASC";
        }

        StringBuilder sbf = new StringBuilder();
        if (MethodUtils.hasEmpty(vFields, cFields)) {
            sbf.append("t4.* ,");
        }
        if (!MethodUtils.hasEmpty(vFields)) {
            for (String field : vFields) {
                sbf.append("t4.");
                sbf.append(field);
                sbf.append(",");
            }
        }
        if (!MethodUtils.hasEmpty(cFields)) {
            for (String field : cFields) {
                sbf.append(field);
                sbf.append(",");
            }
        }

        String fs = sbf.toString();
        fs = fs.substring(0, fs.length() - 1);

        sb.append("        SELECT " + fs + "                        ");
        sb.append("        FROM " + tableName + " t4                ");
        sb.append("        RIGHT JOIN                               ");
        sb.append("        (                                        ");
        sb.append("                SELECT t1.id                     ");
        sb.append("        FROM " + tableName + " t1                ");
        sb.append(where);
        sb.append("        ORDER BY t1.id " + ASC + "               ");
        sb.append("        LIMIT " + pageIndex + "," + pageSize + " ");
        sb.append("             ) t3                                ");
        sb.append("        ON t4.id = t3.id                         ");
        sb.append("        ORDER BY t4.id " + ASC + "               ");


        return sb.toString();
    }

    /**
     * 生成查询数量语句
     *
     * @param tableName 表名称
     * @param filter    过滤条件
     * @return
     */
    public static String makeSelectCountSQL(String tableName, String filter) {
        StringBuilder sb = new StringBuilder();

        String where = "";
        if (filter != null && !filter.isEmpty()) {
            where = "WHERE (" + filter + ")";
        }

        sb.append("        SELECT count(1)                          ");
        sb.append("        FROM " + tableName + " t1                ");
        sb.append(where);

        return sb.toString();
    }

    public static AjaxResult selectEntityListPage(BaseEntityMapper mapper, String tableName, String filter, String order, long pageNmu, long pageSize) {
        try {
            // 查询总数
            String selectCount = makeSelectCountSQL(tableName, filter);
            Integer total = mapper.executeSelectCount(selectCount);

            // 分页查询数据
            String selectPage = makeSelectSQLPage(tableName, filter, order, total, pageNmu, pageSize);
            List entityList = mapper.executeSelectData(selectPage);

            Map data = new HashMap<>();
            data.put("list", EntityVOBuilder.buildVOList(entityList));
            data.put("total", total);

            return AjaxResult.success(data);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    public static AjaxResult ajaxResultEmpty() {
        Map data = new HashMap<>();
        data.put("list", new ArrayList<>());
        data.put("total", 0);

        return AjaxResult.success(data);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy