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

com.stellariver.milky.common.base.PageResult Maven / Gradle / Ivy

The newest version!
package com.stellariver.milky.common.base;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Collections;
import java.util.List;

/**
 * @author houchuang
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class PageResult extends Result> {

    private final Paginator paginator = new Paginator();

    public PageResult() {
        super();
    }

    public PageResult(List data, Integer total, Integer pageNo, Integer pageSize) {
        super();
        this.success = true;
        this.data = data;
        paginator.setPageNo(pageNo);
        paginator.setPageSize(pageSize);
        paginator.setPageCount((total/pageSize + (total%pageSize == 0 ? 0 : 1)));
        paginator.setTotal(total);
    }

    public Paginator getPaginator() {
        return this.paginator;
    }

    public static  PageResult success(List data, Integer total, Integer pageNo, Integer pageSize) {
        return new PageResult<>(data, total, pageNo, pageSize);
    }

    public static  PageResult empty(Integer pageSize) {
        return success(Collections.emptyList(), 0, 1, pageSize);
    }

    public static  PageResult pageError(ErrorEnum errorEnum, ExceptionType type) {
        return pageError(Collections.singletonList(errorEnum), type);
    }

    public static  PageResult pageError(List errorEnums, ExceptionType exceptionType) {
        PageResult result = new PageResult<>();
        result.success = false;
        result.code = errorEnums.get(0).getCode();
        result.detailMessage = errorEnums.get(0).getMessage();
        result.errorEnums = errorEnums;
        result.exceptionType = exceptionType;
        result.message = exceptionType == ExceptionType.BIZ ? result.detailMessage : "系统繁忙,请稍后再试!";
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy