cp_level.go 2.61 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4
package event_s

import (
	"git.hilo.cn/hilo-common/domain"
hujiebin's avatar
hujiebin committed
5 6
	"github.com/jinzhu/now"
	"hilo-user/_const/enum/cp_e"
hujiebin's avatar
hujiebin committed
7 8
	"hilo-user/domain/event/gift_ev"
	"hilo-user/domain/model/cp_m"
hujiebin's avatar
hujiebin committed
9
	"time"
hujiebin's avatar
hujiebin committed
10 11
)

hujiebin's avatar
hujiebin committed
12 13 14
// 送礼增加cp等级
// 送礼增加cp排行榜
func CpGiftEvent() {
hujiebin's avatar
hujiebin committed
15
	gift_ev.AddSendGiftEventSync(func(model *domain.Model, event interface{}) error {
hujiebin's avatar
hujiebin committed
16 17 18 19 20
		sendGiftEvent, ok := event.(*gift_ev.SendGiftEvent)
		if !ok {
			model.Log.Errorf("AddSendGiftEventAsync event type err")
			return nil
		}
hujiebin's avatar
hujiebin committed
21 22 23 24
		// 只处理cp礼物
		if !sendGiftEvent.ResGift.Cp {
			return nil
		}
hujiebin's avatar
hujiebin committed
25 26 27
		for _, receiverUid := range sendGiftEvent.ReceiveUserIds {
			diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum
			// 有cp关系
hujiebin's avatar
hujiebin committed
28
			if cpRelation, exits := cp_m.GetCpRelationPair(model, sendGiftEvent.SendUserId, receiverUid); exits {
hujiebin's avatar
hujiebin committed
29 30 31 32 33 34
				if err := cp_m.AddCpLevelPoints(model, cpRelation, diamonds); err != nil {
					model.Log.Errorf("AddCpLevelPoints fail:%v", err)
				}
				if err := cp_m.AddCpDayRank(model, cpRelation, diamonds); err != nil {
					model.Log.Errorf("AddCpDayRank fail:%v", err)
				}
hujiebin's avatar
hujiebin committed
35
				// 检查最新的等级
hujiebin's avatar
hujiebin committed
36
				if cpLevel := cp_m.GetCpLevel(model, cpRelation.Id); cpLevel.CpId >= 0 {
hujiebin's avatar
hujiebin committed
37 38
					points := cpLevel.Points + cp_e.CpLevelPoints[cpLevel.Level]
					if err := cp_m.UpdateCpAchievement(model, cpLevel.CpId, cpRelation.UserId1, cpRelation.UserId2, cp_e.CpAchievementLevel, points); err != nil {
hujiebin's avatar
hujiebin committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
						model.Log.Errorf("UpdateCpAchievement fail:%v", err)
					}
				}
				// 检查最高的分数
				for _, queryType := range []string{"day", "week", "month"} {
					var beginDate, endDate string
					var cpAchievementType cp_e.CpAchievement
					switch queryType {
					case "day":
						beginDate, endDate = time.Now().Format("2006-01-02"), time.Now().Format("2006-01-02")
						cpAchievementType = cp_e.CpAchievementDayRank
					case "week":
						beginDate = now.BeginningOfWeek().Format("2006-01-02")
						endDate = now.EndOfWeek().Format("2006-01-02")
						cpAchievementType = cp_e.CpAchievementWeekRank
					case "month":
						beginDate = now.BeginningOfMonth().Format("2006-01-02")
						endDate = now.EndOfMonth().Format("2006-01-02")
						cpAchievementType = cp_e.CpAchievementMonthRank
					}
hujiebin's avatar
hujiebin committed
59 60
					if data := cp_m.GetCpDayRank(model, beginDate, endDate, cpRelation.Id); data.Score > 0 {
						if err := cp_m.UpdateCpAchievement(model, cpRelation.Id, cpRelation.UserId1, cpRelation.UserId2, cpAchievementType, data.Score); err != nil {
hujiebin's avatar
hujiebin committed
61 62 63 64 65
							model.Log.Errorf("UpdateCpAchievement fail:%v", err)
						}
					}
				}
				// 检查最新日周月榜单
hujiebin's avatar
hujiebin committed
66
				return nil // 业务场景允许提前break(cp是唯一的)
hujiebin's avatar
hujiebin committed
67 68 69 70 71
			}
		}
		return nil
	})
}