group_support.go 12 KB
Newer Older
1 2 3
package group_s

import (
chenweijian's avatar
chenweijian committed
4 5 6
	"context"
	"git.hilo.cn/hilo-common/_const/enum/msg_e"
	"git.hilo.cn/hilo-common/_const/rediskey"
7
	"git.hilo.cn/hilo-common/domain"
chenweijian's avatar
chenweijian committed
8 9 10
	"git.hilo.cn/hilo-common/resource/mysql"
	"git.hilo.cn/hilo-common/resource/redisCli"
	"github.com/go-redis/redis/v8"
11 12 13 14
	"hilo-group/_const/enum/gift_e"
	"hilo-group/domain/event/group_ev"
	"hilo-group/domain/model/gift_m"
	"hilo-group/domain/model/group_m"
chenweijian's avatar
chenweijian committed
15
	"hilo-group/domain/model/msg_m"
16 17
	"hilo-group/domain/model/res_m"
	"hilo-group/domain/model/user_m"
chenweijian's avatar
chenweijian committed
18
	"strconv"
19 20 21 22
	"time"
)

// 群组支持名单过滤
chenweijian's avatar
chenweijian committed
23
func (s *GroupService) GroupSupportList(groupId string, uids []uint64, supportLevel uint32) ([]uint64, []uint64, error) {
24 25 26 27 28 29 30
	if len(uids) <= 0 {
		return uids, nil, nil
	}

	result := make([]uint64, 0)
	out := make([]uint64, 0)

chenweijian's avatar
chenweijian committed
31
	model := domain.CreateModel(s.svc.CtxAndDb)
32

chenweijian's avatar
chenweijian committed
33 34 35 36 37 38 39 40 41 42 43 44 45
	// 1. 去掉非群管理者
	roles, _, err := group_m.GetRolesInGroup(model, groupId)
	if err != nil {
		model.Log.Errorf("GroupSupportList groupId:%v, uids:%v, err:%v", groupId, uids, err)
		return nil, nil, err
	}
	userIds := make([]uint64, 0)
	for _, i := range uids {
		if _, ok := roles[i]; ok {
			userIds = append(userIds, i)
		} else {
			out = append(out, i)
			model.Log.Infof("GroupSupportList: rule out %d, no role", i)
46
		}
chenweijian's avatar
chenweijian committed
47
	}
48

chenweijian's avatar
chenweijian committed
49 50 51 52 53 54 55 56 57 58 59 60 61
	// TODO: 去掉非群成员
	//(4)1个账户只能做1个群组的管理员(5)1个设备下只允许领取1个管理奖励
	_, _, period := group_m.GetLastSupportPeriod(time.Now())
	gsa := group_m.GroupSupportAwardMgr{Period: period}
	rows, err := gsa.Get(model.Db)
	if err != nil {
		model.Log.Errorf("GroupSupportList groupId:%v, uids:%v, err:%v", groupId, uids, err)
		return nil, nil, err
	}
	awards := make(map[uint64]struct{}, 0)
	for _, i := range rows {
		awards[i.UserId] = struct{}{}
	}
62

chenweijian's avatar
chenweijian committed
63 64 65 66 67
	uids = userIds
	userIds = make([]uint64, 0)
	m := make(map[uint64]uint64)
	for _, u := range uids {
		m, err := user_m.GetSameImeiMap(model, u)
68
		if err != nil {
chenweijian's avatar
chenweijian committed
69 70
			model.Log.Errorf("GroupSupportList groupId:%v, uids:%v, err:%v", groupId, uids, err)
			return nil, nil, err
71 72
		}

chenweijian's avatar
chenweijian committed
73 74 75 76 77 78 79 80 81
		passed := true
		for _, i := range m {
			if _, ok := awards[i]; ok {
				if i == u {
					passed = false
					model.Log.Infof("GroupSupportList: rule out %d, already awarded", i)
				} else {
					passed = false
					model.Log.Infof("GroupSupportList: rule out %d, imei awarded", i)
82 83 84
				}
			}
		}
chenweijian's avatar
chenweijian committed
85 86 87 88
		if passed == true {
			userIds = append(userIds, u)
		} else {
			out = append(out, u)
89
		}
chenweijian's avatar
chenweijian committed
90 91
	}
	model.Log.Infof("GroupSupportList: uids %v, map %v", uids, m)
92

chenweijian's avatar
chenweijian committed
93 94 95 96
	if uint32(len(userIds)) > supportLevel {
		model.Log.Infof("GroupSupportList: rule out %v, limit exeeded", userIds[supportLevel:])
		out = append(out, userIds[supportLevel:]...)
		userIds = userIds[0:supportLevel]
97
	}
chenweijian's avatar
chenweijian committed
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
	result = userIds

	return result, out, nil

	//err := s.svc.Transactional(func() error {
	//	model := domain.CreateModel(s.svc.CtxAndDb)
	//
	//	// 1. 去掉非群管理者
	//	roles, _, err := group_m.GetRolesInGroup(model, groupId)
	//	if err != nil {
	//		return err
	//	}
	//	userIds := make([]uint64, 0)
	//	for _, i := range uids {
	//		if _, ok := roles[i]; ok {
	//			userIds = append(userIds, i)
	//		} else {
	//			out = append(out, i)
	//			model.Log.Infof("GroupSupportList: rule out %d, no role", i)
	//		}
	//	}
	//
	//	// TODO: 去掉非群成员
	//
	//	//(4)1个账户只能做1个群组的管理员(5)1个设备下只允许领取1个管理奖励
	//	_, _, period := group_m.GetLastSupportPeriod(time.Now())
	//	gsa := group_m.GroupSupportAwardMgr{Period: period}
	//	rows, err := gsa.Get(model.Db)
	//	if err != nil {
	//		return err
	//	}
	//	awards := make(map[uint64]struct{}, 0)
	//	for _, i := range rows {
	//		awards[i.UserId] = struct{}{}
	//	}
	//
	//	uids = userIds
	//	userIds = make([]uint64, 0)
	//	m := make(map[uint64]uint64)
	//	for _, u := range uids {
	//		m, err := user_m.GetSameImeiMap(model, u)
	//		if err != nil {
	//			return err
	//		}
	//
	//		passed := true
	//		for _, i := range m {
	//			if _, ok := awards[i]; ok {
	//				if i == u {
	//					passed = false
	//					model.Log.Infof("GroupSupportList: rule out %d, already awarded", i)
	//				} else {
	//					passed = false
	//					model.Log.Infof("GroupSupportList: rule out %d, imei awarded", i)
	//				}
	//			}
	//		}
	//		if passed == true {
	//			userIds = append(userIds, u)
	//		} else {
	//			out = append(out, u)
	//		}
	//	}
	//	model.Log.Infof("GroupSupportList: uids %v, map %v", uids, m)
	//
	//	_, supportLevel, err := s.GetSupportLevel(groupId)
	//	if err != nil {
	//		return err
	//	}
	//
	//	if uint32(len(userIds)) > supportLevel {
	//		model.Log.Infof("GroupSupportList: rule out %v, limit exeeded", userIds[supportLevel:])
	//		out = append(out, userIds[supportLevel:]...)
	//		userIds = userIds[0:supportLevel]
	//	}
	//	result = userIds
	//	return nil
	//})
	//
	//if err == nil {
	//	return result, out, nil
	//} else {
	//	return nil, nil, err
	//}
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
}

