161 lines
4.1 KiB
Go
161 lines
4.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"carrotskin/internal/model"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CreateProfile 创建档案
|
|
func CreateProfile(profile *model.Profile) error {
|
|
return getDB().Create(profile).Error
|
|
}
|
|
|
|
// FindProfileByUUID 根据UUID查找档案
|
|
func FindProfileByUUID(uuid string) (*model.Profile, error) {
|
|
var profile model.Profile
|
|
err := getDB().Where("uuid = ?", uuid).
|
|
Preload("Skin").
|
|
Preload("Cape").
|
|
First(&profile).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &profile, nil
|
|
}
|
|
|
|
// FindProfileByName 根据角色名查找档案
|
|
func FindProfileByName(name string) (*model.Profile, error) {
|
|
var profile model.Profile
|
|
err := getDB().Where("name = ?", name).First(&profile).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &profile, nil
|
|
}
|
|
|
|
// FindProfilesByUserID 获取用户的所有档案
|
|
func FindProfilesByUserID(userID int64) ([]*model.Profile, error) {
|
|
var profiles []*model.Profile
|
|
err := getDB().Where("user_id = ?", userID).
|
|
Preload("Skin").
|
|
Preload("Cape").
|
|
Order("created_at DESC").
|
|
Find(&profiles).Error
|
|
return profiles, err
|
|
}
|
|
|
|
// UpdateProfile 更新档案
|
|
func UpdateProfile(profile *model.Profile) error {
|
|
return getDB().Save(profile).Error
|
|
}
|
|
|
|
// UpdateProfileFields 更新指定字段
|
|
func UpdateProfileFields(uuid string, updates map[string]interface{}) error {
|
|
return getDB().Model(&model.Profile{}).
|
|
Where("uuid = ?", uuid).
|
|
Updates(updates).Error
|
|
}
|
|
|
|
// DeleteProfile 删除档案
|
|
func DeleteProfile(uuid string) error {
|
|
return getDB().Where("uuid = ?", uuid).Delete(&model.Profile{}).Error
|
|
}
|
|
|
|
// CountProfilesByUserID 统计用户的档案数量
|
|
func CountProfilesByUserID(userID int64) (int64, error) {
|
|
var count int64
|
|
err := getDB().Model(&model.Profile{}).
|
|
Where("user_id = ?", userID).
|
|
Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
// SetActiveProfile 设置档案为活跃状态(同时将用户的其他档案设置为非活跃)
|
|
func SetActiveProfile(uuid string, userID int64) error {
|
|
return getDB().Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Model(&model.Profile{}).
|
|
Where("user_id = ?", userID).
|
|
Update("is_active", false).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.Model(&model.Profile{}).
|
|
Where("uuid = ? AND user_id = ?", uuid, userID).
|
|
Update("is_active", true).Error
|
|
})
|
|
}
|
|
|
|
// UpdateProfileLastUsedAt 更新最后使用时间
|
|
func UpdateProfileLastUsedAt(uuid string) error {
|
|
return getDB().Model(&model.Profile{}).
|
|
Where("uuid = ?", uuid).
|
|
Update("last_used_at", gorm.Expr("CURRENT_TIMESTAMP")).Error
|
|
}
|
|
|
|
// FindOneProfileByUserID 根据id找一个角色
|
|
func FindOneProfileByUserID(userID int64) (*model.Profile, error) {
|
|
profiles, err := FindProfilesByUserID(userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(profiles) == 0 {
|
|
return nil, errors.New("未找到角色")
|
|
}
|
|
return profiles[0], nil
|
|
}
|
|
|
|
func GetProfilesByNames(names []string) ([]*model.Profile, error) {
|
|
var profiles []*model.Profile
|
|
err := getDB().Where("name in (?)", names).Find(&profiles).Error
|
|
return profiles, err
|
|
}
|
|
|
|
func GetProfileKeyPair(profileId string) (*model.KeyPair, error) {
|
|
if profileId == "" {
|
|
return nil, errors.New("参数不能为空")
|
|
}
|
|
|
|
var profile model.Profile
|
|
result := getDB().WithContext(context.Background()).
|
|
Select("key_pair").
|
|
Where("id = ?", profileId).
|
|
First(&profile)
|
|
|
|
if result.Error != nil {
|
|
if IsNotFound(result.Error) {
|
|
return nil, errors.New("key pair未找到")
|
|
}
|
|
return nil, fmt.Errorf("获取key pair失败: %w", result.Error)
|
|
}
|
|
|
|
return &model.KeyPair{}, nil
|
|
}
|
|
|
|
func UpdateProfileKeyPair(profileId string, keyPair *model.KeyPair) error {
|
|
if profileId == "" {
|
|
return errors.New("profileId 不能为空")
|
|
}
|
|
if keyPair == nil {
|
|
return errors.New("keyPair 不能为 nil")
|
|
}
|
|
|
|
return getDB().Transaction(func(tx *gorm.DB) error {
|
|
result := tx.WithContext(context.Background()).
|
|
Table("profiles").
|
|
Where("id = ?", profileId).
|
|
UpdateColumns(map[string]interface{}{
|
|
"private_key": keyPair.PrivateKey,
|
|
"public_key": keyPair.PublicKey,
|
|
})
|
|
|
|
if result.Error != nil {
|
|
return fmt.Errorf("更新 keyPair 失败: %w", result.Error)
|
|
}
|
|
return nil
|
|
})
|
|
}
|