package net.northking.cctp.common.db;


import net.northking.cctp.common.util.CamelUtil;

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

/**
 * @Title:
 * @Description:
 * @Company: 京北方信息技术股份有限公司
 * @Author: 谢秀松
 * @CreateDate: 2018/3/19
 * @UpdateUser:
 * @Version: 1.0
 */
public class SqlOrderBy implements OrderBy
{
  private List<Order> orderList = new ArrayList<>();

  public SqlOrderBy addOrderBy(String column, String dir)
  {
    this.orderList.add(new Order(column, dir));
    return this;
  }

  public int size()
  {
    return this.orderList.size();
  }

  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    for (Order order : orderList)
    {
      sb.append(",");
      sb.append(CamelUtil.camelToUnderline(order.getColumn()).toUpperCase());
      sb.append(" ");
      sb.append(order.getDir().toUpperCase());

    }

    return sb.substring(1);
  }

  @Override
  public SqlOrderBy asc(String column)
  {
    addOrderBy(column, "ASC");
    return this;
  }

  @Override
  public SqlOrderBy desc(String column)
  {
    addOrderBy(column, "DESC");
    return this;
  }

  @Override
  public boolean isEmpty()
  {
    return orderList.isEmpty();
  }

  static class Order implements Serializable
  {
    private String column;
    private String dir;

    public Order(String column, String dir)
    {
      this.column = column;
      this.dir = dir;
    }

    public String getColumn()
    {
      return column;
    }

    public void setColumn(String column)
    {
      this.column = column;
    }

    public String getDir()
    {
      return dir;
    }

    public void setDir(String dir)
    {
      this.dir = dir;
    }
  }
}
