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.

92 lines
2.7 KiB
Go

package model
import (
"context"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type GameTiXian struct {
ID string `json:"_id" bson:"_id"`
AgentID string `json:"agent_id" bson:"agent_id"`
Type int64 `json:"type" bson:"type"`
Account string `json:"account" bson:"account"`
Status int64 `json:"status" bson:"status"`
Created_at string `json:"created_at" bson:"created_at"`
Updated_at string `json:"updated_at" bson:"updated_at"`
Deleted_at string `json:"deleted_at" bson:"deleted_at"`
}
func GetGameTixianByID(collection *mongo.Collection, id string) (GameTiXian, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
filter := bson.D{{Key: "_id", Value: id}}
var result GameTiXian
err := collection.FindOne(ctx, filter).Decode(&result)
if err != nil && err != mongo.ErrNoDocuments {
return result, err
}
return result, nil
}
func GetGameTiXianList(collection *mongo.Collection, agent_id string) ([]GameTiXian, int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
query := bson.M{}
if agent_id != "" {
query["agent_id"] = bson.M{"$eq": agent_id}
}
cur, err := collection.Find(ctx, query)
if err != nil {
return nil, 0, err
}
defer cur.Close(ctx)
result := []GameTiXian{}
err = cur.All(context.Background(), &result)
if err != nil {
return nil, 0, err
}
count, err := collection.CountDocuments(ctx, query)
if err != nil {
return nil, 0, err
}
return result, count, nil
}
func AddGameTixian(collection *mongo.Collection, doc GameTiXian) 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 UpdateGameTixian(collection *mongo.Collection, id string, doc GameTiXian) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
filter := bson.D{{Key: "_id", Value: id}}
update := bson.D{{Key: "$set", Value: bson.D{{Key: "type", Value: doc.Type}, {Key: "account", Value: doc.Account}, {Key: "status", Value: doc.Status}, {Key: "updated_at", Value: doc.Updated_at}}}}
_, err := collection.UpdateOne(ctx, filter, update)
if err != nil {
return err
}
return nil
}
func AgreeGameTixian(collection *mongo.Collection, id string, status int64, updated_at string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
filter := bson.D{{Key: "_id", Value: id}}
update := bson.D{{Key: "$set", Value: bson.D{{Key: "status", Value: status}, {Key: "updated_at", Value: updated_at}}}}
_, err := collection.UpdateOne(ctx, filter, update)
if err != nil {
return err
}
return nil
}