Files
pig-farm-controller/internal/app/controller/plan/plan_controller.go
2025-09-14 16:52:59 +08:00

272 lines
12 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package plan
import (
"errors"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// --- 请求和响应 DTO 定义 ---
// CreatePlanRequest 定义创建计划请求的结构体
type CreatePlanRequest struct {
Name string `json:"name" binding:"required" example:"猪舍温度控制计划"`
Description string `json:"description" example:"根据温度自动调节风扇和加热器"`
ExecutionType models.PlanExecutionType `json:"execution_type" binding:"required" example:"automatic"`
CronExpression string `json:"cron_expression" example:"0 0 6 * * *"`
ContentType models.PlanContentType `json:"content_type" binding:"required" example:"tasks"`
SubPlanIDs []uint `json:"sub_plan_ids,omitempty"`
Tasks []TaskRequest `json:"tasks,omitempty"`
}
// PlanResponse 定义计划详情响应的结构体
type PlanResponse struct {
ID uint `json:"id" example:"1"`
Name string `json:"name" example:"猪舍温度控制计划"`
Description string `json:"description" example:"根据温度自动调节风扇和加热器"`
ExecutionType models.PlanExecutionType `json:"execution_type" example:"automatic"`
CronExpression string `json:"cron_expression" example:"0 0 6 * * *"`
ContentType models.PlanContentType `json:"content_type" example:"tasks"`
SubPlans []SubPlanResponse `json:"sub_plans,omitempty"`
Tasks []TaskResponse `json:"tasks,omitempty"`
}
// ListPlansResponse 定义获取计划列表响应的结构体
type ListPlansResponse struct {
Plans []PlanResponse `json:"plans"`
Total int `json:"total" example:"100"`
}
// UpdatePlanRequest 定义更新计划请求的结构体
type UpdatePlanRequest struct {
Name string `json:"name" example:"猪舍温度控制计划V2"`
Description string `json:"description" example:"更新后的描述"`
ExecutionType models.PlanExecutionType `json:"execution_type" example:"automatic"`
CronExpression string `json:"cron_expression" example:"0 0 6 * * *"`
ContentType models.PlanContentType `json:"content_type" example:"tasks"`
SubPlanIDs []uint `json:"sub_plan_ids,omitempty"`
Tasks []TaskRequest `json:"tasks,omitempty"`
}
// SubPlanResponse 定义子计划响应结构体
type SubPlanResponse struct {
ID uint `json:"id" example:"1"`
ParentPlanID uint `json:"parent_plan_id" example:"1"`
ChildPlanID uint `json:"child_plan_id" example:"2"`
ExecutionOrder int `json:"execution_order" example:"1"`
ChildPlan *PlanResponse `json:"child_plan,omitempty"`
}
// TaskRequest 定义任务请求结构体
type TaskRequest struct {
Name string `json:"name" example:"打开风扇"`
Description string `json:"description" example:"打开1号风扇"`
ExecutionOrder int `json:"execution_order" example:"1"`
Type models.TaskType `json:"type" example:"waiting"`
Parameters controller.Properties `json:"parameters,omitempty"`
}
// TaskResponse 定义任务响应结构体
type TaskResponse struct {
ID uint `json:"id" example:"1"`
PlanID uint `json:"plan_id" example:"1"`
Name string `json:"name" example:"打开风扇"`
Description string `json:"description" example:"打开1号风扇"`
ExecutionOrder int `json:"execution_order" example:"1"`
Type models.TaskType `json:"type" example:"waiting"`
Parameters controller.Properties `json:"parameters,omitempty"`
}
// --- Controller 定义 ---
// Controller 定义了计划相关的控制器
type Controller struct {
logger *logs.Logger
planRepo repository.PlanRepository
}
// NewController 创建一个新的 Controller 实例
func NewController(logger *logs.Logger, planRepo repository.PlanRepository) *Controller {
return &Controller{
logger: logger,
planRepo: planRepo,
}
}
// --- 接口方法实现 ---
// CreatePlan godoc
// @Summary 创建计划
// @Description 创建一个新的计划,包括其基本信息和所有关联的子计划/任务。
// @Tags 计划管理
// @Accept json
// @Produce json
// @Param plan body CreatePlanRequest true "计划信息"
// @Success 200 {object} controller.Response{data=plan.PlanResponse} "业务码为201代表创建成功"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 500"
// @Router /plans [post]
func (c *Controller) CreatePlan(ctx *gin.Context) {
var req CreatePlanRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
controller.SendErrorResponse(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error())
return
}
// 使用已有的转换函数,它已经包含了验证和重排逻辑
planToCreate, err := PlanFromCreateRequest(&req)
if err != nil {
controller.SendErrorResponse(ctx, controller.CodeBadRequest, "计划数据校验失败: "+err.Error())
return
}
// 调用仓库方法创建计划
if err := c.planRepo.CreatePlan(planToCreate); err != nil {
controller.SendErrorResponse(ctx, controller.CodeBadRequest, "创建计划失败: "+err.Error())
return
}
// 使用已有的转换函数将创建后的模型转换为响应对象
resp := PlanToResponse(planToCreate)
// 使用统一的成功响应函数
controller.SendResponse(ctx, controller.CodeCreated, "计划创建成功", resp)
}
// GetPlan godoc
// @Summary 获取计划详情
// @Description 根据计划ID获取单个计划的详细信息。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response{data=plan.PlanResponse} "业务码为200代表成功获取"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id} [get]
func (c *Controller) GetPlan(ctx *gin.Context) {
// 1. 从 URL 路径中获取 ID
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
controller.SendErrorResponse(ctx, controller.CodeBadRequest, "无效的计划ID格式")
return
}
// 2. 调用仓库层获取计划详情
plan, err := c.planRepo.GetPlanByID(uint(id))
if err != nil {
// 判断是否为“未找到”错误
if errors.Is(err, gorm.ErrRecordNotFound) {
controller.SendErrorResponse(ctx, controller.CodeNotFound, "计划不存在")
return
}
// 其他数据库错误视为内部错误
c.logger.Errorf("获取计划详情失败: %v", err)
controller.SendErrorResponse(ctx, controller.CodeInternalError, "获取计划详情时发生内部错误")
return
}
// 3. 将模型转换为响应 DTO
resp := PlanToResponse(plan)
// 4. 发送成功响应
controller.SendResponse(ctx, controller.CodeSuccess, "获取计划详情成功", resp)
}
// ListPlans godoc
// @Summary 获取计划列表
// @Description 获取所有计划的列表
// @Tags 计划管理
// @Produce json
// @Success 200 {object} controller.Response{data=plan.ListPlansResponse} "业务码为200代表成功获取列表"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 500"
// @Router /plans [get]
func (c *Controller) ListPlans(ctx *gin.Context) {
// 1. 调用仓库层获取所有计划
plans, err := c.planRepo.ListBasicPlans()
if err != nil {
c.logger.Errorf("获取计划列表失败: %v", err)
controller.SendErrorResponse(ctx, controller.CodeInternalError, "获取计划列表时发生内部错误")
return
}
// 2. 将模型转换为响应 DTO
planResponses := make([]PlanResponse, 0, len(plans))
for _, p := range plans {
planResponses = append(planResponses, *PlanToResponse(&p))
}
// 3. 构造并发送成功响应
resp := ListPlansResponse{
Plans: planResponses,
Total: len(planResponses),
}
controller.SendResponse(ctx, controller.CodeSuccess, "获取计划列表成功", resp)
}
// UpdatePlan godoc
// @Summary 更新计划
// @Description 根据计划ID更新计划的详细信息。
// @Tags 计划管理
// @Accept json
// @Produce json
// @Param id path int true "计划ID"
// @Param plan body UpdatePlanRequest true "更新后的计划信息"
// @Success 200 {object} controller.Response{data=plan.PlanResponse} "业务码为200代表更新成功"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id} [put]
func (c *Controller) UpdatePlan(ctx *gin.Context) {
// 占位符:此处应调用服务层或仓库层来更新计划
c.logger.Infof("收到更新计划请求 (占位符)")
controller.SendResponse(ctx, controller.CodeSuccess, "更新计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
}
// DeletePlan godoc
// @Summary 删除计划
// @Description 根据计划ID删除计划。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id} [delete]
func (c *Controller) DeletePlan(ctx *gin.Context) {
// 占位符:此处应调用服务层或仓库层来删除计划
c.logger.Infof("收到删除计划请求 (占位符)")
controller.SendResponse(ctx, controller.CodeSuccess, "删除计划接口占位符", nil)
}
// StartPlan godoc
// @Summary 启动计划
// @Description 根据计划ID启动一个计划的执行。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表成功启动计划"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id}/start [post]
func (c *Controller) StartPlan(ctx *gin.Context) {
// 占位符:此处应调用服务层或仓库层来启动计划
c.logger.Infof("收到启动计划请求 (占位符)")
controller.SendResponse(ctx, controller.CodeSuccess, "启动计划接口占位符", nil)
}
// StopPlan godoc
// @Summary 停止计划
// @Description 根据计划ID停止一个正在执行的计划。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表成功停止计划"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id}/stop [post]
func (c *Controller) StopPlan(ctx *gin.Context) {
// 占位符:此处应调用服务层或仓库层来停止计划
c.logger.Infof("收到停止计划请求 (占位符)")
controller.SendResponse(ctx, controller.CodeSuccess, "停止计划接口占位符", nil)
}