...
|
...
|
@@ -3,8 +3,10 @@ package domainService |
|
|
import (
|
|
|
"fmt"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/im"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
|
|
|
)
|
|
|
|
|
|
// PgImService 网易云信IM服务
|
...
|
...
|
@@ -12,54 +14,49 @@ type PgImService struct { |
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func (s *PgImService) InitOrUpdateUserIMInfo(userId int64, name string) (imInfo *domain.Im, err error) {
|
|
|
func (ptr *PgImService) InitOrUpdateUserIMInfo(userId int64, flag int) (*domain.Im, error) {
|
|
|
var (
|
|
|
//ImInfoRepository, _ = factory.CreateImInfoRepository(ctx)
|
|
|
checkImRequest *im.CheckImRequest = &im.CheckImRequest{}
|
|
|
IsCreated = false
|
|
|
checkImResponse *im.CheckImResponse
|
|
|
)
|
|
|
var errFind error
|
|
|
//imInfo, errFind = ImInfoRepository.FindOne(map[string]interface{}{"user_id": userId})
|
|
|
// 异常
|
|
|
//if errFind != nil && errFind != domain.QueryNoRow {
|
|
|
// err = errFind
|
|
|
// return
|
|
|
//}
|
|
|
// 不存在
|
|
|
//if errFind == domain.QueryNoRow {
|
|
|
// imInfo = &domain.Im{
|
|
|
// UserId: userId,
|
|
|
// CreateTime: time.Now(),
|
|
|
// }
|
|
|
//}
|
|
|
// 已存在
|
|
|
if errFind == nil && imInfo != nil {
|
|
|
userBaseRepository, _ := repository.NewUserBaseRepository(ptr.transactionContext)
|
|
|
userBase, err := userBaseRepository.FindOne(map[string]interface{}{"account": fmt.Sprintf("%v", userId)})
|
|
|
if err != nil || userBase == nil || userBase.Status != int(domain.UserStatusEnable) {
|
|
|
return nil, fmt.Errorf("账号不存在")
|
|
|
}
|
|
|
if userBase.Im != nil && len(userBase.Im.Accid) > 0 {
|
|
|
IsCreated = true
|
|
|
if flag == domain.RefreshWhenNotExists {
|
|
|
return userBase.Im, nil
|
|
|
}
|
|
|
} else {
|
|
|
id, err := repository.IdWorker.NextId()
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
userBase.Im = &domain.Im{
|
|
|
Accid: fmt.Sprintf("%v", id),
|
|
|
}
|
|
|
|
|
|
if len(imInfo.Accid) == 0 {
|
|
|
//id, _ := utils.NewSnowflakeId()
|
|
|
//imInfo.ImId = fmt.Sprintf("%v", id)
|
|
|
}
|
|
|
checkImRequest = &im.CheckImRequest{
|
|
|
UserId: userId,
|
|
|
ImId: imInfo.Accid,
|
|
|
Uname: name,
|
|
|
CustomerImId: fmt.Sprintf("%v", imInfo.CsAccountId),
|
|
|
ImId: userBase.Im.Accid,
|
|
|
Uname: userBase.UserInfo.UserName,
|
|
|
CustomerImId: fmt.Sprintf("%v", userBase.Im.CsAccountId),
|
|
|
IsCreated: IsCreated,
|
|
|
}
|
|
|
if checkImResponse, err = CheckIm(checkImRequest); err != nil {
|
|
|
return
|
|
|
return nil, err
|
|
|
}
|
|
|
if len(imInfo.CsAccountId) == 0 {
|
|
|
imInfo.CsAccountId = getRandomCustomerAccount(userId)
|
|
|
if len(userBase.Im.CsAccountId) == 0 {
|
|
|
userBase.Im.CsAccountId = fmt.Sprintf("%v", getRandomCustomerAccount(userId))
|
|
|
}
|
|
|
imInfo.ImToken = checkImResponse.ImToken
|
|
|
//if _, err = ImInfoRepository.Save(imInfo); err != nil {
|
|
|
// return
|
|
|
//}
|
|
|
return
|
|
|
userBase.Im.ImToken = checkImResponse.ImToken
|
|
|
if userBase, err = userBaseRepository.Save(userBase); err != nil {
|
|
|
|
|
|
}
|
|
|
return userBase.Im, nil
|
|
|
}
|
|
|
|
|
|
// 检查ImToken
|
...
|
...
|
@@ -139,21 +136,24 @@ func imRefreshToken(request *im.CheckImRequest, rsp *im.CheckImResponse) (err er |
|
|
}
|
|
|
|
|
|
// 获取客服id
|
|
|
func getRandomCustomerAccount(userId int64) (acid string) {
|
|
|
//ImCustomerServiceRepository, _ := factory.CreateImCustomerServiceRepository(ctx)
|
|
|
//total, customers, err := ImCustomerServiceRepository.Find(map[string]interface{}{"sortById": domain.ASC})
|
|
|
//if err != nil {
|
|
|
// log.Error(err)
|
|
|
// return 0
|
|
|
//}
|
|
|
//if total == 0 {
|
|
|
// return 0
|
|
|
//}
|
|
|
//index := userId % total
|
|
|
//if int(index) < len(customers) {
|
|
|
// acid, _ = strconv.ParseInt(customers[index].ImId, 10, 64)
|
|
|
// return
|
|
|
//}
|
|
|
//acid, _ = strconv.ParseInt(customers[0].ImId, 10, 64)
|
|
|
return
|
|
|
func getRandomCustomerAccount(userId int64) int64 {
|
|
|
total, customers := len(constant.CUSTOMER_ACCOUNT), constant.CUSTOMER_ACCOUNT
|
|
|
if total == 0 {
|
|
|
return 0
|
|
|
}
|
|
|
index := (int(userId) & 0xFF) % int(total)
|
|
|
if int(index) < len(customers) {
|
|
|
return customers[index]
|
|
|
}
|
|
|
return customers[0]
|
|
|
}
|
|
|
|
|
|
func NewPgImService(transactionContext *pgTransaction.TransactionContext) (*PgImService, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &PgImService{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|