package com.tofly.feesapi.common.core.dto;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.tofly.feesapi.common.core.vo.PageResult;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@ApiModel("分页参数DTO")
public final class PageDto<T> implements IPage<T> {


    @ApiModelProperty(value = "当前页，默认1", example = "1", position = 1)
    private int current = 1;
    @ApiModelProperty(value = "每页大小，默认20", example = "20", position = 2)
    private int size = 20;

    @ApiModelProperty(value = "排序参数", position = 3)
    private List<Order> orders = new ArrayList<>();

    @ApiModelProperty(value = "总记录数", hidden = true)
    private long total = 0;
    @ApiModelProperty(value = "记录", hidden = true)
    private List<T> records = new ArrayList();



    @Override
    public List<OrderItem> orders() {
        if (orders == null)
            return null;
        return orders.stream()
                .filter(e -> StringUtils.isNotBlank(e.getColumn()))
                .map(e -> new OrderItem(e.getColumn(), e.isAsc()))
                .collect(Collectors.toList());
    }

    public List<Order> getOrders() {
        return orders;
    }

    @Override
    public List<T> getRecords() {
        return records;
    }

    @Override
    public IPage setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    @Override
    public long getTotal() {
        return total;
    }

    @Override
    public IPage setTotal(long total) {
        this.total = total;
        return this;
    }

    @Override
    public long getSize() {
        return size;
    }

    @Override
    public IPage setSize(long size) {
        this.size = (int) size;
        return this;
    }

    @Override
    public long getCurrent() {
        return current;
    }

    @Override
    public IPage setCurrent(long current) {
        this.current = (int) current;
        return this;
    }

    public IPage setOrders(List<Order> orders) {
        this.orders = orders;
        return this;
    }

    public PageResult<T> toVo() {
        return new PageResult(total, records);
    }
}
