com.gitee.fufu669.response.ListResponse Maven / Gradle / Ivy
package com.gitee.fufu669.response;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
/** @author wangfupeng */
@ApiModel("列表返回")
public class ListResponse extends SimpleResponse {
public static final int DEFAULT_PAGE_SIZE = 50;
@ApiModelProperty(value = "总数", example = "1")
private Integer count; // 总数
@ApiModelProperty(value = "页大小", example = "20")
private Integer limit; // 页大小
@ApiModelProperty(value = "总页数", example = "1")
private Integer totalPage; // 总页数
@ApiModelProperty(value = "当前页", example = "1")
private Integer page; // 当前页
@ApiModelProperty(value = "当前偏移量", example = "20")
private Integer offset; // 当前偏移量
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getLimit() {
return limit;
}
public void setLimit(Integer limit) {
this.limit = limit;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getOffset() {
return offset;
}
public void setOffset(Integer offset) {
this.offset = offset;
}
public static ListResponse success(List data, int totalCount, int page) {
return success(data, totalCount, page, 20);
}
public static ListResponse success(List data, int totalCount, int page, int pageSize) {
ListResponse response = new ListResponse();
response.setData(data);
response.setStatus(SUCCESS_STATUS);
response.setMessage(SUCCESS_MESSAGE);
if (pageSize == 0)
pageSize = DEFAULT_PAGE_SIZE;
int totalPage = (int) Math.ceil((double) totalCount / pageSize);
if (page > totalPage)
page = totalPage;
if (page <= 0)
page = 1;
int offset = pageSize * (page - 1);
response.setCount(totalCount);
response.setLimit(pageSize);
response.setTotalPage(totalPage);
response.setPage(page);
response.setOffset(offset);
return response;
}
}