group_mic.go 9.43 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12
package group_mic_s

import (
	"context"
	"encoding/json"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/redisCli"
	uuid "github.com/satori/go.uuid"
	"hilo-group/_const/enum/group_e"
	"hilo-group/_const/redis_key"
	"hilo-group/domain/event/group_ev"
hujiebin's avatar
hujiebin committed
13
	"hilo-group/domain/model/groupPower_m"
hujiebin's avatar
hujiebin committed
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
	"hilo-group/domain/model/group_m"
	"hilo-group/domain/service/signal_s"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
	"time"
)

type GroupMicService struct {
	svc *domain.Service
}

func NewGroupPowerService(myContext *mycontext.MyContext) *GroupMicService {
	svc := domain.CreateService(myContext)
	return &GroupMicService{svc}
}

//修改群组中麦的数量
func (s *GroupMicService) GroupMicNumChange(groupId string, userId uint64, micNumType group_e.GroupMicNumType, micOn bool) error {
	model := domain.CreateModelContext(s.svc.MyContext)
	//数据库修改群组麦的数量
	//检查权限
	if err := group_m.CheckPermission(model, groupId, userId); err != nil {
		return err
	}
	//删除麦位数量类型缓存
	group_m.InitMicNumType(model, groupId, micNumType).ClearCache()

	//
	groupInfo, _ := group_m.GetGroupInfo(model, groupId)
	if groupInfo == nil {
		return bizerr.GroupNotFound
	}

	//判断数据是否发生了变化
	//关闭麦位
	if micOn == false {
		if groupInfo.MicOn == micOn {
			return nil
		}
	} else {
		if groupInfo.MicOn == micOn && groupInfo.MicNumType == micNumType {
			return nil
		}
	}
	//修改数据,然后数据持久化,
	g := group_m.GroupInfo{
		MicOn: micOn,
	}
	fields := []string{"mic_on"}
	//开启麦位才修改micNumType
	returnMicNumType := g.MicNumType
	if micOn == true {
		g.MicNumType = micNumType
		fields = append(fields, "mic_num_type")
		returnMicNumType = micNumType
	}
	db := g.Update(model, groupId, fields)
	if db.Error != nil {
		return myerr.WrapErr(db.Error)
	}

	//增加麦位数量类型缓存
	group_m.InitMicNumType(model, groupId, micNumType).AddCache()

	//麦位上的信息,清理
	group_m.ClearMic(groupId)

	type Content struct {
		MicOn      bool                    `json:"micOn"`
		MicNumType group_e.GroupMicNumType `json:"micNumType"`
		Timestamp  int64                   `json:"timestamp"`
	}

	r := Content{
		MicOn:      micOn,
		MicNumType: returnMicNumType,
		Timestamp:  time.Now().Unix(),
	}
	buf, err := json.Marshal(r)
	if err != nil {
		model.Log.Errorf("GroupMicNumChange Content json.Marshal err:%v", err)
	}
	// 发信令,让前端重新拉取,接受容错,
	signal_s.SendSignalMsg(model, groupId, group_m.GroupSystemMsg{
		MsgId:   group_e.GroupMicChangeSignal,
		Content: string(buf),
	}, false)

	group_m.MicNumChangeRPush(model, groupId, returnMicNumType, micOn)

	return nil
}

//加锁, sign作为密钥存在(预防被别人删除,比如:先者删除了后者的锁),
func redisLock(key string, sign string, expiration time.Duration, callBack func() error) error {
	flag, err := redisCli.GetRedis().SetNX(context.Background(), key, sign, expiration).Result()
	if err != nil {
		return myerr.WrapErr(err)
	}
	if flag {
		err = callBack()
		redisSign, _ := redisCli.GetRedis().Get(context.Background(), key).Result()
		if redisSign == sign {
			redisCli.GetRedis().Del(context.Background(), key)
		}
		return err
	} else {
		return bizerr.GroupConcurrencyLock
	}
}

//加入麦位,锁两秒
func (s *GroupMicService) GroupMicIn(groupUuid string, i int, userId uint64, externalId string) error {
	return redisLock(redis_key.GetPrefixGroupMicUserInLock(userId), uuid.NewV4().String(), time.Second*2, func() error {
		model := domain.CreateModel(s.svc.CtxAndDb)
		mic, err := group_m.GetMic(model, groupUuid, i)
		if err != nil {
			return err
		}
		return mic.In(userId, externalId)
	})
}

//离开麦位
func (s *GroupMicService) GroupMicLeave(groupUuid string, i int, userId uint64, externalId string) error {
	return redisLock(redis_key.GetPrefixGroupMicUserDelLock(groupUuid, i), uuid.NewV4().String(), time.Second*2, func() error {
		model := domain.CreateModel(s.svc.CtxAndDb)
		micUser, err := group_m.GetMicUser(model, groupUuid, i)
		if err != nil {
			return err
		}
		if micUser != nil {
			return micUser.LeaveByUser(userId, externalId)
		}
		return nil
	})
}

//邀请上麦,锁两秒
func (s *GroupMicService) GroupMicInvite(groupUuid string, operateUserId uint64, beInvitedExternalId string) error {
	model := domain.CreateModelContext(s.svc.MyContext)
	if err := group_m.InviteMicIn(model, groupUuid, operateUserId, beInvitedExternalId); err != nil {
		return err
	}
	return nil
}

//麦位加锁,
func (s *GroupMicService) GroupMicLock(userId uint64, externalId string, groupUuid string, i int) error {
	//后果等级不高,不需要加锁
	model := domain.CreateModelContext(s.svc.MyContext)
	mic, err := group_m.GetMic(model, groupUuid, i)
	if err != nil {
		return err
	}
	return mic.MgrLock(userId, externalId)
}

