158 lines
3.8 KiB
Go
158 lines
3.8 KiB
Go
package exclusive
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/db"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
// /装备 数据组件
|
|
type modelComp struct {
|
|
modules.MCompModel
|
|
module *Exclusive
|
|
}
|
|
|
|
// 组件初始化接口
|
|
func (this *modelComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
|
this.TableName = comm.TableExclusive
|
|
this.MCompModel.Init(service, module, comp, opt)
|
|
this.module = module.(*Exclusive)
|
|
//创建uid索引
|
|
_, err = this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
|
|
})
|
|
return
|
|
}
|
|
|
|
// 查询用户装备数据
|
|
func (this *modelComp) QueryUserEquipmentsById(uId, id string) (equipment *pb.DB_Equipment, err error) {
|
|
equipment = &pb.DB_Equipment{}
|
|
err = this.GetListObj(uId, id, equipment)
|
|
return
|
|
}
|
|
|
|
// 查询用户装备数据
|
|
func (this *modelComp) QueryUserEquipmentsByIds(uId string, ids []string) (equipments []*pb.DB_Equipment, err error) {
|
|
equipments = []*pb.DB_Equipment{}
|
|
if err = this.GetListObjs(uId, ids, &equipments); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// /查询用户的武器背包
|
|
func (this *modelComp) QueryUserEquipments(uId string) (exclusives []*pb.DB_Exclusive, err error) {
|
|
var (
|
|
model *db.DBModel
|
|
)
|
|
exclusives = make([]*pb.DB_Exclusive, 0)
|
|
if this.module.IsCross() {
|
|
if model, err = this.module.GetDBModelByUid(uId, this.TableName); err != nil {
|
|
this.module.Errorln(err)
|
|
} else {
|
|
if err = model.GetList(uId, &exclusives); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
}
|
|
} else {
|
|
if err = this.GetList(uId, &exclusives); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (this *modelComp) addEquipments(uid string, exclusives []*pb.DB_Exclusive) (err error) {
|
|
var (
|
|
model *db.DBModel
|
|
equipsMap map[string]*pb.DB_Exclusive = make(map[string]*pb.DB_Exclusive)
|
|
)
|
|
for _, v := range exclusives {
|
|
equipsMap[v.Id] = v
|
|
}
|
|
if this.module.IsCross() {
|
|
if model, err = this.module.GetDBModelByUid(uid, this.TableName); err != nil {
|
|
this.module.Errorln(err)
|
|
} else {
|
|
if err = model.AddLists(uid, equipsMap); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
if err = this.AddLists(uid, equipsMap); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// 删除装备
|
|
func (this *modelComp) DelEquipments(uId string, eIds []string) (change []*pb.DB_Exclusive, err error) {
|
|
var (
|
|
model *db.DBModel
|
|
)
|
|
change = make([]*pb.DB_Exclusive, 0)
|
|
if this.module.IsCross() {
|
|
if model, err = this.module.GetDBModelByUid(uId, this.TableName); err != nil {
|
|
this.module.Errorln(err)
|
|
} else {
|
|
if err = model.DelListlds(uId, eIds); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
if err = this.DelListlds(uId, eIds); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
}
|
|
for _, v := range eIds {
|
|
change = append(change, &pb.DB_Exclusive{
|
|
Id: v,
|
|
UId: uId,
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
// 更新武器挂载信息
|
|
func (this *modelComp) UpdateByHeroId(uid string, exclusives ...*pb.DB_Equipment) (err error) {
|
|
var (
|
|
model *db.DBModel
|
|
)
|
|
if this.module.IsCross() {
|
|
if model, err = this.module.GetDBModelByUid(uid, this.TableName); err != nil {
|
|
this.module.Errorln(err)
|
|
} else {
|
|
for _, v := range exclusives {
|
|
if err = model.ChangeList(uid, v.Id, map[string]interface{}{
|
|
"heroId": v.HeroId,
|
|
}); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
for _, v := range exclusives {
|
|
if err = this.ChangeList(uid, v.Id, map[string]interface{}{
|
|
"heroId": v.HeroId,
|
|
}); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|