修改infra.repository包
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -19,48 +22,52 @@ type PendingCollectionListOptions struct {
|
||||
// PendingCollectionRepository 定义了与待采集请求相关的数据库操作接口。
|
||||
type PendingCollectionRepository interface {
|
||||
// Create 创建一个新的待采集请求。
|
||||
Create(req *models.PendingCollection) error
|
||||
Create(ctx context.Context, req *models.PendingCollection) error
|
||||
|
||||
// FindByCorrelationID 根据关联ID查找一个待采集请求。
|
||||
FindByCorrelationID(correlationID string) (*models.PendingCollection, error)
|
||||
FindByCorrelationID(ctx context.Context, correlationID string) (*models.PendingCollection, error)
|
||||
|
||||
// UpdateStatusToFulfilled 将指定关联ID的请求状态更新为“已完成”。
|
||||
UpdateStatusToFulfilled(correlationID string, fulfilledAt time.Time) error
|
||||
UpdateStatusToFulfilled(ctx context.Context, correlationID string, fulfilledAt time.Time) error
|
||||
|
||||
// MarkAllPendingAsTimedOut 将所有“待处理”请求更新为“已超时”。
|
||||
MarkAllPendingAsTimedOut() (int64, error)
|
||||
MarkAllPendingAsTimedOut(ctx context.Context) (int64, error)
|
||||
|
||||
// List 支持分页和过滤的列表查询
|
||||
List(opts PendingCollectionListOptions, page, pageSize int) ([]models.PendingCollection, int64, error)
|
||||
List(ctx context.Context, opts PendingCollectionListOptions, page, pageSize int) ([]models.PendingCollection, int64, error)
|
||||
}
|
||||
|
||||
// gormPendingCollectionRepository 是 PendingCollectionRepository 的 GORM 实现。
|
||||
type gormPendingCollectionRepository struct {
|
||||
db *gorm.DB
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGormPendingCollectionRepository 创建一个新的 PendingCollectionRepository GORM 实现实例。
|
||||
func NewGormPendingCollectionRepository(db *gorm.DB) PendingCollectionRepository {
|
||||
return &gormPendingCollectionRepository{db: db}
|
||||
func NewGormPendingCollectionRepository(ctx context.Context, db *gorm.DB) PendingCollectionRepository {
|
||||
return &gormPendingCollectionRepository{ctx: ctx, db: db}
|
||||
}
|
||||
|
||||
// Create 创建一个新的待采集请求。
|
||||
func (r *gormPendingCollectionRepository) Create(req *models.PendingCollection) error {
|
||||
return r.db.Create(req).Error
|
||||
func (r *gormPendingCollectionRepository) Create(ctx context.Context, req *models.PendingCollection) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "Create")
|
||||
return r.db.WithContext(repoCtx).Create(req).Error
|
||||
}
|
||||
|
||||
// FindByCorrelationID 根据关联ID查找一个待采集请求。
|
||||
func (r *gormPendingCollectionRepository) FindByCorrelationID(correlationID string) (*models.PendingCollection, error) {
|
||||
func (r *gormPendingCollectionRepository) FindByCorrelationID(ctx context.Context, correlationID string) (*models.PendingCollection, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByCorrelationID")
|
||||
var req models.PendingCollection
|
||||
if err := r.db.First(&req, "correlation_id = ?", correlationID).Error; err != nil {
|
||||
if err := r.db.WithContext(repoCtx).First(&req, "correlation_id = ?", correlationID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
// UpdateStatusToFulfilled 将指定关联ID的请求状态更新为“已完成”。
|
||||
func (r *gormPendingCollectionRepository) UpdateStatusToFulfilled(correlationID string, fulfilledAt time.Time) error {
|
||||
return r.db.Model(&models.PendingCollection{}).
|
||||
func (r *gormPendingCollectionRepository) UpdateStatusToFulfilled(ctx context.Context, correlationID string, fulfilledAt time.Time) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateStatusToFulfilled")
|
||||
return r.db.WithContext(repoCtx).Model(&models.PendingCollection{}).
|
||||
Where("correlation_id = ?", correlationID).
|
||||
Updates(map[string]interface{}{
|
||||
"status": models.PendingStatusFulfilled,
|
||||
@@ -70,8 +77,9 @@ func (r *gormPendingCollectionRepository) UpdateStatusToFulfilled(correlationID
|
||||
|
||||
// MarkAllPendingAsTimedOut 将所有状态为 'pending' 的记录更新为 'timed_out'。
|
||||
// 返回被更新的记录数量和错误。
|
||||
func (r *gormPendingCollectionRepository) MarkAllPendingAsTimedOut() (int64, error) {
|
||||
result := r.db.Model(&models.PendingCollection{}).
|
||||
func (r *gormPendingCollectionRepository) MarkAllPendingAsTimedOut(ctx context.Context) (int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "MarkAllPendingAsTimedOut")
|
||||
result := r.db.WithContext(repoCtx).Model(&models.PendingCollection{}).
|
||||
Where("status = ?", models.PendingStatusPending).
|
||||
Update("status", models.PendingStatusTimedOut)
|
||||
|
||||
@@ -79,7 +87,8 @@ func (r *gormPendingCollectionRepository) MarkAllPendingAsTimedOut() (int64, err
|
||||
}
|
||||
|
||||
// List 实现了分页和过滤查询待采集请求的功能
|
||||
func (r *gormPendingCollectionRepository) List(opts PendingCollectionListOptions, page, pageSize int) ([]models.PendingCollection, int64, error) {
|
||||
func (r *gormPendingCollectionRepository) List(ctx context.Context, opts PendingCollectionListOptions, page, pageSize int) ([]models.PendingCollection, int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "List")
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
@@ -87,7 +96,7 @@ func (r *gormPendingCollectionRepository) List(opts PendingCollectionListOptions
|
||||
var results []models.PendingCollection
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.PendingCollection{})
|
||||
query := r.db.WithContext(repoCtx).Model(&models.PendingCollection{})
|
||||
|
||||
if opts.DeviceID != nil {
|
||||
query = query.Where("device_id = ?", *opts.DeviceID)
|
||||
|
||||
Reference in New Issue
Block a user