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.

94 lines
2.8 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 Item struct {
Name string `json:"name"`
Num int `json:"num"`
}
type GameCode struct {
Code string `json:"code" bson:"code"`
Total int `json:"total" bson:"total"`
Num int `json:"num" bson:"num"`
Vip int `json:"vip" bson:"vip"`
Items []Item `json:"items" bson:"items"`
ExpireType int `json:"expire_type" bson:"expire_type"`
StartExpire string `json:"start_expire" bson:"start_expire"`
EndExpire string `json:"end_expire" bson:"end_expire"`
Status int `json:"status" bson:"status"`
}
func GetGameCodeList(collection *mongo.Collection, offset int64, limit int64) ([]GameCode, int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
opts := options.Find().SetLimit(limit).SetSkip(offset).SetSort(bson.D{{Key: "num", Value: -1}})
cur, err := collection.Find(ctx, bson.D{}, opts)
if err != nil {
return nil, 0, err
}
defer cur.Close(ctx)
list := []GameCode{}
err = cur.All(context.Background(), &list)
if err != nil {
return nil, 0, err
}
count, err := collection.CountDocuments(ctx, bson.D{})
if err != nil {
return nil, 0, err
}
return list, count, nil
}
func AddGameCode(collection *mongo.Collection, doc GameCode) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := collection.InsertOne(ctx, doc)
if err != nil {
return err
}
return nil
}
func UpdateGameCode(collection *mongo.Collection, doc GameCode) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
filter := bson.D{{Key: "code", Value: doc.Code}}
update := bson.D{{Key: "$set", Value: bson.D{{Key: "total", Value: doc.Total}, {Key: "vip", Value: doc.Vip}, {Key: "expire_type", Value: doc.ExpireType}, {Key: "start_expire", Value: doc.StartExpire}, {Key: "end_expire", Value: doc.EndExpire}, {Key: "items", Value: doc.Items}}}}
_, err := collection.UpdateOne(ctx, filter, update)
if err != nil {
return err
}
return nil
}
func UpdateGameCodeStatus(collection *mongo.Collection, code string, status int) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
filter := bson.D{{Key: "code", Value: code}}
update := bson.D{{Key: "$set", Value: bson.D{{Key: "status", Value: status}}}}
_, err := collection.UpdateOne(ctx, filter, update)
if err != nil {
return err
}
return nil
}
func DelGameCode(collection *mongo.Collection, code string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
filter := bson.D{{Key: "code", Value: code}}
_, err := collection.DeleteOne(ctx, filter)
if err != nil {
return err
}
return nil
}