group_info.go 38 KB
Newer Older
1 2 3
package group_r

import (
hujiebin's avatar
hujiebin committed
4 5
	"context"
	"encoding/json"
6 7 8 9
	"fmt"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/mysql"
hujiebin's avatar
hujiebin committed
10 11
	"git.hilo.cn/hilo-common/resource/redisCli"
	"git.hilo.cn/hilo-common/rpc"
12 13 14 15 16 17
	"git.hilo.cn/hilo-common/utils"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"gorm.io/gorm"
	"hilo-group/_const/enum/game_e"
	"hilo-group/_const/enum/group_e"
hujiebin's avatar
hujiebin committed
18 19 20 21
	"hilo-group/_const/enum/mgr_e"
	"hilo-group/_const/enum/msg_e"
	"hilo-group/_const/redis_key"
	"hilo-group/cv/gift_cv"
22 23 24 25 26 27 28 29 30
	"hilo-group/cv/group_cv"
	"hilo-group/cv/group_power_cv"
	"hilo-group/cv/medal_cv"
	"hilo-group/cv/user_cv"
	"hilo-group/domain/cache/res_c"
	"hilo-group/domain/cache/room_c"
	"hilo-group/domain/model/game_m"
	"hilo-group/domain/model/group_m"
	"hilo-group/domain/model/mgr_m"
hujiebin's avatar
hujiebin committed
31
	"hilo-group/domain/model/noble_m"
32 33 34 35 36 37 38 39 40
	"hilo-group/domain/model/res_m"
	"hilo-group/domain/model/rocket_m"
	"hilo-group/domain/model/user_m"
	"hilo-group/domain/model/version_m"
	"hilo-group/domain/service/group_s"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
	"hilo-group/req"
	"hilo-group/resp"
hujiebin's avatar
hujiebin committed
41
	"net/url"
42
	"sort"
hujiebin's avatar
hujiebin committed
43
	"strconv"
44
	"strings"
hujiebin's avatar
hujiebin committed
45
	"time"
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 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
)

// @Tags 群组
// @Summary 查询我创建的群组
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Success 200 {object} group_cv.JoinedGroupInfo
// @Router /v1/imGroup/ownGroup [get]
func GetOwnGroup(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)

	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)

	groupIds, err := group_m.GetGroups(model.Db, userId, group_e.GROUP_OWNER)
	if err != nil {
		return myContext, err
	}
	if len(groupIds) <= 0 {
		resp.ResponseOk(c, []group_cv.JoinedGroupInfo{})
		return myContext, nil
	}

	myService := domain.CreateService(myContext)
	result, _, err := group_cv.BuildJoinedGroupInfo(myService, userId, groupIds, 1, 1)
	if err != nil {
		return myContext, err
	}

	resp.ResponseOk(c, result)
	return myContext, nil
}

// @Tags 群组
// @Summary 查询某人的(公开)群组(新)
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param userExternalId path string true "用户ExternalId"
// @Success 200 {object} group_cv.TheirGroupsInfo
// @Router /v1/imGroup/theirGroup/{userExternalId} [get]
func GetTheirGroups(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)

	externalId := c.Param("userExternalId")
	if len(externalId) <= 0 {
		return myContext, myerr.NewSysError("userExternalId 为必填项")
	}

	model := domain.CreateModelContext(myContext)

	user, err := user_m.GetUserByExtId(model, externalId)
	if err != nil {
		return myContext, err
	}
	userId := user.ID
	if userId == 0 {
		return myContext, bizerr.InvalidParameter
	}

	data, err := group_m.GetJoinedGroups(model.Db, userId)
	if err != nil {
		return myContext, err
	}

	result := group_cv.TheirGroupsInfo{Total: 0}

	groupIds := make([]string, 0)
	for _, i := range data {
		groupIds = append(groupIds, i.GroupId)
	}
	groupInfo, err := group_m.BatchGetGroupInfo(model, groupIds)
	if err != nil {
		return myContext, err
	}
	owners := make([]uint64, 0)
	for _, i := range groupInfo {
		owners = append(owners, i.Owner)
	}

	theirRoles, err := group_m.GetRolesByUser(model, userId)
	if err != nil {
		return myContext, err
	}
	model.Log.Info("GetTheirGroups: theirRoles - ", theirRoles)

