diff --git a/dbproxy/mq/c_invite.go b/dbproxy/mq/c_invite.go index 4d0ec74..c0b6c34 100644 --- a/dbproxy/mq/c_invite.go +++ b/dbproxy/mq/c_invite.go @@ -45,18 +45,18 @@ func init() { } // 增加积分 - add := func(snid int32, score, money, rate int64) error { - if snid == 0 { + add := func(psnid, snid, level, tp int32, score, money, rate int64, addMoney bool) error { + if psnid <= 0 { return nil } - err = svc.AddInviteScore(log.Platform, snid, score*rate/10000, money, now) + err = svc.AddInviteScore(log.Platform, psnid, snid, level, tp, score*rate/10000, rate, money, now, addMoney) if err != nil { - logger.Logger.Errorf("EvtInvite add error:%v snid:%v score:%v rate:%v", err, snid, score, rate) + logger.Logger.Errorf("EvtInvite add error:%v psnid:%v score:%v rate:%v", err, psnid, score, rate) return err } - msg, err := get(snid) + msg, err := get(psnid) if err != nil { - logger.Logger.Errorf("EvtInvite add find error:%v snid:%v score:%v rate:%v", err, snid, score, rate) + logger.Logger.Errorf("EvtInvite add find error:%v psnid:%v score:%v rate:%v", err, psnid, score, rate) return err } mq.Send(model.EvtInviteAck, msg) @@ -120,7 +120,7 @@ func init() { var addRechargeScore bool if log.Tp != common.InviteScoreCheckWeek { - addRechargeScore, err = svc.SaveInviteScore(&log.InviteScore, log.RechargeScore) + addRechargeScore, err = svc.CheckInviteScore(&log.InviteScore) if err != nil { logger.Logger.Errorf("EvtInvite SaveInviteScore error:%v msg:%+v %+v", err, log.InviteScore, log) return err @@ -143,7 +143,7 @@ func init() { // 更新绑定数量 // 更新邀请积分 // 1.邀请人增加积分 - add(log.InviteSnId, log.Score, 0, 10000) + add(log.InviteSnId, log.SnId, 0, log.Tp, log.Score, log.Money, 10000, false) // 2.上级增加积分 var psnid int32 if len(log.Rate) > 0 { @@ -153,7 +153,7 @@ func init() { return err } if psnid > 0 { - add(psnid, log.Score, 0, log.Rate[0]) + add(psnid, log.InviteSnId, 1, log.Tp, log.Score, log.Money, log.Rate[0], false) } } if len(log.Rate) > 1 && psnid > 0 { @@ -163,16 +163,16 @@ func init() { return err } if psnid > 0 { - add(psnid, log.Score, 0, log.Rate[1]) + add(psnid, log.InviteSnId, 2, log.Tp, log.Score, log.Money, log.Rate[1], false) } } case common.InviteScoreTypePay: // 更新充值积分,上级积分增加 - add(log.SnId, log.Score, 0, 10000) + add(log.SnId, 0, 0, log.Tp, log.Score, log.Money, 10000, false) var psnid int32 if len(log.Rate) > 0 { - add(log.InviteSnId, log.Score, log.Money, log.Rate[0]) + add(log.InviteSnId, log.SnId, 1, log.Tp, log.Score, log.Money, log.Rate[0], true) psnid, err = svc.GetPSnId(log.Platform, log.InviteSnId) if err != nil { logger.Logger.Errorf("EvtInvite GetPSnId 3 error:%v snid:%v", err, log.InviteSnId) @@ -180,7 +180,7 @@ func init() { } } if len(log.Rate) > 1 && psnid > 0 { - add(psnid, log.Score, 0, log.Rate[1]) + add(psnid, log.SnId, 2, log.Tp, log.Score, log.Money, log.Rate[1], false) psnid, err = svc.GetPSnId(log.Platform, psnid) if err != nil { logger.Logger.Errorf("EvtInvite GetPSnId 4 error:%v snid:%v", err, psnid) @@ -188,18 +188,19 @@ func init() { } } if len(log.Rate) > 2 && psnid > 0 { - add(psnid, log.Score, 0, log.Rate[2]) + add(psnid, log.SnId, 3, log.Tp, log.Score, log.Money, log.Rate[2], false) } if addRechargeScore { - add(log.InviteSnId, log.RechargeScore, log.Money, 10000) + add(log.InviteSnId, log.SnId, 0, common.InviteScoreTypeRecharge, log.RechargeScore, log.Money, 10000, true) } case common.InviteScoreTypeRecharge: // 更新自己的积分 if addRechargeScore { - add(log.InviteSnId, log.RechargeScore, log.Money, 10000) + add(log.InviteSnId, log.SnId, 0, log.Tp, log.RechargeScore, log.Money, 10000, true) } else { - add(log.InviteSnId, 0, log.Money, 10000) + // 只增加充值金额 + add(log.InviteSnId, log.SnId, 0, log.Tp, 0, log.Money, 10000, true) } case common.InviteScoreCheckWeek: diff --git a/dbproxy/svc/l_invitescore.go b/dbproxy/svc/l_invitescore.go index a09bb62..f094962 100644 --- a/dbproxy/svc/l_invitescore.go +++ b/dbproxy/svc/l_invitescore.go @@ -26,8 +26,9 @@ func InviteScoreCollection(plt string) *mongo.Collection { if s != nil { c, first := s.DB().C(InviteScoreCollName) if first { - c.EnsureIndex(mgo.Index{Key: []string{"snid"}, Background: true, Sparse: true}) - c.EnsureIndex(mgo.Index{Key: []string{"invitesnid"}, Background: true, Sparse: true}) + c.EnsureIndex(mgo.Index{Key: []string{"upsnid"}, Background: true, Sparse: true}) + c.EnsureIndex(mgo.Index{Key: []string{"downsnid"}, Background: true, Sparse: true}) + c.EnsureIndex(mgo.Index{Key: []string{"level"}, Background: true, Sparse: true}) c.EnsureIndex(mgo.Index{Key: []string{"tp"}, Background: true, Sparse: true}) c.EnsureIndex(mgo.Index{Key: []string{"ts"}, Background: true, Sparse: true}) c.EnsureIndex(mgo.Index{Key: []string{"-ts"}, Background: true, Sparse: true}) @@ -43,31 +44,21 @@ func InviteScoreCollection(plt string) *mongo.Collection { type BindScoreSvc struct { } -func SaveInviteScore(req *model.InviteScore, rechargeScore int64) (b bool, err error) { +func CheckInviteScore(req *model.InviteScore) (b bool, err error) { logger.Logger.Tracef("SaveInviteScore req:%+v", *req) if req.InviteSnId == 0 || req.SnId == 0 { return false, nil } - req.Id = bson.NewObjectId() u := PlayerDataCollection(req.Platform) if u == nil { return false, PlayerColError } - c := InviteScoreCollection(req.Platform) - if c == nil { - return false, InviteScoreColError - } - // 不能重复绑定 if req.Tp == common.InviteScoreTypeBind { - a := &model.InviteScore{} - err = c.Find(bson.M{"snid": req.SnId, "tp": common.InviteScoreTypeBind}).One(a) - if err != nil && !errors.Is(err, mgo.ErrNotFound) { - return false, errors.New("find error") - } - if err == nil && a.InviteSnId > 0 { + psnid, err := GetPSnId(req.Platform, req.SnId) + if err == nil && psnid > 0 { // 已经绑定 return false, errors.New("already bind") } @@ -75,11 +66,10 @@ func SaveInviteScore(req *model.InviteScore, rechargeScore int64) (b bool, err e // 必须已经绑定 if req.Tp != common.InviteScoreTypeBind { - a := &model.InviteScore{} - err = c.Find(bson.M{"snid": req.SnId, "tp": common.InviteScoreTypeBind}).One(a) - if err != nil { + psnid, err := GetPSnId(req.Platform, req.SnId) + if err != nil || psnid == 0 { // 还没有绑定上级 - logger.Logger.Errorf("GetInviteScore Find BindTime error:%v", err) + logger.Logger.Errorf("not bind error:%v", err) return false, err } } @@ -87,83 +77,20 @@ func SaveInviteScore(req *model.InviteScore, rechargeScore int64) (b bool, err e // 充值成功记录 if req.Tp == common.InviteScoreTypePay || req.Tp == common.InviteScoreTypeRecharge { // 是否已经记录过 - a := &model.InviteScore{} - err = c.Find(bson.M{"snid": req.SnId, "tp": common.InviteScoreTypeRecharge}).One(a) + c := InviteScoreCollection(req.Platform) + if c == nil { + return false, InviteScoreColError + } + a := &model.LogInviteScore{} + err = c.Find(bson.M{"downsnid": req.SnId, "upsnid": req.InviteSnId, "tp": common.InviteScoreTypeRecharge}).One(a) if err != nil && !errors.Is(err, mgo.ErrNotFound) { return false, errors.New("find error") } - if err == nil && a.InviteSnId > 0 { - // 已有记录 - if req.Tp == common.InviteScoreTypeRecharge { + if err == nil && a.UpSnid > 0 { - } else { - err = c.Insert(req) - if err != nil { - logger.Logger.Errorf("SaveInviteScore Insert 1 error:%v", err) - return false, err - } - // 自己加积分 - err = c.Insert(&model.InviteScore{ - Id: bson.NewObjectId(), - Platform: req.Platform, - SnId: req.SnId, - InviteSnId: req.InviteSnId, - Tp: common.InviteScoreTypePayMe, - Score: req.Score, - Ts: req.Ts, - Money: req.Money, - Rate: req.Rate, - }) - if err != nil { - logger.Logger.Errorf("SaveInviteScore Insert me error:%v", err) - return false, err - } - } } else { - // 没有充值成功记录 - err = c.Insert(req) - if err != nil { - logger.Logger.Errorf("SaveInviteScore Insert 2 error:%v", err) - return false, err - } - if req.Tp == common.InviteScoreTypePay { - // 自己加积分 - err = c.Insert(&model.InviteScore{ - Id: bson.NewObjectId(), - Platform: req.Platform, - SnId: req.SnId, - InviteSnId: req.InviteSnId, - Tp: common.InviteScoreTypePayMe, - Score: req.Score, - Ts: req.Ts, - Money: req.Money, - Rate: req.Rate, - }) - if err != nil { - logger.Logger.Errorf("SaveInviteScore Insert me 2 error:%v", err) - return false, err - } - } - - newReq := *req - if newReq.Tp != common.InviteScoreTypeRecharge { - newReq.Id = bson.NewObjectId() - newReq.Tp = common.InviteScoreTypeRecharge - newReq.Score = rechargeScore - err = c.Insert(newReq) - if err != nil { - logger.Logger.Errorf("SaveInviteScore InviteScoreTypeRecharge Insert error:%v", err) - return false, err - } - } b = true } - } else { - err = c.Insert(req) - if err != nil { - logger.Logger.Errorf("SaveInviteScore Insert 3 error:%v", err) - return false, err - } } return b, nil diff --git a/dbproxy/svc/u_player.go b/dbproxy/svc/u_player.go index 3ca2016..3cae6d2 100644 --- a/dbproxy/svc/u_player.go +++ b/dbproxy/svc/u_player.go @@ -334,13 +334,13 @@ func (svc *PlayerDataSvc) GetPlayerDataBySnId(args *model.GetPlayerDataBySnIdArg if c == nil { return } - res := new(model.InviteScore) - c.Find(bson.M{"snid": args.SnId, "tp": common.InviteScoreTypeBind}).One(res) + res := new(model.LogInviteScore) + c.Find(bson.M{"downsnid": args.SnId, "tp": common.InviteScoreTypeBind}).One(res) param := bson.M{} - if res.InviteSnId > 0 { - p.PSnId = res.InviteSnId - param["psnid"] = res.InviteSnId - code, _ := GetCodeBySnId(args.Plt, res.InviteSnId) + if res.UpSnid > 0 { + p.PSnId = int32(res.UpSnid) + param["psnid"] = p.PSnId + code, _ := GetCodeBySnId(args.Plt, p.PSnId) if code != "" { p.PCode = code param["pcode"] = code @@ -1582,14 +1582,46 @@ func ClearInviteScore(platform string, snId int32, now time.Time) error { return nil } -func AddInviteScore(platform string, snId int32, num, money int64, now time.Time) error { +func AddInviteScore(platform string, psnId, snid, level, tp int32, num, rate, money int64, now time.Time, addMoney bool) error { + logger.Logger.Tracef("AddInviteScore ==> platform %v, psnId %v, snid %v, level %v, tp %v, num %v, rate %v, money %v, now %v, addMoney %v", + platform, psnId, snid, level, tp, num, rate, money, now, addMoney) + i := InviteScoreCollection(platform) + if i == nil { + return InviteScoreColError + } + + id := bson.NewObjectId() + err := i.Insert(&model.LogInviteScore{ + Id: id, + Platform: platform, + UpSnid: psnId, + DownSnid: snid, + Level: level, + Tp: tp, + Rate: rate, + Score: num, + Money: money, + Ts: now.Unix(), + }) + + if err != nil { + logger.Logger.Error("AddInviteScore LogInviteScore error ", err) + return err + } + c := PlayerDataCollection(platform) if c == nil { return PlayerColError } - err := c.Update(bson.M{"snid": snId}, bson.M{"$inc": bson.M{"iscore": num, "imoney": money}, "$set": bson.M{"iscorets": now}}) + myMoney := money + if !addMoney { + myMoney = 0 + } + + err = c.Update(bson.M{"snid": psnId}, bson.M{"$inc": bson.M{"iscore": num, "imoney": myMoney}, "$set": bson.M{"iscorets": now}}) if err != nil { + i.RemoveId(id) logger.Logger.Error("AddInviteScore error ", err) return err } @@ -1600,7 +1632,7 @@ func AddInviteScore(platform string, snId int32, num, money int64, now time.Time INum int64 } res := &m{} - err = c.Find(bson.M{"snid": snId}).Select(bson.M{"iscore": 1, "inum": 1}).One(res) + err = c.Find(bson.M{"snid": psnId}).Select(bson.M{"iscore": 1, "inum": 1}).One(res) if err != nil { logger.Logger.Error("AddInviteScore find error ", err) return err @@ -1608,7 +1640,7 @@ func AddInviteScore(platform string, snId int32, num, money int64, now time.Time SaveRankInvite(&model.RankInvite{ Platform: platform, - SnId: snId, + SnId: psnId, Num: res.INum, Score: res.IScore, Ts: now.Unix(), diff --git a/model/invitecode.go b/model/invitecode.go index b0dfde0..0a7951d 100644 --- a/model/invitecode.go +++ b/model/invitecode.go @@ -47,15 +47,14 @@ func GetSnIdByCode(platform string, code string) (int32, error) { // InviteScore 积分记录 type InviteScore struct { - Id bson.ObjectId `bson:"_id"` - Platform string `bson:"-"` // 平台id - SnId int32 // 被邀请人id - InviteSnId int32 // 邀请人id - Tp int32 // 积分类型 common.InviteScoreType ~ - Score int64 // 积分 - Ts int64 // 时间戳 - Money int64 // 充值金额 - Rate []int64 // 返佣比例 + Platform string // 平台id + SnId int32 // 被邀请人id + InviteSnId int32 // 邀请人id + Tp int32 // 积分类型 common.InviteScoreType ~ + Score int64 // 积分 + Ts int64 // 时间戳 + Money int64 // 充值金额 + Rate []int64 // 返佣比例 } // 邀请消息 @@ -119,3 +118,16 @@ type RankInvite struct { Ts int64 Week int64 } + +type LogInviteScore struct { + Id bson.ObjectId `bson:"_id"` + Platform string `bson:"-"` // 平台id + UpSnid int32 // 上级代理 + DownSnid int32 // 下级代理 + Level int32 // 代理层级 例如 1:DownSnid 是 UpSnid 的 1 级代理; 2: DownSnid 是 UpSnid 的 2 级代理 + Tp int32 // 返佣类型 + Rate int64 // 返佣比例 + Score int64 // 积分 + Money int64 // 充值金额 + Ts int64 // 时间戳 +}