package net.northking.cctp.common.http;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.northking.cctp.common.db.OrderBy;
import net.northking.cctp.common.db.SqlOrderBy;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import javax.validation.constraints.Positive;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页查询对象
 *
 * @author xiusong.xie
 * @since 1.0
 */
@ApiModel(description = "分页查询对象")
public class QueryByPage<T>
{

  /**
   * 查询条件
   */
  @ApiModelProperty("查询条件")
  private T query;

  /**
   * 排序列表
   */
  private List<QueryOrderBy> orderByList = new ArrayList<>();

  /**
   * 每页记录数
   */
  @ApiModelProperty(value = "每页记录数", example = "10")
  @Positive(message = "每页记录数-pageSize: 必须是正整数")
  private int pageSize = 10;

  /**
   * 页号
   */
  @ApiModelProperty(value = "页号", example = "1")
  @Positive(message = "页号-pageNo：必须是正整数")
  private int pageNo = 0;

  /**
   * 获取 query
   *
   * @return the value of query
   */
  public T getQuery()
  {
    return query;
  }

  /**
   * 设置 query
   *
   * @param query query
   */
  public void setQuery(final T query)
  {
    this.query = query;
  }

  /**
   * 获取 orderByList
   *
   * @return the value of orderByList
   */
  public List<QueryOrderBy> getOrderByList()
  {
    return orderByList;
  }

  public boolean hasOrderBy()
  {
    return orderByList != null && !orderByList.isEmpty();
  }

  public void addQueryOrderByDesc(String column)
  {
    orderByList.add(new QueryOrderBy().desc(column));
  }

  public void addQueryOrderByAsc(String column)
  {
    orderByList.add(new QueryOrderBy().asc(column));
  }

  private void addQueryOrderBy(String column, String dir)
  {
    orderByList.add(new QueryOrderBy(column, dir));
  }

  public Pageable toSpringPageable()
  {
    return PageRequest.of(pageNo, pageSize, Sort.by(toSpringOrderList()));
  }

  public List<Sort.Order> toSpringOrderList()
  {
    List<Sort.Order> orderList = new ArrayList<>();
    for (QueryOrderBy orderBy : orderByList)
    {
      if ("desc".equalsIgnoreCase(orderBy.getDir()))
      {
        orderList.add(Sort.Order.desc(orderBy.getColumn()));
      } else
      {
        orderList.add(Sort.Order.asc(orderBy.getColumn()));
      }
    }
    return orderList;
  }

  public OrderBy toSqlOrderBy()
  {
    SqlOrderBy sqlOrderBy = new SqlOrderBy();
    for (QueryOrderBy orderBy : orderByList)
    {
      sqlOrderBy.addOrderBy(orderBy.getColumn(), orderBy.getDir());
    }
    return sqlOrderBy;
  }

  /**
   * 设置 orderByList
   *
   * @param orderByList orderByList
   */
  public void setOrderByList(final List<QueryOrderBy> orderByList)
  {
    this.orderByList = orderByList;
  }

  /**
   * 获取 pageSize
   *
   * @return the value of pageSize
   */
  public int getPageSize()
  {
    return pageSize;
  }

  /**
   * 设置 pageSize
   *
   * @param pageSize pageSize
   */
  public void setPageSize(final int pageSize)
  {
    this.pageSize = pageSize;
  }

  /**
   * 获取 pageNo
   *
   * @return the value of pageNo
   */
  public int getPageNo()
  {
    return pageNo;
  }

  /**
   * 设置 pageNo
   *
   * @param pageNo pageNo
   */
  public void setPageNo(final int pageNo)
  {
    this.pageNo = pageNo;
  }

  /**
   * 翻页并返回页号
   *
   * @return 页号
   */
  public int nextPage()
  {
    this.pageNo++;
    return this.pageNo;
  }

  public Pageable pageable()
  {
    return PageRequest.of(pageNo - 1, pageSize, Sort.by(this.toSpringOrderList()));
  }
}