hujiebin's avatar
hujiebin committed
137
	powerIds, powerNames, powerNameplates, powerGrades, err := group_power_cv.BatchGetGroupPower(model.Db, owners)
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
	if err != nil {
		return myContext, err
	}
	groupMedals, err := group_m.BatchGetMedals(model.Db, groupIds)
	if err != nil {
		return myContext, err
	}
	resMedal, err := res_m.MedalGetAllMap(model.Db)
	if err != nil {
		return myContext, err
	}

	roomMicUserMap, err := group_m.BatchGetAllMicUser(model, groupIds)
	if err != nil {
		return myContext, err
	}
	model.Log.Infof("GetTheirGroups: groupIds %v, roomMicUserMap %v", groupIds, roomMicUserMap)

	uids := owners
	micUsersMap := make(map[string][]uint64, 0)
	for _, i := range groupIds {
		micUsersMap[i] = make([]uint64, 0)

		u := roomMicUserMap[i]
		if len(u) >= 4 {
			micUsersMap[i] = u[0:4]
		} else {
			micUsersMap[i] = u
		}
		uids = append(uids, micUsersMap[i]...)
	}

	uids = utils.UniqueSliceUInt64(uids)
	userTiny, err := user_cv.GetUserTinyMap(uids)
	if err != nil {
		return myContext, err
	}

	model.Log.Infof("GetTheirGroups: userTinyMap: %v", userTiny)

	roomCount, err := group_m.BatchGetRoomCount(model, groupIds)
	if err != nil {
		return nil, err
	}

	onMic, err := group_cv.CheckMicHasUserByGroup(groupIds)
	if err != nil {
		return myContext, err
	}
	model.Log.Info("GetTheirGroups: onMic - ", onMic)

	countryInfo, err := res_c.GetCountryIconMap(model)
	if err != nil {
		return myContext, err
	}

	supportLevels, err := group_s.NewGroupService(myContext).GetWeekMaxSupportLevelMap()
	if err != nil {
		return myContext, err
	}

	rr := rocket_m.RocketResult{}
	maxStageMap, err := rr.GetMaxStage(mysql.Db, groupIds)
	if err != nil {
		return myContext, err
	}

	visitCount, err := group_m.BatchGetRoomVisitCount(model.Log, groupIds)
	if err != nil {
		return myContext, err
	}

	roomEnterTime, err := room_c.GetUserRoomVisit(userId)
	if err != nil {
		return myContext, err
	}
	model.Log.Infof("BuildJoinedGroupInfo, roomEnterTime: %v", roomEnterTime)
	// 正在进行的游戏
	games := game_m.GetNotEndGamesMap(model)

	for _, i := range data {
		gi := groupInfo[i.GroupId]
		// 只要公开的群
		if len(gi.Password) == 0 {
			result.Total++

			role := group_e.GROUP_MEMBER
			r, exists := theirRoles[i.GroupId]
			if exists {
				role = r
			}

			micUsers := make([]user_cv.CvUserTiny, 0)
			for _, j := range micUsersMap[i.GroupId] {
				micUsers = append(micUsers, userTiny[j])
			}

			var maxStage *uint16 = nil
			if s, ok := maxStageMap[i.GroupId]; ok {
				maxStage = &s
			}

			medals := make([]medal_cv.PicElement, 0)
			if m, ok := groupMedals[i.GroupId]; ok {
				for _, j := range m {
					mId := uint32(j)
					if e, ok := resMedal[mId]; ok {
						medals = append(medals, medal_cv.PicElement{
							PicUrl: e.PicUrl,
						})
					}
				}
			}
			// 补上房间流水勋章
			var pe *medal_cv.PicElement
			_, pe, err = medal_cv.GetGroupConsumeMedal(model, i.GroupId)
			if err != nil {
				model.Log.Infof("GetTheirGroups: GetGroupConsumeMedal: %s", err.Error())
			} else if pe != nil {
				medals = append(medals, medal_cv.PicElement{PicUrl: pe.PicUrl})
			}

			elem := group_cv.JoinedGroupInfo{
				PopularGroupInfo: group_cv.PopularGroupInfo{
					GroupInfo: group_cv.GroupInfo{
						GroupBasicInfo: group_cv.GroupBasicInfo{
							GroupId:             gi.TxGroupId,
							Name:                gi.Name,
							Introduction:        gi.Introduction,
							Notification:        gi.Notification,
							FaceUrl:             gi.FaceUrl,
							Code:                gi.Code,
							CountryIcon:         countryInfo[gi.Country],
							SupportLevel:        supportLevels[i.GroupId],
							GroupInUserDuration: visitCount[i.GroupId],
							MicNumType:          int(gi.MicNumType),
							GroupMedals:         medals,
						},
						//HasOnMic:       onMicScore[i.GroupId] != 0,
hujiebin's avatar
hujiebin committed
277 278 279 280 281 282 283 284 285
						GroupPowerId:        powerIds[gi.Owner],
						GroupPowerName:      powerNames[gi.Owner],
						GroupPowerNameplate: powerNameplates[gi.Owner],
						GroupPower: group_cv.GroupPower{
							Id:        powerIds[gi.Owner],
							Name:      powerNames[gi.Owner],
							Nameplate: powerNameplates[gi.Owner],
							Grade:     powerGrades[gi.Owner],
						},
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
					},
					MicUsers:      micUsers,
					RoomUserCount: uint(roomCount[i.GroupId]),
					MaxStage:      maxStage,
					GameTypes:     games[gi.TxGroupId],
				},
				LastEnterTime: roomEnterTime[i.GroupId],
			}

			if role == group_e.GROUP_OWNER {
				result.OwnGroups = append(result.OwnGroups, elem)
			} else {
				result.Groups = append(result.Groups, elem)
			}
		}
	}

	// fixme: 按最近进入时间?
	sort.Slice(result.OwnGroups, func(i, j int) bool {
		return result.OwnGroups[i].LastEnterTime > result.OwnGroups[j].LastEnterTime ||
			result.OwnGroups[i].LastEnterTime == result.OwnGroups[j].LastEnterTime && result.OwnGroups[i].GroupId < result.OwnGroups[j].GroupId
	})
	// fixme:按角色排?
	sort.Slice(result.Groups, func(i, j int) bool {
		return theirRoles[result.Groups[i].GroupId] > theirRoles[result.Groups[j].GroupId] ||
			theirRoles[result.Groups[i].GroupId] == theirRoles[result.Groups[j].GroupId] && result.Groups[i].GroupId < result.Groups[j].GroupId
	})

	resp.ResponseOk(c, result)
	return myContext, nil
}

