package com.ltgk.common.core.page;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ltgk.common.core.domain.BaseAreaCode;
import com.ltgk.common.utils.StringUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.Min;
import java.lang.reflect.Field;

/**
 * @Author: wan kun
 * @Description: 分页demo
 * @Date: 2024/10/26 9:11
 * @Version: 1.0
 */
@Slf4j
@Data
@EqualsAndHashCode(callSuper = true)
public class BasePage extends BaseAreaCode {
    @JsonProperty(value = "pageNum")
    @ApiModelProperty(value = "页码")
    @Min(value = 1, message = "页码必须大于0")
    @TableField(exist = false)
    private Integer pageNum = 1;

    @JsonProperty(value = "pageSize")
    @ApiModelProperty(value = "页长")
    @Min(value = 1, message = "页长必须大于0")
    @TableField(exist = false)
    private Integer pageSize = 15;

    // 校验前端传递数据是否为空字符串，并转换成null
    public void queryCheck() {
        // 使用反射便利所有字段，处理空字符串为null
        Field[] fieldArr = this.getClass().getDeclaredFields();
        try {
            for (Field field : fieldArr) {
                // 设置字段可以被访问
                field.setAccessible(true);
                if (field.get(this) instanceof String) {
                    String value = (String) field.get(this);
                    if (StringUtils.isBlank(value)) {
                        field.set(this, null); // 将空字符串转为 null
                    }
                }
            }
        } catch (IllegalAccessException e) {
            log.error("{}反射失败：详细信息为{}", this.getClass(), e.getMessage());
        }
    }
}
