event_init.go 21.3 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3
package event_s

import (
chenweijian's avatar
chenweijian committed
4
	"context"
hujiebin's avatar
hujiebin committed
5
	"encoding/json"
chenweijian's avatar
chenweijian committed
6
	"git.hilo.cn/hilo-common/_const/rediskey"
hujiebin's avatar
hujiebin committed
7
	"git.hilo.cn/hilo-common/domain"
hujiebin's avatar
hujiebin committed
8
	"git.hilo.cn/hilo-common/resource/mysql"
chenweijian's avatar
chenweijian committed
9
	"git.hilo.cn/hilo-common/resource/redisCli"
hujiebin's avatar
hujiebin committed
10 11
	"git.hilo.cn/hilo-common/rpc"
	"git.hilo.cn/hilo-common/sdk/tencentyun"
chenweijian's avatar
chenweijian committed
12
	"hilo-group/_const/enum/gift_e"
hujiebin's avatar
hujiebin committed
13
	"hilo-group/_const/enum/group_e"
hujiebin's avatar
hujiebin committed
14
	"hilo-group/_const/enum/msg_e"
hujiebin's avatar
hujiebin committed
15
	"hilo-group/_const/enum/task_e"
hujiebin's avatar
hujiebin committed
16
	"hilo-group/domain/cache/group_c"
hujiebin's avatar
hujiebin committed
17
	"hilo-group/domain/event/gift_ev"
18
	"hilo-group/domain/event/group_ev"
hujiebin's avatar
hujiebin committed
19
	"hilo-group/domain/event/group_power_ev"
hujiebin's avatar
hujiebin committed
20
	"hilo-group/domain/event/mic_ev"
21
	"hilo-group/domain/model/diamond_m"
hujiebin's avatar
hujiebin committed
22
	"hilo-group/domain/model/groupPower_m"
hujiebin's avatar
hujiebin committed
23
	"hilo-group/domain/model/group_m"
hujiebin's avatar
hujiebin committed
24
	"hilo-group/domain/model/mic_m"
hujiebin's avatar
hujiebin committed
25
	"hilo-group/domain/model/msg_m"
hujiebin's avatar
hujiebin committed
26
	"hilo-group/domain/model/task_m"
hujiebin's avatar
hujiebin committed
27
	"hilo-group/domain/model/user_m"
hujiebin's avatar
hujiebin committed
28
	"hilo-group/domain/service/group_mic_s"
hujiebin's avatar
hujiebin committed
29
	"hilo-group/myerr"
30
	"strconv"
hujiebin's avatar
hujiebin committed
31
	"time"
hujiebin's avatar
hujiebin committed
32 33 34 35
)

