- settlement(1.1): exhibition_revenue_records 加 UNIQUE(exhibition_id,cycle_start_time) + CreateRevenueRecord ON CONFLICT DO NOTHING; MQ 用 settled_at 替代复用 is_processed; 恢复扫描器过滤改 settled_at IS NULL; created_at 统一毫秒; 删死代码 cleanup_worker.go。 - hours(1.2): 新增 exhibition_hours_log/asset_exhibition_hours_log(source_id 唯一)幂等表; fan_profile/assetLevel 的 AddExhibitionHours 按 sourceID 幂等(事务包裹); 存量重算脚本。 - mint(1.4/1.5/1.6): crystal_transaction_records (source_id,change_type) 部分唯一索引 + UpdateCrystalBalance/CreateMintOrder 幂等; 保底改 crypto/rand; 下线伪 tx_hash; doMint Redis Lua 原子限流。 - migrations 001/002/003; 各服务单测(自包含, 缺 DB t.Skip)。 Co-Authored-By: Claude <noreply@anthropic.com>
84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"github.com/topfans/backend/pkg/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type AssetLevelRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewAssetLevelRepository(db *gorm.DB) *AssetLevelRepository {
|
|
return &AssetLevelRepository{db: db}
|
|
}
|
|
|
|
func (r *AssetLevelRepository) Create(record *models.AssetLevelRecord) error {
|
|
return r.db.Create(record).Error
|
|
}
|
|
|
|
func (r *AssetLevelRepository) Save(record *models.AssetLevelRecord) error {
|
|
return r.db.Save(record).Error
|
|
}
|
|
|
|
func (r *AssetLevelRepository) GetByAssetID(assetID int64) (*models.AssetLevelRecord, error) {
|
|
var record models.AssetLevelRecord
|
|
err := r.db.Where("asset_id = ?", assetID).First(&record).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &record, nil
|
|
}
|
|
|
|
func (r *AssetLevelRepository) GetBySeason(seasonID string) ([]*models.AssetLevelRecord, error) {
|
|
var records []*models.AssetLevelRecord
|
|
err := r.db.Where("season_id = ?", seasonID).Find(&records).Error
|
|
return records, err
|
|
}
|
|
|
|
func (r *AssetLevelRepository) GetAllLevels() ([]*models.AssetLevel, error) {
|
|
var levels []*models.AssetLevel
|
|
err := r.db.Order("level_order ASC").Find(&levels).Error
|
|
return levels, err
|
|
}
|
|
|
|
func (r *AssetLevelRepository) GetLevelConfig(level string) (*models.AssetLevel, error) {
|
|
var config models.AssetLevel
|
|
err := r.db.Where("level = ?", level).First(&config).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &config, nil
|
|
}
|
|
|
|
func (r *AssetLevelRepository) CreateChangeLog(log *models.AssetLevelChangeLog) error {
|
|
return r.db.Create(log).Error
|
|
}
|
|
|
|
func (r *AssetLevelRepository) GetDB() *gorm.DB {
|
|
return r.db
|
|
}
|
|
|
|
// WithTx 返回绑定到 tx 的 repo,供 service 层在 db.Transaction(...) 闭包内调用,
|
|
//让 Create/Save/GetByAssetID 等方法走事务(否则默认走 r.db,事务边界不生效)。
|
|
//
|
|
// 用法:
|
|
// err := r.db.Transaction(func(tx *gorm.DB) error {
|
|
// txRepo := r.WithTx(tx)
|
|
// _ = txRepo.GetByAssetID(...)
|
|
// _ = txRepo.Save(...)
|
|
// return nil
|
|
// })
|
|
func (r *AssetLevelRepository) WithTx(tx *gorm.DB) *AssetLevelRepository {
|
|
return &AssetLevelRepository{db: tx}
|
|
}
|
|
|
|
func (r *AssetLevelRepository) GetChangeLogs(assetID int64, limit, offset int) ([]*models.AssetLevelChangeLog, error) {
|
|
var logs []*models.AssetLevelChangeLog
|
|
err := r.db.Where("asset_id = ?", assetID).
|
|
Order("created_at DESC").
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&logs).Error
|
|
return logs, err
|
|
} |