package com.m2pool.lease.dto;

import com.github.pagehelper.PageInfo;
import com.m2pool.lease.constant.ResponseConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

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

/**
 * @Description 表格分页数据对象
 * @Date 2024/6/13 11:59
 * @Author dy
 */
@Data
@NoArgsConstructor
@ApiModel(description = "分页返回对象")
public class PageResult<T> implements Serializable
{
    private static final long serialVersionUID = 1L;


    /** 成功 */
    public static final int SUCCESS = ResponseConstant.SUCCESS;

    /** 失败 */
    public static final int FAIL = ResponseConstant.FAIL;

    /** 总记录数 */
    @ApiModelProperty(value = "总记录数", example = "1")
    private long total;

    /** 总页数 */
    @ApiModelProperty(value = "总页数", example = "1")
    private long totalPage;

    /** 列表数据 */
    @ApiModelProperty(value = "查询结果集合")
    private List<T> rows;

    /** 消息状态码 */
    @ApiModelProperty(value = "消息状态码", example = "200")
    private int code;

    /** 消息内容 */
    @ApiModelProperty(value = "查询结果描述", example = "成功")
    private String msg;

    @ApiModelProperty(value = "表头信息")
    private List<TableHeadersDto> columns;

    public static <T> PageResult<T> success(List<T> data)
    {
        return setPageResult(data, SUCCESS, "成功");
    }

    public static <T> PageResult<T> successColumns(List<T> data) {
        return setPageResultAndHeader(data, SUCCESS, "成功");
    }

    public static <T> PageResult<T> fail(List<T> data, String msg)
    {
        return setPageResult(data, FAIL, msg);
    }

    private static <T> PageResult<T> setPageResult(List<T> data, int code, String msg)
    {
        PageResult<T> rspData = new PageResult<T>();
        rspData.setCode(SUCCESS);
        rspData.setRows(data);
        rspData.setMsg("查询成功");
        return rspData;
    }
    private static <T> PageResult<T> setPageResultAndHeader(List<T> data, int code, String msg) {
        List<TableHeadersDto> columns = new ArrayList<>();
        columns.add(TableHeadersDto.builder()
                .key("model")
                .label("型号")
                .type("text")
                .fixed("left")
                .width(100)
                .build());
        columns.add(TableHeadersDto.builder()
                .key("price")
                .label("价格")
                .type("amount")
                .width(100)
                .build());

        columns.add(TableHeadersDto.builder()
                .key("monthIncome")
                .label("最大月收益")
                .type("amount")
                .currency("USDT")
                .period("MONTH")
                .width(110)
                .build());

        columns.add(TableHeadersDto.builder()
                .key("maxLeaseDays")
                .label("最大租赁天数")
                .type("days")
                .width(80)
                .build());

        PageResult<T> rspData = new PageResult<T>();
        rspData.setColumns(columns);
        rspData.setCode(SUCCESS);
        rspData.setRows(data);
        rspData.setMsg("查询成功");
        return rspData;
    }
}
