group.go 13.4 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 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 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
package group_cv

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/utils"
	"hilo-group/_const/enum/game_e"
	"hilo-group/_const/enum/group_e"
	"hilo-group/_const/enum/online_e"
	"hilo-group/cv/billboard_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/res_m"
	"hilo-group/domain/model/rocket_m"
	"hilo-group/domain/service/group_s"
	"sort"
)

type GroupBasicInfo struct {
	Id            int64  `json:"id,omitempty"`
	GroupId       string `json:"groupId"`
	Name          string `json:"name"`
	Introduction  string `json:"introduction"`
	Notification  string `json:"notification"`
	FaceUrl       string `json:"faceUrl"`
	Owner_Account string `json:"ownerAccount"`
	CreateTime    int    `json:"createTime"`
	//LastInfoTime    int    `json:"last_info_time"`
	LastMsgTime     int     `json:"lastMsgTime"`
	NextMsgSeq      uint    `json:"nextMsgSeq"`
	MemberNum       uint    `json:"memberNum"`
	MaxMemberNum    uint    `json:"maxMemberNum"`
	ShutUpAllMember string  `json:"shutUpAllMember"`
	Code            string  `json:"code"`
	Password        *string `json:"password"`
	CountryIcon     string  `json:"countryIcon"`
	SupportLevel    uint8   `json:"supportLevel"` // 本周最高的扶持等级
	//1:5人 2:10人
	MicNumType int `json:"micNumType"`
	// N天内的进入用户数量(重复进入去重,实时)
	GroupInUserDuration int64                 `json:"groupInUserDuration"`
	GroupMedals         []medal_cv.PicElement `json:"groupMedals"`    // 群勋章
	TouristMic          uint8                 `json:"touristMic"`     // 游客是否能上麦
	TouristSendMsg      uint8                 `json:"touristSendMsg"` // 游客是否能发言
	TouristSendPic      uint8                 `json:"touristSendPic"` // 游客是否能发图片
	MemberFee           uint64                `json:"memberFee"`      // 加入群组会员需要的黄钻
}

type MemberListInfo struct {
	Member_Account string `json:"memberAccount"`
	JoinTime       int    `json:"joinTime"`
	//MsgSeq          uint   `json:"msg_seq"`
	LastSendMsgTime int64 `json:"lastSendMsgTime"`
	//ShutUpUntil     int    `json:"shut_up_until"`
	// AppMemberDefinedData": [ // 群成员自定义字段 【暂时不用】
}

type GroupInfo struct {
	GroupBasicInfo

	// hilo业务
	EntryLevel     uint32 `json:"entryLevel"`     // obsolete
	HasOnMic       bool   `json:"hasOnMic"`       // 房间麦上是否有人
	GroupPowerId   uint64 `json:"groupPowerId"`   // 群主所在的势力ID
	GroupPowerName string `json:"groupPowerName"` // 群主所在的势力的名称

	// "AppDefinedData": 群组维度的自定义字段 【暂时不用】
	MemberList []MemberListInfo
}

type PopularGroupInfo struct {
	GroupInfo
	MicUsers      []user_cv.CvUserTiny `json:"micUsers"`            // 在麦上的用户,最多4个
	RoomUserCount uint                 `json:"roomUserCount"`       // 当前在房间的用户数
	MaxStage      *uint16              `json:"maxStage"`            // 今天爆过的最大的火箭
	GameTypes     []game_e.GameType    `json:"gameTypes,omitempty"` // 房间内正在进行的游戏 1:ludo 2:uno
}

// 最新的群组列表, 字段暂时应该跟popular一样
type LatestGroupInfo PopularGroupInfo

type TheirGroupsInfo struct {
	Total     uint              `json:"total"`
	OwnGroups []JoinedGroupInfo `json:"ownGroups"`
	Groups    []JoinedGroupInfo `json:"groups"`
}

type JoinedGroupInfo struct {
	PopularGroupInfo
	LastEnterTime int64 `json:"lastEnterTime"` // 最后一次进房间的时间
}

type JoinedGroupRsp struct {
	Total  uint              `json:"total"`
	Groups []JoinedGroupInfo `json:"groups"`
}

type RoleMemberInfo struct {
	user_cv.CvUserBase
	Role         group_e.GroupRoleType     `json:"role"`
	OnlineStatus online_e.OnlineStatusType `json:"onlineStatus"`
}

type MemberDetail struct {
	user_cv.CvUserExtend
	Role         group_e.GroupRoleType     `json:"role"`
	OnlineStatus online_e.OnlineStatusType `json:"onlineStatus"` // IM在线状态
	InRoom       bool                      `json:"inRoom"`       // 是否在房间内
}