// @Tags 群组
// @Summary 客户端组件加载完成
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId path string true "群ID"
// @Success 200
// @Router /v1/imGroup/pluginReady/{groupId} [put]
func PluginReady(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)

	userId, externalId, err := req.GetUserIdAndExtId(c, myContext)
	if err != nil {
		return myContext, err
	}

	groupId := c.Param("groupId")
	if len(groupId) <= 0 {
		return myContext, myerr.NewSysError("groupId 为必填项")
	}

	model := domain.CreateModelContext(myContext)

	groupId, err = group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	model.Log.Infof("PluginReady user %d, %s", userId, externalId)

	/** fixme:这协议没用了?

	go func(gi *group_m.GroupInfo) {
		defer func() {
			if r := recover(); r != nil {
				//打印错误堆栈信息
				mylogrus.MyLog.Errorf("PluginReady SYSTEM ACTION PANIC: %v, stack: %v", r, string(debug.Stack()))
			}
		}()

		if err := myService.OnJoinGroup(externalId, groupId); err != nil {
			model.Log.Infof("PluginReady, OnJoinGroup err %v", err)
		}
	}(groupInfo)*/

	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 查询房间信息
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId path string true "群ID"
hujiebin's avatar
hujiebin committed
373
// @Success 200 {object} group_cv.RoomInfo
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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 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 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
// @Router /v1/imGroup/roomInfo/{groupId} [get]
func GetRoomInfo(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)

	groupId := c.Param("groupId")
	if len(groupId) <= 0 {
		return myContext, myerr.NewSysError("groupId 为必填项")
	}

	model := domain.CreateModelContext(myContext)

	groupInfo, err := group_m.GetInfoByTxGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}
	if groupInfo == nil {
		return myContext, bizerr.GroupNotFound
	}
	groupId = groupInfo.ImGroupId

	memberCount, err := group_m.GetMemberCount(model.Db, groupId)
	if err != nil {
		return myContext, err
	}

	countryInfo, err := res_c.GetCountryIconMap(model)
	if err != nil {
		return myContext, err
	}

	// 版本控制
	_deviceType, _ := c.Get(mycontext.DEVICETYPE)
	_appVersion, _ := c.Get(mycontext.APP_VERSION)
	deviceType, appVersion := cast.ToString(_deviceType), cast.ToString(_appVersion)
	vc := version_m.GetVersionControlByPlatform(model, deviceType)
	allow, err := utils.CompareVersion(appVersion, fmt.Sprintf("<= %s", vc.AuditVersion))
	model.Log.Infof("GetRoomInfo CompareVersion appVersion:%v,auditVersion:%v,allow:%v,err:%v", appVersion, vc.AuditVersion, allow, err)

	themeUrl := ""
	var themeId uint64 = 0
	var themeType uint8 = 1
	if groupInfo.ThemeId > 0 {
		//官方主题
		// 找不到的,默认为空(可能被后台删了)
		themeType = 1
		themes, _ := res_m.GroupThemeGetAll(model.Db)
		for _, i := range themes {
			if i.ID == uint64(groupInfo.ThemeId) {
				themeUrl = i.Url
				themeId = i.ID
				break
			}
		}
	} else {
		//可能是自定义主题
		themeId, themeUrl, err = group_m.GetShowCustomTheme(model, groupInfo.ImGroupId)
		if err != nil {
			return myContext, err
		}
		if themeId != 0 {
			themeType = 2
		}
	}

	result := group_cv.RoomInfo{
		GroupBasicInfo: group_cv.GroupBasicInfo{
			GroupId:        groupInfo.TxGroupId,
			Name:           groupInfo.Name,
			Introduction:   groupInfo.Introduction,
			Notification:   groupInfo.Notification,
			FaceUrl:        groupInfo.FaceUrl,
			MemberNum:      memberCount,
			Code:           groupInfo.Code,
			CountryIcon:    countryInfo[groupInfo.Country],
			MicNumType:     int(groupInfo.MicNumType),
			TouristMic:     groupInfo.TouristMic,
			TouristSendMsg: groupInfo.TouristSendMsg,
			TouristSendPic: groupInfo.TouristSendPic,
			MemberFee:      groupInfo.MemberFee,
		},
		MicOn:       groupInfo.MicOn,
		LoadHistory: groupInfo.LoadHistory,
		ThemeId:     themeId,
		ThemeUrl:    themeUrl,
		ThemeType:   themeType,
		Role:        group_e.GROUP_VISITOR,
		GameConfig:  &game_m.GameConfig{Ludo: &game_m.GameConfigDiamond{Diamond: game_e.GameLudoDiamondList}, Uno: &game_m.GameConfigDiamond{Diamond: game_e.GameLudoDiamondList}},
	}

	roles, _, err := group_m.GetRolesInGroup(model, groupInfo.ImGroupId)
	if err != nil {
		return myContext, err
	}

	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	isGroupMember, err := group_m.IsGroupMember(model.Db, groupId, userId)
	if err != nil {
		return myContext, err
	}
	if isGroupMember {
		result.Role = group_e.GROUP_MEMBER
	}

	emptyStr := ""
	if len(groupInfo.Password) > 0 {
		// 代表有密码
		result.Password = &emptyStr
	}

	//userIds := make([]uint64, 0)
	for u, r := range roles {
		//userIds = append(userIds, u)

		if u == userId {
			if r == group_e.GROUP_OWNER || r == group_e.GROUP_MANAGER {
				// 如果用户是OW或经理,可以看到密码,同时设置角色
				result.Role = r

				if len(groupInfo.Password) > 0 {
					result.Password = &groupInfo.Password
				}
			} else if r == group_e.GROUP_ADMIN {
				// 如果用户是管理员,仅设置角色
				result.Role = r
			}
		}
	}

	roles, orderList, err := group_m.GetRolesInGroup(model, groupId)
	if err != nil {
		return myContext, err
	}

	users, err := user_cv.GetUserTinyMap(orderList)
	if err != nil {
		return myContext, err
	}

	for _, i := range orderList {
		result.RoleMembers = append(result.RoleMembers, group_cv.SimpleRoleInfo{
			ExternalId: users[i].ExternalId,
			Role:       roles[i],
		})
	}

	// ---- 以下部分相对没那么重要,失败也不返回 ----

	supportLevels, err := group_s.NewGroupService(myContext).GetWeekMaxSupportLevelMap()
	if err != nil {
		model.Log.Infof("GetRoomInfo: GetWeekMaxSupportLevelMap: %s", err.Error())
	}
	result.SupportLevel = supportLevels[groupId]

	result.DiceNum = group_e.GROUP_DICE_NUM_DEFAULT
	result.DiceType = 1 // 1: [0-9] 2:[1-6]
	gs := group_m.GroupSetting{GroupId: groupId}
	err = gs.Get(model.Db)
	if err == nil {
		result.DiceNum = gs.DiceNum
		result.DiceType = gs.DiceType
	} else if err != gorm.ErrRecordNotFound {
		model.Log.Infof("GetRoomInfo: GroupSetting: %s", err.Error())
	}

	result.WelcomeText, _, _, err = group_s.NewGroupService(myContext).GetWelcomeText(groupInfo)
	if err != nil {
		return myContext, err
	}
	result.WelcomeText = strings.ReplaceAll(result.WelcomeText, "@%s", "")

	// 补上房间流水勋章
	var pe *medal_cv.PicElement
	result.TotalConsume, pe, err = medal_cv.GetGroupConsumeMedal(model, groupId)
	if err != nil {
		model.Log.Infof("GetRoomInfo: GetGroupConsumeMedal: %s", err.Error())
	} else if pe != nil {
		result.GroupMedals = append(result.GroupMedals, medal_cv.PicElement{PicUrl: pe.PicUrl})
	}

	resMedals := []res_m.ResMedal{}
	err = mysql.Db.Model(&res_m.ResMedal{}).
		Joins("INNER JOIN group_medal m ON m.res_medal_id = res_medal.id").
		Where("m.im_group_id = ?", groupId).
