room.go 22.3 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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
package group_m

import (
	"context"
	"encoding/json"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/resource/mysql"
	"git.hilo.cn/hilo-common/resource/redisCli"
	redis2 "github.com/go-redis/redis/v8"
	"github.com/sirupsen/logrus"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"hilo-group/_const/enum/group_e"
	"hilo-group/_const/redis_key"
	"hilo-group/domain/cache/room_c"
	"hilo-group/domain/model/user_m"
	"hilo-group/myerr"
	"sort"
	"strconv"
	"strings"
	"time"
)

/**
* redis记录。
  sortSet记录房间中的人 (groupUid_userId),5分钟的http麦位信息轮询, 更新sortSet的分值(有效期),并且更新sortSet的过期时间
  选择sortSet,而不是 key的原因:
      1:扩展更多功能,在房间的人,房间内先入后到的排名(score的高低位)
      2:通过sortSet的key的生命周期,一样能保证清理僵尸用户。
*/

func getMemberStr(groupUid string, userId uint64) string {
	return groupUid + "_" + strconv.FormatUint(userId, 10)
}

func analysisMemberStr(memberStr string) (string, uint64) {
	strs := strings.Split(memberStr, "_")
	groupUid := strs[0]
	userId, err := strconv.ParseUint(strs[1], 10, 64)
	if err != nil {
		mylogrus.MyLog.Errorf("analysisMemberStr memberStr:%v err:%+v", memberStr, err)
	}
	return groupUid, userId
}

func RoomLivingExpire(model *domain.Model, groupUid string, userId uint64) {
	model.Log.Infof("room RoomLivingExpire userId:%v, groupUid:%v", userId, groupUid)
	//
	key := redis_key.GetPrefixGroupRoomLiving()
	i, err := redisCli.GetRedis().ZAdd(context.Background(), key, &redis2.Z{
		Score:  float64(time.Now().Unix()),
		Member: getMemberStr(groupUid, userId),
	}).Result()
	if err != nil {
		model.Log.Errorf("RoomLivingExpire ZAdd key:%v, groupUid:%v, userId:%v, err:%v", key, groupUid, userId, err)
	} else {
		model.Log.Infof("RoomLivingExpire ZAdd key:%v, groupUid:%v, userId:%v result:%v", key, groupUid, userId, i)
	}
}

//进入房间
func RoomLivingIn(model *domain.Model, groupUid string, userId uint64, externalId string, robot bool) error {
	model.Log.Infof("room RoomLivingIn userId:%v, groupUid:%v", userId, groupUid)
	//这个可以考虑用go协程
	if !robot {
		RoomLivingLeave(model, userId, externalId, "")
	}
	//
	key := redis_key.GetPrefixGroupRoomLiving()
	i, err := redisCli.GetRedis().ZAdd(context.Background(), key, &redis2.Z{
		Score:  float64(time.Now().Unix()),
		Member: getMemberStr(groupUid, userId),
	}).Result()
	if err != nil {
		model.Log.Errorf("UpdateRoomLiving ZAdd key:%v, groupUid:%v, userId:%v, err:%v", key, groupUid, userId, err)
	} else {
		model.Log.Infof("UpdateRoomLiving ZAdd key:%v, groupUid:%v, userId:%v result:%v", key, groupUid, userId, i)
	}

	go func(myContext *mycontext.MyContext, groupId string) {
hujiebin's avatar
hujiebin committed
83
		model := domain.CreateModelContext(myContext) // 野协程给新的model.Db
hujiebin's avatar
hujiebin committed
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 112 113 114 115 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 143 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 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 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
		roomOnlineUser, err := GetRoomOnlineUser(myContext, groupId)
		if err != nil {
			myContext.Log.Errorf("room RoomLivingIn roomOnlineUser err:%+v, groupUid:%v", err, groupId)
			//错误,跳过,不退出
			return
		}
		buf, err := json.Marshal(roomOnlineUser)
		if err != nil {
			myContext.Log.Errorf("room RoomLivingIn roomOnlineUser json.Marshal err:%+v groupUid:%v", err, groupId)
			//错误,跳过,不退出
			return
		} else {
			myContext.Log.Infof("room RoomLivingIn json.Marshal success groupUid:%v, data:%v", groupId, string(buf))
			//发送在线
			sendSignalMsg(model, groupId, GroupSystemMsg{
				MsgId:   group_e.GroupOnlineUser,
				Content: string(buf),
			}, true)
		}
	}(model.MyContext, groupUid)

	return err
}