//麦位解锁
func (s *GroupMicService) GroupMicUnLock(userId uint64, externalId string, groupUuid string, i int) error {
	//后果等级不高,不需要加锁
	model := domain.CreateModelContext(s.svc.MyContext)
	mic, err := group_m.GetMic(model, groupUuid, i)
	if err != nil {
		return err
	}
	return mic.MgrUnLock(userId, externalId)
}

// 麦位静音
func (s *GroupMicService) GroupMicMute(userId uint64, externalId string, groupUuid string, i int) error {
	//后果等级不高,不需要加锁
	model := domain.CreateModelContext(s.svc.MyContext)
	mic, err := group_m.GetMic(model, groupUuid, i)
	if err != nil {
		return err
	}
	return mic.MgrMute(userId, externalId)
}

// 麦位解除静音
func (s *GroupMicService) GroupMicUnMute(userId uint64, externalId string, groupUuid string, i int) error {
	//后果等级不高,不需要加锁
	model := domain.CreateModelContext(s.svc.MyContext)
	mic, err := group_m.GetMic(model, groupUuid, i)
	if err != nil {
		return err
	}
	return mic.MgrUnMute(userId, externalId)
}

//开启麦
func (s *GroupMicService) GroupMicSpeechOpen(userId uint64, externalId string, groupUuid string, i int) error {
	//自己/管理人开启禁麦,并发率不高,后果等级不高
	model := domain.CreateModelContext(s.svc.MyContext)
	micUser, err := group_m.GetMicUser(model, groupUuid, i)
	if err != nil {
		return err
	}
	return micUser.SpeechOpen(userId, externalId)
}

//关闭麦
func (s *GroupMicService) GroupMicSpeechClose(userId uint64, externalId string, groupUuid string, i int) error {
	model := domain.CreateModelContext(s.svc.MyContext)
	micUser, err := group_m.GetMicUser(model, groupUuid, i)
	if err != nil {
		return err
	}
	return micUser.SpeechClose(userId, externalId)
}

//麦上的人群发消息
func (s *GroupMicService) GroupIMMassByInMic(groupId string, userId uint64, externalId string, content string) error {
	return s.svc.Transactional(func() error {
		model := domain.CreateModel(s.svc.CtxAndDb)
		micUser, err := group_m.GetMicUserByExternalId(model, externalId)
		if err != nil {
			return err
		}
		if err := micUser.ImMass(externalId); err != nil {
			return err
		}
		//校验群组ID
		if micUser.GroupUuid != groupId {
			return myerr.NewSysError("groupId 不一致, http:groupId <> micUser.groupId")
		}

		//获取群成员
		gm, err := group_m.GetMembers(model.Db, groupId)
		if err != nil {
			return err
		}
		uids := make([]uint64, 0)
		for _, i := range gm {
			//排除自己
			if userId != i.UserId {
				uids = append(uids, i.UserId)
			}
		}
		return group_ev.PublishGroupImMass(model, &group_ev.GroupImMassEvent{
			GroupId: groupId,
			UserId:  userId,
			Members: uids,
			Content: content,
		})
	})
}

// 群管理人群发消息
func (s *GroupMicService) GroupIMMassByMgr(groupId string, userId uint64, externalId string, content string) error {
	return s.svc.Transactional(func() error {
		model := domain.CreateModel(s.svc.CtxAndDb)
		//检查权限
		if err := group_m.CheckPermission(model, groupId, userId); err != nil {
			return err
		}
		//获取群成员
		gm, err := group_m.GetMembers(model.Db, groupId)
		if err != nil {
			return err
		}
		uids := make([]uint64, 0)
		for _, i := range gm {
			//排除自己
			if userId != i.UserId {
				uids = append(uids, i.UserId)
			}
		}
		return group_ev.PublishGroupImMass(model, &group_ev.GroupImMassEvent{
			GroupId: groupId,
			UserId:  userId,
			Members: uids,
			Content: content,
		})
	})
}
hujiebin's avatar
hujiebin committed
291

292 293
// 增加势力上麦经验/时长
func (s *GroupMicService) IncrGroupPowerOnMicExpAndTime(groupId string, userId uint64, joinMicTimestamp int64) error {
hujiebin's avatar
hujiebin committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	var model = domain.CreateModelContext(s.svc.MyContext)
	exists, groupPowerId, err := groupPower_m.CheckGroupPowerUser(model, userId)
	if err != nil {
		return err
	}
	if !exists {
		return nil
	}
	// 获取势力下所有群组
	groups, err := groupPower_m.GetGroupPowerGroups(model, groupPowerId)
	if err != nil {
		return err
	}
	inGroup := false
	for _, group := range groups {
		if group.ImGroupId == groupId {
			inGroup = true
			break
		}
	}
	if !inGroup {
		return nil
	}
	// 增加势力上麦经验
318 319 320
	if err := groupPower_m.IncrGroupPowerExpOnMic(model, groupPowerId, userId, joinMicTimestamp); err != nil {
		model.Log.Errorf("IncrGroupPowerExpOnMic fail:%v", err)
	}
hujiebin's avatar
hujiebin committed
321 322 323 324 325 326 327
	// 增加势力上麦时长-月
	if err := groupPower_m.IncrGroupPowerStarOnMicMonth(model, groupPowerId, userId, joinMicTimestamp); err != nil {
		model.Log.Errorf("IncrGroupPowerStarOnMicMonth fail:%v", err)
	}
	// 增加势力上麦时长-天
	if err := groupPower_m.IncrGroupPowerStarOnMicDay(model, groupPowerId, userId, joinMicTimestamp); err != nil {
		model.Log.Errorf("IncrGroupPowerStarOnMicDay fail:%v", err)
328 329
	}
	return nil
hujiebin's avatar
hujiebin committed
330
}