func (s *GroupService) GetSupportLevel(groupId string) (uint64, uint32, error) {
	model := domain.CreateModel(s.svc.CtxAndDb)

	beginTime, endTime, _ := group_m.GetLastSupportPeriod(time.Now())

	g := gift_m.GiftOperate{SceneType: gift_e.GroupSceneType, SceneUid: groupId, Model: model}
	count, consume, err := g.GetConsumeByRange(beginTime, endTime)
	if err != nil {
		return 0, 0, err
	}

	rec, err := res_m.GetResGroupSupportBy(model, count, consume)
	if err != nil {
		return 0, 0, err
	}
	if rec != nil {
		return rec.ID, rec.MgrNum, nil
	}
	return 0, 0, nil
}

chenweijian's avatar
chenweijian committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
func (s *GroupService) GetSupportLevelByRedis(groupId string) (*res_m.ResGroupSupport, uint32, error) {
	model := domain.CreateModel(s.svc.CtxAndDb)

	_, _, period := group_m.GetLastSupportPeriod(time.Now())
	consume, count, err := GetGroupConsumeCount(model, groupId, period)
	if err != nil {
		return nil, 0, err
	}

	rec, err := res_m.GetResGroupSupportBy(model, count, consume)
	if err != nil {
		return nil, 0, err
	}
	if rec != nil {
		return rec, rec.MgrNum, nil
	}
	return rec, 0, nil
}

