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
import (
"git.hilo.cn/hilo-common/resource/config"
"hilo-user/cron/cp_cron"
"hilo-user/cron/gift_cron"
)
......@@ -9,6 +10,7 @@ func Init() {
if !config.IsMaster() {
return
}
gift_cron.SendGiftEventInit() // 礼物消息
gift_cron.GiftRemark() // 礼物消息补偿
gift_cron.SendGiftEventInit() // 礼物消息
gift_cron.GiftRemark() // 礼物消息补偿
cp_cron.ClearCpExpire() // 清理过期cp
}
......@@ -10,28 +10,38 @@ import (
"time"
)
type CpRelationTmp struct {
mysql.Entity
UserId1 mysql.ID
UserId2 mysql.ID
}
func (CpRelationTmp) TableName() string {
return "cp_relation"
}
// cp等级
type CpLevel struct {
mysql.Entity
CpId mysql.ID
SmallUserId mysql.ID
LargeUserId mysql.ID
Points mysql.Num
Level cp_e.CpLevel
ExpireAt time.Time
CpId mysql.ID
UserId1 mysql.ID
UserId2 mysql.ID
Points mysql.Num
Level cp_e.CpLevel
ExpireAt time.Time
}
// cp等级积分明细
type CpLevelDetail struct {
mysql.Entity
CpId mysql.ID
SmallUserId mysql.ID
LargeUserId mysql.ID
AddReduce mysql.AddReduce
Num mysql.Num
BefNum mysql.Num
AftNum mysql.Num
Remark string
CpId mysql.ID
UserId1 mysql.ID
UserId2 mysql.ID
AddReduce mysql.AddReduce
Num mysql.Num
BefNum mysql.Num
AftNum mysql.Num
Remark string
}
// 添加cp等级积分增减明细
......@@ -39,6 +49,30 @@ func AddCpLevelDetail(model *domain.Model, detail CpLevelDetail) 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等级积分
// 送礼1钻石=1点数
// condition
......@@ -46,24 +80,22 @@ func AddCpLevelDetail(model *domain.Model, detail CpLevelDetail) error {
// 2.记录存在
// 2.1 在有效期内,直接加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()
defer func() {
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 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 {
return myerr.WrapErr(err)
}
// 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId,
SmallUserId: small,
LargeUserId: large,
CpId: cpRelation.ID,
UserId1: cpRelation.UserId1,
UserId2: cpRelation.UserId2,
AddReduce: mysql.ADD,
Num: points,
BefNum: 0,
......@@ -82,9 +114,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e
points = points - cp_e.CpLevelPoints[level] // 减去用于已用于升级的积分
// 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId,
SmallUserId: small,
LargeUserId: large,
CpId: cpRelation.ID,
UserId1: cpRelation.UserId1,
UserId2: cpRelation.UserId2,
AddReduce: mysql.REDUCE,
Num: cp_e.CpLevelPoints[level],
BefNum: cp_e.CpLevelPoints[level] + points,
......@@ -93,12 +125,12 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e
})
}
cpLevel = CpLevel{
CpId: cpId,
SmallUserId: small,
LargeUserId: large,
Points: points,
Level: level,
ExpireAt: time.Now().AddDate(0, 0, cp_e.EffectDays),
CpId: cpRelation.ID,
UserId1: cpRelation.UserId1,
UserId2: cpRelation.UserId2,
Points: points,
Level: level,
ExpireAt: time.Now().AddDate(0, 0, cp_e.EffectDays),
}
} else {
// 2.记录存在
......@@ -107,9 +139,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e
cpLevel.Points += points
// 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId,
SmallUserId: small,
LargeUserId: large,
CpId: cpRelation.ID,
UserId1: cpRelation.UserId1,
UserId2: cpRelation.UserId2,
AddReduce: mysql.ADD,
Num: points,
BefNum: cpLevel.Points - points,
......@@ -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)
// 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId,
SmallUserId: small,
LargeUserId: large,
CpId: cpRelation.ID,
UserId1: cpRelation.UserId1,
UserId2: cpRelation.UserId2,
AddReduce: mysql.REDUCE,
Num: 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
cpLevel.Points = points
// 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId,
SmallUserId: small,
LargeUserId: large,
CpId: cpRelation.ID,
UserId1: cpRelation.UserId1,
UserId2: cpRelation.UserId2,
AddReduce: mysql.ADD,
Num: points,
BefNum: oldPoints,
......@@ -166,9 +198,9 @@ func AddCpLevelPoints(model *domain.Model, userId mysql.ID, points mysql.Num) (e
cpLevel.Points -= cp_e.CpLevelPoints[cpLevel.Level] // 减去已用于升级的积分
// 明细
cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
CpId: cpId,
SmallUserId: small,
LargeUserId: large,
CpId: cpRelation.ID,
UserId1: cpRelation.UserId1,
UserId2: cpRelation.UserId2,
AddReduce: mysql.REDUCE,
Num: 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
}
// 清理过期svip积分
// 降级保级: 积分清零,svip去到大于0的等级,有效期90天
// 降级保级: 积分清零,svip去到大于0的等级,有效期30天
// svip0:积分清零,有效期保持过期
func ClearExpireUserSvipPoints(model *domain.Model) error {
func ClearExpireCpPoints(model *domain.Model) error {
var cpLevels []*CpLevel
// 过期 + (积分 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 {
......@@ -222,14 +254,14 @@ func ClearExpireUserSvipPoints(model *domain.Model) error {
cpLevel.ExpireAt = time.Now().AddDate(0, 0, cp_e.EffectDays)
// 明细
if err := AddCpLevelDetail(model, CpLevelDetail{
CpId: cpLevel.CpId,
SmallUserId: cpLevel.SmallUserId,
LargeUserId: cpLevel.LargeUserId,
AddReduce: mysql.REDUCE,
Num: cp_e.CpLevelPoints[newLevel],
BefNum: oldPoints,
AftNum: 0,
Remark: fmt.Sprintf("Become LEVEL%d", newLevel),
CpId: cpLevel.CpId,
UserId1: cpLevel.UserId1,
UserId2: cpLevel.UserId2,
AddReduce: mysql.REDUCE,
Num: cp_e.CpLevelPoints[newLevel],
BefNum: oldPoints,
AftNum: 0,
Remark: fmt.Sprintf("Become LEVEL%d", newLevel),
}); err != nil {
model.Log.Errorf("AddCpLevelDetail fail:%v", err)
}
......@@ -239,14 +271,14 @@ func ClearExpireUserSvipPoints(model *domain.Model) error {
}
// 明细
if err := AddCpLevelDetail(model, CpLevelDetail{
CpId: cpLevel.CpId,
SmallUserId: cpLevel.SmallUserId,
LargeUserId: cpLevel.LargeUserId,
AddReduce: mysql.SET,
Num: oldPoints - cp_e.CpLevelPoints[newLevel],
BefNum: oldPoints - cp_e.CpLevelPoints[newLevel],
AftNum: 0,
Remark: fmt.Sprintf("Expired clear"),
CpId: cpLevel.CpId,
UserId1: cpLevel.UserId1,
UserId2: cpLevel.UserId2,
AddReduce: mysql.SET,
Num: oldPoints - cp_e.CpLevelPoints[newLevel],
BefNum: oldPoints - cp_e.CpLevelPoints[newLevel],
AftNum: 0,
Remark: fmt.Sprintf("Expired clear"),
}); err != nil {
model.Log.Errorf("AddCpLevelDetail fail:%v", err)
}
......
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 (
func EventInit() {
UserBagSendEvent()
CpLevelEvent()
}
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