weaver-hrm-salary/src/com/engine/salary/util/page/SalaryPageUtil.java

109 lines
3.7 KiB
Java
Raw Normal View History

2022-03-17 15:47:14 +08:00
package com.engine.salary.util.page;
import org.apache.commons.collections4.CollectionUtils;
2024-01-17 10:55:42 +08:00
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
2022-06-01 19:14:05 +08:00
import java.util.ArrayList;
2022-03-17 15:47:14 +08:00
import java.util.Collections;
import java.util.List;
2022-05-25 13:10:03 +08:00
public class SalaryPageUtil {
2022-03-17 15:47:14 +08:00
2022-05-19 16:36:18 +08:00
public static <T> PageInfo<T> buildPage(Integer pageNo, Integer pageSize) {
2022-03-17 15:47:14 +08:00
pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
2022-05-19 16:36:18 +08:00
PageInfo<T> pageInfo = new PageInfo<>();
pageInfo.setPageNum(pageNo);
pageInfo.setPageSize(pageSize);
2022-06-01 19:14:05 +08:00
pageInfo.setList(new ArrayList<>());
2022-05-19 16:36:18 +08:00
return pageInfo;
}
2022-12-09 15:28:29 +08:00
public static <T> PageInfo<T> buildPage(Integer pageNo, Integer pageSize, Class<T> clazz) {
2022-05-19 16:36:18 +08:00
pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
PageInfo<T> pageInfo = new PageInfo<>(clazz);
pageInfo.setPageNum(pageNo);
pageInfo.setPageSize(pageSize);
2022-06-01 19:14:05 +08:00
pageInfo.setList(new ArrayList<>());
2022-05-19 16:36:18 +08:00
return pageInfo;
2022-03-17 15:47:14 +08:00
}
2023-12-11 09:52:47 +08:00
public static <T> PageInfo<T> buildPage(Integer pageNo, Integer pageSize, List<T> totalCollection) {
PageInfo<T> pageInfo = new PageInfo<>();
2022-11-11 13:59:37 +08:00
pageInfo.setTotal(totalCollection.size());
totalCollection = subList(pageNo, pageSize, totalCollection);
pageInfo.setPageNum(pageNo);
pageInfo.setPageSize(pageSize);
pageInfo.setList(totalCollection);
return pageInfo;
}
2022-12-09 15:28:29 +08:00
/**
* 内存分页
*
* @param pageNo
* @param pageSize
* @param source
* @param clazz
* @param <T>
* @return
*/
public static <T> PageInfo<T> buildPage(Integer pageNo, Integer pageSize, List<T> source, Class<T> clazz) {
pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
PageInfo<T> pageInfo = new PageInfo<>(clazz);
pageInfo.setPageNum(pageNo);
pageInfo.setPageSize(pageSize);
pageInfo.setTotal(source == null ? 0 : source.size());
pageInfo.setList(subList(pageNo, pageSize, source));
return pageInfo;
}
2022-03-17 15:47:14 +08:00
/**
* 分页
*
* @param pageNo 页码从1开始
* @param pageSize 每页条数
* @param source 待分页的数据
* @param <T> 范型制定类
* @return
*/
2023-01-31 14:15:27 +08:00
public static <T> List<T> subList(Integer pageNo, Integer pageSize, List<T> source) {
2022-03-17 15:47:14 +08:00
if (CollectionUtils.isEmpty(source)) {
return Collections.emptyList();
}
2023-01-31 14:15:27 +08:00
pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
2022-03-17 15:47:14 +08:00
int endIndex = pageNo * pageSize;
int startIndex = (pageNo - 1) * pageSize;
startIndex = startIndex < 0 ? 0 : startIndex;
return source.subList(startIndex > source.size() ? source.size() : startIndex,
endIndex > source.size() ? source.size() : endIndex);
}
2023-12-11 09:52:47 +08:00
2024-01-17 10:55:42 +08:00
static Font font = new Font("Arial", Font.PLAIN, 12); // 设置字体样式、大小等属性
static FontRenderContext frc = new FontRenderContext(null, true, false);
// GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
/**
* 自适应文字长度
* @param chars
* @param width
* @return
*/
2024-01-02 16:37:07 +08:00
public static String selfAdaption(String chars, Integer width) {
2024-01-17 10:55:42 +08:00
if (width != null && width != 0) {
2024-01-02 16:37:07 +08:00
return width + "";
}
2024-01-17 10:55:42 +08:00
Rectangle2D bounds = font.getStringBounds(chars, frc);
int pxLength = (int) Math.ceil(bounds.getWidth());
return pxLength + 55 + "";
2023-12-11 09:52:47 +08:00
}
2022-03-17 15:47:14 +08:00
}