package com.zhiqiyun.open.mybatis.paginator.domain;

import org.apache.ibatis.session.RowBounds;

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

/**
 * 分页查询对象
 *
 * @author badqiu
 * @author hunhun
 * @author miemiedev
 */
public class PageBounds extends RowBounds implements Serializable {
    private final static int NO_PAGE = 1;
    private static final long serialVersionUID = -6414350656252331011L;
    private int current = NO_PAGE;
    private List<Order> orders = new ArrayList<>();

    private int total;

    public PageBounds() {
    }

    public PageBounds(int current, int limit) {
        this(current, limit, new ArrayList<>());
    }


    public PageBounds(List<Order> orders) {
        this(NO_PAGE, NO_ROW_LIMIT, orders);
    }


    public PageBounds(Order... order) {
        this(NO_PAGE, NO_ROW_LIMIT, order);
    }

    public PageBounds(int current, int limit, Order... order) {
        this(current, limit, Arrays.asList(order));
    }

    public PageBounds(int current, int limit, List<Order> orders) {
        super(((Math.max(current, 1)) - 1) * limit, limit);
        this.current = current;
        this.orders = orders;
    }


    public int getCurrent() {
        return current;
    }


    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

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

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

    public PageBounds addOrder(Order order) {
        this.orders.add(order);
        return this;
    }


    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("PageBounds{");
        sb.append("current=").append(current);
        sb.append(", limit=").append(this.getLimit());
        sb.append(", offset=").append(this.getOffset());
        sb.append(", orders=").append(orders);
        sb.append('}');
        return sb.toString();
    }


    public <E> PageResult<E> newPageResult(List<E> listData) {
        PageResult<E> pageResult = new PageResult<>();
        pageResult.setPageBounds(this);
        pageResult.setData(listData);
        return pageResult;
    }

}