package com.hotent.resp;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotent.base.model.CommonResult;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

@ApiModel("分页响应数据格式")
public class PageResp<T> extends CommonResult<PageResp.PageData<T>> {




    /**
     * 分页数据封装
     */
    @ApiModel("分页数据详情")
    @Data
    public static class PageData<T> {
        @ApiModelProperty(value = "总记录数", example = "100")
        private long total;

        @ApiModelProperty(value = "总页数", example = "10")
        private long pages;

        @ApiModelProperty(value = "当前页码", example = "1")
        private long current;

        @ApiModelProperty(value = "每页条数", example = "10")
        private long size;

        @ApiModelProperty(value = "当前页数据列表")
        private List<T> records;

        public PageData(IPage<T> page) {
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.current = page.getCurrent();
            this.size = page.getSize();
            this.records = page.getRecords();
        }
    }

    /**
     * 构建分页响应
     */
    public static <T> PageResp<T> success(IPage<T> page) {
        PageResp<T> response = new PageResp<>();
        response.setCode(200);
        response.setMessage("操作成功");
        response.setValue(new PageData<>(page));
        return response;
    }
    /**
     * 构建错误响应
     */
    public static <T> PageResp<T> error(String msg) {
        PageResp<T> response = new PageResp<>();
        response.setCode(500);
        response.setMessage(msg);
        return response;
    }
}
