controller调整, 增加计划类型
This commit is contained in:
		| @@ -21,11 +21,25 @@ var ( | ||||
| 	ErrDeleteWithReferencedPlan = errors.New("禁止删除正在被引用的计划") | ||||
| ) | ||||
|  | ||||
| // PlanTypeFilter 定义计划类型的过滤器 | ||||
| type PlanTypeFilter string | ||||
|  | ||||
| const ( | ||||
| 	PlanTypeFilterAll    PlanTypeFilter = "all" | ||||
| 	PlanTypeFilterCustom PlanTypeFilter = "custom" | ||||
| 	PlanTypeFilterSystem PlanTypeFilter = "system" | ||||
| ) | ||||
|  | ||||
| // ListPlansOptions 定义了查询计划时的可选参数 | ||||
| type ListPlansOptions struct { | ||||
| 	PlanType PlanTypeFilter | ||||
| } | ||||
|  | ||||
| // PlanRepository 定义了与计划模型相关的数据库操作接口 | ||||
| // 这是为了让业务逻辑层依赖于抽象,而不是具体的数据库实现 | ||||
| type PlanRepository interface { | ||||
| 	// ListBasicPlans 获取所有计划的基本信息,不包含子计划和任务详情 | ||||
| 	ListBasicPlans() ([]models.Plan, error) | ||||
| 	// ListPlans 获取计划列表,支持过滤和分页 | ||||
| 	ListPlans(opts ListPlansOptions, page, pageSize int) ([]models.Plan, int64, error) | ||||
| 	// GetBasicPlanByID 根据ID获取计划的基本信息,不包含子计划和任务详情 | ||||
| 	GetBasicPlanByID(id uint) (*models.Plan, error) | ||||
| 	// GetPlanByID 根据ID获取计划,包含子计划和任务详情 | ||||
| @@ -81,15 +95,37 @@ func NewGormPlanRepository(db *gorm.DB) PlanRepository { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // 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 | ||||
| // ListPlans 获取计划列表,支持过滤和分页 | ||||
| func (r *gormPlanRepository) ListPlans(opts ListPlansOptions, page, pageSize int) ([]models.Plan, int64, error) { | ||||
| 	if page <= 0 || pageSize <= 0 { | ||||
| 		return nil, 0, ErrInvalidPagination | ||||
| 	} | ||||
| 	return plans, nil | ||||
|  | ||||
| 	var plans []models.Plan | ||||
| 	var total int64 | ||||
|  | ||||
| 	query := r.db.Model(&models.Plan{}) | ||||
|  | ||||
| 	switch opts.PlanType { | ||||
| 	case PlanTypeFilterCustom: | ||||
| 		query = query.Where("plan_type = ?", models.PlanTypeCustom) | ||||
| 	case PlanTypeFilterSystem: | ||||
| 		query = query.Where("plan_type = ?", models.PlanTypeSystem) | ||||
| 	case PlanTypeFilterAll: | ||||
| 		// 不添加 plan_type 的过滤条件 | ||||
| 	default: | ||||
| 		// 默认查询自定义 | ||||
| 		query = query.Where("plan_type = ?", models.PlanTypeCustom) | ||||
| 	} | ||||
|  | ||||
| 	if err := query.Count(&total).Error; err != nil { | ||||
| 		return nil, 0, err | ||||
| 	} | ||||
|  | ||||
| 	offset := (page - 1) * pageSize | ||||
| 	err := query.Limit(pageSize).Offset(offset).Order("id DESC").Find(&plans).Error | ||||
|  | ||||
| 	return plans, total, err | ||||
| } | ||||
|  | ||||
| // GetBasicPlanByID 根据ID获取计划的基本信息,不包含子计划和任务详情 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user