fix
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"tyapi-server/internal/domains/product/entities"
|
||||
"tyapi-server/internal/domains/product/repositories"
|
||||
"tyapi-server/internal/shared/database"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
ProductDocumentationsTable = "product_documentations"
|
||||
)
|
||||
|
||||
type GormProductDocumentationRepository struct {
|
||||
*database.CachedBaseRepositoryImpl
|
||||
}
|
||||
|
||||
func (r *GormProductDocumentationRepository) Delete(ctx context.Context, id string) error {
|
||||
return r.DeleteEntity(ctx, id, &entities.ProductDocumentation{})
|
||||
}
|
||||
|
||||
var _ repositories.ProductDocumentationRepository = (*GormProductDocumentationRepository)(nil)
|
||||
|
||||
func NewGormProductDocumentationRepository(db *gorm.DB, logger *zap.Logger) repositories.ProductDocumentationRepository {
|
||||
return &GormProductDocumentationRepository{
|
||||
CachedBaseRepositoryImpl: database.NewCachedBaseRepositoryImpl(db, logger, ProductDocumentationsTable),
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建文档
|
||||
func (r *GormProductDocumentationRepository) Create(ctx context.Context, documentation *entities.ProductDocumentation) error {
|
||||
return r.CreateEntity(ctx, documentation)
|
||||
}
|
||||
|
||||
// Update 更新文档
|
||||
func (r *GormProductDocumentationRepository) Update(ctx context.Context, documentation *entities.ProductDocumentation) error {
|
||||
return r.UpdateEntity(ctx, documentation)
|
||||
}
|
||||
|
||||
// FindByID 根据ID查找文档
|
||||
func (r *GormProductDocumentationRepository) FindByID(ctx context.Context, id string) (*entities.ProductDocumentation, error) {
|
||||
var entity entities.ProductDocumentation
|
||||
err := r.SmartGetByID(ctx, id, &entity)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &entity, nil
|
||||
}
|
||||
|
||||
// FindByProductID 根据产品ID查找文档
|
||||
func (r *GormProductDocumentationRepository) FindByProductID(ctx context.Context, productID string) (*entities.ProductDocumentation, error) {
|
||||
var entity entities.ProductDocumentation
|
||||
err := r.GetDB(ctx).Where("product_id = ?", productID).First(&entity).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &entity, nil
|
||||
}
|
||||
|
||||
// FindByProductIDs 根据产品ID列表批量查找文档
|
||||
func (r *GormProductDocumentationRepository) FindByProductIDs(ctx context.Context, productIDs []string) ([]*entities.ProductDocumentation, error) {
|
||||
var documentations []entities.ProductDocumentation
|
||||
err := r.GetDB(ctx).Where("product_id IN ?", productIDs).Find(&documentations).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 转换为指针切片
|
||||
result := make([]*entities.ProductDocumentation, len(documentations))
|
||||
for i := range documentations {
|
||||
result[i] = &documentations[i]
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// UpdateBatch 批量更新文档
|
||||
func (r *GormProductDocumentationRepository) UpdateBatch(ctx context.Context, documentations []*entities.ProductDocumentation) error {
|
||||
if len(documentations) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 使用事务进行批量更新
|
||||
return r.GetDB(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
for _, doc := range documentations {
|
||||
if err := tx.Save(doc).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// CountByProductID 统计指定产品的文档数量
|
||||
func (r *GormProductDocumentationRepository) CountByProductID(ctx context.Context, productID string) (int64, error) {
|
||||
var count int64
|
||||
err := r.GetDB(ctx).Model(&entities.ProductDocumentation{}).Where("product_id = ?", productID).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
@@ -289,3 +289,26 @@ func (r *GormSubscriptionRepository) WithTx(tx interface{}) interfaces.Repositor
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// IncrementAPIUsageWithOptimisticLock 使用乐观锁增加API使用次数
|
||||
func (r *GormSubscriptionRepository) IncrementAPIUsageWithOptimisticLock(ctx context.Context, subscriptionID string, increment int64) error {
|
||||
// 使用原生SQL进行乐观锁更新
|
||||
result := r.GetDB(ctx).WithContext(ctx).Exec(`
|
||||
UPDATE subscription
|
||||
SET api_used = api_used + ?, version = version + 1, updated_at = NOW()
|
||||
WHERE id = ? AND version = (
|
||||
SELECT version FROM subscription WHERE id = ?
|
||||
)
|
||||
`, increment, subscriptionID, subscriptionID)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// 检查是否有行被更新
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user