86 lines
2.6 KiB
Go
86 lines
2.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"github.com/topfans/backend/pkg/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CastloveConfigRepository 铸爱工艺配置 Repository 接口
|
|
type CastloveConfigRepository interface {
|
|
// ListActiveCategories 读取所有 is_active=true 且未软删的分类
|
|
// 返回按 sort_order ASC, id ASC 排序
|
|
ListActiveCategories() ([]*models.CastloveCategory, error)
|
|
|
|
// ListActiveCrafts 读取所有 is_active=true 且未软删的卡片
|
|
// 返回按 sort_order ASC, id ASC 排序
|
|
ListActiveCrafts() ([]*models.CastloveCraft, error)
|
|
|
|
// MaxUpdatedAt 返回两表所有相关记录 updated_at 最大值(毫秒)
|
|
// 用于构造 version 字段(便于调试/条件请求)
|
|
MaxUpdatedAt() (int64, error)
|
|
}
|
|
|
|
type castloveConfigRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewCastloveConfigRepository 创建铸爱工艺配置 Repository 实例
|
|
func NewCastloveConfigRepository(db *gorm.DB) CastloveConfigRepository {
|
|
return &castloveConfigRepository{db: db}
|
|
}
|
|
|
|
// ListActiveCategories 读取所有 is_active=true 且未软删的分类
|
|
func (r *castloveConfigRepository) ListActiveCategories() ([]*models.CastloveCategory, error) {
|
|
var categories []*models.CastloveCategory
|
|
err := r.db.
|
|
Where("is_active = ? AND deleted_at IS NULL", true).
|
|
Order("sort_order ASC, id ASC").
|
|
Find(&categories).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return categories, nil
|
|
}
|
|
|
|
// ListActiveCrafts 读取所有 is_active=true 且未软删的卡片
|
|
func (r *castloveConfigRepository) ListActiveCrafts() ([]*models.CastloveCraft, error) {
|
|
var crafts []*models.CastloveCraft
|
|
err := r.db.
|
|
Where("is_active = ? AND deleted_at IS NULL", true).
|
|
Order("sort_order ASC, id ASC").
|
|
Find(&crafts).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return crafts, nil
|
|
}
|
|
|
|
// MaxUpdatedAt 返回两表所有相关记录 updated_at 最大值(毫秒)
|
|
// category_max = MAX(updated_at) FROM castlove_categories WHERE is_active AND !deleted
|
|
// craft_max = MAX(updated_at) FROM castlove_crafts WHERE is_active AND !deleted
|
|
// 返回两者中的较大值
|
|
func (r *castloveConfigRepository) MaxUpdatedAt() (int64, error) {
|
|
var catMax, craftMax int64
|
|
|
|
if err := r.db.
|
|
Model(&models.CastloveCategory{}).
|
|
Select("COALESCE(MAX(updated_at), 0)").
|
|
Where("is_active = ? AND deleted_at IS NULL", true).
|
|
Scan(&catMax).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if err := r.db.
|
|
Model(&models.CastloveCraft{}).
|
|
Select("COALESCE(MAX(updated_at), 0)").
|
|
Where("is_active = ? AND deleted_at IS NULL", true).
|
|
Scan(&craftMax).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if catMax > craftMax {
|
|
return catMax, nil
|
|
}
|
|
return craftMax, nil
|
|
}
|