send_gift_redis.go 5.5 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3
package gift_cron

import (
chenweijian's avatar
chenweijian committed
4 5
	"context"
	"git.hilo.cn/hilo-common/_const/rediskey"
hujiebin's avatar
hujiebin committed
6 7
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
chenweijian's avatar
chenweijian committed
8 9
	"git.hilo.cn/hilo-common/resource/redisCli"
	"hilo-group/_const/enum/gift_e"
hujiebin's avatar
hujiebin committed
10 11 12 13
	"hilo-group/_const/enum/groupPower_e"
	"hilo-group/domain/cache/gift_c"
	"hilo-group/domain/event/gift_ev"
	"hilo-group/domain/model/groupPower_m"
chenweijian's avatar
chenweijian committed
14 15
	"hilo-group/domain/model/group_m"
	"time"
hujiebin's avatar
hujiebin committed
16 17 18 19 20 21 22 23 24 25 26
)

// 送礼事件
func SendGiftEvent() {
	//if !config.IsMaster() {
	//	return
	//}
	go func() {
		for true {
			model := domain.CreateModelNil()
			if sendGiftEvent := gift_c.BLPopQueueSendGift(model); sendGiftEvent != nil {
chenweijian's avatar
chenweijian committed
27 28 29
				groupPowerGrade(model, sendGiftEvent)        // 家族经验
				groupPowerStar(model, sendGiftEvent)         // 家族之星
				groupSupportAddConsume(model, sendGiftEvent) // 群组扶持
hujiebin's avatar
hujiebin committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
			}
		}
	}()
}

// 群组势力经验
func groupPowerGrade(model *domain.Model, sendGiftEvent *gift_ev.SendGiftEvent) {
	model.Log.Infof("AddSendGiftEventAsync %+v", sendGiftEvent)
	if sendGiftEvent.ResGift.GiftType != mysql.DiamondYellow {
		return
	}
	exist, groupPowerId, err := groupPower_m.CheckGroupPowerUser(model, sendGiftEvent.SendUserId)
	if err != nil {
		model.Log.Infof("CheckGroupPowerUser fail %+v", err)
		return
	}
	if exist {
		exp := sendGiftEvent.GiftN * mysql.Num(len(sendGiftEvent.ReceiveUserIds)) * sendGiftEvent.ResGift.DiamondNum
		//return model.Transaction(func(model *domain.Model) error {
		//	return groupPower_m.IncrGroupPowerExp(model, groupPowerId, exp, sendGiftEvent.SendUserId, "送礼")
		//})
		//return groupPower_c.QueueGroupPowerGradeExp(model, groupPowerId, exp, sendGiftEvent.SendUserId, "送礼")
		if err := model.Transaction(func(model *domain.Model) error {
			return groupPower_m.IncrGroupPowerExp(model, groupPowerId, exp, sendGiftEvent.SendUserId, "送礼")
		}); err != nil {
			model.Log.Errorf("IncrGroupPowerExp fail,data:%v-err:%v", *sendGiftEvent, err)
		} else {
			model.Log.Infof("IncrGroupPowerExp success,data:%v", *sendGiftEvent)
		}
	}
}

