修改infra.repository包

This commit is contained in:
2025-11-05 23:00:07 +08:00
parent 97aea66f7c
commit 10b123ab93
25 changed files with 877 additions and 608 deletions

View File

@@ -1,43 +1,49 @@
package repository
import (
"context"
"errors"
"fmt"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"gorm.io/gorm"
)
// DeviceTemplateRepository 定义了设备模板数据访问的接口
type DeviceTemplateRepository interface {
Create(deviceTemplate *models.DeviceTemplate) error
FindByID(id uint) (*models.DeviceTemplate, error)
FindByName(name string) (*models.DeviceTemplate, error)
ListAll() ([]*models.DeviceTemplate, error)
Update(deviceTemplate *models.DeviceTemplate) error
Delete(id uint) error
IsInUse(id uint) (bool, error)
Create(ctx context.Context, deviceTemplate *models.DeviceTemplate) error
FindByID(ctx context.Context, id uint) (*models.DeviceTemplate, error)
FindByName(ctx context.Context, name string) (*models.DeviceTemplate, error)
ListAll(ctx context.Context) ([]*models.DeviceTemplate, error)
Update(ctx context.Context, deviceTemplate *models.DeviceTemplate) error
Delete(ctx context.Context, id uint) error
IsInUse(ctx context.Context, id uint) (bool, error)
}
// gormDeviceTemplateRepository 是 DeviceTemplateRepository 的 GORM 实现
type gormDeviceTemplateRepository struct {
db *gorm.DB
ctx context.Context
db *gorm.DB
}
// NewGormDeviceTemplateRepository 创建一个新的 gormDeviceTemplateRepository 实例
func NewGormDeviceTemplateRepository(db *gorm.DB) DeviceTemplateRepository {
return &gormDeviceTemplateRepository{db: db}
func NewGormDeviceTemplateRepository(ctx context.Context, db *gorm.DB) DeviceTemplateRepository {
return &gormDeviceTemplateRepository{ctx: ctx, db: db}
}
// Create 在数据库中创建一个新的设备模板
func (r *gormDeviceTemplateRepository) Create(deviceTemplate *models.DeviceTemplate) error {
return r.db.Create(deviceTemplate).Error
func (r *gormDeviceTemplateRepository) Create(ctx context.Context, deviceTemplate *models.DeviceTemplate) error {
repoCtx := logs.AddFuncName(ctx, r.ctx, "Create")
return r.db.WithContext(repoCtx).Create(deviceTemplate).Error
}
// FindByID 根据ID查找设备模板
func (r *gormDeviceTemplateRepository) FindByID(id uint) (*models.DeviceTemplate, error) {
func (r *gormDeviceTemplateRepository) FindByID(ctx context.Context, id uint) (*models.DeviceTemplate, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByID")
var deviceTemplate models.DeviceTemplate
if err := r.db.First(&deviceTemplate, id).Error; err != nil {
if err := r.db.WithContext(repoCtx).First(&deviceTemplate, id).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf("设备模板未找到: %w", err)
}
@@ -47,9 +53,10 @@ func (r *gormDeviceTemplateRepository) FindByID(id uint) (*models.DeviceTemplate
}
// FindByName 根据名称查找设备模板
func (r *gormDeviceTemplateRepository) FindByName(name string) (*models.DeviceTemplate, error) {
func (r *gormDeviceTemplateRepository) FindByName(ctx context.Context, name string) (*models.DeviceTemplate, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByName")
var deviceTemplate models.DeviceTemplate
if err := r.db.Where("name = ?", name).First(&deviceTemplate).Error; err != nil {
if err := r.db.WithContext(repoCtx).Where("name = ?", name).First(&deviceTemplate).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf("设备模板未找到: %w", err)
}
@@ -59,31 +66,35 @@ func (r *gormDeviceTemplateRepository) FindByName(name string) (*models.DeviceTe
}
// ListAll 获取所有设备模板
func (r *gormDeviceTemplateRepository) ListAll() ([]*models.DeviceTemplate, error) {
func (r *gormDeviceTemplateRepository) ListAll(ctx context.Context) ([]*models.DeviceTemplate, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListAll")
var deviceTemplates []*models.DeviceTemplate
if err := r.db.Find(&deviceTemplates).Error; err != nil {
if err := r.db.WithContext(repoCtx).Find(&deviceTemplates).Error; err != nil {
return nil, fmt.Errorf("获取设备模板列表失败: %w", err)
}
return deviceTemplates, nil
}
// Update 更新数据库中的设备模板
func (r *gormDeviceTemplateRepository) Update(deviceTemplate *models.DeviceTemplate) error {
return r.db.Save(deviceTemplate).Error
func (r *gormDeviceTemplateRepository) Update(ctx context.Context, deviceTemplate *models.DeviceTemplate) error {
repoCtx := logs.AddFuncName(ctx, r.ctx, "Update")
return r.db.WithContext(repoCtx).Save(deviceTemplate).Error
}
// IsInUse 检查设备模板是否正在被设备使用
func (r *gormDeviceTemplateRepository) IsInUse(id uint) (bool, error) {
func (r *gormDeviceTemplateRepository) IsInUse(ctx context.Context, id uint) (bool, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "IsInUse")
var count int64
if err := r.db.Model(&models.Device{}).Where("device_template_id = ?", id).Count(&count).Error; err != nil {
if err := r.db.WithContext(repoCtx).Model(&models.Device{}).Where("device_template_id = ?", id).Count(&count).Error; err != nil {
return false, fmt.Errorf("检查设备模板使用情况失败: %w", err)
}
return count > 0, nil
}
// Delete 软删除数据库中的设备模板
func (r *gormDeviceTemplateRepository) Delete(id uint) error {
if err := r.db.Delete(&models.DeviceTemplate{}, id).Error; err != nil {
func (r *gormDeviceTemplateRepository) Delete(ctx context.Context, id uint) error {
repoCtx := logs.AddFuncName(ctx, r.ctx, "Delete")
if err := r.db.WithContext(repoCtx).Delete(&models.DeviceTemplate{}, id).Error; err != nil {
return fmt.Errorf("删除设备模板失败: %w", err)
}
return nil