chenweijian's avatar
chenweijian committed
561
		Where("(expire_at is null or expire_at > ?)", time.Now().Format(utils.DATETIME_FORMAT)).
chenweijian's avatar
chenweijian committed
562
		Order("res_medal.sort").
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
		Find(&resMedals).Error
	if err == nil {
		for _, r := range resMedals {
			result.GroupMedals = append(result.GroupMedals, medal_cv.PicElement{
				PicUrl:  r.PicUrl,
				SvgaUrl: r.SvgaUrl,
			})
		}
	} else {
		model.Log.Infof("GetRoomInfo: group_medal: %s", err.Error())
	}
	// 获取国家信息
	country, area, err := user_m.GetUserCountryArea(model, userId)
	if err != nil {
		model.Log.Errorf("GetRoomInfo 获取国家资源错误 userId:%d, err:%v", userId, err)
		return myContext, err
	}

	rb := mgr_m.RoomBanner{Status: mysql.YES}
	rows, err := rb.GetRoomBanners(model.Db, allow)
	if err == nil {
		for _, i := range rows {
			// 是否在banner设定区域内
			if (i.Area == 1 || i.Area == 2) && area != i.Area { // 不在区域内
				continue
			}
			if i.Area == 3 && !utils.IsInStringList(country, i.CountryList) {
				continue
			}

			result.Banners = append(result.Banners, group_cv.BannerElement{
				H5Url:     i.Url,
				BannerUrl: i.Image,
hujiebin's avatar
hujiebin committed
596
				ActionUrl: i.Url, // 支持direction=1横屏
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
			})
		}
	} else {
		model.Log.Infof("GetRoomInfo: GetRoomBanners: %s", err.Error())
	}
	result.LuckyWheel, err = group_cv.GetLuckWheelState(model, groupId)
	if err != nil {
		model.Log.Infof("GetRoomInfo: GetLuckWheelState: %s", err.Error())
	}
	// 群主的详情
	result.Owner, err = user_cv.GetUserDetail(model, groupInfo.Owner, userId)
	if err != nil {
		model.Log.Errorf("GetRoomInfo: GetUserBase: %s", err.Error())
	}

	resp.ResponseOk(c, result)
	return myContext, nil
}

// @Tags 群组
// @Summary 查询群管理角色(经理、管理)
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId path string true "群ID"
// @Success 200 {object} group_cv.MemberDetail
// @Router /v1/imGroup/admin/{groupId} [get]
func GetGroupAdmin(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)

	groupId := c.Param("groupId")
	if len(groupId) <= 0 {
		return myContext, myerr.NewSysError("groupId 为必填项")
	}

	myUserId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)

	groupId, err = group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	roles, orderList, err := group_m.GetRolesInGroup(model, groupId)
	if err != nil {
		return myContext, err
	}

	userIds := make([]uint64, 0)
	for _, i := range orderList {
		if roles[i] == group_e.GROUP_MANAGER || roles[i] == group_e.GROUP_ADMIN {
			userIds = append(userIds, i)
		}
	}

	result := make([]group_cv.MemberDetail, 0)
	if len(userIds) > 0 {
		users, err := user_cv.BatchGetUserExtend(model, userIds, myUserId)
		if err != nil {
			return myContext, err
		}

		for _, i := range userIds {
			result = append(result, group_cv.MemberDetail{
				CvUserExtend: users[i],
				Role:         roles[i],
			})
		}
	}

	resp.ResponseOk(c, result)
	return myContext, nil
}
hujiebin's avatar
hujiebin committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