type GroupDetail struct {
	GroupBasicInfo

	// hilo业务
	EntryLevel  uint32 `json:"entryLevel"` // obsolete
	MicOn       bool   `json:"micOn"`
	LoadHistory bool   `json:"loadHistory"`
	ThemeId     uint64 `json:"themeId"`
	ThemeUrl    string `json:"themeUrl"`
	// 1:官方 2:自定义
	ThemeType    uint8                            `json:"themeType"`
	Role         group_e.GroupRoleType            `json:"role"`
	MsgStatus    uint8                            `json:"msgStatus"`    // 消息提醒状态
	WelcomeText  string                           `json:"welcomeText"`  // 新成员入群欢迎语
	TotalConsume uint64                           `json:"totalConsume"` // 群内消费总额
	TopConsumers []billboard_cv.BillboardUserInfo `json:"topConsumers"` // 月最高消费三甲
	DiceNum      uint16                           `json:"diceNum"`      // 骰子游戏的数量
	DiceType     uint16                           `json:"diceType"`     // 骰子游戏的数字点数
	// "AppDefinedData": 群组维度的自定义字段 【暂时不用】
	RoleMembers []RoleMemberInfo      `json:"role_members"`
	Owner       *user_cv.CvUserDetail `json:"owner"` // 群主信息
}

type SimpleRoleInfo struct {
	ExternalId string                `json:"externalId"`
	Role       group_e.GroupRoleType `json:"role"`
}

type BannerElement struct {
	H5Url     string `json:"h5Url"`     // h5链接
	BannerUrl string `json:"bannerUrl"` // 图片地址
}

type RoomInfo struct {
	GroupBasicInfo

	// hilo业务
	MicOn       bool   `json:"micOn"`
	LoadHistory bool   `json:"loadHistory"`
	ThemeId     uint64 `json:"themeId"`
	ThemeUrl    string `json:"themeUrl"`
	// 1:官方 2:自定义
	ThemeType    uint8                 `json:"themeType"`
	Role         group_e.GroupRoleType `json:"role"`
	DiceNum      uint16                `json:"diceNum"`  // 骰子游戏的数量
	DiceType     uint16                `json:"diceType"` // 骰子游戏类型
	RoleMembers  []SimpleRoleInfo      `json:"roleMembers"`
	WelcomeText  string                `json:"welcomeText"` // 新成员入群欢迎语
	Banners      []BannerElement       `json:"banners"`
	LuckyWheel   LuckyWheelState       `json:"luckyWheel"`
	TotalConsume uint64                `json:"totalConsume"`
	GameConfig   *game_m.GameConfig    `json:"gameConfig"`
	Owner        *user_cv.CvUserDetail `json:"owner"`
}

type SupportPageDetail struct {
	GroupId             string               `json:"groupId"`
	CurrentConsume      uint64               `json:"currentConsume"`      // 当前周期的消费额(钻石)
	CurrentCount        uint32               `json:"currentCount"`        // 当前周期的消费人数
	LastConsume         uint64               `json:"lastConsume"`         // 上一个周期的消费额(钻石)
	LastCount           uint32               `json:"lastCount"`           // 上一个周期的消费人数
	SupportLevel        string               `json:"supportLevel"`        // 上一个周期(已结算)的支持等级
	SupporterLimit      uint32               `json:"supporterLimit"`      // 上一个周期(已结算)的支持者的上限
	CurrentSupportLevel string               `json:"currentSupportLevel"` // 当前的支持等级
	RemainSecond        int64                `json:"remainSecond"`        // 离下一次结算还有多少秒
	ProfitAllocator     user_cv.CvUserBase   `json:"profitAllocator"`     // 利益分配人,群主或由运营指定(官方群)
	Supporters          []user_cv.CvUserBase `json:"supporters"`          // 扶持管理员
	IsDispatched        bool                 `json:"isDispatched"`        // 奖励是否已经分配
}

type AwardResult struct {
	Success []string `json:"success"`
	Failed  []string `json:"failed"`
}

type GroupPowerTitle struct {
	Id             uint64               `json:"id"`
	Name           string               `json:"name"`
	GroupId        string               `json:"groupId"` // 势力的主群
	Avatar         string               `json:"avatar"`
	MemberNum      uint                 `json:"memberNum"`      // 成员人数
	Owner          user_cv.CvUserBase   `json:"owner"`          // 势力主
	Assistants     []user_cv.CvUserBase `json:"assistants"`     // 势力助手,目前就是主群的管理
	IsMyGroupPower bool                 `json:"isMyGroupPower"` // “我”是否属于这个势力
}

type RoomMedalInfo struct {
	Level      uint16 `json:"level"`
	PreviewUrl string `json:"previewUrl"`
	PicUrl     string `json:"picUrl"`
	Desc       string `json:"desc"`
}

