package com.haha.common.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

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

@Data
public class PageResult<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    private List<T> list;
    
    private long total;
    
    private long pageSize;
    
    private long currentPage;
    
    private long totalPages;

    public PageResult() {
    }

    public PageResult(List<T> list, long total, long pageSize, long currentPage) {
        this.list = list;
        this.total = total;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalPages = pageSize > 0 ? (total + pageSize - 1) / pageSize : 0;
    }

    public static <T> PageResult<T> of(IPage<T> page) {
        PageResult<T> result = new PageResult<>();
        result.setList(page.getRecords());
        result.setTotal(page.getTotal());
        result.setPageSize(page.getSize());
        result.setCurrentPage(page.getCurrent());
        result.setTotalPages(page.getPages());
        return result;
    }

    public static <T, R> PageResult<R> of(IPage<T> page, java.util.function.Function<T, R> mapper) {
        PageResult<R> result = new PageResult<>();
        result.setList(page.getRecords().stream().map(mapper).toList());
        result.setTotal(page.getTotal());
        result.setPageSize(page.getSize());
        result.setCurrentPage(page.getCurrent());
        result.setTotalPages(page.getPages());
        return result;
    }
}
