删除计划时停止这个计划的执行,停止计划前判断一下是不是未启用的计划 #15
| @@ -348,14 +348,35 @@ func (c *Controller) DeletePlan(ctx *gin.Context) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// 2. 调用仓库层删除计划 | 	// 2. 检查计划是否存在 | ||||||
|  | 	plan, err := c.planRepo.GetBasicPlanByID(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. 停止这个计划 | ||||||
|  | 	if plan.Status == models.PlanStatusEnabled { | ||||||
|  | 		if err := c.planRepo.StopPlanTransactionally(uint(id)); err != nil { | ||||||
|  | 			c.logger.Errorf("停止计划 #%d 失败: %v", id, err) | ||||||
|  | 			controller.SendErrorResponse(ctx, controller.CodeInternalError, "停止计划时发生内部错误: "+err.Error()) | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// 4. 调用仓库层删除计划 | ||||||
| 	if err := c.planRepo.DeletePlan(uint(id)); err != nil { | 	if err := c.planRepo.DeletePlan(uint(id)); err != nil { | ||||||
| 		c.logger.Errorf("删除计划失败: %v", err) | 		c.logger.Errorf("删除计划失败: %v", err) | ||||||
| 		controller.SendErrorResponse(ctx, controller.CodeInternalError, "删除计划时发生内部错误") | 		controller.SendErrorResponse(ctx, controller.CodeInternalError, "删除计划时发生内部错误") | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// 3. 发送成功响应 | 	// 5. 发送成功响应 | ||||||
| 	controller.SendResponse(ctx, controller.CodeSuccess, "计划删除成功", nil) | 	controller.SendResponse(ctx, controller.CodeSuccess, "计划删除成功", nil) | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -450,13 +471,31 @@ func (c *Controller) StopPlan(ctx *gin.Context) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// 2. 调用仓库层方法,该方法内部处理事务 | 	// 2. 检查计划是否存在 | ||||||
|  | 	plan, err := c.planRepo.GetBasicPlanByID(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. 检查计划当前状态 | ||||||
|  | 	if plan.Status != models.PlanStatusEnabled { | ||||||
|  | 		controller.SendErrorResponse(ctx, controller.CodeBadRequest, "计划当前不是启用状态") | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// 4. 调用仓库层方法,该方法内部处理事务 | ||||||
| 	if err := c.planRepo.StopPlanTransactionally(uint(id)); err != nil { | 	if err := c.planRepo.StopPlanTransactionally(uint(id)); err != nil { | ||||||
| 		c.logger.Errorf("停止计划 #%d 失败: %v", id, err) | 		c.logger.Errorf("停止计划 #%d 失败: %v", id, err) | ||||||
| 		controller.SendErrorResponse(ctx, controller.CodeInternalError, "停止计划时发生内部错误: "+err.Error()) | 		controller.SendErrorResponse(ctx, controller.CodeInternalError, "停止计划时发生内部错误: "+err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// 3. 发送成功响应 | 	// 5. 发送成功响应 | ||||||
| 	controller.SendResponse(ctx, controller.CodeSuccess, "计划已成功停止", nil) | 	controller.SendResponse(ctx, controller.CodeSuccess, "计划已成功停止", nil) | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user