// 家族之星
func groupPowerStar(model *domain.Model, sendGiftEvent *gift_ev.SendGiftEvent) {
	if sendGiftEvent.ResGift.GiftType != mysql.DiamondYellow {
		return
	}
	var userIds = []mysql.ID{sendGiftEvent.SendUserId}
	userIds = append(userIds, sendGiftEvent.ReceiveUserIds...)
	groupPowers, err := groupPower_m.BatchGetGroupPowerUser(model, userIds)
	if err != nil {
		model.Log.Errorf("AddSendGiftEventAsync fail:%v", err)
		return
	}
	// 送礼加分
	if data, ok := groupPowers[sendGiftEvent.SendUserId]; ok {
		diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum * mysql.Num(len(sendGiftEvent.ReceiveUserIds))
		if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds, 0); err != nil {
			model.Log.Errorf("IncrGroupPowerMonthStarScore famous fail:%v", err)
		}
		if err := groupPower_m.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds, 0); err != nil {
			model.Log.Errorf("IncrGroupPowerDayStarScore famous fail:%v", err)
		}
	}
	// 收礼加分
	for _, userId := range sendGiftEvent.ReceiveUserIds {
		if data, ok := groupPowers[userId]; ok {
			diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum
			if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds, 0); err != nil {
				model.Log.Errorf("IncrGroupPowerMonthStarScore charm fail:%v", err)
			}
			if err := groupPower_m.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds, 0); err != nil {
				model.Log.Errorf("IncrGroupPowerDayStarScore charm fail:%v", err)
			}
		}
	}
	return
}
chenweijian's avatar
chenweijian committed
98 99 100

// 群组扶持增加流水数据
func groupSupportAddConsume(model *domain.Model, sendGiftEvent *gift_ev.SendGiftEvent) {
chenweijian's avatar
chenweijian committed
101 102 103
	if time.Now().Unix() <= 1692843600 {
		return
	}
chenweijian's avatar
chenweijian committed
104
	model.Log.Infof("groupSupportAddConsume UserId:%d, sendGiftEvent:%+v", sendGiftEvent.SendUserId, sendGiftEvent)
chenweijian's avatar
chenweijian committed
105
	if sendGiftEvent.SceneType != gift_e.GroupSceneType || sendGiftEvent.SceneUid == "" {
chenweijian's avatar
chenweijian committed
106
		model.Log.Infof("groupSupportAddConsume UserId:%d, sendGiftEvent:%+v", sendGiftEvent.SendUserId, sendGiftEvent)
chenweijian's avatar
chenweijian committed
107 108 109 110 111 112 113 114
		return
	}
	_, _, period := group_m.GetSupportLevelTime(time.Now())
	// 钻石数
	diamond := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum
	keyDiamond := rediskey.GetGroupSupportConsumeSummary(period)
	_, err := model.RedisCluster.ZIncrBy(context.Background(), keyDiamond, float64(diamond), sendGiftEvent.SceneUid).Result()
	if err != nil {
chenweijian's avatar
chenweijian committed
115
		model.Log.Errorf("groupSupportAddConsume key:%s, val:%d, member:%s, err:%v",
chenweijian's avatar
chenweijian committed
116 117 118 119
			keyDiamond, diamond, sendGiftEvent.SceneUid, err)
	}
	err = redisCli.SetExpire(model.RedisCluster, keyDiamond, time.Hour*24*14) // 保留两周
	if err != nil {
chenweijian's avatar
chenweijian committed
120
		model.Log.Errorf("groupSupportAddConsume key:%s, val:%d, member:%s, err:%v",
chenweijian's avatar
chenweijian committed
121 122 123 124 125 126
			keyDiamond, diamond, sendGiftEvent.SceneUid, err)
	}
	// 支持者数量
	keySupportNum := rediskey.GetGroupSupportCountSupporter(period, sendGiftEvent.SceneUid)
	err = model.RedisCluster.SAdd(context.Background(), keySupportNum, sendGiftEvent.SendUserId).Err()
	if err != nil {
chenweijian's avatar
chenweijian committed
127
		model.Log.Errorf("groupSupportAddConsume key:%s, UserId:%d, err:%v", keySupportNum, sendGiftEvent.SendUserId, err)
chenweijian's avatar
chenweijian committed
128 129 130
	}
	err = redisCli.SetExpire(model.RedisCluster, keySupportNum, time.Hour*24*14) // 保留两周
	if err != nil {
chenweijian's avatar
chenweijian committed
131
		model.Log.Errorf("groupSupportAddConsume key:%s, UserId:%d, err:%v", keySupportNum, sendGiftEvent.SendUserId, err)
chenweijian's avatar
chenweijian committed
132 133 134
	}
	return
}