func EventInit() {
	GroupPowerEvents()
36
	GroupSupportEvents()
hujiebin's avatar
hujiebin committed
37 38 39
	GroupEvents()
	GroupImMass()
	GroupTheme()
chenweijian's avatar
chenweijian committed
40
	SendGift()              // 送礼事件
hujiebin's avatar
hujiebin committed
41 42
	OnMic()                 // 在麦上事件
	GroupInMicChangeEvent() // 用户进房推送mic位置信息
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

func GroupSupportEvents() {
	//群组支持
	group_ev.AddGroupSupportSync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupSupportEvent)
		if !ok {
			model.Log.Errorf("AddGroupSupportSync fail data")
			return nil
		}
		model.Log.Infof("diamond AddGroupSupport admin:%v mgrs:%v", event.GroupSupportAwardAdmin, event.GroupSupportAwardMgrs)
		diamondAccountAdmin, err := diamond_m.GetDiamondAccountByUserId(model, event.GroupSupportAwardAdmin.UserId)
		if err != nil {
			return err
		}
		diamondAccountDetailAdmin, err := diamondAccountAdmin.GroupSupportAdmin(event.GroupSupportAwardAdmin.GroupSupportAwardId, event.GroupSupportAwardAdmin.DiamondNum)
		if err != nil {
			return err
		}
		if err := diamondAccountDetailAdmin.Persistent(); err != nil {
			return err
		}
		for i, _ := range event.GroupSupportAwardMgrs {
			diamondAccount, err := diamond_m.GetDiamondAccountByUserId(model, event.GroupSupportAwardMgrs[i].UserId)
			if err != nil {
				return err
			}
			diamondAccountDetail, err := diamondAccount.GroupSupportMgr(event.GroupSupportAwardMgrs[i].GroupSupportAwardId, event.GroupSupportAwardMgrs[i].DiamondNum)
			if err != nil {
				return err
			}
			if err := diamondAccountDetail.Persistent(); err != nil {
				return err
			}
		}
		return nil
	})

	//群组支持奖励
	group_ev.AddGroupSupportAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupSupportEvent)
		if !ok {
			model.Log.Errorf("AddGroupSupportSync fail data")
			return nil
		}
		model.Log.Infof("msg AddGroupSupportAsync admin:%v, mgr:%v", event.GroupSupportAwardAdmin, event.GroupSupportAwardMgrs)
		for i, _ := range event.GroupSupportAwardMgrs {
			user, err := user_m.GetUser(model, event.GroupSupportAwardMgrs[i].UserId)
			if err != nil {
				model.Log.Errorf("msg AddGroupSupportAsync GetUser userId:%v, err:%v", event.GroupSupportAwardMgrs[i].UserId, err)
			}
			if err := msg_m.NewUserRecord(model, user.ID, msg_e.GroupSupport, user.Nick, user.ID, strconv.FormatUint(uint64(event.GroupSupportAwardMgrs[i].DiamondNum), 10), "", "", "", event.GroupCode).Persistent(); err != nil {
				model.Log.Errorf("msg AddGroupSupportAsync NewUserRecord err:%v", err)
				break
			}
			msg_m.SendEmasMsgAssistant(model, user.ExternalId, user.DeviceType)
		}
		user, err := user_m.GetUser(model, event.GroupSupportAwardAdmin.UserId)
		if err != nil {
			model.Log.Errorf("msg AddGroupSupportAsync GetUser userId:%v, err:%v", event.GroupSupportAwardAdmin.UserId, err)
		}
		if err := msg_m.NewUserRecord(model, user.ID, msg_e.GroupSupport, user.Nick, user.ID, strconv.FormatUint(uint64(event.GroupSupportAwardAdmin.DiamondNum), 10), "", "", "", event.GroupCode).Persistent(); err != nil {
			model.Log.Errorf("msg AddGroupSupportAsync NewUserRecord err:%v", err)
			return nil
		}
		msg_m.SendEmasMsgAssistant(model, user.ExternalId, user.DeviceType)
		return nil
	})

hujiebin's avatar
hujiebin committed
112 113 114
}

func GroupPowerEvents() {
hujiebin's avatar
hujiebin committed
115
	// 加入势力
hujiebin's avatar
hujiebin committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	group_power_ev.AddGroupPowerJoinAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_power_ev.GroupPowerJoinEvent)
		if !ok {
			model.Log.Errorf("AddGroupPowerJoinAsync event type err")
			return nil
		}
		model.Log.Infof("msg AddGroupPowerJoinAsync UserId:%v, GroupPowerId:%v", event.UserId, event.GroupPowerId)
		//获取势力主
		mgrUserId, err := groupPower_m.GetGroupPowerMgr(model, event.GroupPowerId)
		if err != nil {
			return err
		}
		mgrUser, err := user_m.GetUser(model, mgrUserId)
		if err != nil {
			return err
		}
		joinUser, err := user_m.GetUser(model, event.UserId)
		if err != nil {
			return err
		}
		if err := msg_m.NewUserRecord(model, mgrUser.ID, msg_e.GroupPowerUserJoin, joinUser.Nick, joinUser.ID, "", "", "", "", "").Persistent(); err != nil {
			model.Log.Errorf("msg AddGroupPowerJoinAsync NewUserRecord err:%v", err)
			return nil
		}
		msg_m.SendEmasMsgAssistant(model, mgrUser.ExternalId, mgrUser.DeviceType)
		return nil
	})
hujiebin's avatar
hujiebin committed
143
	// 离开势力
