group_support.go 8.68 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 18
	"hilo-group/domain/model/res_m"
	"hilo-group/domain/model/user_m"
	"hilo-group/myerr/bizerr"
chenweijian's avatar
chenweijian committed
19
	"strconv"
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
	"time"
)

// 群组支持名单过滤
func (s *GroupService) GroupSupportList(groupId string, uids []uint64) ([]uint64, []uint64, error) {
	if len(uids) <= 0 {
		return uids, nil, nil
	}

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

	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
	}
}

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
}

//群组支持奖励
func (s *GroupService) GroupSupportAward(groupId string, profitAllocator uint64, userIds []uint64, resId uint64, period string) error {
	return s.svc.Transactional(func() error {
		model := domain.CreateModel(s.svc.CtxAndDb)
		//
		groupInfo, err := group_m.GetGroupInfo(model, groupId)
		if groupInfo == nil {
			return bizerr.GroupNotFound
		}
		//发放奖励
		groupSupportAwardAdmin, groupSupportAwardMgrs, err := group_m.AddGroupSupportAward(model, groupId, profitAllocator, resId, userIds, period)
		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
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

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()
}