//离开房间
func roomLivingLeave(model *domain.Model, userId uint64, groupId string) ([]string, error) {
	model.Log.Infof("RoomLivingLeave userId:%v", userId)
	key := redis_key.GetPrefixGroupRoomLiving()

	//if err := redisCli.ClearExpired(key, expireMinute); err != nil {
	if err := model.Redis.ZRemRangeByScore(model, key, "0", strconv.FormatInt(time.Now().Unix()-expireMinute, 10)).Err(); err != nil {
		return nil, myerr.WrapErr(err)
	}

	data, err := redisCli.GetRedis().ZRange(context.Background(), key, 0, -1).Result()
	if err != nil {
		return nil, myerr.WrapErr(err)
	}

	groupIds := make([]string, 0)
	for i, _ := range data {
		gid, uid := analysisMemberStr(data[i])
		if uid == userId && (groupId == "" || gid == groupId) {
			if _, err := redisCli.GetRedis().ZRem(context.Background(), key, getMemberStr(gid, uid)).Result(); err != nil {
				model.Log.Errorf("RoomLivingLeave ZRem key:%s, groupId:%s, userId:%d, err:%v", key, gid, uid, err)
				return nil, myerr.WrapErr(err)
			} else {
				groupIds = append(groupIds, gid)
				model.Log.Infof("RoomLivingLeave ZRem success key:%s, groupId:%s, userId:%d", key, gid, uid)
			}

			// 发信令,让前端重新拉取,接受容错,
			user, err := user_m.GetUser(model, userId)
			if err != nil || user == nil {
				model.Log.Errorf("RoomLivingLeave GetUser err:%v", err)
			} else {
				sendSignalMsg(model, gid, GroupSystemMsg{
					MsgId:  group_e.GroupOutSignal,
					Source: user.ExternalId,
				}, false)
			}
		}
	}

	go func(myContext *mycontext.MyContext, groupIds []string) {
		for i, groupId := range groupIds {
			roomOnlineUser, err := GetRoomOnlineUser(myContext, groupIds[i])
			if err != nil {
				myContext.Log.Errorf("RoomLivingLeave roomOnlineUser err:%+v, groupUid:%v", err, groupId)
				//错误,跳过,不退出
				continue
			}
			buf, err := json.Marshal(roomOnlineUser)
			if err != nil {
				myContext.Log.Errorf("RoomLivingLeave roomOnlineUser json.Marshal err:%+v groupUid:%v", err, groupId)
				//错误,跳过,不退出
				continue
			} else {
				myContext.Log.Infof("RoomLivingLeave json.Marshal success groupUid:%v, data:%v", groupId, string(buf))
				//发送在线
				sendSignalMsg(domain.CreateModelContext(myContext), groupId, GroupSystemMsg{
					MsgId:   group_e.GroupOnlineUser,
					Content: string(buf),
				}, true)
			}
		}
	}(model.MyContext, groupIds)

	return groupIds, nil
}

//离开房间
func RoomLivingLeave(model *domain.Model, userId uint64, externalId string, groupId string) ([]string, error) {
	model.Log.Infof("RoomLivingLeave userId:%v", userId)
	//获取用户是否在麦上, 让用户离开麦
	micUser, err := GetMicUserByExternalId(model, externalId)
	if err != nil {
		return nil, err
	}
	if micUser != nil {
		if err = micUser.LeaveByUser(userId, externalId); err != nil {
			return nil, err
		}
	}
	return roomLivingLeave(model, userId, groupId)
}