// @Tags 群组
// @Summary 修改群消息设置(默认,静音,免打扰)(obsolete)
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId formData string true "群ID"
// @Param status formData string true "消息状态 0:默认 1:静音 2:免打扰"
// @Success 200
// @Router /v1/imGroup/user/msg/status [put]
func GroupUserMsg(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	status, err := strconv.ParseUint(c.PostForm("status"), 10, 64)
	if err != nil {
		return myContext, err
	}
	groupId := c.PostForm("groupId")
	if err := group_s.NewGroupService(myContext).GroupUserMsgStatus(userId, groupId, mysql.Type(status)); err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 举报
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId formData string true "群ID"
// @Param reasonType formData int false "投诉原因 1:语言骚扰(verbal harassment)2:Nudity 3:不良消息 4:不雅图片" Enums(1, 2, 3, 4)
// @Param imageUrl formData string false "证据图片url"
// @Param reason formData string false "投诉内容"
// @Success 200
// @Router /v1/imGroup/report [post]
func GroupReport(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	groupId := c.PostForm("groupId")
	if len(groupId) <= 0 {
		return myContext, bizerr.InvalidParameter
	}

	model := domain.CreateModelContext(myContext)
	groupId, err = group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	reasonTypeStr := c.PostForm("reasonType")
	var reasonType uint64 = 0
	if reasonTypeStr != "" {
		reasonType, err = strconv.ParseUint(reasonTypeStr, 10, 64)
		if err != nil {
			return myContext, myerr.WrapErr(err)
		}
	}
	imageUrl := c.PostForm("imageUrl")
	reason := c.PostForm("reason")
	if err = group_s.NewGroupService(myContext).ReportGroup(userId, groupId, mgr_e.ReportReasonType(reasonType), imageUrl, reason); err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, nil)
	return myContext, nil
}

type GroupBanner struct {
	//h5链接
	H5Url string `json:"h5Url"`
	//图片地址
	BannerUrl string `json:"bannerUrl"`
	//群主Id
	GroupId string `json:"groupId"`
hujiebin's avatar
hujiebin committed
756 757
	//统跳地址 http开头:web页面,query参数direction(0:竖屏 1:横屏); hilo开头:原生页面
	ActionUrl string `json:"actionUrl"`
hujiebin's avatar
hujiebin committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
}

// @Tags 群组
// @Summary banner列表
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Success 200 {object} GroupBanner
// @Router /v1/imGroup/banner/list [get]
func GroupBannerList(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	var groupBanners []GroupBanner
	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	mgrGroupBanners := []mgr_m.GroupBanner{}
	if err := mysql.Db.Model(&mgr_m.GroupBanner{}).Where(&mgr_m.GroupBanner{
		Status: mysql.YES,
	}).Order("n desc").Find(&mgrGroupBanners).Error; err != nil {
		return myContext, err
	}
	_deviceType, _ := c.Get(mycontext.DEVICETYPE)
	_appVersion, _ := c.Get(mycontext.APP_VERSION)
	model := domain.CreateModelNil()
	deviceType, appVersion := cast.ToString(_deviceType), cast.ToString(_appVersion)
	vc := version_m.GetVersionControlByPlatform(model, deviceType)
	allow, err := utils.CompareVersion(appVersion, fmt.Sprintf("<= %s", vc.AuditVersion))
	model.Log.Infof("CompareVersion appVersion:%v,auditVersion:%v,allow:%v,err:%v", appVersion, vc.AuditVersion, allow, err)

	// 获取国家信息
	country, area, err := user_m.GetUserCountryArea(model, userId)
	if err != nil {
		model.Log.Errorf("GroupBannerList 获取国家资源错误 userId:%d, err:%v", userId, err)
		return myContext, err
	}

	groupCodes := []string{}
	for i := 0; i < len(mgrGroupBanners); i++ {
		groupCodes = append(groupCodes, mgrGroupBanners[i].GroupCode)
	}

	groupMap := map[string]string{}

	if len(groupCodes) > 0 {
		var groupInfos []group_m.GroupInfo
		if err := mysql.Db.Model(&group_m.GroupInfo{}).Where("code in (?) ", groupCodes).Find(&groupInfos).Error; err != nil {
			return myContext, err
		}
		for i := 0; i < len(groupInfos); i++ {
			groupMap[groupInfos[i].Code] = groupInfos[i].TxGroupId
		}
	}

	nowUnix := time.Now().Unix()
	for _, v := range mgrGroupBanners {
		if !allow && v.Url == "https://h5.whoisamy.shop/action/hiloHtml/22_05_30_recharge/topup.html" {
			model.Log.Infof("CompareVersion appVersion:%v,auditVersion:%v,allow:%v,err:%v,skip top recharge", appVersion, vc.AuditVersion, allow, err)
			continue
		}
hujiebin's avatar
hujiebin committed
818 819
		// 版本控制
		if !allow && (v.ID == 2301 || v.ID == 4711 || v.ID == 2321 || v.ID == 5341 || v.ID == 5331 || v.ID == 5631 || v.ID == 5471 || v.ID == 5461 || v.ID == 5771 || v.ID == 5811) {
hujiebin's avatar
hujiebin committed
820 821 822 823 824 825 826 827 828 829 830 831 832 833
			model.Log.Infof("CompareVersion appVersion:%v,auditVersion:%v,allow:%v,err:%v,skip top recharge", appVersion, vc.AuditVersion, allow, err)
			continue
		}
		// 是否在上架时间内
		if (v.StartAt > 0 && v.EndAt > 0) && (nowUnix < v.StartAt || nowUnix > v.EndAt) {
			continue
		}
		// 是否在上架区域内
		if (v.Area == 1 || v.Area == 2) && area != v.Area { // 不在区域内
			continue
		}
		if v.Area == 3 && !utils.IsInStringList(country, v.CountryList) {
			continue
		}
hujiebin's avatar
hujiebin committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
		actionUrl := v.Url
		if uri, err := url.Parse(actionUrl); err == nil {
			query := uri.RawQuery
			queryArr := strings.Split(query, "&")
			v := url.Values{}
			if len(queryArr) > 0 {
				for _, q := range queryArr {
					if arr := strings.Split(q, "="); len(arr) == 2 {
						v.Add(arr[0], arr[1])
					}
				}
			}
			//v.Add("direction", "1") // 横屏的需要这样
			uri.RawQuery = v.Encode()
			actionUrl = uri.String()
		}
hujiebin's avatar
hujiebin committed
850 851 852 853
		groupBanners = append(groupBanners, GroupBanner{
			H5Url:     v.Url,
			BannerUrl: v.Image,
			GroupId:   groupMap[v.GroupCode],
hujiebin's avatar
hujiebin committed
854
			ActionUrl: actionUrl,
hujiebin's avatar
hujiebin committed
855 856 857 858 859 860 861 862 863
		})
	}
	resp.ResponseOk(c, groupBanners)
	return myContext, nil
}