func BuildJoinedGroupInfo(myService *domain.Service, myUserId uint64, groupIds []string, pageSize, pageIndex int) ([]JoinedGroupInfo, int, error) {
	model := domain.CreateModel(myService.CtxAndDb)

	groupInfo, err := group_m.BatchGetGroupInfo(model, groupIds)
	if err != nil {
		return nil, 0, err
	}

	// todo: 可以移到后面,减小查询范围,因为roomMicUserMap不影响排序
	roomMicUserMap, err := group_m.BatchGetAllMicUser(model, groupIds)
	if err != nil {
		return nil, 0, err
	}
	uids := make([]uint64, 0)
	micUsersMap := make(map[string][]uint64, 0)
	for _, i := range groupInfo {
		micUsersMap[i.ImGroupId] = make([]uint64, 0)

		if len(i.Password) <= 0 {
			// 密码群不显示麦上头像
			u := roomMicUserMap[i.ImGroupId]
			if len(u) >= 4 {
				micUsersMap[i.ImGroupId] = u[0:4]
			} else if len(u) > 0 {
				micUsersMap[i.ImGroupId] = u
			}
			uids = append(uids, micUsersMap[i.ImGroupId]...)
		}
	}

	uids = utils.UniqueSliceUInt64(uids)
	userTiny, err := user_cv.GetUserTinyMap(uids)
	if err != nil {
		return nil, 0, err
	}
	roomCount, err := group_m.BatchGetRoomCount(model, groupIds)
	if err != nil {
		return nil, 0, err
	}

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

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

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

	roomEnterTime, err := room_c.GetUserRoomVisit(myUserId)
	if err != nil {
		return nil, 0, err
	}
	model.Log.Infof("BuildJoinedGroupInfo, roomEnterTime: %v", roomEnterTime)

	// 排序优先级V8.0版本
	sort.Slice(groupIds, func(i, j int) bool {
		gi := groupIds[i]
		gj := groupIds[j]
		return roomEnterTime[gi] > roomEnterTime[gj] ||
			roomEnterTime[gi] == roomEnterTime[gj] && visitCount[gj] > visitCount[gj] ||
			roomEnterTime[gi] == roomEnterTime[gj] && visitCount[gj] == visitCount[gj] && i >= j
	})

	result := make([]JoinedGroupInfo, 0)
	beginPos := pageSize * (pageIndex - 1)
	endPos := pageSize * pageIndex
	if beginPos < len(groupIds) {
		if endPos > len(groupIds) {
			endPos = len(groupIds)
		}

		groupIds = groupIds[beginPos:endPos]
		owners := make([]uint64, 0)
		for _, i := range groupIds {
			owners = append(owners, groupInfo[i].Owner)
		}
		powerIds, powerNames, err := group_power_cv.BatchGetGroupPower(model.Db, owners)
		if err != nil {
			return nil, 0, err
		}
		groupMedals, err := group_m.BatchGetMedals(model.Db, groupIds)
		if err != nil {
			return nil, 0, err
		}
		resMedal, err := res_m.MedalGetAllMap(model.Db)
		if err != nil {
			return nil, 0, err
		}

		rr := rocket_m.RocketResult{}
		maxStageMap, err := rr.GetMaxStage(model.Db, groupIds)
		if err != nil {
			return nil, 0, err
		}
		// 正在进行的游戏
		games := game_m.GetNotEndGamesMap(model)

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

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

			medals := make([]medal_cv.PicElement, 0)
			if m, ok := groupMedals[i]; 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)
			if err != nil {
				model.Log.Infof("BuildJoinedGroupInfo: GetGroupConsumeMedal: %s", err.Error())
			} else if pe != nil {
				medals = append(medals, medal_cv.PicElement{PicUrl: pe.PicUrl})
			}

			var password *string = nil
			if len(g.Password) > 0 && g.Owner != myUserId {
				emptyStr := ""
				password = &emptyStr
			}

			result = append(result, JoinedGroupInfo{
				PopularGroupInfo: PopularGroupInfo{
					GroupInfo: GroupInfo{
						GroupBasicInfo: GroupBasicInfo{
							GroupId:             g.TxGroupId,
							Name:                g.Name,
							Notification:        g.Notification,
							Introduction:        g.Introduction,
							FaceUrl:             g.FaceUrl,
							Code:                g.Code,
							CountryIcon:         countryInfo[g.Country],
							Password:            password,
							SupportLevel:        supportLevels[i],
							GroupInUserDuration: visitCount[i],
							MicNumType:          int(g.MicNumType),
							GroupMedals:         medals,
						},
						HasOnMic:       len(micUsers) > 0,
						GroupPowerId:   powerIds[g.Owner],
						GroupPowerName: powerNames[g.Owner],
					},
					MicUsers:      micUsers,
					RoomUserCount: uint(roomCount[i]),
					MaxStage:      maxStage,
					GameTypes:     games[g.TxGroupId],
				},
				LastEnterTime: roomEnterTime[i],
			})
		}
	}
	return result, len(groupInfo), nil
}