package pay import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/mgo" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/x/bsonx" ) ///每日礼包 type modelDailyComp struct { modules.MCompModel module *Pay } //组件初始化接口 func (this *modelDailyComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) { this.MCompModel.Init(service, module, comp, opt) this.module = module.(*Pay) this.TableName = comm.TablePayDaily //创建uid索引 this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, }) return } //查询用户重置数据 func (this *modelDailyComp) queryUserDaily(uId string) (result *pb.DBPayDaily, err error) { result = &pb.DBPayDaily{ Uid: uId, Items: make(map[int32]*pb.PayDailyItem), } if err = this.Get(uId, result); err != nil && err != mgo.MongodbNil { this.module.Errorf("err:%v", err) } if err == mgo.MongodbNil { err = nil } return } //添加用户订单数据 func (this *modelDailyComp) updateUserDaily(info *pb.DBPayDaily) (err error) { if err = this.Change(info.Uid, map[string]interface{}{ "items": info.Items, }); err != nil { this.module.Errorf("err:%v", err) return } return } //每日礼包发货 func (this *modelDailyComp) delivery(session comm.IUserSession, pid string) (errdata *pb.ErrorData, items []*pb.UserAssets) { var ( info *pb.DBPayDaily conf *cfg.GamePayPackageData err error ) if conf, err = this.module.configure.getPayPackageDataByPid(pid); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } if info, err = this.queryUserDaily(session.GetUserId()); err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } if info.Items[conf.Id] == nil { info.Items[conf.Id] = &pb.PayDailyItem{ Id: conf.Id, Buyunm: conf.BuyNum, Lastrefresh: configure.Now().Unix(), } } info.Items[conf.Id].Buyunm-- if err = this.updateUserDaily(info); err != nil { return } items = make([]*pb.UserAssets, len(conf.Item)) for i, v := range conf.Item { items[i] = &pb.UserAssets{ A: v.A, T: v.T, N: v.N, } } if errdata = this.module.DispenseRes(session, conf.Item, true); errdata != nil { return } return }