package com.yuchen.saas.park.api.commons;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.function.Function;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageWrapper<T> {
    private int current;
    private int size = 20;

    @JsonDeserialize
    @JsonUnwrapped
    private T t;

    public <E> IPage<E> getPage() {
        return new Page<>(current, size);
    }

    public <R> PageWrapper<R> map(Function<T, R> function) {
        return new PageWrapper<>(current, size, function.apply(t));
    }
}
