118 lines
3.2 KiB
Go
118 lines
3.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// PaginationParams 分页参数
|
|
type PaginationParams struct {
|
|
Page int `form:"page" json:"page"` // 页码,从1开始
|
|
PageSize int `form:"page_size" json:"page_size"` // 每页数量
|
|
Search string `form:"search" json:"search"` // 搜索关键词
|
|
}
|
|
|
|
// WelcomeQueryParams 欢迎语查询参数
|
|
type WelcomeQueryParams struct {
|
|
PaginationParams
|
|
IsActive *bool `form:"is_active" json:"is_active"` // 启用状态筛选
|
|
MessageType string `form:"message_type" json:"message_type"` // 消息类型筛选
|
|
}
|
|
|
|
// CommonQueryParams 通用查询参数(关键词/快捷回复/群发)
|
|
type CommonQueryParams struct {
|
|
PaginationParams
|
|
IsActive *bool `form:"is_active" json:"is_active"` // 启用状态筛选
|
|
MessageType string `form:"message_type" json:"message_type"` // 消息类型筛选
|
|
}
|
|
|
|
// PaginationResponse 分页响应
|
|
type PaginationResponse struct {
|
|
List interface{} `json:"list"`
|
|
Total int64 `json:"total"`
|
|
Page int `json:"page"`
|
|
PageSize int `json:"page_size"`
|
|
Pages int `json:"pages"`
|
|
}
|
|
|
|
// GetPaginationParams 从请求中获取分页参数
|
|
func GetPaginationParams(ctx *gin.Context) PaginationParams {
|
|
params := PaginationParams{
|
|
Page: 1,
|
|
PageSize: 20,
|
|
Search: "",
|
|
}
|
|
|
|
if pageStr := ctx.Query("page"); pageStr != "" {
|
|
if page, err := strconv.Atoi(pageStr); err == nil && page > 0 {
|
|
params.Page = page
|
|
}
|
|
}
|
|
|
|
if pageSizeStr := ctx.Query("page_size"); pageSizeStr != "" {
|
|
if pageSize, err := strconv.Atoi(pageSizeStr); err == nil && pageSize > 0 && pageSize <= 100 {
|
|
params.PageSize = pageSize
|
|
}
|
|
}
|
|
|
|
params.Search = ctx.Query("search")
|
|
|
|
return params
|
|
}
|
|
|
|
// GetWelcomeQueryParams 从请求中获取欢迎语查询参数
|
|
func GetWelcomeQueryParams(ctx *gin.Context) WelcomeQueryParams {
|
|
params := WelcomeQueryParams{
|
|
PaginationParams: GetPaginationParams(ctx),
|
|
MessageType: ctx.Query("message_type"),
|
|
}
|
|
|
|
if isActiveStr := ctx.Query("is_active"); isActiveStr != "" {
|
|
if isActive, err := strconv.ParseBool(isActiveStr); err == nil {
|
|
params.IsActive = &isActive
|
|
}
|
|
}
|
|
|
|
return params
|
|
}
|
|
|
|
// GetCommonQueryParams 从请求中获取通用查询参数
|
|
func GetCommonQueryParams(ctx *gin.Context) CommonQueryParams {
|
|
params := CommonQueryParams{
|
|
PaginationParams: GetPaginationParams(ctx),
|
|
MessageType: ctx.Query("message_type"),
|
|
}
|
|
|
|
if isActiveStr := ctx.Query("is_active"); isActiveStr != "" {
|
|
if isActive, err := strconv.ParseBool(isActiveStr); err == nil {
|
|
params.IsActive = &isActive
|
|
}
|
|
}
|
|
|
|
return params
|
|
}
|
|
|
|
// ApplyPagination 应用分页到查询
|
|
func ApplyPagination(db *gorm.DB, params PaginationParams) *gorm.DB {
|
|
offset := (params.Page - 1) * params.PageSize
|
|
return db.Offset(offset).Limit(params.PageSize)
|
|
}
|
|
|
|
// CreatePaginationResponse 创建分页响应
|
|
func CreatePaginationResponse(list interface{}, total int64, params PaginationParams) PaginationResponse {
|
|
pages := int((total + int64(params.PageSize) - 1) / int64(params.PageSize))
|
|
if pages == 0 {
|
|
pages = 1
|
|
}
|
|
|
|
return PaginationResponse{
|
|
List: list,
|
|
Total: total,
|
|
Page: params.Page,
|
|
PageSize: params.PageSize,
|
|
Pages: pages,
|
|
}
|
|
}
|