2025-11-28 23:30:49 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"carrotskin/internal/model"
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-02 22:52:33 +08:00
|
|
|
// profileRepository ProfileRepository的实现
|
|
|
|
|
type profileRepository struct {
|
|
|
|
|
db *gorm.DB
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-02 22:52:33 +08:00
|
|
|
// NewProfileRepository 创建ProfileRepository实例
|
|
|
|
|
func NewProfileRepository(db *gorm.DB) ProfileRepository {
|
|
|
|
|
return &profileRepository{db: db}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) Create(ctx context.Context, profile *model.Profile) error {
|
|
|
|
|
return r.db.WithContext(ctx).Create(profile).Error
|
2025-12-02 22:52:33 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) FindByUUID(ctx context.Context, uuid string) (*model.Profile, error) {
|
2025-11-28 23:30:49 +08:00
|
|
|
var profile model.Profile
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Where("uuid = ?", uuid).
|
2025-11-28 23:30:49 +08:00
|
|
|
Preload("Skin").
|
|
|
|
|
Preload("Cape").
|
|
|
|
|
First(&profile).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &profile, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) FindByName(ctx context.Context, name string) (*model.Profile, error) {
|
2025-11-28 23:30:49 +08:00
|
|
|
var profile model.Profile
|
2025-12-03 10:58:39 +08:00
|
|
|
// 使用 LOWER 函数进行不区分大小写的查询,并预加载 Skin 和 Cape
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Where("LOWER(name) = LOWER(?)", name).
|
2025-12-03 10:58:39 +08:00
|
|
|
Preload("Skin").
|
|
|
|
|
Preload("Cape").
|
|
|
|
|
First(&profile).Error
|
2025-11-28 23:30:49 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &profile, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) FindByUserID(ctx context.Context, userID int64) ([]*model.Profile, error) {
|
2025-11-28 23:30:49 +08:00
|
|
|
var profiles []*model.Profile
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Where("user_id = ?", userID).
|
2025-11-28 23:30:49 +08:00
|
|
|
Preload("Skin").
|
|
|
|
|
Preload("Cape").
|
|
|
|
|
Order("created_at DESC").
|
|
|
|
|
Find(&profiles).Error
|
2025-12-02 10:33:19 +08:00
|
|
|
return profiles, err
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) FindByUUIDs(ctx context.Context, uuids []string) ([]*model.Profile, error) {
|
|
|
|
|
if len(uuids) == 0 {
|
|
|
|
|
return []*model.Profile{}, nil
|
|
|
|
|
}
|
|
|
|
|
var profiles []*model.Profile
|
|
|
|
|
// 使用 IN 查询优化批量查询,并预加载关联
|
|
|
|
|
err := r.db.WithContext(ctx).Where("uuid IN ?", uuids).
|
|
|
|
|
Preload("Skin").
|
|
|
|
|
Preload("Cape").
|
|
|
|
|
Find(&profiles).Error
|
|
|
|
|
return profiles, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *profileRepository) Update(ctx context.Context, profile *model.Profile) error {
|
|
|
|
|
return r.db.WithContext(ctx).Save(profile).Error
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) UpdateFields(ctx context.Context, uuid string, updates map[string]interface{}) error {
|
|
|
|
|
return r.db.WithContext(ctx).Model(&model.Profile{}).
|
2025-11-28 23:30:49 +08:00
|
|
|
Where("uuid = ?", uuid).
|
|
|
|
|
Updates(updates).Error
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) Delete(ctx context.Context, uuid string) error {
|
|
|
|
|
return r.db.WithContext(ctx).Where("uuid = ?", uuid).Delete(&model.Profile{}).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *profileRepository) BatchUpdate(ctx context.Context, uuids []string, updates map[string]interface{}) (int64, error) {
|
|
|
|
|
if len(uuids) == 0 {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
result := r.db.WithContext(ctx).Model(&model.Profile{}).Where("uuid IN ?", uuids).Updates(updates)
|
|
|
|
|
return result.RowsAffected, result.Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *profileRepository) BatchDelete(ctx context.Context, uuids []string) (int64, error) {
|
|
|
|
|
if len(uuids) == 0 {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
result := r.db.WithContext(ctx).Where("uuid IN ?", uuids).Delete(&model.Profile{})
|
|
|
|
|
return result.RowsAffected, result.Error
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) CountByUserID(ctx context.Context, userID int64) (int64, error) {
|
2025-11-28 23:30:49 +08:00
|
|
|
var count int64
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Model(&model.Profile{}).
|
2025-11-28 23:30:49 +08:00
|
|
|
Where("user_id = ?", userID).
|
|
|
|
|
Count(&count).Error
|
|
|
|
|
return count, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) UpdateLastUsedAt(ctx context.Context, uuid string) error {
|
|
|
|
|
return r.db.WithContext(ctx).Model(&model.Profile{}).
|
2025-11-28 23:30:49 +08:00
|
|
|
Where("uuid = ?", uuid).
|
|
|
|
|
Update("last_used_at", gorm.Expr("CURRENT_TIMESTAMP")).Error
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) GetByNames(ctx context.Context, names []string) ([]*model.Profile, error) {
|
2025-11-28 23:30:49 +08:00
|
|
|
var profiles []*model.Profile
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Where("name in (?)", names).
|
2025-12-03 10:58:39 +08:00
|
|
|
Preload("Skin").
|
|
|
|
|
Preload("Cape").
|
|
|
|
|
Find(&profiles).Error
|
2025-12-02 10:33:19 +08:00
|
|
|
return profiles, err
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) GetKeyPair(ctx context.Context, profileId string) (*model.KeyPair, error) {
|
2025-11-28 23:30:49 +08:00
|
|
|
if profileId == "" {
|
|
|
|
|
return nil, errors.New("参数不能为空")
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 10:33:19 +08:00
|
|
|
var profile model.Profile
|
2025-12-03 15:27:12 +08:00
|
|
|
result := r.db.WithContext(ctx).
|
2025-12-02 10:33:19 +08:00
|
|
|
Select("key_pair").
|
|
|
|
|
Where("id = ?", profileId).
|
|
|
|
|
First(&profile)
|
2025-11-28 23:30:49 +08:00
|
|
|
|
|
|
|
|
if result.Error != nil {
|
2025-12-02 22:52:33 +08:00
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2025-11-28 23:30:49 +08:00
|
|
|
return nil, errors.New("key pair未找到")
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("获取key pair失败: %w", result.Error)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 10:33:19 +08:00
|
|
|
return &model.KeyPair{}, nil
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *profileRepository) UpdateKeyPair(ctx context.Context, profileId string, keyPair *model.KeyPair) error {
|
2025-11-28 23:30:49 +08:00
|
|
|
if profileId == "" {
|
|
|
|
|
return errors.New("profileId 不能为空")
|
|
|
|
|
}
|
|
|
|
|
if keyPair == nil {
|
|
|
|
|
return errors.New("keyPair 不能为 nil")
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
result := tx.Table("profiles").
|
2025-12-02 10:33:19 +08:00
|
|
|
Where("id = ?", profileId).
|
2025-11-28 23:30:49 +08:00
|
|
|
UpdateColumns(map[string]interface{}{
|
2025-12-02 10:33:19 +08:00
|
|
|
"private_key": keyPair.PrivateKey,
|
|
|
|
|
"public_key": keyPair.PublicKey,
|
2025-11-28 23:30:49 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
|
return fmt.Errorf("更新 keyPair 失败: %w", result.Error)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|