func GetGroupConsumeCount(model *domain.Model, imGroupId, period string) (uint64, uint32, error) {
	// 流水
	keyDiamond := rediskey.GetGroupSupportConsumeSummary(period)
	consume, err := model.RedisCluster.ZScore(context.Background(), keyDiamond, imGroupId).Result()
chenweijian's avatar
chenweijian committed
228
	if err != nil && err != redis.Nil {
chenweijian's avatar
chenweijian committed
229 230 231 232 233 234
		model.Log.Errorf("GetSupportLevelByRedis key:%v, groupId:%v, err:%v", keyDiamond, imGroupId, err)
		return 0, 0, err
	}
	// 支持者数量
	keySupportNum := rediskey.GetGroupSupportCountSupporter(period, imGroupId)
	count, err := model.RedisCluster.SCard(context.Background(), keySupportNum).Result()
chenweijian's avatar
chenweijian committed
235
	if err != nil && err != redis.Nil {
chenweijian's avatar
chenweijian committed
236 237 238 239 240 241
		model.Log.Errorf("GetSupportLevelByRedis key:%v, groupId:%v, err:%v", keySupportNum, imGroupId, err)
		return 0, 0, err
	}
	return uint64(consume), uint32(count), nil
}

242
//群组支持奖励
chenweijian's avatar
chenweijian committed
243 244
func (s *GroupService) GroupSupportAward(groupId string, profitAllocator uint64, userIds []uint64, resSupport *res_m.ResGroupSupport,
	period string, groupInfo *group_m.GroupInfo) error {
245 246 247
	return s.svc.Transactional(func() error {
		model := domain.CreateModel(s.svc.CtxAndDb)
		//发放奖励
chenweijian's avatar
chenweijian committed
248
		groupSupportAwardAdmin, groupSupportAwardMgrs, err := group_m.AddGroupSupportAward(model, groupId, profitAllocator, resSupport, userIds, period)
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
		if err != nil {
			return err
		}

		if err := groupSupportAwardAdmin.Persistent(); err != nil {
			return err
		}

		groupSupportEvent := group_ev.InitGroupSupportEvent(len(groupSupportAwardMgrs), groupInfo.Code)
		//数据持久化
		groupSupportEvent.AddAdmin(groupSupportAwardAdmin.ID, groupSupportAwardAdmin.UserId, groupSupportAwardAdmin.DiamondNum)
		for i, _ := range groupSupportAwardMgrs {
			if err := groupSupportAwardMgrs[i].Persistent(); err != nil {
				return err
			}
			groupSupportEvent.AddMgr(groupSupportAwardMgrs[i].ID, groupSupportAwardMgrs[i].UserId, groupSupportAwardMgrs[i].DiamondNum)
		}
		return group_ev.PublishGroupSupport(model, groupSupportEvent)
	})
}

func (s *GroupService) RenewGroupSupporter(groupId string, userIds []uint64) error {
	return s.svc.Transactional(func() error {
		model := domain.CreateModel(s.svc.CtxAndDb)
		gs := group_m.GroupSupporter{GroupId: groupId}
		if err := gs.Delete(model.Db); err != nil {
			return err
		}

		if len(userIds) > 0 {
			gs = group_m.GroupSupporter{GroupId: groupId}
			if err := gs.BatchSave(model.Db, userIds); err != nil {
				return err
			}
		}
		return nil
	})
}
chenweijian's avatar
chenweijian committed
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