//被踢出离开房间
func RoomLivingLeaveByKick(model *domain.Model, groupUuid string, beKickuserId uint64, beKickExternalId string, userExternalId string) ([]string, error) {
	model.Log.Infof("RoomLivingLeaveByKick userId:%v", beKickuserId)
	//获取用户是否在麦上, 让用户离开麦
	micUser, err := GetMicUserByExternalId(model, beKickExternalId)
	if err != nil {
		return nil, err
	}
	if micUser != nil {
		if err = micUser.LeaveByUser(beKickuserId, beKickExternalId); err != nil {
			return nil, err
		}
	}
	// 发信令,让前端重新拉取,接受容错,
	/*	sendSignalMsg(model, groupUuid, GroupSystemMsg{
		MsgId:  group_enum.GroupKickOut,
		Source: userExternalId,
		Target: beKickExternalId,
	}, false)*/

	MicGroupKickOutRPush(model, groupUuid, userExternalId, beKickExternalId)
	return roomLivingLeave(model, beKickuserId, groupUuid)
}

// 直播群状态回调导致的下麦
func RoomLivingLeaveByOffline(model *domain.Model, userId uint64, externalId string, groupId string) ([]string, error) {
	//获取用户是否在麦上, 让用户离开麦
	micUser, err := GetMicUserByExternalId(model, externalId)
	if err != nil {
		return nil, err
	}

	if micUser != nil {
		model.Log.Infof("RoomLivingLeaveByOffline user %d, %s, groupId:%s", userId, externalId, groupId)

		if err = micUser.LeaveByUser(userId, externalId); err != nil {
			return nil, err
		}
		MicSocketMicOutRPush(model, micUser.GroupUuid, micUser.ExternalId)
	}
	return roomLivingLeave(model, userId, groupId)
}

//socket掉线被提出
func RoomLivingLeaveByMgr(model *domain.Model, userId uint64, externalId string, groupId string) ([]string, error) {
	model.Log.Infof("RoomLivingLeaveByMgr userId:%v", userId)
	//获取用户是否在麦上, 让用户离开麦
	micUser, err := GetMicUserByExternalId(model, externalId)
	if err != nil {
		return nil, err
	}
	if micUser != nil {
		if err = micUser.LeaveByUser(userId, externalId); err != nil {
			return nil, err
		}
		MicSocketMicOutRPush(model, micUser.GroupUuid, micUser.ExternalId)
	}
	groupIds, err := roomLivingLeave(model, userId, groupId)
	if err != nil {
		return groupIds, err
	}

	// 发送信令
	type removeHistoryParam struct {
		RemoveHistory bool `json:"removeHistory"`
	}
	r := removeHistoryParam{RemoveHistory: true}
	buf, err := json.Marshal(r)
	if err == nil {
		systemMsg := GroupSystemMsg{MsgId: group_e.GroupMemberRemoveSignal,
			Target: externalId, Content: string(buf)}
		for i, _ := range groupIds {
			sendSignalMsg(model, groupIds[i], systemMsg, false)
		}
	}
	return groupIds, err
}

//获取在房间的用户(其中成员的位置按分数值递增(从大到小)来排序)
func RoomLivingExistsUserId(groupUid string) ([]uint64, error) {
	key := redis_key.GetPrefixGroupRoomLiving()
	//if err := redisCli.ClearExpired(key, expireMinute); err != nil {
	var model = domain.CreateModelNil()
	if err := model.Redis.ZRemRangeByScore(model, key, "0", strconv.FormatInt(time.Now().Unix()-expireMinute, 10)).Err(); err != nil {
		return nil, myerr.WrapErr(err)
	}

	groupUserIdstrs, err := redisCli.GetRedis().ZRevRange(context.Background(), key, 0, -1).Result()
	if err != nil {
		return nil, myerr.WrapErr(err)
	}
	mylogrus.MyLog.Infof("group_room_living RoomLivingExistsUserId groupUserIdstrs:%v", groupUserIdstrs)
	userIds := make([]uint64, 0, len(groupUserIdstrs))
	for i, _ := range groupUserIdstrs {
		tempGroupUid, userId := analysisMemberStr(groupUserIdstrs[i])
		if tempGroupUid == groupUid {
			userIds = append(userIds, userId)
		}
	}
	return userIds, nil
}

