send_gift_redis.go 5.58 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
	"hilo-group/_const/enum/groupPower_e"
	"hilo-group/domain/cache/gift_c"
12
	"hilo-group/domain/cache/groupPower_c"
hujiebin's avatar
hujiebin committed
13 14
	"hilo-group/domain/event/gift_ev"
	"hilo-group/domain/model/groupPower_m"
chenweijian's avatar
chenweijian committed
15 16
	"hilo-group/domain/model/group_m"
	"time"
hujiebin's avatar
hujiebin committed
17 18 19 20 21 22 23 24 25 26 27
)

// 送礼事件
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
28 29 30
				groupPowerGrade(model, sendGiftEvent)        // 家族经验
				groupPowerStar(model, sendGiftEvent)         // 家族之星
				groupSupportAddConsume(model, sendGiftEvent) // 群组扶持
hujiebin's avatar
hujiebin committed
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
			}
		}
	}()
}

// 群组势力经验
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)
		}
81 82
		if err := groupPower_c.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId,
			groupPower_e.GroupPowerStarTypeFamous, diamonds); err != nil {
hujiebin's avatar
hujiebin committed
83 84 85 86 87 88 89 90 91 92
			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)
			}
93 94
			if err := groupPower_c.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId,
				groupPower_e.GroupPowerStarTypeCharm, diamonds); err != nil {
hujiebin's avatar
hujiebin committed
95 96 97 98 99 100
				model.Log.Errorf("IncrGroupPowerDayStarScore charm fail:%v", err)
			}
		}
	}
	return
}
chenweijian's avatar
chenweijian committed
101 102 103 104 105 106 107 108 109 110 111 112 113

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