service/app/mix/service/utilservice.go

223 lines
7.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"fmt"
"service/api/errcode"
accountproto "service/api/proto/account/proto"
contact_customer_service_sessionproto "service/api/proto/contact_customer_service_session/proto"
loginproto "service/api/proto/login/proto"
streamerproto "service/api/proto/streamer/proto"
streamerlinkproto "service/api/proto/streamerlink/proto"
"service/bizcommon/util"
"service/dbstruct"
"service/library/logger"
"github.com/gin-gonic/gin"
goproto "google.golang.org/protobuf/proto"
)
// 不向外暴露的辅助公共函数
// 注册账户
func (s *Service) utilRegisterUser(ctx *gin.Context, req *loginproto.MobilePhoneInfoComponent) (login *dbstruct.Login, account *dbstruct.Account, ec errcode.ErrCode) {
var err error
ec = errcode.ErrCodeLoginSrvOk
// 使用发号器发放一个user_id
userIdSeq, err := _DefaultUserId.OpGetNextNormalUserId(ctx)
if err != nil {
logger.Error("Generate user id fail, err: %v", err)
ec = errcode.ErrCodeAccountSrvFail
return
}
// 获取初始账户信息,开始填充
account, err = _DefaultAccount.GenerateOriginalAccount()
if err != nil {
logger.Error("GenerateOriginalAccount failed, err: %v", err)
ec = errcode.ErrCodeLoginSrvFail
return
}
account.UserId = goproto.Int64(userIdSeq.UserId)
account.Name = goproto.String(fmt.Sprintf("user_%v", userIdSeq.UserId))
account.MobilePhone = goproto.String(req.MobilePhone)
account.RegionCode = goproto.String(req.RegionCode)
account.PhoneHash = goproto.String(req.PhoneHash)
// 创建账户生成mid
err = _DefaultAccount.OpCreate(ctx, &accountproto.OpCreateReq{
Account: account,
})
if err != nil {
logger.Error("Account OpCreate failed, err: %v", err)
ec = errcode.ErrCodeAccountSrvFail
return
}
// 获取初始登录信息,开始填充
login = _DefaultLogin.GenerateOriginalLogin()
login.Mid = account.Mid
login.PhoneHash = account.PhoneHash
login.RegionCode = account.RegionCode
// 创建登录信息
err = _DefaultLogin.OpCreate(ctx, &loginproto.OpCreateReq{
Login: login,
})
if err != nil {
logger.Error("Login OpCreate failed, err: %v", err)
ec = errcode.ErrCodeLoginSrvFail
return
}
return
}
// 提供account将之扩张为streamer_ext
func (s *Service) utilExtendAccountIntoStreamerExt(ctx *gin.Context, account *dbstruct.Account, streamerExt streamerproto.StreamerExtVO) (ec errcode.ErrCode) {
ec = errcode.ErrCodeStreamerSrvOk
//1.查询主播侧数据
streamer, err := _DefaultStreamer.OpListByMid(ctx, &streamerproto.OpListByMidReq{
Mid: account.Mid,
})
if err != nil || streamer == nil {
logger.Error("Streamer OpListByMid fail, req: %v, err: %v", account, err)
ec = errcode.ErrCodeStreamerSrvFail
return
}
//2.再查询主播链接将link_no聚合生成streamerlinkVO
streamerlinkList, err := _DefaultStreamerLink.OpListByMid(ctx, &streamerlinkproto.OpListByMidReq{
Mid: account.Mid,
})
if err != nil {
logger.Error("StreamerLink OpListByMid fail, req: %v, err: %v", account, err)
ec = errcode.ErrCodeStreamerLinkSrvFail
return
}
linkNoList := make([]int64, len(streamerlinkList))
for i, streamerlink := range streamerlinkList {
linkNoList[i] = util.DerefInt64(streamerlink.LinkNo)
}
platformCfgList, ec := s.OpGetPlatformListByIds(ctx, linkNoList)
if ec != errcode.ErrCodeOk {
return ec
}
if len(platformCfgList) != len(linkNoList) {
return errcode.ErrCodeApolloReadFail
}
streamerlinkVOList := make([]*streamerlinkproto.StreamerLinkVO, len(streamerlinkList))
for i, _ := range streamerlinkVOList {
streamerlinkVOList[i] = streamerlinkproto.NewStreamerLinkVO()
streamerlinkVOList[i].CopyStreamerLink(streamerlinkList[i]).CopyPlatformCfg(platformCfgList[i])
}
// 主播vas信息
userVas, _ := _DefaultVas.GetUserVasInfo(ctx, *account.Mid)
// 组装返回内容
streamerExt.CopyAccount(account)
streamerExt.CopyStreamer(streamer)
streamerExt.CopyPlatforms(&streamerlinkVOList)
streamerExt.CopyUserVas(userVas)
return
}
// 提供accountlist将之扩张为streamer_ext
func (s *Service) utilExtendAccountsIntoStreamerExts(ctx *gin.Context, accountList []*dbstruct.Account, list []streamerproto.StreamerExtVO) (ec errcode.ErrCode) {
ec = errcode.ErrCodeStreamerSrvOk
//1.打包所有查询得到的Mid查询主播侧数据也按mid正序排序
midList := make([]int64, len(accountList))
for i, account := range accountList {
midList[i] = util.DerefInt64(account.Mid)
}
streamerList, err := _DefaultStreamer.OpListByMids(ctx, &streamerproto.OpListByMidsReq{
Mids: midList,
Sort: []string{"mid"},
})
if err != nil {
logger.Error("Streamer OpListByMids fail, req: %v, err: %v", accountList, err)
ec = errcode.ErrCodeStreamerSrvFail
return
}
//2.再根据打包的Mid查询主播的链接按midorder正序排序并将link_no按mid进行聚合
streamerlinkList, err := _DefaultStreamerLink.OpListByMids(ctx, &streamerlinkproto.OpListByMidsReq{
Mids: midList,
Sort: []string{"mid", "order"},
})
if err != nil {
logger.Error("StreamerLink OpListByMids fail, req: %v, err: %v", accountList, err)
ec = errcode.ErrCodeStreamerLinkSrvFail
return
}
streamerlinkMap := make(map[int64][]*dbstruct.StreamerLink)
for _, streamerlink := range streamerlinkList {
streamerlinkSlice := streamerlinkMap[util.DerefInt64(streamerlink.Mid)]
streamerlinkMap[util.DerefInt64(streamerlink.Mid)] = append(streamerlinkSlice, streamerlink)
}
//3.组装返回内容
for i := range list {
account := accountList[i]
streamer := streamerList[i]
accountMid := util.DerefInt64(account.Mid)
//判断account和streamer是否匹配
if util.DerefInt64(streamer.Mid) != accountMid {
logger.Error("Bad Match: the account and streamer data of streamers do not match each other!")
ec = errcode.ErrCodeStreamerSrvFail
return
}
streamerlinkList := streamerlinkMap[accountMid]
linkNoList := make([]int64, len(streamerlinkList))
for i, streamerlink := range streamerlinkList {
linkNoList[i] = util.DerefInt64(streamerlink.LinkNo)
}
platformCfgList, ec := s.OpGetPlatformListByIds(ctx, linkNoList)
if ec != errcode.ErrCodeOk {
return ec
}
streamerlinkVOList := make([]*streamerlinkproto.StreamerLinkVO, len(streamerlinkList))
for i := range streamerlinkVOList {
streamerlinkVOList[i] = streamerlinkproto.NewStreamerLinkVO()
streamerlinkVOList[i].CopyStreamerLink(streamerlinkList[i]).CopyPlatformCfg(platformCfgList[i])
}
list[i].CopyAccount(account)
list[i].CopyStreamer(streamer)
list[i].CopyPlatforms(&streamerlinkVOList)
}
return
}
// 提供session_id返回session_id -> Session的Map
func (s *Service) utilGetContactCustomerServiceSessionMap(ctx *gin.Context, sessionIds []int64) (_map map[int64]*dbstruct.ContactCustomerServiceSession, err error) {
list, err := _DefaultContactCustomerServiceSession.OpListBySessionIds(ctx, &contact_customer_service_sessionproto.OpListBySessionIdsReq{
SessionIds: sessionIds,
})
if err != nil {
logger.Error("OpListBySessionIds fail: %v", err)
return
}
_map = make(map[int64]*dbstruct.ContactCustomerServiceSession)
for _, session := range list {
_map[util.DerefInt64(session.Id)] = session
}
return
}