Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"slices"
|
||||
|
||||
modellog "github.com/perfect-panel/server/internal/model/log"
|
||||
"github.com/perfect-panel/server/pkg/tool"
|
||||
"github.com/perfect-panel/server/pkg/xerr"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
@@ -188,26 +189,35 @@ func fillGiftBenefits(ctx context.Context, db *gorm.DB, benefits map[int64]Benef
|
||||
return nil
|
||||
}
|
||||
|
||||
func QueryIdentifiers(ctx context.Context, db *gorm.DB, userIds []int64) (map[int64]string, error) {
|
||||
identifiers := make(map[int64]string, len(userIds))
|
||||
type DeviceIdentifier struct {
|
||||
Identifier string
|
||||
DeviceNo string
|
||||
}
|
||||
|
||||
func QueryDeviceIdentifiers(ctx context.Context, db *gorm.DB, userIds []int64) (map[int64]DeviceIdentifier, error) {
|
||||
identifiers := make(map[int64]DeviceIdentifier, len(userIds))
|
||||
if len(userIds) == 0 {
|
||||
return identifiers, nil
|
||||
}
|
||||
|
||||
type identifierRow struct {
|
||||
UserId int64 `gorm:"column:user_id"`
|
||||
DeviceId int64 `gorm:"column:device_id"`
|
||||
Identifier string `gorm:"column:identifier"`
|
||||
}
|
||||
var rows []identifierRow
|
||||
if err := db.WithContext(ctx).
|
||||
Table("user_auth_methods uam").
|
||||
Select("uam.user_id, uam.auth_identifier as identifier").
|
||||
Joins("JOIN (SELECT user_id, MIN(id) AS id FROM user_auth_methods WHERE user_id IN ? GROUP BY user_id) first_uam ON first_uam.id = uam.id", userIds).
|
||||
Table("user_device ud").
|
||||
Select("ud.user_id, ud.id as device_id, ud.identifier as identifier").
|
||||
Joins("JOIN (SELECT user_id, MIN(id) AS id FROM user_device WHERE user_id IN ? GROUP BY user_id) first_ud ON first_ud.id = ud.id", userIds).
|
||||
Scan(&rows).Error; err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query user identifiers failed: %v", err)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query user device identifiers failed: %v", err)
|
||||
}
|
||||
for _, row := range rows {
|
||||
identifiers[row.UserId] = row.Identifier
|
||||
identifiers[row.UserId] = DeviceIdentifier{
|
||||
Identifier: row.Identifier,
|
||||
DeviceNo: tool.DeviceIdToHash(row.DeviceId),
|
||||
}
|
||||
}
|
||||
return identifiers, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user