hujiebin's avatar
hujiebin committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	group_power_ev.AddGroupPowerLeaveAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_power_ev.GroupPowerLeaveEvent)
		if !ok {
			model.Log.Errorf("AddGroupPowerLeaveAsync event type err")
			return nil
		}
		model.Log.Infof("msg AddGroupPowerLeaveAsync UserId:%v, GroupPowerId:%v", event.UserId, event.GroupPowerId)
		//获取势力主
		mgrUserId, err := groupPower_m.GetGroupPowerMgr(model, event.GroupPowerId)
		if err != nil {
			return err
		}
		mgrUser, err := user_m.GetUser(model, mgrUserId)
		if err != nil {
			return err
		}
		joinUser, err := user_m.GetUser(model, event.UserId)
		if err != nil {
			return err
		}
		if err := msg_m.NewUserRecord(model, mgrUser.ID, msg_e.GroupPowerUserLeave, joinUser.Nick, joinUser.ID, "", "", "", "", "").Persistent(); err != nil {
			model.Log.Errorf("msg AddGroupPowerLeaveAsync NewUserRecord err:%v", err)
			return nil
		}
		msg_m.SendEmasMsgAssistant(model, mgrUser.ExternalId, mgrUser.DeviceType)
		return nil
	})
hujiebin's avatar
hujiebin committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	// 势力升级
	group_power_ev.AddGroupPowerUpgradeAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_power_ev.GroupPowerUpgradeEvent)
		if !ok {
			model.Log.Errorf("AddGroupPowerUpgradeAsync event type err")
			return nil
		}
		model.Log.Infof("msg AddGroupPowerUpgradeAsync GroupPowerId:%v", event.GroupPowerId)
		users, err := groupPower_m.GetGroupPowerUser(model, event.GroupPowerId)
		if err != nil {
			return err
		}
		var userIds []mysql.ID
		for _, u := range users {
			userIds = append(userIds, u.UserId)
		}
		userMaps, err := user_m.GetUserMapByIds(model, userIds)
		if err != nil {
			return err
		}
		for uid, u := range userMaps {
			if err := msg_m.NewUserRecord(model, uid, msg_e.GroupPowerUpgrade, "", 0, "", "", "", "", "").Persistent(); err != nil {
				model.Log.Errorf("msg AddGroupPowerLeaveAsync NewUserRecord err:%v", err)
				return nil
			}
			msg_m.SendEmasMsgAssistant(model, u.ExternalId, u.DeviceType)
		}
		return nil
	})

	// 势力等级即将过期
	group_power_ev.AddGroupPowerExpireNoticeAsync(func(model *domain.Model, e interface{}) error {
203 204 205 206 207 208 209 210
		start := time.Now()
		model.Log.Infof("msg AddGroupPowerExpireNoticeAsync,start:%v", start)
		lastId := mysql.ID(0)
		for {
			powerUsers, nextLastId, err := groupPower_m.BatchGetAllGroupPowerUsers(model, lastId, 500)
			if err != nil {
				model.Log.Errorf("AddGroupPowerExpireNoticeAsync fail:%v", err)
				return err
hujiebin's avatar
hujiebin committed
211
			}
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
			if len(powerUsers) <= 0 {
				break
			}
			userMaps, err := user_m.GetUserMapByIds(model, powerUsers)
			if err != nil {
				return err
			}
			for uid, u := range userMaps {
				if err := msg_m.NewUserRecord(model, uid, msg_e.GroupPowerExpireNotice, "", 0, "", "", "", "", "").Persistent(); err != nil {
					model.Log.Errorf("msg AddGroupPowerExpireNoticeAsync NewUserRecord err:%v", err)
					return nil
				}
				_ = msg_m.SendEmasMsgAssistant(model, u.ExternalId, u.DeviceType)
			}
			if nextLastId <= 0 {
				break
			}
			lastId = nextLastId
			time.Sleep(time.Second) // 停顿一秒
hujiebin's avatar
hujiebin committed
231
		}
232
		model.Log.Infof("msg AddGroupPowerExpireNoticeAsync,end:%v", time.Now().Sub(start).Seconds())
hujiebin's avatar
hujiebin committed
233 234
		return nil
	})
