You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type Reward struct {
|
|
Key string `json:"key"`
|
|
Value int `json:"value"`
|
|
}
|
|
|
|
type Mail struct {
|
|
ID int `json:"id" bson:"id"`
|
|
Title string `json:"title" bson:"title"`
|
|
Content string `json:"content" bson:"content"`
|
|
Rewards []Reward `json:"rewards" bson:"rewards"`
|
|
Temp string `json:"temp" bson:"temp"`
|
|
Sender string `json:"sender" bson:"sender"`
|
|
Receiver int `json:"receiver" bson:"receiver"`
|
|
Created_at string `json:"created_at" bson:"created_at"`
|
|
}
|
|
|
|
func GetMailList(collection *mongo.Collection, offset int64, limit int64) ([]Mail, int64, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
opts := options.Find().SetLimit(limit).SetSkip(offset).SetSort(bson.D{{Key: "created_at", Value: -1}})
|
|
var filter = bson.D{{Key: "temp", Value: "N"}}
|
|
project := bson.M{"rewards": 0} // 过滤掉 rewards 字段
|
|
cur, err := collection.Find(ctx, filter, opts.SetProjection(project))
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer cur.Close(ctx)
|
|
list := []Mail{}
|
|
err = cur.All(context.Background(), &list)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
count, err := collection.CountDocuments(ctx, filter)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return list, count, nil
|
|
}
|