112 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			112 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package repository
 | |
| 
 | |
| import (
 | |
| 	"time"
 | |
| 
 | |
| 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
 | |
| 	"gorm.io/gorm"
 | |
| )
 | |
| 
 | |
| // PigBatchLogListOptions 定义了查询猪批次日志时的可选参数
 | |
| type PigBatchLogListOptions struct {
 | |
| 	PigBatchID *uint
 | |
| 	ChangeType *models.LogChangeType
 | |
| 	OperatorID *uint
 | |
| 	StartTime  *time.Time // 基于 happened_at 字段
 | |
| 	EndTime    *time.Time // 基于 happened_at 字段
 | |
| 	OrderBy    string     // 例如 "happened_at asc"
 | |
| }
 | |
| 
 | |
| // PigBatchLogRepository 定义了与猪批次日志相关的数据库操作接口。
 | |
| type PigBatchLogRepository interface {
 | |
| 	// CreateTx 在指定的事务中创建一条新的猪批次日志。
 | |
| 	CreateTx(tx *gorm.DB, log *models.PigBatchLog) error
 | |
| 
 | |
| 	// GetLogsByBatchIDAndDateRangeTx 在指定的事务中,获取指定批次在特定时间范围内的所有日志记录。
 | |
| 	GetLogsByBatchIDAndDateRangeTx(tx *gorm.DB, batchID uint, startDate, endDate time.Time) ([]*models.PigBatchLog, error)
 | |
| 
 | |
| 	// GetLastLogByBatchIDTx 在指定的事务中,获取某批次的最后一条日志记录。
 | |
| 	GetLastLogByBatchIDTx(tx *gorm.DB, batchID uint) (*models.PigBatchLog, error)
 | |
| 
 | |
| 	// List 支持分页和过滤的列表查询
 | |
| 	List(opts PigBatchLogListOptions, page, pageSize int) ([]models.PigBatchLog, int64, error)
 | |
| }
 | |
| 
 | |
| // gormPigBatchLogRepository 是 PigBatchLogRepository 的 GORM 实现。
 | |
| type gormPigBatchLogRepository struct {
 | |
| 	db *gorm.DB
 | |
| }
 | |
| 
 | |
| // NewGormPigBatchLogRepository 创建一个新的 PigBatchLogRepository 实例。
 | |
| func NewGormPigBatchLogRepository(db *gorm.DB) PigBatchLogRepository {
 | |
| 	return &gormPigBatchLogRepository{db: db}
 | |
| }
 | |
| 
 | |
| // CreateTx 实现了在事务中创建猪批次日志的逻辑。
 | |
| func (r *gormPigBatchLogRepository) CreateTx(tx *gorm.DB, log *models.PigBatchLog) error {
 | |
| 	return tx.Create(log).Error
 | |
| }
 | |
| 
 | |
| // GetLogsByBatchIDAndDateRangeTx 实现了在指定的事务中,获取指定批次在特定时间范围内的所有日志记录的逻辑。
 | |
| func (r *gormPigBatchLogRepository) GetLogsByBatchIDAndDateRangeTx(tx *gorm.DB, batchID uint, startDate, endDate time.Time) ([]*models.PigBatchLog, error) {
 | |
| 	var logs []*models.PigBatchLog
 | |
| 	err := tx.Where("pig_batch_id = ? AND created_at >= ? AND created_at <= ?", batchID, startDate, endDate).Find(&logs).Error
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return logs, nil
 | |
| }
 | |
| 
 | |
| // GetLastLogByBatchIDTx 实现了在指定的事务中,获取某批次的最后一条日志记录的逻辑。
 | |
| func (r *gormPigBatchLogRepository) GetLastLogByBatchIDTx(tx *gorm.DB, batchID uint) (*models.PigBatchLog, error) {
 | |
| 	var log models.PigBatchLog
 | |
| 	err := tx.Where("pig_batch_id = ?", batchID).Order("id DESC").First(&log).Error
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return &log, nil
 | |
| }
 | |
| 
 | |
| // List 实现了分页和过滤查询猪批次日志的功能
 | |
| func (r *gormPigBatchLogRepository) List(opts PigBatchLogListOptions, page, pageSize int) ([]models.PigBatchLog, int64, error) {
 | |
| 	if page <= 0 || pageSize <= 0 {
 | |
| 		return nil, 0, ErrInvalidPagination
 | |
| 	}
 | |
| 
 | |
| 	var results []models.PigBatchLog
 | |
| 	var total int64
 | |
| 
 | |
| 	query := r.db.Model(&models.PigBatchLog{})
 | |
| 
 | |
| 	if opts.PigBatchID != nil {
 | |
| 		query = query.Where("pig_batch_id = ?", *opts.PigBatchID)
 | |
| 	}
 | |
| 	if opts.ChangeType != nil {
 | |
| 		query = query.Where("change_type = ?", *opts.ChangeType)
 | |
| 	}
 | |
| 	if opts.OperatorID != nil {
 | |
| 		query = query.Where("operator_id = ?", *opts.OperatorID)
 | |
| 	}
 | |
| 	if opts.StartTime != nil {
 | |
| 		query = query.Where("happened_at >= ?", *opts.StartTime)
 | |
| 	}
 | |
| 	if opts.EndTime != nil {
 | |
| 		query = query.Where("happened_at <= ?", *opts.EndTime)
 | |
| 	}
 | |
| 
 | |
| 	if err := query.Count(&total).Error; err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 
 | |
| 	orderBy := "happened_at DESC"
 | |
| 	if opts.OrderBy != "" {
 | |
| 		orderBy = opts.OrderBy
 | |
| 	}
 | |
| 	query = query.Order(orderBy)
 | |
| 
 | |
| 	offset := (page - 1) * pageSize
 | |
| 	err := query.Limit(pageSize).Offset(offset).Find(&results).Error
 | |
| 
 | |
| 	return results, total, err
 | |
| }
 |