func (s *GroupService) GroupSupportResult(time time.Time) error {
	model := domain.CreateModelContext(s.svc.MyContext)

	_, _, period := group_m.GetSupportLevelTime(time)
	// 群组扶持数值
	groupGradeMap, err := getGroupGradeMap(model, period)
	if err != nil {
		model.Log.Errorf("GroupSupportResult time:%v, err:%v", time, err)
		return err
	}

	model.Log.Infof("GroupSupportResult period:%s, len:%d, groupUidGadeMap:%v", period, len(groupGradeMap), groupGradeMap)
	// 房间访问人数
	roomVisitCount, err := GetAllRoomNonZeroVisitCount()
	if err != nil {
		model.Log.Errorf("GroupSupportResult err:%v", err)
		return err
	}

	// 入库群组扶持结果
	for g, grade := range groupGradeMap {
		r := group_m.InitGroupSupportResult(model, g, grade, period, roomVisitCount[g])
		r.SetOnDuplicateKeyIGNORE()
		if err = r.Persistent(); err != nil {
			model.Log.Errorf("GroupSupportResult InitGroupSupportResult r:%+v, err:%v", r, err)
		}
	}
	// 小助手通知
	AssistantNotification(model, groupGradeMap)
	return nil
}

func getGroupGradeMap(model *domain.Model, period string) (map[string]uint8, error) {
	// 配置
	gsConfig, err := res_m.GetResGroupSupportByValid(model)
	if err != nil {
		model.Log.Errorf("GroupSupportResult err:%v", err)
		return nil, err
	}

	// 群组扶持数值
	groupGradeMap := make(map[string]uint8, 0)
	//userNum := make(map[string]uint32, 0)

	// 流水
	keyDiamond := rediskey.GetGroupSupportConsumeSummary(period)
	var start int64
	count := int64(999)
	var zList []redis.Z
	// 一次取1000个处理
	for start == 0 || len(zList) > 0 {
		stop := start + count
		zList, err = model.RedisCluster.ZRangeWithScores(context.Background(), keyDiamond, start, stop).Result()
		if err != nil {
			model.Log.Errorf("GroupSupportResult err:%v", err)
			return nil, err
		}
		if len(zList) == 0 {
			break
		}
		for _, v := range zList {
			imGroupId := v.Member.(string)
			consume := mysql.Num(v.Score)
			// 支持者数量
			keySupportNum := rediskey.GetGroupSupportCountSupporter(period, imGroupId)
			supportNum, err := model.RedisCluster.SCard(context.Background(), keySupportNum).Result()
			if err != nil {
				model.Log.Errorf("GroupSupportResult key:%s, err:%v", keySupportNum, err)
				return nil, err
			}
			for j := len(gsConfig) - 1; j >= 0; j-- {
				if mysql.Num(supportNum) >= gsConfig[j].ContributeUserNum && consume >= gsConfig[j].ContributeDiamondNum {
					groupGradeMap[imGroupId] = gsConfig[j].Grade
					//userNum[imGroupId] = uint32(supportNum)
					break
				}
			}
		}
		start = stop + 1
	}
	return groupGradeMap, nil
}

func AssistantNotification(model *domain.Model, groupGradeMap map[string]uint8) {
	// 小助手通知
	for g, _ := range groupGradeMap {
		userId, err := group_m.GetProfitAllocator(model, g)
		if err != nil {
			model.Log.Errorf("GroupSupportResult msg GetProfitAllocator err:%v, groupUid:%v", err, g)
			continue
		}
		//推送
		user, err := user_m.GetUser(model, userId)
		if err != nil {
			model.Log.Infof("GroupSupportResult msg GetUser userId:=%v, err:=%v", userId, err)
			continue
		}
		record := msg_m.NewUserRecord(model, user.ID, msg_e.GroupSupportResult, "", 0, "", "", "", "", "")
		if err := record.Persistent(); err != nil {
			model.Log.Infof("GroupSupportResult msg record.Persistent() err:%v", err)
			continue
		}
		msg_m.SendEmasMsgAssistant(model, user.ExternalId, user.DeviceType)
	}
}

func GetAllRoomNonZeroVisitCount() (map[string]uint, error) {
	m, err := GetAllRoomVisitCount()
	if err != nil {
		return nil, err
	}
	result := make(map[string]uint, 0)
	for g, s := range m {
		if c, err := strconv.Atoi(s); err == nil && c > 0 {
			result[g] = uint(c)
		}
	}
	return result, nil
}

func GetAllRoomVisitCount() (map[string]string, error) {
	key := rediskey.GetPrefixRoomVisitCount()
	return redisCli.GetRedis().HGetAll(context.Background(), key).Result()
}