hujiebin's avatar
hujiebin committed
235
}
hujiebin's avatar
hujiebin committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

func GroupEvents() {
	// 进房事件
	group_ev.AddGroupInAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupInEvent)
		if !ok {
			model.Log.Errorf("AddGroupInAsync for room: %+v", event)
			return nil
		}
		model.Log.Infof("AddGroupInAsync for room: %+v", event)

		uer := group_m.UserEnterRoom{
			UserId:    event.UserId,
			GroupId:   event.GroupId,
			EnterTime: time.Now(),
		}
		err := uer.Save(model.Db)
		model.Log.Infof("AddGroupInAsync, UserEnterRoom err: %v", err)
hujiebin's avatar
hujiebin committed
254
		group_c.ZAddUserEnterRoom(model, event.UserId, event.GroupId) // redis存储
hujiebin's avatar
hujiebin committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
		return err
	})

	// 进入房间时,
	group_ev.AddGroupInAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupInEvent)
		if !ok {
			model.Log.Errorf("AddGroupInAsync for room: %+v", event)
			return nil
		}
		model.Log.Infof("AddGroupInAsync for user: %+v", event)

		user, err := user_m.GetUser(model, event.UserId)
		if err != nil {
			return err
		}

		medals, err := user_m.GetUserMedalMerge(model.Log, model.Db, event.UserId)
		if err != nil {
			model.Log.Errorf("tim_m user AddGroupInAsync GetUserMedal err:%v, userId:%v", err, event.UserId)
			return err
		}

		wealthGrade, _, err := user_m.GetWealthGrade(model, event.UserId)
		if err != nil {
			return err
		}

		charmGrade, _, err := user_m.GetCharmGrade(model, event.UserId)
		if err != nil {
			return err
		}

		if err = FlushGrades(event.ExternalId, wealthGrade, charmGrade); err != nil {
			model.Log.Info("AddGroupInAsync, FlushGrades failed: ", err)
		}

		_, powerName, err := groupPower_m.GetUserGroupPower(model, event.UserId)
		if err != nil {
			return err
		}

		if err = FlushHiloInfo(user.ExternalId, event.IsVip, user.Code != user.OriginCode, medals, powerName, event.NobleLevel); err != nil {
			model.Log.Info("AddGroupInAsync, FlushHiloInfo failed: ", err)
		}
		return nil
	})
	//被踢出
	group_ev.AddGroupKickOutAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupKickOutEvent)
		if !ok {
			model.Log.Errorf("AddGroupKickOutAsync fail data")
			return nil
		}
		model.Log.Infof("publicScreenMsg AddGroupKickOutAsync GroupId:%v, OperatorExternalId:%v, MemberExternalId:%v", event.GroupId, event.OperatorExternalId, event.MemberExternalId)

		groupKickOutMsg := group_m.CommonPublicMsg{
			Type:               group_e.UserKickPublicScreenMsg,
			OperatorExternalId: event.OperatorExternalId,
			OperatorNick:       event.OperatorName,
			OperatorAvatar:     event.OperatorFaceUrl,
			ExternalId:         event.MemberExternalId,
			Nick:               event.MemberName,
			Avatar:             event.MemberAvatar,
		}
		//
		body, err := json.Marshal(groupKickOutMsg)
		if err != nil {
			return myerr.WrapErr(err)
		}

		txGroupId, err := group_m.ToTxGroupId(model, event.GroupId)
		if err != nil {
			return err
		}
		//发送公屏消息,
		u, err := tencentyun.SendCustomMsg(model.Log, txGroupId, nil, string(body), "")
		model.Log.Infof("publicScreenMsg AddGroupKickOutAsync result response.MsgSeq:%v, err:%v", u, err)
		return err
	})
}

