1.使用工具PageUtil
List<AdminStationVO> startPage = PageUtil.startPage(findAdminStation, params.getPageNum(), params.getPageSize());
2.PageUtil工具类
package com.kldhq.weight.utils;
import java.util.ArrayList;
import java.util.List;
/** 数据分页工具类 */
public class PageUtil {
/**
* 自定义List分页
*
* @param list
* @param pageNum 页码
* @param pageSize 每页多少条数据
* @return
*/
public static <T> List<T> startPage(List<T> list, Integer pageNum, Integer pageSize) {
List<T> result = new ArrayList<>();
if (list == null || list.size() == 0) {
return result;
}
Integer count = list.size(); // 记录总数
Integer pageCount = 0; // 页数
if (count % pageSize == 0) {
pageCount = count / pageSize;
} else {
pageCount = count / pageSize + 1;
}
int fromIndex = (pageNum - 1) * pageSize; // 开始索引
int toIndex = fromIndex + pageSize; // 结束索引
if (pageNum > pageCount) {
return result;
}
if (toIndex > count) {
toIndex = count;
}
List<T> pageList = list.subList(fromIndex, toIndex);
return pageList;
}
}
3.创建工具用来封装数据
MyPageInfo<AdminStationVO> myPageInfo = new MyPageInfo<AdminStationVO>(params.getPageNum(),findAdminStation.size(),startPage);
4.MyPageInfo类的创建
package com.kldhq.weight.model;
import java.util.List;
import com.github.pagehelper.PageInfo;
public class MyPageInfo<T> {
private int pageNum;
private long total;
private List<T> list;
public MyPageInfo() {
}
public MyPageInfo(PageInfo<T> info) {
this.pageNum = info.getPageNum();
this.total = info.getTotal();
this.list = info.getList();
}
public MyPageInfo(List<T> list) {
PageInfo<T> pageInfo = new PageInfo<T>(list);
this.pageNum = pageInfo.getPageNum();
this.total = pageInfo.getTotal();
this.list = list;
}
public MyPageInfo(int pageNum, long total, List<T> list) {
super();
this.pageNum = pageNum;
this.total = total;
this.list = list;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((list == null) ? 0 : list.hashCode());
result = prime * result + pageNum;
result = prime * result + (int) (total ^ (total >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyPageInfo<?> other = (MyPageInfo<?>) obj;
if (list == null) {
if (other.list != null)
return false;
} else if (!list.equals(other.list))
return false;
if (pageNum != other.pageNum)
return false;
if (total != other.total)
return false;
return true;
}
@Override
public String toString() {
return "MyPageInfo [pageNum=" + pageNum + ", total=" + total + ", list=" + list + "]";
}
}
5综合代码。findAdminStationData实现的接口
@Override
public MyPageInfo<AdminStationVO> findAdminStationData(Integer uid, PageParams params,AdminStationVO adminStationVO) {
// 这里才是重点分页逻辑实现
List<AdminStationVO> findAdminStation = findAdminStation(uid,adminStationVO);
List<AdminStationVO> startPage = PageUtil.startPage(findAdminStation, params.getPageNum(), params.getPageSize());
MyPageInfo<AdminStationVO> myPageInfo = new MyPageInfo<AdminStationVO>(params.getPageNum(),findAdminStation.size(),startPage);
System.out.println("myPageInfo 最终数据"+myPageInfo);
return myPageInfo;
}
public List<AdminStationVO> findAdminStation(Integer uid,AdminStationVO adminStationVO) {
//权限校验
Integer station = ValidUtils.AdminStation(uid);
//station返回2,说明有权限,0没有权限
List<AdminStationVO> newDataList=new ArrayList<AdminStationVO>();
if(station==2) {
//1获取数据
List<AdminStationVO> findAdminStationList = mapper.findAdminStation(adminStationVO);
//2对获取的数据进行从新封装
System.out.println("findAdminStationList"+findAdminStationList);
//遍历获取的数据findAdminStationList
for (AdminStationVO adminStationData : findAdminStationList) {
//通过磅点查询用户数量
Integer userCount = userStationMapper.findStationIdCount(adminStationData.getId());
//创建一个对象接收数据
AdminStationVO vo=new AdminStationVO();
String id = adminStationData.getId();
String stationname = adminStationData.getStationname();
Integer devicetypeid = adminStationData.getDevicetypeid();
String deptName = adminStationData.getDeptName();
String username = adminStationData.getSalesman();
String channel = adminStationData.getChannel();
Date createdime = adminStationData.getCreatedime();//开通时间
Date expirationdate = adminStationData.getExpirationdate();//到期时间
String serialnumber = adminStationData.getSerialnumber();//设备号
String authorizekey = adminStationData.getAuthorizekey();//加密狗的key
String address = adminStationData.getAddress();//地址
String remarks = adminStationData.getRemarks();//开通的功能
String adminUsername = adminStationData.getAdminUsername();//审核人员
Integer serialamount = adminStationData.getSerialamount();
Integer keyamount = adminStationData.getKeyamount();
String checker = adminStationData.getChecker();//加密狗审核人员
Date checkdate = adminStationData.getCheckdate();//加密狗审核时间
String remark = adminStationData.getRemark();//磅点的备注
String function = adminStationData.getFunction();//开通功能的编号
Integer funcid = adminStationData.getFuncid();
Integer type = adminStationData.getType();//cloud_func表 的type字段
String operator = adminStationData.getOperator();
Date time = adminStationData.getTime();
String phone = adminStationData.getPhone();//业务员电话
Integer onlinestatus = adminStationData.getOnlinestatus();
vo.setId(id);
vo.setStationname(stationname);
vo.setDevicetypeid(devicetypeid);
vo.setDeptName(deptName);
vo.setSalesman(username);
vo.setChannel(channel);
vo.setCreatedime(createdime);
vo.setExpirationdate(expirationdate);
vo.setSerialnumber(serialnumber);
vo.setAuthorizekey(authorizekey);
vo.setAddress(address);
vo.setRemarks(remarks);
vo.setAdminUsername(adminUsername);
vo.setType(type);
vo.setFunction(function);
vo.setFuncid(funcid);
vo.setSerialamount(serialamount);
vo.setKeyamount(keyamount);
vo.setChecker(checker);
vo.setCheckdate(checkdate);
vo.setRemark(remark);
vo.setOperator(operator);
vo.setTime(time);
vo.setPhone(phone);
vo.setUserCount(userCount);
vo.setOnlinestatus(onlinestatus);
//对象添加到集合
newDataList.add(vo);
}
List<AdminStationVO> arrayList = new ArrayList<AdminStationVO>();
HashMap<String,AdminStationVO> tempMap = new HashMap<String,AdminStationVO>();
String invests = null;
String quantity = null;
System.out.println("newDataList "+newDataList);
//去掉重复的key
for (AdminStationVO newdata : newDataList) {
String temp = newdata.getId();//user_id相同的属性
String userName = newdata.getStationname();
Integer devicetypeid = newdata.getDevicetypeid();
String deptName = newdata.getDeptName();
String username = newdata.getSalesman();
String channel = newdata.getChannel();
Date createdime = newdata.getCreatedime();//开通时间
Date expirationdate = newdata.getExpirationdate();//到期时间
String serialnumber = newdata.getSerialnumber();//设备号
String authorizekey = newdata.getAuthorizekey();//加密狗的key
String address = newdata.getAddress();//地址
String remarks = newdata.getRemarks();//开通的功能
String adminUsername = newdata.getAdminUsername();//审核人员
Integer type = newdata.getType();//cloud_func表 的type字段
Integer serialamount = newdata.getSerialamount();
Integer keyamount = newdata.getKeyamount();
String checker = newdata.getChecker();
Date checkdate = newdata.getCheckdate();
String remark = newdata.getRemark();
String stationData = newdata.getStationData();
String operator = newdata.getOperator();
Date time = newdata.getTime();
String phone = newdata.getPhone();
Integer userCount = newdata.getUserCount();
Integer onlinestatus = newdata.getOnlinestatus();
System.out.println("stationData"+stationData);
//containsKey(Object key)该方法判断Map集合中是否包含指定的键名,如果包含返回true,不包含返回false
System.out.println("temp"+temp);
if(tempMap.containsKey(temp)){
System.out.println("tempMap.get(temp).getFuncid()"+tempMap.get(temp).getFuncid());
AdminStationVO newInvest = new AdminStationVO();
newInvest.setId(temp);
newInvest.setStationname(userName);
newInvest.setDevicetypeid(devicetypeid);
newInvest.setDeptName(deptName);
newInvest.setSalesman(username);
newInvest.setChannel(channel);
newInvest.setCreatedime(createdime);
newInvest.setExpirationdate(expirationdate);
newInvest.setSerialnumber(serialnumber);
newInvest.setAuthorizekey(authorizekey);
newInvest.setAddress(address);
newInvest.setSerialamount(serialamount);
newInvest.setKeyamount(keyamount);
// newInvest.setAdminUsername(adminUsername);
newInvest.setType(type);
newInvest.setChecker(checker);
newInvest.setCheckdate(checkdate);
newInvest.setRemark(remark);
newInvest.setPhone(phone);
newInvest.setUserCount(userCount);
newInvest.setOnlinestatus(onlinestatus);
// newInvest.setOperator(operator);
// newInvest.setTime(time);
//合并相同invest的 getFuncid();
String fun=tempMap.get(temp).getStationData()+",";
//invests = tempMap.get(temp).getType() + newdata.getType();
invests =fun + newdata.getFuncid();
System.out.println("newdata.getFuncid();"+newdata.getFuncid());
quantity=stationData+invests;
System.out.println("quantity"+quantity);
// quantity = tempMap.get(temp).getQuantity() + investOrder.getQuantity();
newInvest.setStationData(invests);
tempMap.put(temp,newInvest);
}else{
//不存在重复的存入map .第一次把获取到的目标属性字段 添加 到另一个字段中保存
newdata.setStationData(newdata.getFuncid()+"");
tempMap.put(temp,newdata );
}
}
//去除重复key的list
List<AdminStationVO> newList = new ArrayList<AdminStationVO>();
for(String temp:tempMap.keySet()){
newList.add(tempMap.get(temp));
}
//对数据进行分页
System.out.println("newList"+newList.size()+"newList数据 条数"+newList);
return newList;
}else {
throw new MyException("请使用管理员用户");
}
}