83 lines
2.8 KiB
Go
83 lines
2.8 KiB
Go
package db
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/pb"
|
|
|
|
"go_dreamfactory/lego/core"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
const ( //Redis
|
|
DB_PackTable core.SqlTable = "pack" //背包表
|
|
)
|
|
|
|
type IPack interface {
|
|
///初始化用户物品表
|
|
Pack_InitUserPack(uId string) (pack *pb.DB_UserPackData, err error)
|
|
///查询用户背包
|
|
Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error)
|
|
///更新用户背包数据
|
|
Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (pack *pb.DB_UserPackData, err error)
|
|
///修改用户背包格子的标识
|
|
Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (pack *pb.DB_UserPackData, err error)
|
|
}
|
|
|
|
func (this *DB) Pack_InitUserPack(uId string) (pack *pb.DB_UserPackData, err error) {
|
|
pack = &pb.DB_UserPackData{UserId: uId, Pack: make([]*pb.DB_GridData, 0)}
|
|
_, err = this.mgo.InsertOne(DB_PackTable, pack)
|
|
return
|
|
}
|
|
|
|
///查询用户背包数据
|
|
func (this *DB) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) {
|
|
pack = &pb.DB_UserPackData{}
|
|
err = this.mgo.FindOne(DB_PackTable, bson.M{"_id": uId}).Decode(pack)
|
|
return
|
|
}
|
|
|
|
//更新背包格子数据
|
|
func (this *DB) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (pack *pb.DB_UserPackData, err error) {
|
|
pack = &pb.DB_UserPackData{}
|
|
update := bson.M{}
|
|
for _, v := range grids {
|
|
update[fmt.Sprintf("pack.%d.gridid", v.GridId)] = v.GridId
|
|
update[fmt.Sprintf("pack.%d.isempty", v.GridId)] = v.IsEmpty
|
|
update[fmt.Sprintf("pack.%d.itemid", v.GridId)] = v.ItemId
|
|
update[fmt.Sprintf("pack.%d.amount", v.GridId)] = v.Amount
|
|
update[fmt.Sprintf("pack.%d.isnewitem", v.GridId)] = v.IsNewItem
|
|
}
|
|
|
|
err = this.mgo.FindOneAndUpdate(DB_PackTable,
|
|
bson.M{"_id": uId},
|
|
bson.M{"$set": update},
|
|
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack)
|
|
return
|
|
}
|
|
|
|
//更新背包格子物品的数据
|
|
func (this *DB) Pack_AddGridAmountToUserPack(uId string, grid int32, amount int32) (pack *pb.DB_UserPackData, err error) {
|
|
pack = &pb.DB_UserPackData{}
|
|
err = this.mgo.FindOneAndUpdate(DB_PackTable,
|
|
bson.M{"_id": uId},
|
|
bson.M{"$inc": bson.M{
|
|
fmt.Sprintf("pack.%d.amount", grid): amount,
|
|
}},
|
|
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack)
|
|
return
|
|
}
|
|
|
|
//修改背包格子IsNew标识
|
|
func (this *DB) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (pack *pb.DB_UserPackData, err error) {
|
|
pack = &pb.DB_UserPackData{}
|
|
identifier := []interface{}{bson.M{"item.gridid": bson.M{"$in": grids}}}
|
|
err = this.mgo.FindOneAndUpdate(DB_PackTable,
|
|
bson.M{"_id": uId},
|
|
bson.M{"$set": bson.M{
|
|
"pack.$[item].isNewitem": false,
|
|
}}, options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{Filters: identifier}).SetReturnDocument(options.After)).Decode(pack)
|
|
return
|
|
}
|