func GroupImMass() {
	//支付群主群发IM
	group_ev.AddGroupImMassSync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupImMassEvent)
		if !ok {
			model.Log.Errorf("AddGroupImMassSync data fail")
			return nil
		}
		model.Log.Infof("diamond AddGroupImMass groupId:%v userId:%v", event.GroupId, event.UserId)
		diamondAccount, err := diamond_m.GetDiamondAccountByUserId(model, event.UserId)
		if err != nil {
			return err
		}
		diamondAccountDetail, err := diamondAccount.GroupImMass()
		if err != nil {
			return err
		}
		return diamondAccountDetail.Persistent()
	})

	// 任务
	group_ev.AddGroupImMassAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupImMassEvent)
		if !ok {
			model.Log.Errorf("AddGroupImMassSync data fail")
			return nil
		}
		model.Log.Infof("task AddGroupImMassAsync %+v", event)
		_, err := task_m.AddTaskUser(model, task_e.GroupImMass, event.UserId, 0)
		return err
	})

	// 麦上的人/管理员群发消息,弹窗 fixme:放在这里已经不合适了
	group_ev.AddGroupImMassAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupImMassEvent)
		if !ok {
			model.Log.Errorf("AddGroupImMassSync data fail")
			return nil
		}
		model.Log.Infof("AddGroupImMassAsync begin groupId = %s, userId %d, members: %v, content: %s",
			event.GroupId, event.UserId, event.Members, event.Content)

		flag, err := group_m.IsHiddenGroup(model.Db, event.GroupId)
		if err != nil {
			return err
		}
		if flag {
			model.Log.Infof("AddGroupImMassAsync, skip hidden group %s", event.GroupId)
			return nil
		}

		if len(event.Members) <= 0 {
			return nil
		}
		user, err := user_m.GetUser(model, event.UserId)
		if err != nil {
			return err
		}

		//过滤用户黑名单,只要单方面拉黑就不发
		memberUserIds, err := user_m.FilterBlock(model, event.UserId, event.Members)
		if err != nil {
			return err
		}

		groupInfo, err := group_m.GetGroupInfo(model, event.GroupId)
		if err != nil {
			return err
		}

		userInCount, err := group_m.GetRoomVisitCount(event.GroupId)
		if err != nil {
			return err
		}
		if userInCount < 0 {
			userInCount = 0
		}

		// 注意发消息使用了TxGroupId
		err = rpc.SendGroupChatNotice(event.UserId, memberUserIds, user.ExternalId, user.Code, uint32(user.Sex), user.Avatar,
			event.Content, groupInfo.TxGroupId, groupInfo.Name, groupInfo.Code, groupInfo.FaceUrl, uint32(userInCount))
		model.Log.Infof("AddGroupImMassAsync, groupId = %s ended, err = %v", event.GroupId, err)
		return nil
	})

}

func GroupTheme() {
	//购买自定义主题
	group_ev.AddBuyGroupCustomThemeSync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.BuyGroupCustomThemeEvent)
		if !ok {
			model.Log.Errorf("AddBuyGroupCustomThemeSync data fail")
			return nil
		}
		model.Log.Infof("diamond AddBuyGroupCustomTheme userId:%v, GroupCustomThemeId:%v", event.UserId, event.GroupCustomThemeId)
		diamondAccount, err := diamond_m.GetDiamondAccountByUserId(model, event.UserId)
		if err != nil {
			return err
		}
		diamondAccountDetail, err := diamondAccount.BuyGroupCustomTheme(event.GroupCustomThemeId)
		if err != nil {
			return err
		}
		if err := diamondAccountDetail.Persistent(); err != nil {
			return err
		}
		return nil
	})
}

func FlushGrades(userExtId string, wealthGrade uint32, charmGrade uint32) error {
	level := (charmGrade & 0x000000FF << 8) | wealthGrade&0x000000FF

	return tencentyun.SetUserLevel(userExtId, level)
}

type TimHiloInfo struct {
	IsVip      bool     `json:"isVip"`
	IsPretty   bool     `json:"isPretty"`
	Medals     []uint32 `json:"medals"`
	PowerName  string   `json:"powerName"` // 用户加入的国家势力的绑定群组的名称
	NobleLevel uint16   `json:"nobleLevel"`
}

