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 GameAgent struct { ID string `json:"_id" bson:"_id"` UserName string `json:"username" bson:"username"` RealName string `json:"realname" bson:"realname"` Money float64 `json:"money" bson:"money"` PassWord string `json:"password" bson:"password"` Salt string `json:"salt" bson:"salt"` Type int `json:"type" bson:"type"` Account string `json:"account" bson:"account"` LoginTime string `json:"login_time" bson:"login_time"` LoginIp string `json:"login_ip" bson:"login_ip"` Disable int `json:"disable" bson:"disable"` DelFlag int `json:"del_flag" bson:"del_flag"` 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 GetAgent(collection *mongo.Collection, id string) (GameAgent, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() filter := bson.D{{Key: "_id", Value: id}} var result GameAgent err := collection.FindOne(ctx, filter).Decode(&result) if err != nil && err != mongo.ErrNoDocuments { return result, err } return result, nil } func GetGameAgentByUserName(collection *mongo.Collection, username string) (GameAgent, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() filter := bson.D{{Key: "username", Value: username}} var result GameAgent err := collection.FindOne(ctx, filter).Decode(&result) if err != nil && err != mongo.ErrNoDocuments { return result, err } return result, nil } func AddGameAgent(collection *mongo.Collection, doc GameAgent) 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 UpdatePwdGameAgent(collection *mongo.Collection, id, password, salt, 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: "password", Value: password}, {Key: "salt", Value: salt}, {Key: "updated_at", Value: updated_at}}}} _, err := collection.UpdateOne(ctx, filter, update) if err != nil { return err } return nil } func GetGameAgentList(collection *mongo.Collection, id string, username string, offset int64, limit int64) ([]GameAgent, 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}}) query := bson.M{} query["del_flag"] = bson.M{"$ne": 1} if id != "" { query["_id"] = bson.M{"$eq": id} } if username != "" { query["username"] = bson.M{"$eq": username} } cur, err := collection.Find(ctx, query, opts) if err != nil { return nil, 0, err } defer cur.Close(ctx) users := []GameAgent{} err = cur.All(context.Background(), &users) if err != nil { return nil, 0, err } count, err := collection.CountDocuments(ctx, query) if err != nil { return nil, 0, err } return users, count, nil } func UpdateGameAgent(collection *mongo.Collection, id, login_ip, login_time 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: "login_ip", Value: login_ip}, {Key: "login_time", Value: login_time}}}} _, err := collection.UpdateOne(ctx, filter, update) if err != nil { return err } return nil } func DelGameAgent(collection *mongo.Collection, id, deleted_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: "del_flag", Value: 1}, {Key: "deleted_at", Value: deleted_at}}}} _, err := collection.UpdateOne(ctx, filter, update) if err != nil { return err } return nil } func SetAgentFreezeStatus(collection *mongo.Collection, id string, status int64) 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: "disable", Value: status}}}} _, err := collection.UpdateOne(ctx, filter, update) if err != nil { return err } return nil } func DecrMoney(collection *mongo.Collection, agent_id string, money float64) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() filter := bson.D{{Key: "_id", Value: agent_id}} update := bson.D{{Key: "$inc", Value: bson.D{{Key: "money", Value: money}}}} _, err := collection.UpdateOne(ctx, filter, update) if err != nil { return err } return nil } func UpdateAccount(collection *mongo.Collection, agent_id string, tpe int, account string) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() filter := bson.D{{Key: "_id", Value: agent_id}} update := bson.D{{Key: "$set", Value: bson.D{{Key: "type", Value: tpe}, {Key: "account", Value: account}}}} _, err := collection.UpdateOne(ctx, filter, update) if err != nil { return err } return nil }