type RoomBanner struct {
	H5Url     string `json:"h5Url"`     // h5链接
	BannerUrl string `json:"bannerUrl"` // 图片地址
hujiebin's avatar
hujiebin committed
864
	ActionUrl string `json:"actionUrl"` // 统跳地址 http开头:web页面,query参数direction(0:竖屏 1:横屏); hilo开头:原生页面
hujiebin's avatar
hujiebin committed
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
}

// @Tags 群组
// @Summary banner列表
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Success 200 {object} RoomBanner
// @Router /v1/imGroup/roomBanners [get]
func RoomBannerList(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	model := domain.CreateModelContext(myContext)

	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	// 版本控制
	_deviceType, _ := c.Get(mycontext.DEVICETYPE)
	_appVersion, _ := c.Get(mycontext.APP_VERSION)
	deviceType, appVersion := cast.ToString(_deviceType), cast.ToString(_appVersion)
	vc := version_m.GetVersionControlByPlatform(model, deviceType)
	allow, err := utils.CompareVersion(appVersion, fmt.Sprintf("<= %s", vc.AuditVersion))
	model.Log.Infof("CompareVersion appVersion:%v,auditVersion:%v,allow:%v,err:%v", appVersion, vc.AuditVersion, allow, err)

	result := make([]RoomBanner, 0)

	// 获取国家信息
	country, area, err := user_m.GetUserCountryArea(model, userId)
	if err != nil {
		model.Log.Errorf("GroupBannerList 获取国家资源错误 userId:%d, err:%v", userId, err)
		return myContext, err
	}

	rb := mgr_m.RoomBanner{Status: mysql.YES}
	rows, err := rb.GetRoomBanners(model.Db, allow)
	if err != nil {
		return myContext, err
	}

	for _, i := range rows {
		// 是否在banner设定区域内
		if (i.Area == 1 || i.Area == 2) && area != i.Area { // 不在区域内
			continue
		}
		if i.Area == 3 && !utils.IsInStringList(country, i.CountryList) {
			continue
		}

		result = append(result, RoomBanner{
			H5Url:     i.Url,
			BannerUrl: i.Image,
hujiebin's avatar
hujiebin committed
917
			ActionUrl: i.Url,
hujiebin's avatar
hujiebin committed
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
		})
	}
	resp.ResponseOk(c, result)
	return myContext, nil
}

// @Tags 群组
// @Summary 通知客户端banner列表更新
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Success 200
// @Router /v1/imGroup/roomBanners [put]
func NotifyRoomBannerListChange(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)

	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	if !user_m.IsSuperUser(userId) {
		return myContext, bizerr.NoPrivileges
	}

	if err = rpc.SendConfigChange(userId, rpc.RoomBannerChange); err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 获取房间内的在线用户
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId query string true "群ID"
// @Success 200 {object} OnlineUserResult
// @Router /v1/imGroup/online/users [get]
func GroupInUsers(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupId := c.Query("groupId")
	if len(groupId) <= 0 {
		return myContext, bizerr.InvalidParameter
	}

	model := domain.CreateModelContext(myContext)
	groupId, err := group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	myContext.Log.WithField("groupId", groupId)
	userIds, err := group_m.RoomLivingExistsUserId(groupId)
	if err != nil {
		return myContext, err
	}
	myContext.Log.Infof("GroupInUsers userIds:%v", userIds)
	cacheDate, err := redisCli.GetRedis().Get(context.Background(), redis_key.GetGroupOnlineUser(groupId)).Result()
	if err != nil {
		//myContext.Log.Errorf("GroupInUsers GetRedis Get err:%v, groupId:%v", err, groupId)
		//查找数据库
		return GroupInUsersDb(c, myContext, userIds, groupId)
	} else if cacheDate == "" {
		//查找数据库
		return GroupInUsersDb(c, myContext, userIds, groupId)
	} else {
		onlineUserResult := OnlineUserResult{}
		if err := json.Unmarshal([]byte(cacheDate), &onlineUserResult); err != nil {
			myContext.Log.Errorf("GroupInUsers Unmarshal err:%v", err)
			//查找数据库
			return GroupInUsersDb(c, myContext, userIds, groupId)
		}
		//对比数量
		if len(userIds) != len(onlineUserResult.RedisUserIds) {
			myContext.Log.Infof("GroupInUsers cache groupId:%v size:%v RedisUserIds size:%v", groupId, len(userIds), len(onlineUserResult.RedisUserIds))
			//查找数据库
			return GroupInUsersDb(c, myContext, userIds, groupId)
		} else {
			//对比顺序
			for i, _ := range userIds {
				//出现不一致
				if userIds[i] != onlineUserResult.RedisUserIds[i] {
					myContext.Log.Infof("GroupInUsers cache groupId:%v RedisUserIds userId no same", groupId)
					return GroupInUsersDb(c, myContext, userIds, groupId)
				}
			}
			myContext.Log.Infof("GroupInUsers cache groupId:%v hit", groupId)
			resp.ResponseOk(c, onlineUserResult)
			return myContext, nil
		}

	}

}

