85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
package pay
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/mgo"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"time"
|
|
|
|
"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{}
|
|
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(uid, pid string) (code pb.ErrorCode) {
|
|
var (
|
|
info *pb.DBPayDaily
|
|
conf *cfg.GamePayPackageData
|
|
err error
|
|
)
|
|
if conf, err = this.module.configure.getPayPackageDataByPid(pid); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
}
|
|
if info, err = this.queryUserDaily(uid); err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
}
|
|
if info.Items[conf.Id] == nil {
|
|
info.Items[conf.Id] = &pb.PayDailyItem{
|
|
Id: conf.Id,
|
|
Buyunm: conf.BuyNum,
|
|
Lastrefresh: time.Now().Unix(),
|
|
}
|
|
}
|
|
info.Items[conf.Id].Buyunm--
|
|
if err = this.updateUserDaily(info); err != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|