package com.ibeetl.admin.core.web.query;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ibeetl.admin.core.annotation.Query;
import java.lang.reflect.Field;
import java.util.Objects;
import org.beetl.sql.core.page.DefaultPageRequest;
import org.beetl.sql.core.page.PageRequest;

/**
 * 子类继承此类获得翻页功能
 *
 * @author lijiazhi
 */
public class PageParam {

  private Integer page = null;

  private Integer limit = null;

  public Integer getPage() {

    return page;
  }

  public void setPage(Integer page) {

    this.page = page;
  }

  public Integer getLimit() {

    return limit;
  }

  public void setLimit(Integer limit) {

    this.limit = limit;
  }

  /**
   * 以当前类中被{@link Query} 注解的字段为SQL参数
   *
   * @author 一日看尽长安花
   * Created on  2020/3/26
   */
  @JsonIgnore
  public <T> PageRequest<T> getPageRequest() {

    Field[] fs = this.getClass().getDeclaredFields();
    for (Field f : fs) {
      Query query = f.getAnnotation(Query.class);
      if (query == null) {
        continue;
      }
      if (query.fuzzy()) {
        try {
          if (f.getType() == String.class) {
            f.setAccessible(true);
            Object o = f.get(this);
            if (o != null && !o.toString().isEmpty()) {
              f.set(this, "%" + o.toString() + "%");
            }
          }
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        }
      }
    }
    PageRequest pageRequest;

    if (Objects.nonNull(this.page)) {
      pageRequest = DefaultPageRequest.of(page, limit);
    } else {
      pageRequest = new DefaultPageRequest();
    }
    return pageRequest;
  }

  /**
   * 以当前类所有字段为SQL 参数
   *
   * @author 一日看尽长安花
   */
  @JsonIgnore
  public PageRequest createPageRequest() {

    PageRequest pageRequest;

    if (Objects.nonNull(this.page)) {
      pageRequest = DefaultPageRequest.of(page, limit);
    } else {
      pageRequest = new DefaultPageRequest();
    }
    return pageRequest;
  }

}