type OnlineUserResult struct {
	Total       int                   `json:"total"`
	Users       []*user_cv.CvUserBase `json:"users"`
	ServiceTime int64                 `json:"serviceTime"`
	//利用这个来判断是否要更新缓存
	RedisUserIds []uint64 `json:"redisUserIds"`
}

//显示的最大数量是15个。
func GroupInUsersDb(c *gin.Context, myContext *mycontext.MyContext, userIds []uint64, groupId string) (*mycontext.MyContext, error) {
	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}
	if len(userIds) == 0 {
		if err != nil {
			return myContext, err
		}
		resp.ResponseOk(c, OnlineUserResult{
			Total: len(userIds),
			Users: nil,
		})
		return myContext, nil
	}

	//贵族
	nobleLevelMap, err := noble_m.BatchGetNobleLevel(mysql.Db, userIds)
	if err != nil {
		return myContext, err
	}

	vipMap, err := user_m.BatchGetVips(userIds)
	if err != nil {
		return myContext, err
	}
	//
	var maxNum int = 15
	vipList := make([]uint64, 0, maxNum)
	userList := make([]uint64, 0, maxNum)
	//认为需求中,不会再增加贵族6,贵族7了
	nobleLevel5 := make([]uint64, 0, maxNum)
	nobleLevel4 := make([]uint64, 0, maxNum)
	nobleLevel3 := make([]uint64, 0, maxNum)
	nobleLevel2 := make([]uint64, 0, maxNum)
	nobleLevel1 := make([]uint64, 0, maxNum)
	//
	for i, userId := range userIds {
		if len(vipList)+len(userList) >= maxNum {
			break
		}
		if level, flag := nobleLevelMap[userId]; flag {
			if level == 5 {
				nobleLevel5 = append(nobleLevel5, userId)
			} else if level == 4 {
				nobleLevel4 = append(nobleLevel4, userId)
			} else if level == 3 {
				nobleLevel3 = append(nobleLevel3, userId)
			} else if level == 2 {
				nobleLevel2 = append(nobleLevel2, userId)
			} else if level == 1 {
				nobleLevel1 = append(nobleLevel1, userId)
			} else if level == 0 {
				if vipMap[userIds[i]] != nil {
					vipList = append(vipList, userIds[i])
				} else {
					userList = append(userList, userIds[i])
				}
			}
		}
	}
	myContext.Log.Infof("GroupInUsers vip and noble ready")

	//进入房间时间排序
	roomUsers := []uint64{}
	if err := mysql.Db.Model(&group_m.GroupUser{}).Select("user_id").Where(&group_m.GroupUser{
		GroupId: groupId,
	}).Where("user_id IN (?)", userList).Order("in_room_time DESC").Find(&roomUsers).Error; err != nil {
		return myContext, err
	}
	//转换成map
	roomUserMap := map[uint64]struct{}{}
	for i, _ := range roomUsers {
		roomUserMap[roomUsers[i]] = struct{}{}
	}

	//
	resultList := make([]uint64, 0, maxNum)
	resultList = append(resultList, nobleLevel5...)
	resultList = append(resultList, nobleLevel4...)
	resultList = append(resultList, nobleLevel3...)
	resultList = append(resultList, nobleLevel2...)
	resultList = append(resultList, nobleLevel1...)
	resultList = append(resultList, vipList...)
	resultList = append(resultList, roomUsers...)
	//预防groupRoom中没有数据的,毕竟进入房间的时候,异常没有处理
	for i, _ := range userList {
		if _, flag := roomUserMap[userList[i]]; !flag {
			resultList = append(resultList, userList[i])
		}
	}
	myContext.Log.Infof("GroupInUsers resultList ready")

	userBaseMap, err := user_cv.GetUserBaseMap(resultList, userId)
	if err != nil {
		return myContext, err
	}

	users := make([]*user_cv.CvUserBase, 0, maxNum)
	for i, _ := range resultList {
		if u, flag := userBaseMap[resultList[i]]; flag {
			users = append(users, u)
		}
	}
	onlineUserResult := OnlineUserResult{
		Total:        len(userIds),
		Users:        users,
		ServiceTime:  time.Now().Unix(),
		RedisUserIds: userIds,
	}
	resp.ResponseOk(c, onlineUserResult)

	if bytes, err := json.Marshal(onlineUserResult); err != nil {
		myContext.Log.Errorf("GroupInUsers Marshal err:%v", err)
	} else {
		if _, err := redisCli.GetRedis().Set(context.Background(), redis_key.GetGroupOnlineUser(groupId), string(bytes), 5*time.Minute).Result(); err != nil {
			myContext.Log.Infof("GroupInUsers cache Set groupId:%v, err:%v", groupId, err)
		} else {
			myContext.Log.Infof("GroupInUsers cache success groupId:%v", groupId)
		}
	}
	return myContext, nil
}

