package org.eco.common.core.core.page;

import com.mybatisflex.core.paginate.Page;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

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

/**
 * 分页响应结果
 *
 * @author wgk
 * @date 2024/1/25 15:29
 */
@Data
@NoArgsConstructor
@Accessors(chain = true)
public class PageResult<T> implements Serializable {
    private List<T> list;
    private Long total;
    private Long pageNum;
    private Long pageSize;

    public PageResult(Long total) {
        this.list = new ArrayList<>();
        this.total = total;
    }

    public PageResult(List<T> list, Long total) {
        this.list = list;
        this.total = total;
    }

    /**
     * @param page 分页参数
     * @param <T>  结果泛型信息
     * @return 分页结果
     */
    public static <T> PageResult<T> build(Page<T> page) {
        PageResult<T> result = new PageResult<>();
        result.setList(page.getRecords()).setTotal(page.getTotalRow()).setPageNum(page.getPageNumber()).setPageSize(page.getPageSize());
        return result;
    }

    public static <T> PageResult<T> empty() {
        return new PageResult<>(0L);
    }

    public static <T> PageResult<T> empty(Long total) {
        return new PageResult<>(total);
    }

}
