diff --git a/domain/model/group_m/mic.go b/domain/model/group_m/mic.go index 31f02683c78bea1cd434851d6387b046dcf2677e..7fc0bb4e2f19115a3dc6d73277440a0c379b592e 100644 --- a/domain/model/group_m/mic.go +++ b/domain/model/group_m/mic.go @@ -193,7 +193,7 @@ const micUpdateScript = "local flag = redis.call('EXISTS', '{prefixGroupUserMic} //上麦(自己), //规则:1:加锁了不能上麦 2:麦上有人,不能上麦 //cpUserId如果有 -func (mic *Mic) In(userId uint64, externalId string, cpUserId uint64) error { +func (mic *Mic) In(userId uint64, externalId string) error { // 群是否被封禁, 呃,,,呃,,,呃,,, banned := GroupBanned{ImGroupId: mic.GroupUuid} if err := banned.Get(mic.model); err != gorm.ErrRecordNotFound { @@ -218,7 +218,6 @@ func (mic *Mic) In(userId uint64, externalId string, cpUserId uint64) error { I: mic.I, ExternalId: externalId, UserId: userId, - CpUserId: cpUserId, Forbid: false, Timestamp: time.Now().Unix(), }) diff --git a/domain/service/group_mic_s/group_mic.go b/domain/service/group_mic_s/group_mic.go index 546d608bf8e98e3c35c7bc89c6d0457e0fa49cf1..e3dc63d8858c654bb5e2c279f35efc33e4effad7 100644 --- a/domain/service/group_mic_s/group_mic.go +++ b/domain/service/group_mic_s/group_mic.go @@ -126,51 +126,14 @@ func redisLock(key string, sign string, expiration time.Duration, callBack func( //加入麦位,锁两秒 func (s *GroupMicService) GroupMicIn(groupUuid string, i int, userId uint64, externalId string) error { - model := domain.CreateModelContext(s.svc.MyContext) - // 填充cp麦位 - var cpUserId uint64 - var cpI int - if cpRelation, _ := rpc.GetUserCpRelation(model, userId); cpRelation != nil { - micNumType, err := group_m.GetMicNumType(model, groupUuid) - if err != nil { - return err - } - _, micUsers, err := group_m.GetAllMic(groupUuid, micNumType) - if err != nil { - return err - } - for _, u := range micUsers { - if u.UserId == cpRelation.CpUserId { - cpUserId = u.UserId - cpI = u.I - break - } - } - } - err := redisLock(redis_key.GetPrefixGroupMicUserInLock(userId), uuid.NewV4().String(), time.Second*2, func() 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, cpUserId) + return mic.In(userId, externalId) }) - if err != nil { - return err - } - // 更新麦上cp的信息 - if cpUserId > 0 { - user, _ := user_m.GetUser(model, cpUserId) - return redisLock(redis_key.GetPrefixGroupMicUserInLock(cpUserId), uuid.NewV4().String(), time.Second*2, func() error { - model := domain.CreateModel(s.svc.CtxAndDb) - mic, err := group_m.GetMic(model, groupUuid, cpI) - if err != nil { - return err - } - return mic.Update(cpUserId, user.ExternalId, userId) - }) - } - return nil } //离开麦位 @@ -367,3 +330,55 @@ func (s *GroupMicService) IncrGroupPowerOnMicExpAndTime(groupId string, userId u } return nil } + +func (s *GroupMicService) CheckCpOnMic(groupUuid string) { + model := domain.CreateModelContext(s.svc.MyContext) + // 填充cp麦位 + micNumType, err := group_m.GetMicNumType(model, groupUuid) + if err != nil { + return + } + _, micUsers, err := group_m.GetAllMic(groupUuid, micNumType) + if err != nil { + return + } + // userId->micIndex + var userMicIndex = make(map[uint64]int) + var userIds []uint64 + for _, u := range micUsers { + userMicIndex[u.UserId] = u.I + userIds = append(userIds, u.UserId) + } + cpPairs, err := rpc.MGetUserCpPairs(model, userIds) + // 更新麦上cp的信息 + if err != nil { + model.Log.Errorf("MGetUserCpPairs fail:%v", err) + return + } + users, err := user_m.GetUserMapByIds(model, userIds) + if err != nil { + model.Log.Errorf("GetUserMapByIds fail:%v", err) + return + } + for _, pairs := range cpPairs { + userId := pairs[0] + cpUserId := pairs[1] + 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, userMicIndex[userId]) + if err != nil { + return err + } + return mic.Update(userId, users[userId].ExternalId, cpUserId) + }) + redisLock(redis_key.GetPrefixGroupMicUserInLock(cpUserId), uuid.NewV4().String(), time.Second*2, func() error { + model := domain.CreateModel(s.svc.CtxAndDb) + mic, err := group_m.GetMic(model, groupUuid, userMicIndex[cpUserId]) + if err != nil { + return err + } + return mic.Update(cpUserId, users[cpUserId].ExternalId, userId) + }) + } + return +} diff --git a/route/group_r/group_mic.go b/route/group_r/group_mic.go index a1c999d2caa075fd8a10976432e228134498dd57..b3c02f2d425dbbedf37fea89705389a6c489bc18 100644 --- a/route/group_r/group_mic.go +++ b/route/group_r/group_mic.go @@ -600,6 +600,8 @@ func GroupMicIn(c *gin.Context) (*mycontext.MyContext, error) { } else { resp.ResponseOk(c, nil) } + // check cp 麦位置 + go group_mic_s.NewGroupPowerService(myContext).CheckCpOnMic(groupUuid) return myContext, nil }