// @Tags 群组
// @Summary 获取房间内的在线用户
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId query string true "群ID"
// @Success 200 {object} group_m.RoomOnlineUser
// @Router /v1/imGroup/online/users/new [get]
func GroupInUserNew(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupId := c.Query("groupId")
	if len(groupId) <= 0 {
		return myContext, bizerr.InvalidParameter
	}

	model := domain.CreateModelContext(myContext)
	groupId, err := group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	roomOnlineUser, err := group_m.GetRoomOnlineUser(myContext, groupId)
	//roomOnlineUser, err := cv.GetGroupInUser(myContext, groupId)
	if err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, roomOnlineUser)
	return myContext, nil
}

// @Tags 群组
// @Summary 获取勋章
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId query string true "群ID"
// @Success 200 {object} ResultGroupTheme
// @Router /v1/imGroup/medal/all [get]
func GroupMedalAll(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	groupId := c.Query("groupId")
	if len(groupId) <= 0 {
		return myContext, bizerr.InvalidParameter
	}

	model := domain.CreateModelContext(myContext)
	groupId, err := group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	resMedals := []res_m.ResMedal{}
chenweijian's avatar
chenweijian committed
1197 1198 1199
	if err := mysql.Db.Model(&res_m.ResMedal{}).Joins("inner join group_medal m on m.res_medal_id = res_medal.id").
		Where("m.im_group_id = ?", groupId).
		Where("(expire_at is null or expire_at > ?)", time.Now().Format(utils.DATETIME_FORMAT)).
chenweijian's avatar
chenweijian committed
1200
		Order("res_medal.sort").Find(&resMedals).Error; err != nil {
hujiebin's avatar
hujiebin committed
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
		return myContext, err
	}

	returnGroupMedals := make([]medal_cv.ReturnGroupMedal, 0, len(resMedals)+1)

	// 补上房间流水勋章
	var pe *medal_cv.PicElement
	_, pe, err = medal_cv.GetGroupConsumeMedal(model, groupId)
	if err != nil {
		model.Log.Infof("GetGroupConsumeMedal: %s", err.Error())
	} else if pe != nil {
		returnGroupMedals = append(returnGroupMedals, medal_cv.ReturnGroupMedal{PicUrl: pe.PicUrl})
	}

	for _, r := range resMedals {
		returnGroupMedals = append(returnGroupMedals, medal_cv.ReturnGroupMedal{
			PicUrl:  r.PicUrl,
			SvgaUrl: r.SvgaUrl,
		})
	}

	resp.ResponseOk(c, returnGroupMedals)
	return myContext, nil
} // @Tags 群组

// @Tags 群组
// @Summary 查询房间勋章
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId query string false "群ID"
hujiebin's avatar
hujiebin committed
1232
// @Success 200 {object} group_cv.RoomMedalInfo
hujiebin's avatar
hujiebin committed
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
// @Router /v1/imGroup/medal/room [get]
func GetRoomMedal(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)

	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	groupId := c.Query("groupId")
	result := make([]group_cv.RoomMedalInfo, 0)

	model := domain.CreateModelContext(myContext)

	var consumeTotal uint64
	if len(groupId) > 0 {
		groupId, err = group_m.ToImGroupId(model, groupId)
		if err != nil {
			return myContext, bizerr.GroupNotFound
		}
	} else {
		groups, err := group_m.FindGroupByOwner(model, userId)
		if err != nil {
			return myContext, err
		}
		if len(groups) > 0 {
			groupId = groups[0].ImGroupId
		}
	}

	if len(groupId) > 0 {
		consumeTotal, err = gift_cv.GetGroupConsumeTotal(model, groupId)
		if err != nil {
			return myContext, err
		}
	}
	model.Log.Infof("Room %s totoal consume = %d", groupId, consumeTotal)

	rec, err := res_m.GetRoomMedalConfig(model.Db)
	if err != nil {
		return myContext, err
	}

	ui, err := user_m.GetUser(model, userId)
	if err != nil {
		return myContext, err
	}
	lang := utils.DEFAULT_LANG
	if len(ui.Language) > 0 {
		lang = ui.Language
	}
	template, err := res_m.GetResMultiTextBy(model.Db, msg_e.MSG_ID_ROOM_MEADAL, lang)
	if err != nil {
		template = nil
	}

	for _, i := range rec {
		previewUrl := i.InactiveUrl
		if consumeTotal >= i.Threshold {
			previewUrl = i.ActiveUrl
		}

		desc := i.Desc
		if template != nil {
			num := strconv.Itoa(int(i.Threshold)/1000000) + "M"
			desc = fmt.Sprintf(template.Content, num)
		}

		result = append(result, group_cv.RoomMedalInfo{
			Level:      i.Level,
			PreviewUrl: previewUrl,
			PicUrl:     i.ActiveUrl,
			Desc:       desc,
		})
	}

	resp.ResponseOk(c, result)
	return myContext, nil
}