修改controller包

This commit is contained in:
2025-11-05 17:24:19 +08:00
parent 4cae93ef34
commit ef4ca397ce
11 changed files with 449 additions and 354 deletions

View File

@@ -1,6 +1,7 @@
package plan
import (
"context"
"errors"
"strconv"
@@ -16,14 +17,14 @@ import (
// Controller 定义了计划相关的控制器
type Controller struct {
logger *logs.Logger
ctx context.Context
planService service.PlanService
}
// NewController 创建一个新的 Controller 实例
func NewController(logger *logs.Logger, planService service.PlanService) *Controller {
func NewController(ctx context.Context, planService service.PlanService) *Controller {
return &Controller{
logger: logger,
ctx: ctx,
planService: planService,
}
}
@@ -41,17 +42,18 @@ func NewController(logger *logs.Logger, planService service.PlanService) *Contro
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为201代表创建成功"
// @Router /api/v1/plans [post]
func (c *Controller) CreatePlan(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePlan")
var req dto.CreatePlanRequest
const actionType = "创建计划"
if err := ctx.Bind(&req); err != nil {
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
// 调用服务层创建计划
resp, err := c.planService.CreatePlan(&req)
resp, err := c.planService.CreatePlan(reqCtx, &req)
if err != nil {
c.logger.Errorf("%s: 服务层创建计划失败: %v", actionType, err)
logger.Errorf("%s: 服务层创建计划失败: %v", actionType, err)
// 根据服务层返回的错误类型转换为相应的HTTP状态码
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划数据校验失败或关联计划不存在", req)
@@ -60,7 +62,7 @@ func (c *Controller) CreatePlan(ctx echo.Context) error {
}
// 使用统一的成功响应函数
c.logger.Infof("%s: 计划创建成功, ID: %d", actionType, resp.ID)
logger.Infof("%s: 计划创建成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "计划创建成功", resp, actionType, "计划创建成功", resp)
}
@@ -74,19 +76,20 @@ func (c *Controller) CreatePlan(ctx echo.Context) error {
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为200代表成功获取"
// @Router /api/v1/plans/{id} [get]
func (c *Controller) GetPlan(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPlan")
const actionType = "获取计划详情"
// 1. 从 URL 路径中获取 ID
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
}
// 调用服务层获取计划详情
resp, err := c.planService.GetPlanByID(uint(id))
resp, err := c.planService.GetPlanByID(reqCtx, uint(id))
if err != nil {
c.logger.Errorf("%s: 服务层获取计划详情失败: %v, ID: %d", actionType, err, id)
logger.Errorf("%s: 服务层获取计划详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
}
@@ -94,7 +97,7 @@ func (c *Controller) GetPlan(ctx echo.Context) error {
}
// 4. 发送成功响应
c.logger.Infof("%s: 获取计划详情成功, ID: %d", actionType, id)
logger.Infof("%s: 获取计划详情成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划详情成功", resp, actionType, "获取计划详情成功", resp)
}
@@ -108,21 +111,22 @@ func (c *Controller) GetPlan(ctx echo.Context) error {
// @Success 200 {object} controller.Response{data=dto.ListPlansResponse} "业务码为200代表成功获取列表"
// @Router /api/v1/plans [get]
func (c *Controller) ListPlans(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPlans")
const actionType = "获取计划列表"
var query dto.ListPlansQuery
if err := ctx.Bind(&query); err != nil {
c.logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", query)
}
// 调用服务层获取计划列表
resp, err := c.planService.ListPlans(&query)
resp, err := c.planService.ListPlans(reqCtx, &query)
if err != nil {
c.logger.Errorf("%s: 服务层获取计划列表失败: %v", actionType, err)
logger.Errorf("%s: 服务层获取计划列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划列表失败: "+err.Error(), actionType, "服务层获取计划列表失败", nil)
}
c.logger.Infof("%s: 获取计划列表成功, 数量: %d", actionType, len(resp.Plans))
logger.Infof("%s: 获取计划列表成功, 数量: %d", actionType, len(resp.Plans))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划列表成功", resp, actionType, "获取计划列表成功", resp)
}
@@ -138,26 +142,27 @@ func (c *Controller) ListPlans(ctx echo.Context) error {
// @Success 200 {object} controller.Response{data=dto.PlanResponse} "业务码为200代表更新成功"
// @Router /api/v1/plans/{id} [put]
func (c *Controller) UpdatePlan(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePlan")
const actionType = "更新计划"
// 1. 从 URL 路径中获取 ID
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
}
// 2. 绑定请求体
var req dto.UpdatePlanRequest
if err := ctx.Bind(&req); err != nil {
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
// 调用服务层更新计划
resp, err := c.planService.UpdatePlan(uint(id), &req)
resp, err := c.planService.UpdatePlan(reqCtx, uint(id), &req)
if err != nil {
c.logger.Errorf("%s: 服务层更新计划失败: %v, ID: %d", actionType, err, id)
logger.Errorf("%s: 服务层更新计划失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
} else if errors.Is(err, plan.ErrPlanCannotBeModified) { // 修改为 plan.ErrPlanCannotBeModified
@@ -167,7 +172,7 @@ func (c *Controller) UpdatePlan(ctx echo.Context) error {
}
// 9. 发送成功响应
c.logger.Infof("%s: 计划更新成功, ID: %d", actionType, resp.ID)
logger.Infof("%s: 计划更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划更新成功", resp, actionType, "计划更新成功", resp)
}
@@ -181,19 +186,20 @@ func (c *Controller) UpdatePlan(ctx echo.Context) error {
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Router /api/v1/plans/{id} [delete]
func (c *Controller) DeletePlan(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePlan")
const actionType = "删除计划"
// 1. 从 URL 路径中获取 ID
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
}
// 调用服务层删除计划
err = c.planService.DeletePlan(uint(id))
err = c.planService.DeletePlan(reqCtx, uint(id))
if err != nil {
c.logger.Errorf("%s: 服务层删除计划失败: %v, ID: %d", actionType, err, id)
logger.Errorf("%s: 服务层删除计划失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
} else if errors.Is(err, plan.ErrPlanCannotBeDeleted) { // 修改为 plan.ErrPlanCannotBeDeleted
@@ -203,7 +209,7 @@ func (c *Controller) DeletePlan(ctx echo.Context) error {
}
// 6. 发送成功响应
c.logger.Infof("%s: 计划删除成功, ID: %d", actionType, id)
logger.Infof("%s: 计划删除成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划删除成功", nil, actionType, "计划删除成功", id)
}
@@ -217,19 +223,20 @@ func (c *Controller) DeletePlan(ctx echo.Context) error {
// @Success 200 {object} controller.Response "业务码为200代表成功启动计划"
// @Router /api/v1/plans/{id}/start [post]
func (c *Controller) StartPlan(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "StartPlan")
const actionType = "启动计划"
// 1. 从 URL 路径中获取 ID
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
}
// 调用服务层启动计划
err = c.planService.StartPlan(uint(id))
err = c.planService.StartPlan(reqCtx, uint(id))
if err != nil {
c.logger.Errorf("%s: 服务层启动计划失败: %v, ID: %d", actionType, err, id)
logger.Errorf("%s: 服务层启动计划失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
} else if errors.Is(err, plan.ErrPlanCannotBeStarted) { // 修改为 plan.ErrPlanCannotBeStarted
@@ -241,7 +248,7 @@ func (c *Controller) StartPlan(ctx echo.Context) error {
}
// 6. 发送成功响应
c.logger.Infof("%s: 计划已成功启动, ID: %d", actionType, id)
logger.Infof("%s: 计划已成功启动, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划已成功启动", nil, actionType, "计划已成功启动", id)
}
@@ -255,19 +262,20 @@ func (c *Controller) StartPlan(ctx echo.Context) error {
// @Success 200 {object} controller.Response "业务码为200代表成功停止计划"
// @Router /api/v1/plans/{id}/stop [post]
func (c *Controller) StopPlan(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "StopPlan")
const actionType = "停止计划"
// 1. 从 URL 路径中获取 ID
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
logger.Errorf("%s: 计划ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的计划ID格式", actionType, "计划ID格式错误", idStr)
}
// 调用服务层停止计划
err = c.planService.StopPlan(uint(id))
err = c.planService.StopPlan(reqCtx, uint(id))
if err != nil {
c.logger.Errorf("%s: 服务层停止计划失败: %v, ID: %d", actionType, err, id)
logger.Errorf("%s: 服务层停止计划失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, plan.ErrPlanNotFound) { // 修改为 plan.ErrPlanNotFound
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "计划不存在", id)
} else if errors.Is(err, plan.ErrPlanCannotBeStopped) { // 修改为 plan.ErrPlanCannotBeStopped
@@ -279,6 +287,6 @@ func (c *Controller) StopPlan(ctx echo.Context) error {
}
// 6. 发送成功响应
c.logger.Infof("%s: 计划已成功停止, ID: %d", actionType, id)
logger.Infof("%s: 计划已成功停止, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "计划已成功停止", nil, actionType, "计划已成功停止", id)
}