Files
huangguo_server/models/commod/commod.go
T
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

157 lines
3.5 KiB
Go

package commod
import (
"reflect"
"91porn-server/common/stderr"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Page 客户端传低过来的分页信息 对外暴露
type Page struct {
PageNumber uint64 `form:"pageNumber" json:"pageNumber" binding:"required,min=1"` // 当前页
PageSize uint64 `form:"pageSize" json:"pageSize" binding:"required,min=1,max=100"` // 每页条数
Sort []Sorts `form:"sort" json:"sort"`
}
type Sorts struct {
SortKey string `json:"sortKey"`
SortVal int `json:"sortVal"`
}
func (p Page) Skip() uint64 {
return (p.PageNumber - 1) * p.PageSize
}
func (p Page) Skip64() int64 {
return int64((p.PageNumber - 1) * p.PageSize)
}
func (p Page) Limit64() int64 {
return int64(p.PageSize)
}
func (p Page) Limit() uint64 {
return p.PageSize
}
// 注意:Page.Sort内元素顺序决定排序顺序
func (p Page) GetSort() bson.D {
sorts := make(bson.D, len(p.Sort))
for i, v := range p.Sort {
sorts[i] = bson.E{Key: v.SortKey, Value: v.SortVal}
}
return sorts
}
// Sig 签名结构体
type Sig struct {
AccessKey string `form:"accessKey" json:"accessKey" binding:"required"`
BucketName string `form:"bucketName" json:"bucketName" binding:"required"`
}
// Resp 返回结构体
type Resp struct {
Code stderr.Code `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
// ListResp 分页列表 公共返回结构体
type ListResp struct {
Total int64 `json:"total"`
HasNext bool `json:"hasNext"`
List interface{} `json:"list"`
}
// Page 客户端传低过来的分页信息 内部使用
type PageBy struct {
CheckNext bool
Num uint64
Size uint64
}
// OrderBy 排序
type OrderBy struct {
Key string // 排序的字段
Desc bool // 排序方式 是否倒序
}
// StdQuery 客户端传过来的时间条件信息
type StdQuery struct {
Page *PageBy
Order *[]OrderBy
}
// ConvertToListQuery 分页列表 通用条件查询条件组装
func ConvertToListQuery(s StdQuery) (opts *options.FindOptions) {
opts = &options.FindOptions{}
if s.Page != nil {
skip := int64((s.Page.Num - 1) * s.Page.Size)
limit := int64(s.Page.Size)
if s.Page.CheckNext {
limit++
}
opts.Skip = &skip
opts.Limit = &limit
}
if s.Order != nil && len(*s.Order) > 0 {
sort := bson.D{}
for _, v := range *s.Order {
e := bson.E{}
if v.Desc {
e.Key = v.Key
e.Value = -1
} else {
e.Key = v.Key
e.Value = 1
}
sort = append(sort, e)
}
opts.Sort = sort
}
return
}
// StructToMap2 对象转map,去掉无效值
func StructToMap3(u interface{}) map[string]interface{} {
t := reflect.TypeOf(u)
v := reflect.ValueOf(u)
m := make(map[string]interface{})
for i := 0; i < t.NumField(); i++ {
fv := v.Field(i).Type()
vv := v.Field(i)
k, _ := t.Field(i).Tag.Lookup("bson")
switch fv.Kind() {
case reflect.String:
if vv.String() != "" {
if k == "_id" {
idv, _ := primitive.ObjectIDFromHex(vv.String())
m["_id"] = idv
} else {
m[k] = vv.String()
}
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if vv.Int() != 0 {
m[k] = vv.Interface()
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
if vv.Uint() != 0 {
m[k] = vv.Interface()
}
case reflect.Interface, reflect.Ptr:
if vv.Interface() != nil {
m[k] = vv.Interface()
}
}
}
return m
}