func FlushHiloInfo(extId string, isVip bool, isPrettyCode bool, medals []uint32, groupPowerName string, nobleLevel uint16) error {
	info := TimHiloInfo{IsVip: isVip, IsPretty: isPrettyCode, Medals: medals, PowerName: groupPowerName, NobleLevel: nobleLevel}
	buf, err := json.Marshal(info)
	if err != nil {
		return err
	}

	if err = tencentyun.SetUserHiloInfo(extId, string(buf)); err != nil {
		return err
	}
	return nil
}
hujiebin's avatar
hujiebin committed
474 475

func SendGift() {
hujiebin's avatar
hujiebin committed
476
	// 送礼事件-势力经验
hujiebin's avatar
hujiebin committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	//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
	//	}
	//	model.Log.Infof("AddSendGiftEventAsync %+v", sendGiftEvent)
	//	if sendGiftEvent.ResGift.GiftType != mysql.DiamondYellow {
	//		return nil
	//	}
	//	exist, groupPowerId, err := groupPower_m.CheckGroupPowerUser(model, sendGiftEvent.SendUserId)
	//	if err != nil {
	//		model.Log.Infof("CheckGroupPowerUser fail %+v", err)
	//		return err
	//	}
	//	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, "送礼")
	//	}
	//	return nil
	//})
hujiebin's avatar
hujiebin committed
501
	// 家族之星-送礼/收礼
hujiebin's avatar
hujiebin committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	//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
	//	}
	//	if sendGiftEvent.ResGift.GiftType != mysql.DiamondYellow {
	//		return nil
	//	}
	//	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 err
	//	}
	//	// 送礼加分
	//	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 nil
	//})
chenweijian's avatar
chenweijian committed
542

chenweijian's avatar
chenweijian committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
	// 送礼事件-群组扶持数据累加
	gift_ev.AddSendGiftEventAsync(func(model *domain.Model, event interface{}) error {
		if time.Now().Unix() < 1692964800 { // cwj----
			return nil
		}
		sendGiftEvent, ok := event.(*gift_ev.SendGiftEvent)
		if !ok {
			model.Log.Errorf("AddSendGiftEventAsync event type err 群组扶持")
			return nil
		}
		if sendGiftEvent.SceneType != gift_e.GroupSceneType || sendGiftEvent.SceneUid == "" {
			return nil
		}
		_, _, 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 {
			model.Log.Errorf("AddSendGiftEventAsync groupSupport 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("AddSendGiftEventAsync groupSupport 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("AddSendGiftEventAsync groupSupport 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("AddSendGiftEventAsync groupSupport key:%s, UserId:%d, err:%v", keySupportNum, sendGiftEvent.SendUserId, err)
		}
		return nil
	})
hujiebin's avatar
hujiebin committed
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
}

func OnMic() {
	// 上麦经验/上麦时长
	mic_ev.AddOnMicEventAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*mic_ev.OnMicEvent)
		if !ok {
			model.Log.Errorf("AddOnMicEventSync event type err")
			return nil
		}
		if err := group_mic_s.NewGroupPowerService(model.MyContext).IncrGroupPowerOnMicExpAndTime(event.GroupUuid, event.UserId, event.Timestamp); err != nil {
			model.Log.Errorf("cron micIn GroupPowerOnMicExp err:%v", err)
		} else {
			model.Log.Infof("cron micIn GroupPowerOnMicExp success, groupId:%v, userId:%v", event.GroupUuid, event.UserId)
		}
		return nil
	})
	// 用户上麦时长
	mic_ev.AddOnMicEventAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*mic_ev.OnMicEvent)
		if !ok {
			model.Log.Errorf("AddOnMicEventSync event type err")
			return nil
		}
		if err := mic_m.IncrUserOnMic(model, event.UserId, event.Timestamp); err != nil {
			model.Log.Errorf("cron micIn IncrUserOnMic err:%v", err)
		} else {
			model.Log.Infof("cron micIn IncrUserOnMic success,userId:%v", event.UserId)
		}
iamhujiebin's avatar
iamhujiebin committed
611 612
		// 处理活动数据 活动已经结束
		//go rpc.AddActPoint(model, event.UserId, 1, 0)
hujiebin's avatar
hujiebin committed
613 614 615
		return nil
	})
}