优化架构

This commit is contained in:
huangxiaolei
2022-11-23 18:05:11 +08:00
parent 3efed3defe
commit 43403202b5
6760 changed files with 33748 additions and 554768 deletions

126
dispatch/dao/account_dao.go Normal file
View File

@@ -0,0 +1,126 @@
package dao
import (
"context"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
dbEntity "hk4e/dispatch/entity/db"
"hk4e/logger"
)
func (d *Dao) GetNextYuanShenUid() (uint64, error) {
db := d.db.Collection("player_id_counter")
find := db.FindOne(context.TODO(), bson.D{{"_id", "default"}})
item := new(dbEntity.PlayerIDCounter)
err := find.Decode(item)
if err != nil {
if err == mongo.ErrNoDocuments {
item := &dbEntity.PlayerIDCounter{
ID: "default",
PlayerID: 100000001,
}
_, err := db.InsertOne(context.TODO(), item)
if err != nil {
return 0, errors.New("insert new PlayerID error")
}
return item.PlayerID, nil
} else {
return 0, err
}
}
item.PlayerID++
_, err = db.UpdateOne(
context.TODO(),
bson.D{
{"_id", "default"},
},
bson.D{
{"$set", bson.D{
{"PlayerID", item.PlayerID},
}},
},
)
if err != nil {
return 0, err
}
return item.PlayerID, nil
}
func (d *Dao) InsertAccount(account *dbEntity.Account) (primitive.ObjectID, error) {
db := d.db.Collection("account")
id, err := db.InsertOne(context.TODO(), account)
if err != nil {
return primitive.ObjectID{}, err
} else {
_id, ok := id.InsertedID.(primitive.ObjectID)
if !ok {
logger.LOG.Error("get insert id error")
return primitive.ObjectID{}, nil
}
return _id, nil
}
}
func (d *Dao) DeleteAccountByUsername(username string) (int64, error) {
db := d.db.Collection("account")
deleteCount, err := db.DeleteOne(
context.TODO(),
bson.D{
{"username", username},
},
)
if err != nil {
return 0, err
} else {
return deleteCount.DeletedCount, nil
}
}
func (d *Dao) UpdateAccountFieldByFieldName(fieldName string, fieldValue any, fieldUpdateName string, fieldUpdateValue any) (int64, error) {
db := d.db.Collection("account")
updateCount, err := db.UpdateOne(
context.TODO(),
bson.D{
{fieldName, fieldValue},
},
bson.D{
{"$set", bson.D{
{fieldUpdateName, fieldUpdateValue},
}},
},
)
if err != nil {
return 0, err
} else {
return updateCount.ModifiedCount, nil
}
}
func (d *Dao) QueryAccountByField(fieldName string, fieldValue any) (*dbEntity.Account, error) {
db := d.db.Collection("account")
find, err := db.Find(
context.TODO(),
bson.D{
{fieldName, fieldValue},
},
)
if err != nil {
return nil, err
}
result := make([]*dbEntity.Account, 0)
for find.Next(context.TODO()) {
item := new(dbEntity.Account)
err := find.Decode(item)
if err != nil {
return nil, err
}
result = append(result, item)
}
if len(result) == 0 {
return nil, nil
} else {
return result[0], nil
}
}

34
dispatch/dao/dao.go Normal file
View File

@@ -0,0 +1,34 @@
package dao
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"hk4e/common/config"
"hk4e/logger"
)
type Dao struct {
client *mongo.Client
db *mongo.Database
}
func NewDao() (r *Dao) {
r = new(Dao)
clientOptions := options.Client().ApplyURI(config.CONF.Database.Url)
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
logger.LOG.Error("mongo connect error: %v", err)
return nil
}
r.client = client
r.db = client.Database("gate_hk4e")
return r
}
func (d *Dao) CloseDao() {
err := d.client.Disconnect(context.TODO())
if err != nil {
logger.LOG.Error("mongo close error: %v", err)
}
}