//获取在房间的用户 返回值:map,key:userId, value:groupUuid
func RoomLivingUserIdFilter(userIds []mysql.ID) (map[mysql.ID]string, error) {
	userIdSet := map[mysql.ID]struct{}{}
	for i, _ := range userIds {
		userIdSet[userIds[i]] = struct{}{}
	}
	key := redis_key.GetPrefixGroupRoomLiving()
	//if err := redisCli.ClearExpired(key, expireMinute); err != nil {
	model := domain.CreateModelNil()
	if err := model.Redis.ZRemRangeByScore(model, key, "0", strconv.FormatInt(time.Now().Unix()-expireMinute, 10)).Err(); err != nil {
		return nil, myerr.WrapErr(err)
	}

	groupUserIdstrs, err := redisCli.GetRedis().ZRange(context.Background(), key, 0, -1).Result()
	if err != nil {
		return nil, myerr.WrapErr(err)
	}

	resultUserSet := map[mysql.ID]string{}
	for i, _ := range groupUserIdstrs {
		tempGroupUid, userId := analysisMemberStr(groupUserIdstrs[i])
		mylogrus.MyLog.Debugf("RoomLivingUserIdFilter, analysisMemberStr %s, %d", tempGroupUid, userId)

		if _, flag := userIdSet[userId]; flag {
			resultUserSet[userId] = tempGroupUid
		}
	}
	return resultUserSet, nil
}

//获取有人的房间, 返回值:Map[groupUid]Set<UserId>
func RoomLivingExistsGroup(model *domain.Model) (map[string]map[uint64]struct{}, error) {
	key := redis_key.GetPrefixGroupRoomLiving()
	//if err := redisCli.ClearExpired(key, expireMinute); err != nil {
	if err := model.Redis.ZRemRangeByScore(model, key, "0", strconv.FormatInt(time.Now().Unix()-expireMinute, 10)).Err(); err != nil {
		model.Log.Infof("RoomLivingExistsGroup: err:%v", err)
		return nil, myerr.WrapErr(err)
	}

	groupUserIdstrs, err := redisCli.GetRedis().ZRange(context.Background(), key, 0, -1).Result()
	if err != nil {
		return nil, myerr.WrapErr(err)
	}
	model.Log.Infof("group_room_living RoomLivingExistsGroup groupUserIdstrs:%v", groupUserIdstrs)
	groupGroup := map[string]map[uint64]struct{}{}
	for i, _ := range groupUserIdstrs {
		tempGroupUid, userId := analysisMemberStr(groupUserIdstrs[i])
		if a, flag := groupGroup[tempGroupUid]; flag {
			a[userId] = struct{}{}
		} else {
			groupGroup[tempGroupUid] = map[uint64]struct{}{userId: {}}
		}
	}
	model.Log.Infof("RoomLivingExistsGroup size = %d", len(groupGroup))
	return groupGroup, nil
}

func BatchGetRoomCount(model *domain.Model, groupIds []string) (map[string]int, error) {
	liveRoom, err := RoomLivingExistsGroup(model)
	if err != nil {
		return nil, err
	}

	result := make(map[string]int, 0)
	for _, i := range groupIds {
		result[i] = len(liveRoom[i])
	}
	return result, nil
}

// 取所有有人的房间,以房间人数排序
func GetAllGroupsSorted(model *domain.Model) ([]string, error) {
	// 1. 当前有人的房间
	liveRoom, err := RoomLivingExistsGroup(model)
	if err != nil {
		model.Log.Infof("GetAllGroupsSorted: err:%v", err)
		return nil, err
	}
	roomCount := make(map[string]int, 0)
	for k, i := range liveRoom {
		roomCount[k] = len(i)
	}
	model.Log.Infof("GetAllGroupsSorted, living room : %v", roomCount)

	groupIds := make([]string, 0)
	for k, _ := range liveRoom {
		groupIds = append(groupIds, k)
	}

	sort.Slice(groupIds, func(i, j int) bool {
		gi := groupIds[i]
		gj := groupIds[j]
		return roomCount[gi] > roomCount[gj] || roomCount[gi] == roomCount[gj] && gi < gj
	})
	return groupIds, nil
}

