package com.qiaoba.common.base.entity;

import cn.hutool.http.HttpStatus;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

/**
 * 基础分页封装对象
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023-04-23 15:37:43
 */
@Getter
@Setter
@NoArgsConstructor
public class BasePage<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 总记录数
     */
    public long total;

    /**
     * 列表数据
     */
    public List<T> rows;

    /**
     * 消息状态码
     */
    public int code;

    /**
     * 消息内容
     */
    public String msg;

    /**
     * 分页
     *
     * @param list  列表数据
     * @param total 总记录数
     */
    public BasePage(List<T> list, long total) {
        this.rows = list;
        this.total = total;
    }


    public static <T> BasePage<T> build(List<T> list) {
        BasePage<T> basePage = new BasePage<>();
        basePage.setCode(HttpStatus.HTTP_OK);
        basePage.setMsg("查询成功");
        basePage.setRows(list);
        basePage.setTotal(list.size());
        return basePage;
    }

    public static <T> BasePage<T> build(Long total, List<T> list) {
        BasePage<T> basePage = new BasePage<>();
        basePage.setCode(HttpStatus.HTTP_OK);
        basePage.setMsg("查询成功");
        basePage.setRows(list);
        basePage.setTotal(total);
        return basePage;
    }

    public static <T> BasePage<T> build() {
        BasePage<T> basePage = new BasePage<>();
        basePage.setCode(HttpStatus.HTTP_OK);
        basePage.setMsg("查询成功");
        return basePage;
    }

}
