95 lines
2.5 KiB
Go
95 lines
2.5 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"mongo.games.com/goserver/core/logger"
|
|
"mongo.games.com/goserver/core/mongox"
|
|
|
|
"mongo.games.com/game/dao/internal"
|
|
modelpkg "mongo.games.com/game/model"
|
|
)
|
|
|
|
var (
|
|
_ = context.Background()
|
|
_ = logger.Logger
|
|
_ = bson.M{}
|
|
_ = mongo.Database{}
|
|
)
|
|
|
|
type AccountLogColumns = internal.AccountLogColumns
|
|
|
|
type AccountLog struct {
|
|
*internal.AccountLog
|
|
}
|
|
|
|
func GetAccountLog(key string) (*AccountLog, error) {
|
|
return mongox.GetDao(key, NewAccountLog)
|
|
}
|
|
|
|
func NewAccountLog(db *mongo.Database, c *mongo.Collection) (*AccountLog, any) {
|
|
if db == nil || c == nil {
|
|
return &AccountLog{}, &modelpkg.AccountLog{}
|
|
}
|
|
|
|
v := internal.NewAccountLog()
|
|
v.Database = db
|
|
v.Collection = c
|
|
|
|
c.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{"platform", 1}}, // 1 表示升序,-1 表示降序
|
|
Options: options.Index().SetBackground(true).SetSparse(true), // 后台创建索引,稀疏索引
|
|
},
|
|
{
|
|
Keys: bson.D{{"snid", 1}}, // 1 表示升序,-1 表示降序
|
|
Options: options.Index().SetBackground(true).SetSparse(true), // 后台创建索引,稀疏索引
|
|
},
|
|
{
|
|
Keys: bson.D{{"faceBookId", 1}}, // 1 表示升序,-1 表示降序
|
|
Options: options.Index().SetBackground(true).SetSparse(true), // 后台创建索引,稀疏索引
|
|
},
|
|
})
|
|
|
|
return &AccountLog{AccountLog: v}, &modelpkg.AccountLog{}
|
|
}
|
|
|
|
func (a *AccountLog) GetByFaceBookId(id string) (*modelpkg.AccountLog, error) {
|
|
ret, err := a.FindOne(context.Background(), func(cols *internal.AccountLogColumns) interface{} {
|
|
return bson.M{cols.FaceBookId: id}
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
func (a *AccountLog) Save(log *modelpkg.AccountLog) error {
|
|
old, err := a.FindOne(context.Background(), func(cols *internal.AccountLogColumns) interface{} {
|
|
return bson.M{cols.SnId: log.SnId}
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if old != nil {
|
|
// 更新
|
|
log.ID = old.ID
|
|
_, err = a.UpdateOne(context.Background(), func(cols *internal.AccountLogColumns) interface{} {
|
|
return bson.M{cols.SnId: log.SnId}
|
|
}, func(cols *internal.AccountLogColumns) interface{} {
|
|
return bson.M{"$set": log}
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
// 插入
|
|
_, err = a.InsertOne(context.Background(), log)
|
|
return err
|
|
}
|