// 取房间最近N天的访问人数
func GetRoomVisitCount(groupId string) (int64, error) {
	// 每群定时请一次数据可以了
	now := time.Now()
	if now.Second()%redis_key.GroupInDurationClearPeriod == 0 {
		// 先移除(N天之前的),后统计
		if _, err := redisCli.GetRedis().ZRemRangeByScore(context.Background(), redis_key.GetPrefixGroupInUserDuration(groupId),
			"0", strconv.FormatInt(time.Now().AddDate(0, 0, -redis_key.GroupInDurationTTL).Unix(), 10)).
			Result(); err != nil {
			return 0, err
		}
	}

	visitCount, err := redisCli.GetRedis().ZCard(context.Background(), redis_key.GetPrefixGroupInUserDuration(groupId)).Result()
	if err != nil {
		return 0, err
	}
	return visitCount, nil

}

// 批量查询房间的麦位 TODO: 优化性能
func GetRoomMicUsers(model *domain.Model, groupIds []string) (map[string][]uint64, error) {
	result := make(map[string][]uint64, 0)
	for _, g := range groupIds {
		userIds, err := GetAllMicUser(g)
		if err != nil {
			model.Log.Warnf("GetRoomMicUsers, GetAllMicUser: groupId %s err %s", g, err.Error())
		} else {
			result[g] = userIds
		}
	}
	return result, nil
}

// 批量查询房间最近N天的访问人数。使用二级缓存
func BatchGetRoomVisitCount(logE *logrus.Entry, groupIds []string) (map[string]int64, error) {
	//roomVisitCount, err := redis.GetAllRoomVisitCount()
	roomVisitCount, err := room_c.MGetRoomVisitCount(groupIds)
	if err != nil {
		return nil, err
	}
	logE.Infof("MGetRoomVisitCount:%v", roomVisitCount)

	visitCount := make(map[string]int64)
	for _, groupId := range groupIds {
		// 先从二级缓存中找
		if c, ok := roomVisitCount[groupId]; ok {
			if vc, err := strconv.ParseInt(c, 10, 64); err == nil && vc > 0 {
				logE.Debugf("GetRoomVisitCount, from roomVisitCount %s - %d", groupId, vc)

				visitCount[groupId] = vc
			}
		} else {
			// 如果没有,就从roomVisit中取
			if vc, err := room_c.GetSetRoomVisitCount(groupId); err == nil && vc > 0 {
				logE.Infof("GetRoomVisitCount, from roomVisit %s - %d", groupId, vc)

				visitCount[groupId] = vc
			}
		}
	}
	return visitCount, nil
}

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
type RoomVisit struct {
	ImGroupId string
	Count     int64
}

// 批量查询房间最近N天的访问人数。使用二级缓存
func BatchGetRoomVisitCountList(logE *logrus.Entry, groupIds []string) (map[string]int64, []*RoomVisit, error) {
	//roomVisitCount, err := redis.GetAllRoomVisitCount()
	roomVisitCount, err := room_c.MGetRoomVisitCount(groupIds)
	if err != nil {
		return nil, nil, err
	}
	logE.Infof("MGetRoomVisitCount:%v", roomVisitCount)

	res := make([]*RoomVisit, 0, len(groupIds))
	visitCount := make(map[string]int64)
	for _, groupId := range groupIds {
		// 先从二级缓存中找
		if c, ok := roomVisitCount[groupId]; ok {
			if vc, err := strconv.ParseInt(c, 10, 64); err == nil && vc > 0 {
				logE.Debugf("GetRoomVisitCount, from roomVisitCount %s - %d", groupId, vc)

				visitCount[groupId] = vc
				res = append(res, &RoomVisit{ImGroupId: groupId, Count: vc})
			}
		} else {
			// 如果没有,就从roomVisit中取
			if vc, err := room_c.GetSetRoomVisitCount(groupId); err == nil && vc > 0 {
				logE.Infof("GetRoomVisitCount, from roomVisit %s - %d", groupId, vc)

				visitCount[groupId] = vc
				res = append(res, &RoomVisit{ImGroupId: groupId, Count: vc})
			}
		}
	}
	sort.Slice(res, func(i, j int) bool {
		return res[i].Count >= res[j].Count
	})
	return visitCount, res, nil
}

