/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.webcore.bean;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.codvision.webcore.util.PageUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 查询参数
 *
 * @author lingee
 * @date 2023/7/20
 */
@Data
@ApiModel(value = "QueryPage", description = "分页查询参数")
public class QueryPage<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "查询条件")
    private T condition;

    @ApiModelProperty(value = "分页对象", hidden = true)
    private IPage<T> page;

    @ApiModelProperty(value = "页号")
    private Integer pageNumber = 1;

    @ApiModelProperty(value = "页面大小")
    private Integer pageSize = 10;

    @ApiModelProperty(value = "排序字段")
    private String sort;

    @ApiModelProperty(value = "排序方式 asc/desc")
    private String order;

    @ApiModelProperty(value = "需要驼峰转换蛇形", notes = "一般不做处理，如果数据库中就是蛇形，则这块需要处理。")
    private Boolean notConvert;

    public QueryPage() {
    }

    public QueryPage(Integer pageNumber, Integer pageSize) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
    }

    public QueryPage(T condition, Integer pageNumber, Integer pageSize) {
        this.condition = condition;
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
    }

    public IPage<T> getPage() {
        return PageUtil.initPage(this);
    }

    @JsonIgnore
    public QueryWrapper<T> getWrapper() {
        return PageUtil.initWrapper(condition);
    }

    public String getSort() {
        if (CharSequenceUtil.isNotEmpty(sort)) {
            if (notConvert == null || Boolean.FALSE.equals(notConvert)) {
                return StringUtils.camelToUnderline(sort);
            } else {
                return sort;
            }
        }
        return sort;
    }
}
