package com.stdproject.common;

import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.Data;



/**
 * 分页查询请求类
 *
 * @author StdProject
 */
@Data
public class PageRequest {

    /**
     * 当前页码，从1开始
     */
    @NotNull(message = "页码不能为空")
    @Min(value = 1, message = "页码必须大于0")    @Min(value = 1, message = "页码必须大于0")
    private Integer current = 1;

    /**
     * 每页大小
     */
    @NotNull(message = "每页大小不能为空")
    @Min(value = 1, message = "每页大小必须大于0")
    private Integer size = 20;

    /**
     * 排序字段
     */
    private String orderBy;

    /**
     * 排序方向：asc-升序，desc-降序
     */
    private String orderDirection = "desc";

    /**
     * 搜索关键字
     */
    private String keyword;

    /**
     * 获取偏移量
     *
     * @return 偏移量
     */
    public Integer getOffset() {
        return (current - 1) * size;
    }

    /**
     * 是否升序排序
     *
     * @return 是否升序
     */
    public boolean isAsc() {
        return "asc".equalsIgnoreCase(orderDirection);
    }

    /**
     * 是否降序排序
     *
     * @return 是否降序
     */
    public boolean isDesc() {
        return "desc".equalsIgnoreCase(orderDirection);
    }
}