hujiebin's avatar
hujiebin committed
496 497 498 499 500 501
type UserEnterRoom struct {
	UserId    uint64
	GroupId   string
	EnterTime time.Time
}

hujiebin's avatar
hujiebin committed
502
func (uer *UserEnterRoom) Save(db *gorm.DB) error {
hujiebin's avatar
hujiebin committed
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 542 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 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 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
	return db.Clauses(clause.OnConflict{UpdateAll: true}).Create(uer).Error
}

func (uer *UserEnterRoom) Find(db *gorm.DB) ([]UserEnterRoom, error) {
	rows := make([]UserEnterRoom, 0)
	if err := db.Where(uer).Order("enter_time DESC").Find(&rows).Error; err != nil {
		return nil, err
	}
	return rows, nil
}

type RoomMonthConsume struct {
	GroupId string
	Month   string
	Diamond uint64
}

func (rmc *RoomMonthConsume) AddDiamond(db *gorm.DB) error {
	return db.Clauses(clause.OnConflict{
		DoUpdates: clause.Assignments(map[string]interface{}{
			"diamond": gorm.Expr("diamond + ?", rmc.Diamond)}),
	}).Create(rmc).Error
}

func (rmc *RoomMonthConsume) GetTotalDiamond(db *gorm.DB) (uint64, error) {
	rows := make([]struct{ Total uint64 }, 0)
	if err := db.Model(&RoomMonthConsume{}).Select("SUM(diamond) AS total").
		Where(rmc).Find(&rows).Error; err != nil {
		return 0, err
	}
	if len(rows) > 0 {
		return rows[0].Total, nil
	}
	return 0, nil
}

type RoomWeekConsume struct {
	GroupId string
	Week    string
	Diamond uint64
}

func (rwc *RoomWeekConsume) AddDiamond(db *gorm.DB) error {
	return db.Clauses(clause.OnConflict{
		DoUpdates: clause.Assignments(map[string]interface{}{
			"diamond": gorm.Expr("diamond + ?", rwc.Diamond)}),
	}).Create(rwc).Error
}

type RoomDayConsume struct {
	GroupId string
	Day     string
	Diamond uint64
}

func (rdc *RoomDayConsume) AddDiamond(db *gorm.DB) error {
	return db.Clauses(clause.OnConflict{
		DoUpdates: clause.Assignments(map[string]interface{}{
			"diamond": gorm.Expr("diamond + ?", rdc.Diamond)}),
	}).Create(rdc).Error
}

type RoomUserDayConsume struct {
	Day        string
	GroupId    string
	SendUserId uint64
	Diamond    uint64
}

func (rudc *RoomUserDayConsume) AddDiamond(db *gorm.DB) error {
	return db.Clauses(clause.OnConflict{
		DoUpdates: clause.Assignments(map[string]interface{}{
			"diamond": gorm.Expr("diamond + ?", rudc.Diamond)}),
	}).Create(rudc).Error
}

