46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package repository
|
||
|
||
import (
|
||
"context"
|
||
|
||
"github.com/topfans/backend/pkg/models"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// AppDownloadRepository 下载配置数据访问层
|
||
type AppDownloadRepository struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
// NewAppDownloadRepository 构造函数
|
||
func NewAppDownloadRepository(db *gorm.DB) *AppDownloadRepository {
|
||
return &AppDownloadRepository{db: db}
|
||
}
|
||
|
||
// FindByType 按包类型查询(用于公开接口,只返回 native_app)
|
||
func (r *AppDownloadRepository) FindByType(ctx context.Context, pkgType string) ([]models.AppDownloadConfig, error) {
|
||
var configs []models.AppDownloadConfig
|
||
err := r.db.WithContext(ctx).
|
||
Where("type = ?", pkgType).
|
||
Find(&configs).Error
|
||
return configs, err
|
||
}
|
||
|
||
// UpsertAll 批量 upsert(platform + type 唯一)
|
||
func (r *AppDownloadRepository) UpsertAll(ctx context.Context, configs []models.AppDownloadConfig) error {
|
||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||
for _, cfg := range configs {
|
||
if err := tx.Where("platform = ? AND type = ?", cfg.Platform, cfg.Type).
|
||
Assign(map[string]interface{}{
|
||
"download_url": cfg.DownloadURL,
|
||
"version": cfg.Version,
|
||
"updated_at": cfg.UpdatedAt,
|
||
}).
|
||
FirstOrCreate(&cfg).Error; err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return nil
|
||
})
|
||
}
|