go_dreamfactory/modules/pay/modelDiamond.go
2024-01-04 16:18:44 +08:00

110 lines
2.8 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"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
//钻石商城
type modelDiamondComp struct {
modules.MCompModel
module *Pay
}
// 组件初始化接口
func (this *modelDiamondComp) 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.TablePayDiamond
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 查询用户重置数据
func (this *modelDiamondComp) getmodel(uid string) (result *pb.DBPayDiamond, err error) {
result = &pb.DBPayDiamond{}
if err = this.Get(uid, result); err != nil && err != mgo.MongodbNil {
this.module.Errorf("err:%v", err)
}
if err == mgo.MongodbNil {
result = &pb.DBPayDiamond{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Items: make(map[int32]int32),
}
err = this.Add(uid, result)
}
return
}
// 添加用户订单数据
func (this *modelDiamondComp) updatemodel(data *pb.DBPayDiamond) (err error) {
if err = this.Change(data.Uid, map[string]interface{}{
"items": data.Items,
}); err != nil {
this.module.Errorln(err)
}
return
}
// 每日礼包发货
func (this *modelDiamondComp) delivery(session comm.IUserSession, id int32) (errdata *pb.ErrorData, items []*pb.UserAtno) {
var (
info *pb.DBPayDiamond
conf *cfg.GameRechargeDiamondData
res []*cfg.Gameatn
ok bool
err error
)
if conf, err = this.module.configure.getGameRechargeDiamond(id); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound,
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
if info, err = this.getmodel(session.GetUserId()); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound,
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
if _, ok = info.Items[conf.Id]; ok {
res = conf.Award
} else {
res = conf.DiamondAward
}
info.Items[conf.Id]++
if errdata, items = this.module.DispenseAtno(session, res, true); errdata != nil {
return
}
if err = this.updatemodel(info); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
go this.module.AsynHandleSession(session.Clone(), func(session comm.IUserSession) {
this.module.WriteUserLog(session.GetUserId(), id, comm.GMResAddType, "deliverybyid", items)
})
return
}