// todo todo 这里可能需要!!!
//func init() {
//	event.AddGroupInAsync(func(model *domain.Model, e interface{}) error {
//		event := e.(*event.GroupInEvent)
//		model.Log.Infof("AddGroupInAsync for room: %+v", event)
//
//		uer := UserEnterRoom{
//			UserId:    event.UserId,
//			GroupId:   event.GroupId,
//			EnterTime: time.Now(),
//		}
//		err := uer.save(model.Db)
//		model.Log.Infof("AddGroupInAsync, UserEnterRoom err: %v", err)
//		return err
//	})
//
//	event.AddSendGift(func(model *domain.Model, event *event.SendGiftEvent) error {
//		if event.NoExecSync {
//			model.Log.Infof("Skip %+v", event)
//			return nil
//		}
//		model.Log.Infof("AddSendGift room: %+v", event)
//
//		// 群消费月表涉及总消费额,需要在同步处理中,但不报错
//		if event.ResGift.GiftType == mysql.DiamondYellow && event.SceneType == gift_m2.GroupSceneType && len(event.SceneUid) > 0 {
//			now := time.Now()
//			rmc := RoomMonthConsume{
//				GroupId: event.SceneUid,
//				Month:   now.Format(common.COMPACT_MONTH_FORMAT),
//				Diamond: event.GetDiamondTotal(),
//			}
//			err := rmc.AddDiamond(model.Db)
//			if err != nil {
//				model.Log.Infof("AddSendGiftAsync room, %+v AddDiamond failed: %v", rmc, err)
//			} else {
//				// 增加后在事务中读出来,供异步事件中使用
//				rmc = RoomMonthConsume{GroupId: event.SceneUid}
//				if consume, err := rmc.GetTotalDiamond(model.Db); err == nil {
//					event.TotalConsume = consume
//				}
//			}
//		}
//		return nil
//	})
//
//	event.AddSendGiftAsync(func(model *domain.Model, event *event.SendGiftEvent) error {
//		model.Log.Infof("AddSendGiftAsync room: %+v", event)
//
//		if event.SceneType == gift_m2.GroupSceneType && len(event.SceneUid) > 0 {
//			now := time.Now()
//			rwc := RoomWeekConsume{
//				GroupId: event.SceneUid,
//				Week:    common.GetMonday(now).Format(common.DATE_FORMAT),
//				Diamond: event.GetDiamondTotal(),
//			}
//			err := rwc.AddDiamond(model.Db)
//			if err != nil {
//				model.Log.Infof("AddSendGiftAsync room, %+v AddDiamond failed: %v", rwc, err)
//			}
//
//			rdc := RoomDayConsume{
//				GroupId: event.SceneUid,
//				Day:     now.Format(common.DATE_FORMAT),
//				Diamond: event.GetDiamondTotal(),
//			}
//			err = rdc.AddDiamond(model.Db)
//			if err != nil {
//				model.Log.Infof("AddSendGiftAsync room, %+v AddDiamond failed: %v", rdc, err)
//			}
//
//			err = redis.ClearGroupConsume(event.SceneUid)
//			if err != nil {
//				model.Log.Infof("AddSendGiftAsync room, group %s redis.ClearGroupConsume failed: %v", event.SceneUid, err)
//			}
//		}
//		return nil
//	})
//
//	event.AddSendGiftAsync(func(model *domain.Model, event *event.SendGiftEvent) error {
//		model.Log.Infof("AddSendGiftAsync roomUserDay: %+v", event)
//
//		if event.SceneType == gift_m2.GroupSceneType && len(event.SceneUid) > 0 {
//			now := time.Now()
//			rudc := RoomUserDayConsume{
//				Day:        now.Format(common.DATE_FORMAT),
//				GroupId:    event.SceneUid,
//				SendUserId: event.SendUserId,
//				Diamond:    event.GetDiamondTotal(),
//			}
//			err := rudc.AddDiamond(model.Db)
//			if err != nil {
//				model.Log.Infof("AddSendGiftAsync roomUserDay, %+v AddDiamond failed: %v", rudc, err)
//			}
//			return err
//		}
//		return nil
//	})
//
//	event.AddUserFreezeAsync(func(model *domain.Model, event *event.UserFreezeEvent) error {
//		model.Log.Infof("AddUserFreezeAsync room: %+v", event)
//		user, err := user_m.GetUser(model, event.UserId)
//		if err != nil {
//			return err
//		}
//		if _, err := RoomLivingLeaveByMgr(model, user.ID, user.ExternalId, ""); err != nil {
//			model.Log.Errorf("AddUserFreezeAsync room err:%v", err)
//		}
//
//		// 连带重置群信息
//		if gi, err := FindGroupByOwner(model, event.UserId); err == nil && len(gi) > 0 {
//			if err = ResetGroupInfo(model, gi[0].ImGroupId, gi[0].Code); err != nil {
//				model.Log.Warnf("ResetGroupInfo user %d's group %s failed", event.UserId, gi[0].ImGroupId)
//			}
//		}
//		return nil
//	})
//
//}