Commit b377da45 authored by hujiebin's avatar hujiebin

feat: giftSendEvent

parent 1864cc93
package cp_cron
import (
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/config"
"github.com/robfig/cron"
"hilo-user/domain/model/cp_m"
)
func ClearCpExpire() {
c := cron.New()
spec := "0 0 */1 * * ?"
if !config.AppIsRelease() {
spec = "* * * * * ?"
}
// 1小时清理一次
_ = c.AddFunc(spec, func() {
var model = domain.CreateModelNil()
if err := cp_m.ClearExpireCpPoints(model); err != nil {
model.Log.Errorf("ClearExpireCpPoints fail:%v", err)
}
})
c.Start()
}
...@@ -2,6 +2,7 @@ package cron ...@@ -2,6 +2,7 @@ package cron
import ( import (
"git.hilo.cn/hilo-common/resource/config" "git.hilo.cn/hilo-common/resource/config"
"hilo-user/cron/cp_cron"
"hilo-user/cron/gift_cron" "hilo-user/cron/gift_cron"
) )
...@@ -11,4 +12,5 @@ func Init() { ...@@ -11,4 +12,5 @@ func Init() {
} }
gift_cron.SendGiftEventInit() // 礼物消息 gift_cron.SendGiftEventInit() // 礼物消息
gift_cron.GiftRemark() // 礼物消息补偿 gift_cron.GiftRemark() // 礼物消息补偿
cp_cron.ClearCpExpire() // 清理过期cp
} }
...@@ -10,12 +10,22 @@ import ( ...@@ -10,12 +10,22 @@ import (
"time" "time"
) )
type CpRelationTmp struct {
mysql.Entity
UserId1 mysql.ID
UserId2 mysql.ID
}
func (CpRelationTmp) TableName() string {
return "cp_relation"
}
// cp等级 // cp等级
type CpLevel struct { type CpLevel struct {
mysql.Entity mysql.Entity
CpId mysql.ID CpId mysql.ID
SmallUserId mysql.ID UserId1 mysql.ID
LargeUserId mysql.ID UserId2 mysql.ID
Points mysql.Num Points mysql.Num
Level cp_e.CpLevel Level cp_e.CpLevel
ExpireAt time.Time ExpireAt time.Time
...@@ -25,8 +35,8 @@ type CpLevel struct { ...@@ -25,8 +35,8 @@ type CpLevel struct {
type CpLevelDetail struct { type CpLevelDetail struct {
mysql.Entity mysql.Entity
CpId mysql.ID CpId mysql.ID
SmallUserId mysql.ID UserId1 mysql.ID
LargeUserId mysql.ID UserId2 mysql.ID
AddReduce mysql.AddReduce AddReduce mysql.AddReduce
Num mysql.Num Num mysql.Num
BefNum mysql.Num BefNum mysql.Num
...@@ -39,6 +49,30 @@ func AddCpLevelDetail(model *domain.Model, detail CpLevelDetail) error { ...@@ -39,6 +49,30 @@ func AddCpLevelDetail(model *domain.Model, detail CpLevelDetail) error {
return model.DB().Create(&detail).Error return model.DB().Create(&detail).Error
} }
// 获取cpRelation
// 因为cp的业务逻辑唯一性,只要user_id1命中其中一个即可
func GetCpRelation(model *domain.Model, userId1, userId2 mysql.ID) (cpRelation CpRelationTmp, exits bool) {
if err := model.DB().Model(CpRelationTmp{}).Where("user_id1 = ?", userId1).First(&cpRelation).Error; err != nil {
if err != gorm.ErrRecordNotFound {
model.Log.Errorf("GetCpRelation fail:%v", err)
return
} else {
// gorm.ErrRecordNotFound
if err := model.DB().Model(CpRelationTmp{}).Where("user_id1 = ?", userId2).First(&cpRelation).Error; err != nil {
if err != gorm.ErrRecordNotFound {
model.Log.Errorf("GetCpRelation fail:%v", err)
return
}
} else {
exits = true
}
}
} else {
exits = true
}
return
}
// 增加cp等级积分 // 增加cp等级积分
// 送礼1钻石=1点数 // 送礼1钻石=1点数
// condition // condition
...@@ -46,24 +80,22 @@ func AddCpLevelDetail(model *domain.Model, detail CpLevelDetail) error { ...@@ -46,24 +80,22 @@ func AddCpLevelDetail(model *domain.Model, detail CpLevelDetail) error {
// 2.记录存在 // 2.记录存在
// 2.1 在有效期内,直接加points后判断新level,升级需要更新有效期 // 2.1 在有效期内,直接加points后判断新level,升级需要更新有效期
// 2.2 不有效期内,算首充,重置points后判断新level,升级需要更新有效期 // 2.2 不有效期内,算首充,重置points后判断新level,升级需要更新有效期
func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (err error) { func AddCpLevelPoints(model *domain.Model, cpRelation CpRelationTmp, points mysql.Num) (err error) {
start := time.Now() start := time.Now()
defer func() { defer func() {
model.Log.Infof("AddCpLevelPoints cost:%v,err:%v", time.Now().Sub(start), err) model.Log.Infof("AddCpLevelPoints cost:%v,err:%v", time.Now().Sub(start), err)
}() }()
cpId := mysql.ID(1) // todo get from weijian
small, large := mysql.ID(4552), mysql.ID(7642) // todo get from weijian
var cpLevel CpLevel var cpLevel CpLevel
var cpLevelDetails []CpLevelDetail var cpLevelDetails []CpLevelDetail
if err := model.DB().Model(CpLevel{}).Where("cp_id = ?", cpId).First(&cpLevel).Error; err != nil { if err := model.DB().Model(CpLevel{}).Where("cp_id = ?", cpRelation.ID).First(&cpLevel).Error; err != nil {
if err != gorm.ErrRecordNotFound { if err != gorm.ErrRecordNotFound {
return myerr.WrapErr(err) return myerr.WrapErr(err)
} }
// 明细 // 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{ cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId, CpId: cpRelation.ID,
SmallUserId: small, UserId1: cpRelation.UserId1,
LargeUserId: large, UserId2: cpRelation.UserId2,
AddReduce: mysql.ADD, AddReduce: mysql.ADD,
Num: points, Num: points,
BefNum: 0, BefNum: 0,
...@@ -82,9 +114,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e ...@@ -82,9 +114,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e
points = points - cp_e.CpLevelPoints[level] // 减去用于已用于升级的积分 points = points - cp_e.CpLevelPoints[level] // 减去用于已用于升级的积分
// 明细 // 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{ cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId, CpId: cpRelation.ID,
SmallUserId: small, UserId1: cpRelation.UserId1,
LargeUserId: large, UserId2: cpRelation.UserId2,
AddReduce: mysql.REDUCE, AddReduce: mysql.REDUCE,
Num: cp_e.CpLevelPoints[level], Num: cp_e.CpLevelPoints[level],
BefNum: cp_e.CpLevelPoints[level] + points, BefNum: cp_e.CpLevelPoints[level] + points,
...@@ -93,9 +125,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e ...@@ -93,9 +125,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e
}) })
} }
cpLevel = CpLevel{ cpLevel = CpLevel{
CpId: cpId, CpId: cpRelation.ID,
SmallUserId: small, UserId1: cpRelation.UserId1,
LargeUserId: large, UserId2: cpRelation.UserId2,
Points: points, Points: points,
Level: level, Level: level,
ExpireAt: time.Now().AddDate(0, 0, cp_e.EffectDays), ExpireAt: time.Now().AddDate(0, 0, cp_e.EffectDays),
...@@ -107,9 +139,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e ...@@ -107,9 +139,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e
cpLevel.Points += points cpLevel.Points += points
// 明细 // 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{ cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId, CpId: cpRelation.ID,
SmallUserId: small, UserId1: cpRelation.UserId1,
LargeUserId: large, UserId2: cpRelation.UserId2,
AddReduce: mysql.ADD, AddReduce: mysql.ADD,
Num: points, Num: points,
BefNum: cpLevel.Points - points, BefNum: cpLevel.Points - points,
...@@ -131,9 +163,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e ...@@ -131,9 +163,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e
cpLevel.ExpireAt = time.Now().AddDate(0, 0, cp_e.EffectDays) cpLevel.ExpireAt = time.Now().AddDate(0, 0, cp_e.EffectDays)
// 明细 // 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{ cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId, CpId: cpRelation.ID,
SmallUserId: small, UserId1: cpRelation.UserId1,
LargeUserId: large, UserId2: cpRelation.UserId2,
AddReduce: mysql.REDUCE, AddReduce: mysql.REDUCE,
Num: cp_e.CpLevelPoints[cpLevel.Level] - cp_e.CpLevelPoints[oldLevel], Num: cp_e.CpLevelPoints[cpLevel.Level] - cp_e.CpLevelPoints[oldLevel],
BefNum: cpLevel.Points + cp_e.CpLevelPoints[cpLevel.Level] - cp_e.CpLevelPoints[oldLevel], BefNum: cpLevel.Points + cp_e.CpLevelPoints[cpLevel.Level] - cp_e.CpLevelPoints[oldLevel],
...@@ -147,9 +179,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e ...@@ -147,9 +179,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e
cpLevel.Points = points cpLevel.Points = points
// 明细 // 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{ cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId, CpId: cpRelation.ID,
SmallUserId: small, UserId1: cpRelation.UserId1,
LargeUserId: large, UserId2: cpRelation.UserId2,
AddReduce: mysql.ADD, AddReduce: mysql.ADD,
Num: points, Num: points,
BefNum: oldPoints, BefNum: oldPoints,
...@@ -166,9 +198,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e ...@@ -166,9 +198,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e
cpLevel.Points -= cp_e.CpLevelPoints[cpLevel.Level] // 减去已用于升级的积分 cpLevel.Points -= cp_e.CpLevelPoints[cpLevel.Level] // 减去已用于升级的积分
// 明细 // 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{ cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId, CpId: cpRelation.ID,
SmallUserId: small, UserId1: cpRelation.UserId1,
LargeUserId: large, UserId2: cpRelation.UserId2,
AddReduce: mysql.REDUCE, AddReduce: mysql.REDUCE,
Num: cp_e.CpLevelPoints[cpLevel.Level], Num: cp_e.CpLevelPoints[cpLevel.Level],
BefNum: cpLevel.Points + cp_e.CpLevelPoints[cpLevel.Level], BefNum: cpLevel.Points + cp_e.CpLevelPoints[cpLevel.Level],
...@@ -189,9 +221,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e ...@@ -189,9 +221,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e
} }
// 清理过期svip积分 // 清理过期svip积分
// 降级保级: 积分清零,svip去到大于0的等级,有效期90天 // 降级保级: 积分清零,svip去到大于0的等级,有效期30天
// svip0:积分清零,有效期保持过期 // svip0:积分清零,有效期保持过期
func ClearExpireUserSvipPoints(model *domain.Model) error { func ClearExpireCpPoints(model *domain.Model) error {
var cpLevels []*CpLevel var cpLevels []*CpLevel
// 过期 + (积分 or level) 大于0 // 过期 + (积分 or level) 大于0
if err := model.DB().Model(CpLevel{}).Where("expire_at < ? AND (points > 0 or level > 0) ", time.Now()).Find(&cpLevels).Error; err != nil { if err := model.DB().Model(CpLevel{}).Where("expire_at < ? AND (points > 0 or level > 0) ", time.Now()).Find(&cpLevels).Error; err != nil {
...@@ -223,8 +255,8 @@ func ClearExpireUserSvipPoints(model *domain.Model) error { ...@@ -223,8 +255,8 @@ func ClearExpireUserSvipPoints(model *domain.Model) error {
// 明细 // 明细
if err := AddCpLevelDetail(model, CpLevelDetail{ if err := AddCpLevelDetail(model, CpLevelDetail{
CpId: cpLevel.CpId, CpId: cpLevel.CpId,
SmallUserId: cpLevel.SmallUserId, UserId1: cpLevel.UserId1,
LargeUserId: cpLevel.LargeUserId, UserId2: cpLevel.UserId2,
AddReduce: mysql.REDUCE, AddReduce: mysql.REDUCE,
Num: cp_e.CpLevelPoints[newLevel], Num: cp_e.CpLevelPoints[newLevel],
BefNum: oldPoints, BefNum: oldPoints,
...@@ -240,8 +272,8 @@ func ClearExpireUserSvipPoints(model *domain.Model) error { ...@@ -240,8 +272,8 @@ func ClearExpireUserSvipPoints(model *domain.Model) error {
// 明细 // 明细
if err := AddCpLevelDetail(model, CpLevelDetail{ if err := AddCpLevelDetail(model, CpLevelDetail{
CpId: cpLevel.CpId, CpId: cpLevel.CpId,
SmallUserId: cpLevel.SmallUserId, UserId1: cpLevel.UserId1,
LargeUserId: cpLevel.LargeUserId, UserId2: cpLevel.UserId2,
AddReduce: mysql.SET, AddReduce: mysql.SET,
Num: oldPoints - cp_e.CpLevelPoints[newLevel], Num: oldPoints - cp_e.CpLevelPoints[newLevel],
BefNum: oldPoints - cp_e.CpLevelPoints[newLevel], BefNum: oldPoints - cp_e.CpLevelPoints[newLevel],
......
package event_s
import (
"git.hilo.cn/hilo-common/domain"
"hilo-user/domain/event/gift_ev"
"hilo-user/domain/model/cp_m"
)
// 送礼增加cp值
func CpLevelEvent() {
gift_ev.AddSendGiftEventAsync(func(model *domain.Model, event interface{}) error {
sendGiftEvent, ok := event.(*gift_ev.SendGiftEvent)
if !ok {
model.Log.Errorf("AddSendGiftEventAsync event type err")
return nil
}
for _, receiverUid := range sendGiftEvent.ReceiveUserIds {
diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum
// 有cp关系
if cpRelation, exits := cp_m.GetCpRelation(model, sendGiftEvent.SendUserId, receiverUid); exits {
return cp_m.AddCpLevelPoints(model, cpRelation, diamonds) // 业务场景允许提前break(cp是唯一的)
}
}
return nil
})
}
...@@ -14,6 +14,7 @@ import ( ...@@ -14,6 +14,7 @@ import (
func EventInit() { func EventInit() {
UserBagSendEvent() UserBagSendEvent()
CpLevelEvent()
} }
func UserBagSendEvent() { func UserBagSendEvent() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment