2026-04-29 20:14:53 +08:00

96 lines
2.1 KiB
Go

package vip
import (
"context"
"chatapp3-golang/internal/model"
)
type vipResourceRecord struct {
ID int64 `gorm:"column:id"`
Name string `gorm:"column:name"`
Cover string `gorm:"column:cover"`
SourceURL string `gorm:"column:source_url"`
}
type vipResourceLookup map[int64]vipResourceRecord
func (s *Service) loadResourceLookup(ctx context.Context, resourceIDs []int64) (vipResourceLookup, error) {
ids := compactResourceIDs(resourceIDs)
if len(ids) == 0 {
return nil, nil
}
var rows []vipResourceRecord
if err := s.db.WithContext(ctx).
Table("props_source_record").
Select("id, name, cover, source_url").
Where("id IN ?", ids).
Find(&rows).Error; err != nil {
return nil, err
}
lookup := make(vipResourceLookup, len(rows))
for _, row := range rows {
lookup[row.ID] = row
}
return lookup, nil
}
func compactResourceIDs(resourceIDs []int64) []int64 {
seen := make(map[int64]struct{}, len(resourceIDs))
ids := make([]int64, 0, len(resourceIDs))
for _, id := range resourceIDs {
if id <= 0 {
continue
}
if _, exists := seen[id]; exists {
continue
}
seen[id] = struct{}{}
ids = append(ids, id)
}
return ids
}
func resourceIDsFromConfigRows(rows []model.VipLevelConfig) []int64 {
ids := make([]int64, 0, len(rows)*5)
for _, row := range rows {
ids = appendResourceIDsFromConfig(ids, row)
}
return ids
}
func resourceIDsFromConfigMap(configs map[int]model.VipLevelConfig) []int64 {
ids := make([]int64, 0, len(configs)*5)
for _, row := range configs {
ids = appendResourceIDsFromConfig(ids, row)
}
return ids
}
func appendResourceIDsFromConfig(ids []int64, row model.VipLevelConfig) []int64 {
return append(
ids,
row.BadgeResourceID,
row.AvatarFrameResourceID,
row.EntryEffectResourceID,
row.ChatBubbleResourceID,
row.FloatPictureResourceID,
)
}
func appendResourceIDsFromState(ids []int64, row *model.UserVipState) []int64 {
if row == nil {
return ids
}
return append(
ids,
row.BadgeResourceID,
row.AvatarFrameResourceID,
row.EntryEffectResourceID,
row.ChatBubbleResourceID,
row.FloatPictureResourceID,
)
}