Files
pig-farm-controller/internal/app/controller/plan/plan_controller.go

468 lines
21 KiB
Go
Raw Normal View History

2025-09-13 14:09:22 +08:00
package plan
import (
2025-09-14 16:19:09 +08:00
"errors"
"strconv"
2025-09-13 14:09:22 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
2025-10-03 23:02:43 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
2025-10-02 00:18:13 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/domain/task"
2025-09-13 14:09:22 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
2025-09-14 14:00:42 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
2025-09-13 14:09:22 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
"github.com/gin-gonic/gin"
2025-09-14 16:19:09 +08:00
"gorm.io/gorm"
2025-09-13 14:09:22 +08:00
)
// --- Controller 定义 ---
// Controller 定义了计划相关的控制器
type Controller struct {
logger *logs.Logger
planRepo repository.PlanRepository
analysisPlanTaskManager *task.AnalysisPlanTaskManager
2025-09-13 14:09:22 +08:00
}
// NewController 创建一个新的 Controller 实例
func NewController(logger *logs.Logger, planRepo repository.PlanRepository, analysisPlanTaskManager *task.AnalysisPlanTaskManager) *Controller {
2025-09-13 14:09:22 +08:00
return &Controller{
logger: logger,
planRepo: planRepo,
analysisPlanTaskManager: analysisPlanTaskManager,
2025-09-13 14:09:22 +08:00
}
}
// --- 接口方法实现 ---
// CreatePlan godoc
// @Summary 创建计划
// @Description 创建一个新的计划,包括其基本信息和所有关联的子计划/任务。
// @Tags 计划管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-13 14:09:22 +08:00
// @Accept json
// @Produce json
2025-10-03 23:02:43 +08:00
// @Param plan body dto.CreatePlanRequest true "计划信息"
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为201代表创建成功"
2025-09-19 15:55:56 +08:00
// @Router /api/v1/plans [post]
2025-09-14 15:56:55 +08:00
func (c *Controller) CreatePlan(ctx *gin.Context) {
2025-10-03 23:02:43 +08:00
var req dto.CreatePlanRequest
2025-09-28 01:28:54 +08:00
const actionType = "创建计划"
2025-09-14 15:56:55 +08:00
if err := ctx.ShouldBindJSON(&req); err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
2025-09-14 15:56:55 +08:00
return
}
// 使用已有的转换函数,它已经包含了验证和重排逻辑
2025-10-03 23:02:43 +08:00
planToCreate, err := dto.NewPlanFromCreateRequest(&req)
2025-09-14 15:56:55 +08:00
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 计划数据校验失败: %v", actionType, err)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "计划数据校验失败: "+err.Error(), actionType, "计划数据校验失败", req)
2025-09-14 15:56:55 +08:00
return
}
2025-09-23 18:11:21 +08:00
// --- 自动判断 ContentType ---
if len(req.SubPlanIDs) > 0 {
planToCreate.ContentType = models.PlanContentTypeSubPlans
} else {
// 如果 SubPlanIDs 未提供,则默认为 Tasks 类型(即使 Tasks 字段也未提供)
planToCreate.ContentType = models.PlanContentTypeTasks
}
2025-09-14 15:56:55 +08:00
// 调用仓库方法创建计划
if err := c.planRepo.CreatePlan(planToCreate); err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 数据库创建计划失败: %v", actionType, err)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建计划失败: "+err.Error(), actionType, "数据库创建计划失败", planToCreate)
2025-09-14 15:56:55 +08:00
return
}
2025-09-20 22:32:00 +08:00
// 创建成功后,调用 manager 确保触发器任务定义存在,但不立即加入待执行队列
if err := c.analysisPlanTaskManager.EnsureAnalysisTaskDefinition(planToCreate.ID); err != nil {
// 这是一个非阻塞性错误,我们只记录日志,因为主流程(创建计划)已经成功
2025-09-20 22:32:00 +08:00
c.logger.Errorf("为新创建的计划 %d 确保触发器任务定义失败: %v", planToCreate.ID, err)
}
2025-09-14 15:56:55 +08:00
// 使用已有的转换函数将创建后的模型转换为响应对象
2025-10-03 23:02:43 +08:00
resp, err := dto.NewPlanToResponse(planToCreate)
2025-09-20 17:11:04 +08:00
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 序列化响应失败: %v", actionType, err)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "计划创建成功,但响应生成失败", actionType, "响应序列化失败", planToCreate)
2025-09-20 17:11:04 +08:00
return
}
2025-09-14 15:56:55 +08:00
// 使用统一的成功响应函数
2025-09-28 01:33:19 +08:00
c.logger.Infof("%s: 计划创建成功, ID: %d", actionType, planToCreate.ID)
2025-09-28 01:28:54 +08:00
controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "计划创建成功", resp, actionType, "计划创建成功", resp)
2025-09-13 14:09:22 +08:00
}
// GetPlan godoc
// @Summary 获取计划详情
// @Description 根据计划ID获取单个计划的详细信息。
// @Tags 计划管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-13 14:09:22 +08:00
// @Produce json
// @Param id path int true "计划ID"
2025-10-03 23:02:43 +08:00
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为200代表成功获取"
2025-09-19 15:55:56 +08:00
// @Router /api/v1/plans/{id} [get]
2025-09-14 16:19:09 +08:00
func (c *Controller) GetPlan(ctx *gin.Context) {
2025-09-28 01:28:54 +08:00
const actionType = "获取计划详情"
2025-09-14 16:19:09 +08:00
// 1. 从 URL 路径中获取 ID
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
2025-09-14 16:19:09 +08:00
return
}
// 2. 调用仓库层获取计划详情
plan, err := c.planRepo.GetPlanByID(uint(id))
if err != nil {
// 判断是否为“未找到”错误
if errors.Is(err, gorm.ErrRecordNotFound) {
2025-09-28 01:33:19 +08:00
c.logger.Warnf("%s: 计划不存在, ID: %d", actionType, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "计划不存在", actionType, "计划不存在", id)
2025-09-14 16:19:09 +08:00
return
}
// 其他数据库错误视为内部错误
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 数据库查询失败: %v, ID: %d", actionType, err, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划详情时发生内部错误", actionType, "数据库查询失败", id)
2025-09-14 16:19:09 +08:00
return
}
// 3. 将模型转换为响应 DTO
2025-10-03 23:02:43 +08:00
resp, err := dto.NewPlanToResponse(plan)
2025-09-20 17:11:04 +08:00
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 序列化响应失败: %v, Plan: %+v", actionType, err, plan)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划详情失败: 内部数据格式错误", actionType, "响应序列化失败", plan)
2025-09-20 17:11:04 +08:00
return
}
2025-09-13 14:09:22 +08:00
2025-09-14 16:19:09 +08:00
// 4. 发送成功响应
2025-09-28 01:33:19 +08:00
c.logger.Infof("%s: 获取计划详情成功, ID: %d", actionType, id)
2025-09-28 01:28:54 +08:00
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划详情成功", resp, actionType, "获取计划详情成功", resp)
2025-09-13 14:09:22 +08:00
}
// ListPlans godoc
// @Summary 获取计划列表
// @Description 获取所有计划的列表
// @Tags 计划管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-13 14:09:22 +08:00
// @Produce json
2025-10-03 23:02:43 +08:00
// @Success 200 {object} controller.Response{data=[]dto.PlanResponse} "业务码为200代表成功获取列表"
2025-09-19 15:55:56 +08:00
// @Router /api/v1/plans [get]
2025-09-14 16:42:54 +08:00
func (c *Controller) ListPlans(ctx *gin.Context) {
2025-09-28 01:28:54 +08:00
const actionType = "获取计划列表"
2025-09-14 16:52:59 +08:00
// 1. 调用仓库层获取所有计划
plans, err := c.planRepo.ListBasicPlans()
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 数据库查询失败: %v", actionType, err)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划列表时发生内部错误", actionType, "数据库查询失败", nil)
2025-09-14 16:52:59 +08:00
return
}
// 2. 将模型转换为响应 DTO
2025-10-03 23:02:43 +08:00
planResponses := make([]dto.PlanResponse, 0, len(plans))
2025-09-14 16:52:59 +08:00
for _, p := range plans {
2025-10-03 23:02:43 +08:00
resp, err := dto.NewPlanToResponse(&p)
2025-09-20 17:11:04 +08:00
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 序列化响应失败: %v, Plan: %+v", actionType, err, p)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划列表失败: 内部数据格式错误", actionType, "响应序列化失败", p)
2025-09-20 17:11:04 +08:00
return
}
planResponses = append(planResponses, *resp)
2025-09-14 16:52:59 +08:00
}
// 3. 构造并发送成功响应
2025-10-03 23:02:43 +08:00
resp := dto.ListPlansResponse{
2025-09-14 16:52:59 +08:00
Plans: planResponses,
Total: len(planResponses),
}
2025-09-28 01:33:19 +08:00
c.logger.Infof("%s: 获取计划列表成功, 数量: %d", actionType, len(planResponses))
2025-09-28 01:28:54 +08:00
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划列表成功", resp, actionType, "获取计划列表成功", resp)
2025-09-13 14:09:22 +08:00
}
// UpdatePlan godoc
// @Summary 更新计划
// @Description 根据计划ID更新计划的详细信息。
// @Tags 计划管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-13 14:09:22 +08:00
// @Accept json
// @Produce json
// @Param id path int true "计划ID"
2025-10-03 23:02:43 +08:00
// @Param plan body dto.UpdatePlanRequest true "更新后的计划信息"
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为200代表更新成功"
2025-09-19 15:55:56 +08:00
// @Router /api/v1/plans/{id} [put]
2025-09-14 16:42:54 +08:00
func (c *Controller) UpdatePlan(ctx *gin.Context) {
2025-09-28 01:28:54 +08:00
const actionType = "更新计划"
2025-09-14 17:39:44 +08:00
// 1. 从 URL 路径中获取 ID
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
2025-09-14 17:39:44 +08:00
return
}
// 2. 绑定请求体
2025-10-03 23:02:43 +08:00
var req dto.UpdatePlanRequest
2025-09-14 17:39:44 +08:00
if err := ctx.ShouldBindJSON(&req); err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
2025-09-14 17:39:44 +08:00
return
}
// 3. 将请求转换为模型(转换函数带校验)
2025-10-03 23:02:43 +08:00
planToUpdate, err := dto.NewPlanFromUpdateRequest(&req)
2025-09-14 17:39:44 +08:00
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 计划数据校验失败: %v", actionType, err)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "计划数据校验失败: "+err.Error(), actionType, "计划数据校验失败", req)
2025-09-14 17:39:44 +08:00
return
}
planToUpdate.ID = uint(id) // 确保ID被设置
2025-09-23 18:11:21 +08:00
// --- 自动判断 ContentType ---
if len(req.SubPlanIDs) > 0 {
planToUpdate.ContentType = models.PlanContentTypeSubPlans
} else {
// 如果 SubPlanIDs 未提供,则默认为 Tasks 类型(即使 Tasks 字段也未提供)
planToUpdate.ContentType = models.PlanContentTypeTasks
}
2025-09-14 17:39:44 +08:00
// 4. 检查计划是否存在
_, err = c.planRepo.GetBasicPlanByID(uint(id))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2025-09-28 01:33:19 +08:00
c.logger.Warnf("%s: 计划不存在, ID: %d", actionType, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "计划不存在", actionType, "计划不存在", id)
2025-09-14 17:39:44 +08:00
return
}
2025-10-03 23:02:43 +08:00
c.logger.Errorf("%s: 获取计划信息失败: %v, ID: %d", actionType, err, id)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划信息时发生内部错误", actionType, "数据库查询失败", id)
2025-09-14 17:39:44 +08:00
return
}
// 5. 调用仓库方法更新计划
2025-09-23 22:38:04 +08:00
// 只要是更新任务,就重置执行计数器
planToUpdate.ExecuteCount = 0 // 重置计数器
c.logger.Infof("计划 #%d 被更新,执行计数器已重置为 0。", planToUpdate.ID)
2025-09-14 17:39:44 +08:00
if err := c.planRepo.UpdatePlan(planToUpdate); err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 数据库更新计划失败: %v, Plan: %+v", actionType, err, planToUpdate)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新计划失败: "+err.Error(), actionType, "数据库更新计划失败", planToUpdate)
2025-09-14 17:39:44 +08:00
return
}
2025-09-20 22:32:00 +08:00
// 更新成功后,调用 manager 确保触发器任务定义存在
if err := c.analysisPlanTaskManager.EnsureAnalysisTaskDefinition(planToUpdate.ID); err != nil {
// 这是一个非阻塞性错误,我们只记录日志
2025-09-20 22:32:00 +08:00
c.logger.Errorf("为更新后的计划 %d 确保触发器任务定义失败: %v", planToUpdate.ID, err)
}
2025-09-14 17:39:44 +08:00
// 6. 获取更新后的完整计划用于响应
updatedPlan, err := c.planRepo.GetPlanByID(uint(id))
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 获取更新后计划详情失败: %v, ID: %d", actionType, err, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取更新后计划详情时发生内部错误", actionType, "获取更新后计划详情失败", id)
2025-09-14 17:39:44 +08:00
return
}
// 7. 将模型转换为响应 DTO
2025-10-03 23:02:43 +08:00
resp, err := dto.NewPlanToResponse(updatedPlan)
2025-09-20 17:11:04 +08:00
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 序列化响应失败: %v, Updated Plan: %+v", actionType, err, updatedPlan)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "计划更新成功,但响应生成失败", actionType, "响应序列化失败", updatedPlan)
2025-09-20 17:11:04 +08:00
return
}
2025-09-14 17:39:44 +08:00
// 8. 发送成功响应
2025-09-28 01:33:19 +08:00
c.logger.Infof("%s: 计划更新成功, ID: %d", actionType, updatedPlan.ID)
2025-09-28 01:28:54 +08:00
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划更新成功", resp, actionType, "计划更新成功", resp)
2025-09-13 14:09:22 +08:00
}
// DeletePlan godoc
// @Summary 删除计划
2025-09-28 01:33:19 +08:00
// @Description 根据计划ID删除计划。软删除
2025-09-13 14:09:22 +08:00
// @Tags 计划管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-13 14:09:22 +08:00
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
2025-09-19 15:55:56 +08:00
// @Router /api/v1/plans/{id} [delete]
2025-09-14 16:42:54 +08:00
func (c *Controller) DeletePlan(ctx *gin.Context) {
2025-09-28 01:28:54 +08:00
const actionType = "删除计划"
2025-09-14 17:02:36 +08:00
// 1. 从 URL 路径中获取 ID
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
2025-09-14 17:02:36 +08:00
return
}
// 2. 检查计划是否存在
plan, err := c.planRepo.GetBasicPlanByID(uint(id))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2025-09-28 01:33:19 +08:00
c.logger.Warnf("%s: 计划不存在, ID: %d", actionType, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "计划不存在", actionType, "计划不存在", id)
return
}
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 获取计划信息失败: %v, ID: %d", actionType, err, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划信息时发生内部错误", actionType, "数据库查询失败", id)
return
}
// 3. 停止这个计划
if plan.Status == models.PlanStatusEnabled {
if err := c.planRepo.StopPlanTransactionally(uint(id)); err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 停止计划失败: %v, ID: %d", actionType, err, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "停止计划时发生内部错误: "+err.Error(), actionType, "停止计划失败", id)
return
}
}
// 4. 调用仓库层删除计划
2025-09-14 17:02:36 +08:00
if err := c.planRepo.DeletePlan(uint(id)); err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 数据库删除失败: %v, ID: %d", actionType, err, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除计划时发生内部错误", actionType, "数据库删除失败", id)
2025-09-14 17:02:36 +08:00
return
}
// 5. 发送成功响应
2025-09-28 01:33:19 +08:00
c.logger.Infof("%s: 计划删除成功, ID: %d", actionType, id)
2025-09-28 01:28:54 +08:00
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划删除成功", nil, actionType, "计划删除成功", id)
2025-09-13 14:09:22 +08:00
}
// StartPlan godoc
// @Summary 启动计划
// @Description 根据计划ID启动一个计划的执行。
// @Tags 计划管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-13 14:09:22 +08:00
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表成功启动计划"
2025-09-19 15:55:56 +08:00
// @Router /api/v1/plans/{id}/start [post]
2025-09-14 16:42:54 +08:00
func (c *Controller) StartPlan(ctx *gin.Context) {
2025-09-28 01:28:54 +08:00
const actionType = "启动计划"
2025-09-22 14:21:08 +08:00
// 1. 从 URL 路径中获取 ID
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
2025-09-22 14:21:08 +08:00
return
}
// 2. 检查计划是否存在
plan, err := c.planRepo.GetBasicPlanByID(uint(id))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2025-09-28 01:33:19 +08:00
c.logger.Warnf("%s: 计划不存在, ID: %d", actionType, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "计划不存在", actionType, "计划不存在", id)
2025-09-22 14:21:08 +08:00
return
}
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 获取计划信息失败: %v, ID: %d", actionType, err, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划信息时发生内部错误", actionType, "数据库查询失败", id)
2025-09-22 14:21:08 +08:00
return
}
// 3. 检查计划当前状态
if plan.Status == models.PlanStatusEnabled {
2025-09-28 01:33:19 +08:00
c.logger.Warnf("%s: 计划已处于启动状态,无需重复操作, ID: %d", actionType, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "计划已处于启动状态,无需重复操作", actionType, "计划已处于启动状态", id)
2025-09-22 14:21:08 +08:00
return
}
2025-09-23 22:18:59 +08:00
// 4. 检查并重置执行计数器,然后更新计划状态为“已启动”
// 只有当计划是从非 Enabled 状态(如 Disabled, Stopeed, Failed启动时才需要重置计数器
if plan.Status != models.PlanStatusEnabled {
// 如果计划是从停止或失败状态重新启动且计数器不为0则重置执行计数
if plan.ExecuteCount > 0 {
if err := c.planRepo.UpdateExecuteCount(plan.ID, 0); err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 重置计划执行计数失败: %v, ID: %d", actionType, err, plan.ID)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "重置计划执行计数失败", actionType, "重置执行计数失败", plan.ID)
2025-09-23 22:18:59 +08:00
return
}
c.logger.Infof("计划 #%d 的执行计数器已重置为 0。", plan.ID)
}
// 更新计划状态为“已启动”
if err := c.planRepo.UpdatePlanStatus(plan.ID, models.PlanStatusEnabled); err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 更新计划状态失败: %v, ID: %d", actionType, err, plan.ID)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新计划状态失败", actionType, "更新计划状态失败", plan.ID)
2025-09-23 22:18:59 +08:00
return
}
c.logger.Infof("已成功更新计划 #%d 的状态为 '已启动'。", plan.ID)
} else {
// 如果计划已经处于 Enabled 状态,则无需更新
c.logger.Infof("计划 #%d 已处于启动状态,无需重复操作。", plan.ID)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "计划已处于启动状态,无需重复操作", actionType, "计划已处于启动状态", plan.ID)
2025-09-22 14:21:08 +08:00
return
}
// 5. 为计划创建或更新触发器
if err := c.analysisPlanTaskManager.CreateOrUpdateTrigger(plan.ID); err != nil {
// 此处错误不回滚状态,因为状态更新已成功,但需要明确告知用户触发器创建失败
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 创建或更新触发器失败: %v, ID: %d", actionType, err, plan.ID)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "计划状态已更新,但创建执行触发器失败,请检查计划配置或稍后重试", actionType, "创建执行触发器失败", plan.ID)
2025-09-22 14:21:08 +08:00
return
}
// 6. 发送成功响应
2025-09-28 01:33:19 +08:00
c.logger.Infof("%s: 计划已成功启动, ID: %d", actionType, id)
2025-09-28 01:28:54 +08:00
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划已成功启动", nil, actionType, "计划已成功启动", id)
2025-09-13 14:09:22 +08:00
}
// StopPlan godoc
// @Summary 停止计划
// @Description 根据计划ID停止一个正在执行的计划。
// @Tags 计划管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-13 14:09:22 +08:00
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表成功停止计划"
2025-09-19 15:55:56 +08:00
// @Router /api/v1/plans/{id}/stop [post]
2025-09-14 16:42:54 +08:00
func (c *Controller) StopPlan(ctx *gin.Context) {
2025-09-28 01:28:54 +08:00
const actionType = "停止计划"
2025-09-22 15:17:54 +08:00
// 1. 从 URL 路径中获取 ID
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
2025-09-22 15:17:54 +08:00
return
}
// 2. 检查计划是否存在
plan, err := c.planRepo.GetBasicPlanByID(uint(id))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2025-09-28 01:33:19 +08:00
c.logger.Warnf("%s: 计划不存在, ID: %d", actionType, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "计划不存在", actionType, "计划不存在", id)
return
}
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 获取计划信息失败: %v, ID: %d", actionType, err, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划信息时发生内部错误", actionType, "数据库查询失败", id)
return
}
// 3. 检查计划当前状态
if plan.Status != models.PlanStatusEnabled {
2025-09-28 01:33:19 +08:00
c.logger.Warnf("%s: 计划当前不是启用状态, ID: %d, Status: %s", actionType, id, plan.Status)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "计划当前不是启用状态", actionType, "计划未启用", id)
return
}
// 4. 调用仓库层方法,该方法内部处理事务
2025-09-22 15:17:54 +08:00
if err := c.planRepo.StopPlanTransactionally(uint(id)); err != nil {
2025-09-28 01:33:19 +08:00
c.logger.Errorf("%s: 停止计划失败: %v, ID: %d", actionType, err, id)
2025-09-28 01:28:54 +08:00
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "停止计划时发生内部错误: "+err.Error(), actionType, "停止计划失败", id)
2025-09-22 15:17:54 +08:00
return
}
// 5. 发送成功响应
2025-09-28 01:33:19 +08:00
c.logger.Infof("%s: 计划已成功停止, ID: %d", actionType, id)
2025-09-28 01:28:54 +08:00
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划已成功停止", nil, actionType, "计划已成功停止", id)
2025-09-13 14:09:22 +08:00
}