50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
|
|
package repository
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
|||
|
|
"gorm.io/gorm"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// PlanRepository 定义了与计划模型相关的数据库操作接口
|
|||
|
|
// 这是为了让业务逻辑层依赖于抽象,而不是具体的数据库实现
|
|||
|
|
type PlanRepository interface {
|
|||
|
|
// ListBasicPlans 获取所有计划的基本信息,不包含子计划和任务详情
|
|||
|
|
ListBasicPlans() ([]models.Plan, error)
|
|||
|
|
// GetBasicPlanByID 根据ID获取计划的基本信息,不包含子计划和任务详情
|
|||
|
|
GetBasicPlanByID(id uint) (*models.Plan, error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// gormPlanRepository 是 PlanRepository 的 GORM 实现
|
|||
|
|
type gormPlanRepository struct {
|
|||
|
|
db *gorm.DB
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// NewGormPlanRepository 创建一个新的 PlanRepository GORM 实现实例
|
|||
|
|
func NewGormPlanRepository(db *gorm.DB) PlanRepository {
|
|||
|
|
return &gormPlanRepository{
|
|||
|
|
db: db,
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ListBasicPlans 获取所有计划的基本信息,不包含子计划和任务详情
|
|||
|
|
func (r *gormPlanRepository) ListBasicPlans() ([]models.Plan, error) {
|
|||
|
|
var plans []models.Plan
|
|||
|
|
// GORM 默认不会加载关联,除非使用 Preload,所以直接 Find 即可满足要求
|
|||
|
|
result := r.db.Find(&plans)
|
|||
|
|
if result.Error != nil {
|
|||
|
|
return nil, result.Error
|
|||
|
|
}
|
|||
|
|
return plans, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetBasicPlanByID 根据ID获取计划的基本信息,不包含子计划和任务详情
|
|||
|
|
func (r *gormPlanRepository) GetBasicPlanByID(id uint) (*models.Plan, error) {
|
|||
|
|
var plan models.Plan
|
|||
|
|
// GORM 默认不会加载关联,除非使用 Preload,所以直接 First 即可满足要求
|
|||
|
|
result := r.db.First(&plan, id)
|
|||
|
|
if result.Error != nil {
|
|||
|
|
return nil, result.Error
|
|||
|
|
}
|
|||
|
|
return &plan, nil
|
|||
|
|
}
|