package com.ai.da.common.response;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @ClassName PageResponse
 * @Description 分页响应
 * @Author dwjian
 * @Date 2020/1/13 0:26
 */
@Data
@NoArgsConstructor
@ApiModel("分页响应结果")
public class PageResponse<T> extends Response<List<T>> {
    @ApiModelProperty("页码")
    private long page;
    @ApiModelProperty("每页数量")
    private long size;
    @ApiModelProperty("总页数")
    private long pages;
    @ApiModelProperty("总条数")
    private long total;

    @ApiModelProperty("结果集")
    private List<T> content;


    public PageResponse(Response<List<T>> response, long page, long size, long total, long pages) {
        if (response != null) {
            this.setData(response.getData());
            this.setErrCode(response.getErrCode());
            this.setErrMsg(response.getErrMsg());
        }
        this.page = page;
        this.size = size;
        this.total = total;
        this.pages = pages;
        this.content = response.getData();
    }

    public static <T> PageResponse<T> success(IPage<T> page) {
        Response<List<T>> response = success(page.getRecords());
        return new PageResponse<>(response, page.getCurrent(), page.getSize(), page.getTotal(), page.getPages());
    }
}
