group_support.go 4.64 KB
Newer Older
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
package group_s

import (
	"git.hilo.cn/hilo-common/domain"
	"hilo-group/_const/enum/gift_e"
	"hilo-group/domain/event/group_ev"
	"hilo-group/domain/model/gift_m"
	"hilo-group/domain/model/group_m"
	"hilo-group/domain/model/res_m"
	"hilo-group/domain/model/user_m"
	"hilo-group/myerr/bizerr"
	"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
	})
}