93 lines
2.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.example.caseData.request.caseExchangeRequest;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import lombok.Data;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
@Data
public class getCaseExchangeSearchPage {
// ✅ 分页参数
@Min(value = 1,message = "页码最小为 1")
private Integer page = 1;
@JsonProperty("page_size")
@Min(value = 1, message = "每页个数最小为 1")
private Integer pageSize = 20;
// 标题/作者名称/疾病名称
@JsonProperty("keyword")
private String keyword;
// 用户id
@JsonProperty("user_id")
private String userId;
// 是否被精选0:否 1:是)
@JsonProperty("is_selected")
private Integer isSelected;
// 排序字段
private OrderRequest order;
/**
* 排序字段嵌套结构体
*/
@Data
public static class OrderRequest {
@JsonProperty("created_at")
private String createdAt; // 创建时间
@JsonProperty("read_num")
private String readNum; // 阅读量
@JsonProperty("push_date")
private String pushDate; // 发表时间
public Map<String, String> toMap() {
Map<String, String> map = new HashMap<>();
if (StringUtils.hasText(createdAt)) {
map.put("a.created_at", createdAt);
}
if (StringUtils.hasText(readNum)) {
map.put("a.read_num", readNum);
}
if (StringUtils.hasText(pushDate)) {
map.put("a.push_date", pushDate);
}
// 默认排序(如果用户未传递任何排序字段)
if (map.isEmpty()) {
map.put("a.updated_at", "desc");
}
return map;
}
}
/**
* 获取排序字段,若无用户输入则使用默认排序
*/
public Map<String, String> handleOrder() {
return order != null ? order.toMap() : new OrderRequest().toMap();
}
// ✅ 校验分页参数
public void validateForPage() {
// 如果 page 为空,设为默认值 1
if (page == null) {
page = 1;
}
if (pageSize == null) {
pageSize = 20;
}
}
}