package repository import ( "context" "fmt" "git.huangwc.com/pig/pig-farm-controller/internal/infra/logs" "git.huangwc.com/pig/pig-farm-controller/internal/infra/models" "gorm.io/gorm" ) // AreaControllerRepository 定义了对 AreaController 模型的数据库操作接口 type AreaControllerRepository interface { FindByID(ctx context.Context, id uint) (*models.AreaController, error) FindByNetworkID(ctx context.Context, networkID string) (*models.AreaController, error) Create(ctx context.Context, ac *models.AreaController) error ListAll(ctx context.Context) ([]*models.AreaController, error) Update(ctx context.Context, ac *models.AreaController) error Delete(ctx context.Context, id uint) error } // gormAreaControllerRepository 是 AreaControllerRepository 的 GORM 实现。 type gormAreaControllerRepository struct { ctx context.Context db *gorm.DB } // NewGormAreaControllerRepository 创建一个新的 AreaControllerRepository GORM 实现实例。 func NewGormAreaControllerRepository(ctx context.Context, db *gorm.DB) AreaControllerRepository { return &gormAreaControllerRepository{ ctx: ctx, db: db, } } // Create 创建一个新的 AreaController 记录。 func (r *gormAreaControllerRepository) Create(ctx context.Context, ac *models.AreaController) error { repoCtx := logs.AddFuncName(ctx, r.ctx, "Create") return r.db.WithContext(repoCtx).Create(ac).Error } // ListAll 返回所有 AreaController 的列表。 func (r *gormAreaControllerRepository) ListAll(ctx context.Context) ([]*models.AreaController, error) { repoCtx := logs.AddFuncName(ctx, r.ctx, "ListAll") var areaControllers []*models.AreaController if err := r.db.WithContext(repoCtx).Find(&areaControllers).Error; err != nil { return nil, err } return areaControllers, nil } // Update 更新一个已存在的 AreaController 记录。 func (r *gormAreaControllerRepository) Update(ctx context.Context, ac *models.AreaController) error { repoCtx := logs.AddFuncName(ctx, r.ctx, "Update") return r.db.WithContext(repoCtx).Save(ac).Error } // Delete 删除一个 AreaController 记录。 func (r *gormAreaControllerRepository) Delete(ctx context.Context, id uint) error { repoCtx := logs.AddFuncName(ctx, r.ctx, "Delete") if err := r.db.WithContext(repoCtx).Delete(&models.AreaController{}, id).Error; err != nil { return fmt.Errorf("删除区域主控失败: %w", err) } return nil } // FindByID 通过 ID 查找一个 AreaController。 func (r *gormAreaControllerRepository) FindByID(ctx context.Context, id uint) (*models.AreaController, error) { repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByID") var areaController models.AreaController if err := r.db.WithContext(repoCtx).First(&areaController, id).Error; err != nil { return nil, err } return &areaController, nil } // FindByNetworkID 通过 NetworkID 查找一个 AreaController。 func (r *gormAreaControllerRepository) FindByNetworkID(ctx context.Context, networkID string) (*models.AreaController, error) { repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByNetworkID") var areaController models.AreaController if err := r.db.WithContext(repoCtx).Where("network_id = ?", networkID).First(&areaController).Error; err != nil { return nil, err } return &areaController, nil }