dtp/mongo/mongo.go

167 lines
3.8 KiB
Go
Raw Normal View History

2023-12-24 15:29:12 +08:00
package mongo
import (
"context"
"fmt"
2024-03-16 23:39:10 +08:00
"time"
2023-12-24 15:29:12 +08:00
2023-12-24 22:22:45 +08:00
"github.com/Leufolium/test/dbstruct"
2023-12-24 15:29:12 +08:00
"github.com/qiniu/qmgo"
)
type UserIdMap struct {
Seq int64 `json:"seq" bson:"_id"` //用户业务id序列号
UserId int64 `json:"user_id" bson:"user_id"` //映射后用户业务id序列号
}
type Mongo struct {
clientMix *qmgo.Client
}
func NewMongo() (mongo *Mongo, err error) {
mongo = new(Mongo)
mongo.clientMix, err = NewMongoClient()
if err != nil {
fmt.Printf("NewMongoClient fail, cfg: %v, err: %v", nil, err)
return
}
return
}
const (
DBUserIdSeq = "user_id_seq"
2023-12-24 22:22:45 +08:00
COLUserIdSeq = "user_id_seq"
2023-12-24 15:29:12 +08:00
COLUserIdMap = "user_id_map"
2023-12-24 22:22:45 +08:00
DBAccountIdSeq = "account_id_seq"
COLAccountIdSeq = "account_id_seq"
DBAccount = "account"
COLAccount = "account"
DBLogin = "login"
COLLogin = "login"
2023-12-24 15:29:12 +08:00
)
2023-12-24 22:22:45 +08:00
func (m *Mongo) getColUserIdSeq() *qmgo.Collection {
return m.clientMix.Database(DBUserIdSeq).Collection(COLUserIdSeq)
}
2023-12-24 15:29:12 +08:00
func (m *Mongo) getColUserIdMap() *qmgo.Collection {
return m.clientMix.Database(DBUserIdSeq).Collection(COLUserIdMap)
}
2023-12-24 22:22:45 +08:00
// Account表
func (m *Mongo) getColAccount() *qmgo.Collection {
return m.clientMix.Database(DBAccount).Collection(COLAccount)
}
// 登录表
func (m *Mongo) getColLogin() *qmgo.Collection {
return m.clientMix.Database(DBLogin).Collection(COLLogin)
}
// AccountIdSeq序列表
func (m *Mongo) getColAccountIdSeq() *qmgo.Collection {
return m.clientMix.Database(DBAccountIdSeq).Collection(COLAccountIdSeq)
}
2023-12-24 15:29:12 +08:00
func (m *Mongo) CreateBatch(ctx context.Context, userIdSeqs []*UserIdMap) error {
col := m.getColUserIdMap()
_, err := col.InsertMany(ctx, userIdSeqs)
return err
}
2023-12-24 22:22:45 +08:00
// account相关
func (m *Mongo) CreateAccount(ctx context.Context, account *dbstruct.Account) error {
col := m.getColAccount()
_, err := col.InsertOne(ctx, account)
return err
}
func (m *Mongo) CreateLogin(ctx context.Context, login *dbstruct.Login) error {
col := m.getColLogin()
_, err := col.InsertOne(ctx, login)
return err
}
func (m *Mongo) GetAndUpdateAccountIdSeq(ctx context.Context) (accountIdSeq *dbstruct.AccountIdSeq, err error) {
col := m.getColAccountIdSeq()
change := qmgo.Change{
Update: qmgo.M{"$inc": qmgo.M{"seq": 1}},
Upsert: true,
ReturnNew: false,
}
accountIdSeqInstance := dbstruct.AccountIdSeq{}
if err = col.Find(ctx, qmgo.M{"_id": "account_id_seq_id"}).Apply(change, &accountIdSeqInstance); err != nil {
fmt.Printf("change error : %v", err)
return
}
return &accountIdSeqInstance, err
}
func (m *Mongo) GetAndUpdateUserIdSeq(ctx context.Context) (userIdSeq *dbstruct.UserIdSeq, err error) {
col := m.getColUserIdSeq()
change := qmgo.Change{
Update: qmgo.M{"$inc": qmgo.M{"seq": 1}},
Upsert: true,
ReturnNew: false,
}
userIdSeqInstance := dbstruct.UserIdSeq{}
if err = col.Find(ctx, qmgo.M{"_id": "user_id_seq_id"}).Apply(change, &userIdSeqInstance); err != nil {
fmt.Printf("change error : %v", err)
return
}
return &userIdSeqInstance, err
}
func (m *Mongo) GetMappedUserId(ctx context.Context, userIdSeq int64) (*dbstruct.UserIdMap, error) {
userIdMap := &dbstruct.UserIdMap{}
col := m.getColUserIdMap()
query := qmgo.M{
"_id": userIdSeq,
}
err := col.Find(ctx, query).One(&userIdMap)
return userIdMap, err
}
2024-03-16 23:39:10 +08:00
func (m *Mongo) GetAccountListByMid(ctx context.Context, mid int64) (*dbstruct.Account, error) {
col := m.getColAccount()
account := &dbstruct.Account{}
query := qmgo.M{
"_id": mid,
}
err := col.Find(ctx, query).One(account)
if err == qmgo.ErrNoSuchDocuments {
err = nil
return nil, err
}
if err != nil {
return nil, err
}
return account, err
}
func (m *Mongo) UpdateAccount(ctx context.Context, account *dbstruct.Account) error {
col := m.getColAccount()
set := qmgo.M{
"user_id_string": *account.UserIdString,
}
set["ut"] = time.Now().Unix()
up := qmgo.M{
"$set": set,
}
err := col.UpdateId(ctx, *account.Mid, up)
return err
}