package com.cpy.common.util;
import com.github.pagehelper.PageInfo;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @description: 分页数据对象实体
* @version: 1.0
*/
public class PageBean {
/**
* 当前页号
*/
private Integer page;
/**
* 总页数
*/
private Integer total;
/**
* 总记录数
*/
private Long records;
/**
* 数据列表
*/
private List<Map<String, Object[]>> rows = null;
/**
* @description 设置分页数据
* @param pageInfo 分页对象
* @return void
*/
private void setPageInfo(PageInfo<?> pageInfo) {
this.page = pageInfo.getPageNum();
this.total = pageInfo.getPages();
this.records = pageInfo.getTotal();
}
public PageBean() {
}
public PageBean(List<?> list, String[] propertyNames) {
try {
this.rows = buildRows(list, propertyNames);
} catch (Exception e) {
e.printStackTrace();
}
}
public PageBean(List<?> list, String[] propertyNames, String orderName) {
try {
this.rows = buildRowsWithOrder(list, propertyNames, orderName);
} catch (Exception e) {
e.printStackTrace();
}
}
public PageBean(PageInfo<?> pageInfo) {
setPageInfo(pageInfo);
}
/**
* @description 构建分页列表对象
* @param pageInfo 分页对象
* @param propertyNames 列表展示属性
* @return
*/
public PageBean(PageInfo<?> pageInfo, String[] propertyNames) {
setPageInfo(pageInfo);
try {
this.rows = buildRows(pageInfo.getList(), propertyNames);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @description 构建带自增列的分页对象
* @param pageInfo 分页对象
* @param propertyNames 列表展示属性
* @param orderName 自增列的属性名
* @return
*/
public PageBean(PageInfo<?> pageInfo, String[] propertyNames, String orderName) {
setPageInfo(pageInfo);
try {
this.rows = buildRowsWithOrder(pageInfo.getList(), propertyNames, orderName);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @description: 根据属性顺序构建返回的列表数据
* @param: [pageList, propertyNames]
* @return java.util.List<java.util.Map < java.lang.String, java.lang.Object [ ]>>
*/
public List<Map<String, Object[]>> buildRows(List<?> pageList, String[] propertyNames) throws InvocationTargetException, IllegalAccessException, IntrospectionException {
int rowCount = pageList.size();
List<Map<String, Object[]>> rows = new ArrayList<Map<String, Object[]>>(rowCount);
if (rowCount > 0) {
Method[] methods = BeanUtils.getReadMethods(pageList.get(0).getClass(), propertyNames);
int columnLen = methods.length;
Map<String, Object[]> rowMap = null;
Object[] colums = null;
Object bean = null;
Object value = null;
int row = 0, col = 0;
for (row = 0; row < rowCount; row++) {
bean = pageList.get(row);
colums = new Object[columnLen];
for (col = 0; col < columnLen; col++) {
value = methods[col].invoke(bean);
colums[col] = value == null ? "" : String.valueOf(value);
}
rowMap = new HashMap<String, Object[]>(1);
rowMap.put("cell", colums);
rows.add(rowMap);
}
}
return rows;
}
/**
* @description 根据属性顺序构建返回的列表数据
* @param pageList 列表数据
* @param propertyNames 列表展示属性
* @param orderProperty 排序属性名
* @return java.util.List<java.util.Map < java.lang.String, java.lang.Object [ ]>>
*/
public List<Map<String, Object[]>> buildRowsWithOrder(List<?> pageList, String[] propertyNames, String orderProperty) throws InvocationTargetException, IllegalAccessException, IntrospectionException {
int rowCount = pageList.size();
List<Map<String, Object[]>> rows = new ArrayList<Map<String, Object[]>>(rowCount);
if (rowCount > 0) {
Method[] methods = BeanUtils.getReadMethods(pageList.get(0).getClass(), propertyNames);
int columnLen = methods.length;
Map<String, Object[]> rowMap = null;
Object[] colums = null;
Object bean = null;
Object value = null;
int row = 0, col = 0;
for (row = 0; row < rowCount; row++) {
bean = pageList.get(row);
colums = new Object[columnLen];
for (col = 0; col < columnLen; col++) {
if (propertyNames[col].equals(orderProperty)) { //自增序号列
value = row + 1;
} else {
value = methods[col].invoke(bean);
}
colums[col] = value == null ? "" : String.valueOf(value);
}
rowMap = new HashMap<String, Object[]>(1);
rowMap.put("cell", colums);
rows.add(rowMap);
}
}
return rows;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Long getRecords() {
return records;
}
public void setRecords(Long records) {
this.records = records;
}
public List<Map<String, Object[]>> getRows() {
return rows;
}
public void setRows(List<Map<String, Object[]>> rows) {
this.rows = rows;
}
}