package com.bonus.canteen.core.nutrition.common.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bonus.common.houqin.constant.LeConstants;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.pagehelper.page.PageMethod;
import io.swagger.annotations.ApiModelProperty;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class PageDTO {
   @ApiModelProperty("当前页(默认1)")
   private Long current;
   @ApiModelProperty("每页显示条数(默认10) -1查询全部")
   private Long size;
   @ApiModelProperty("是否包含count查询 1是 2否 默认是")
   private Integer ifCount;
   @ApiModelProperty("size空(-1)时是否查询全部 1是 2否 默认是")
   private Integer ifPageSizeZero;
   @ApiModelProperty("排序，支持多个（仅支持PageHelper）")
   private List<OrderItem> orders;

   public Long getCurrent() {
      return !ObjectUtil.isNull(this.current) && this.current > 0L ? this.current : 1L;
   }

   public Long getSize() {
      return !ObjectUtil.isNull(this.size) && this.size != 0L ? this.size : 10L;
   }

   public List<OrderItem> getOrders() {
      return ObjectUtil.isNull(this.orders) ? Collections.emptyList() : this.orders;
   }

   @ApiModelProperty(
      value = "分页条件",
      hidden = true
   )
   public String getOrderString() {
      return (String)this.getOrders().stream().map(OrderItem::orderString).collect(Collectors.joining(","));
   }

   @ApiModelProperty(
      value = "PageHelper使用字段：pageNum",
      hidden = true
   )
   public int getPageNum() {
      return this.getCurrent().intValue();
   }

   @ApiModelProperty(
      value = "PageHelper使用字段：pageSize",
      hidden = true
   )
   public int getPageSize() {
      int sizeValue = this.getSize().intValue();
      return sizeValue == -1 ? 0 : sizeValue;
   }

   @ApiModelProperty(
      value = "PageHelper使用字段：orderBy",
      hidden = true
   )
   public String getOrderBy() {
      return this.getOrderString();
   }

   @ApiModelProperty(
      value = "PageHelper使用字段：countSql（是否包含count查询）",
      hidden = true
   )
   public Boolean getCountSql() {
      return LeConstants.COMMON_YES.equals(this.ifCount);
   }

   @ApiModelProperty(
      value = "PageHelper使用字段：pageSizeZero（size 0是否查询全部）",
      hidden = true
   )
   public Boolean getPageSizeZero() {
      return LeConstants.COMMON_YES.equals(this.ifPageSizeZero);
   }

   public void startPage() {
      PageMethod.startPage(this);
   }

   public static PageDTO of(Long current, Long size) {
      PageDTO pageDTO = new PageDTO();
      pageDTO.setCurrent(current);
      pageDTO.setSize(size);
      return pageDTO;
   }

   public static PageDTO countOnly() {
      PageDTO pageDTO = new PageDTO();
      pageDTO.setCurrent(1L);
      pageDTO.setSize(-1L);
      pageDTO.setIfCount(LeConstants.COMMON_YES);
      pageDTO.setIfPageSizeZero(LeConstants.COMMON_NO);
      return pageDTO;
   }

   public static void startPage(PageDTO page) {
      if (!Objects.isNull(page)) {
         page.startPage();
      }
   }

   public PageDTO() {
      this.ifCount = LeConstants.COMMON_YES;
      this.ifPageSizeZero = LeConstants.COMMON_YES;
   }

   public Integer getIfCount() {
      return this.ifCount;
   }

   public Integer getIfPageSizeZero() {
      return this.ifPageSizeZero;
   }

   public void setCurrent(final Long current) {
      this.current = current;
   }

   public void setSize(final Long size) {
      this.size = size;
   }

   public void setIfCount(final Integer ifCount) {
      this.ifCount = ifCount;
   }

   public void setIfPageSizeZero(final Integer ifPageSizeZero) {
      this.ifPageSizeZero = ifPageSizeZero;
   }

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

   public static class OrderItem {
      @ApiModelProperty("字段名")
      private String column;
      @ApiModelProperty("true增序 false降序")
      private Boolean asc = true;

      @JsonIgnore
      public String orderString() {
         String var10000 = StrUtil.toUnderlineCase(this.column);
         return var10000 + " " + (this.asc ? "ASC" : "DESC");
      }

      public String getColumn() {
         return this.column;
      }

      public Boolean getAsc() {
         return this.asc;
      }

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

      public void setAsc(final Boolean asc) {
         this.asc = asc;
      }
   }
}
