Files
smartmate/backend/gateway/api/schedule.go
Losita 3b6fca44a6 Version: 0.9.77.dev.260505
后端:
1.阶段 6 CP4/CP5 目录收口与共享边界纯化
- 将 backend 根目录收口为 services、client、gateway、cmd、shared 五个一级目录
- 收拢 bootstrap、inits、infra/kafka、infra/outbox、conv、respond、pkg、middleware,移除根目录旧实现与空目录
- 将 utils 下沉到 services/userauth/internal/auth,将 logic 下沉到 services/schedule/core/planning
- 将迁移期 runtime 桥接实现统一收拢到 services/runtime/{conv,dao,eventsvc,model},删除 shared/legacy 与未再被 import 的旧 service 实现
- 将 gateway/shared/respond 收口为 HTTP/Gin 错误写回适配,shared/respond 仅保留共享错误语义与状态映射
- 将 HTTP IdempotencyMiddleware 与 RateLimitMiddleware 收口到 gateway/middleware
- 将 GormCachePlugin 下沉到 shared/infra/gormcache,将共享 RateLimiter 下沉到 shared/infra/ratelimit,将 agent token budget 下沉到 services/agent/shared
- 删除 InitEino 兼容壳,收缩 cmd/internal/coreinit 仅保留旧组合壳残留域初始化语义
- 更新微服务迁移计划与桌面 checklist,补齐 CP4/CP5 当前切流点、目录终态与验证结果
- 完成 go test ./...、git diff --check 与最终真实 smoke;health、register/login、task/create+get、schedule/today、task-class/list、memory/items、agent chat/meta/timeline/context-stats 全部 200,SSE 合并结果为 CP5_OK 且 [DONE] 只有 1 个
2026-05-05 23:25:07 +08:00

217 lines
6.9 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 api
import (
"context"
"net/http"
"strconv"
"time"
"github.com/LoveLosita/smartflow/backend/gateway/shared/respond"
schedulecontracts "github.com/LoveLosita/smartflow/backend/shared/contracts/schedule"
"github.com/LoveLosita/smartflow/backend/shared/ports"
"github.com/gin-gonic/gin"
)
const scheduleRequestTimeout = 6 * time.Second
type ScheduleAPI struct {
scheduleClient ports.ScheduleCommandClient
}
func NewScheduleAPI(scheduleClient ports.ScheduleCommandClient) *ScheduleAPI {
return &ScheduleAPI{
scheduleClient: scheduleClient,
}
}
func (s *ScheduleAPI) GetUserTodaySchedule(c *gin.Context) {
// 1. 从请求上下文中获取用户ID
userID := c.GetInt("user_id")
//2.调用服务层方法获取用户当天的日程安排
ctx, cancel := context.WithTimeout(c.Request.Context(), scheduleRequestTimeout)
defer cancel() // 记得释放资源
todaySchedules, err := s.scheduleClient.GetUserTodaySchedule(ctx, userID)
if err != nil {
respond.DealWithError(c, err)
return
}
//3.返回日程安排数据给前端
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, todaySchedules))
}
func (s *ScheduleAPI) GetUserWeeklySchedule(c *gin.Context) {
// 1. 从请求上下文中获取用户ID
userID := c.GetInt("user_id")
// 2. 从查询参数中获取 week 参数
week, err := strconv.Atoi(c.Query("week"))
if err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
//3.调用服务层方法获取用户当周的日程安排
ctx, cancel := context.WithTimeout(c.Request.Context(), scheduleRequestTimeout)
defer cancel() // 记得释放资源
weeklySchedules, err := s.scheduleClient.GetUserWeeklySchedule(ctx, userID, week)
if err != nil {
respond.DealWithError(c, err)
return
}
//4.返回日程安排数据给前端
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, weeklySchedules))
}
func (s *ScheduleAPI) DeleteScheduleEvent(c *gin.Context) {
// 1. 从请求上下文中获取用户ID
userID := c.GetInt("user_id")
// 2. 从请求体中获取要删除的日程事件信息
var req []schedulecontracts.UserDeleteScheduleEvent
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
//3.调用服务层方法删除指定的日程事件
ctx, cancel := context.WithTimeout(c.Request.Context(), scheduleRequestTimeout)
defer cancel() // 记得释放资源
err := s.scheduleClient.DeleteScheduleEvent(ctx, schedulecontracts.DeleteScheduleEventsRequest{
UserID: userID,
Events: req,
})
if err != nil {
respond.DealWithError(c, err)
return
}
//4.返回删除成功的响应给前端
c.JSON(http.StatusOK, respond.Ok)
}
func (s *ScheduleAPI) GetUserRecentCompletedSchedules(c *gin.Context) {
// 1. 从请求上下文中获取用户ID以及其他查询参数如 index 和 limit
userID := c.GetInt("user_id")
index := c.Query("index")
limit := c.Query("limit")
intIndex, err := strconv.Atoi(index)
if err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
intLimit, err := strconv.Atoi(limit)
if err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
//2.调用服务层方法获取用户最近完成的日程事件
ctx, cancel := context.WithTimeout(c.Request.Context(), scheduleRequestTimeout)
defer cancel() // 记得释放资源
completedSchedules, err := s.scheduleClient.GetUserRecentCompletedSchedules(ctx, schedulecontracts.RecentCompletedRequest{
UserID: userID,
Index: intIndex,
Limit: intLimit,
})
if err != nil {
respond.DealWithError(c, err)
return
}
//3.返回数据给前端
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, completedSchedules))
}
func (s *ScheduleAPI) GetUserOngoingSchedule(c *gin.Context) {
// 1. 从请求上下文中获取用户ID
userID := c.GetInt("user_id")
//2.调用服务层方法获取用户正在进行的日程事件
ctx, cancel := context.WithTimeout(c.Request.Context(), scheduleRequestTimeout)
defer cancel() // 记得释放资源
ongoingSchedule, err := s.scheduleClient.GetUserOngoingSchedule(ctx, userID)
if err != nil {
respond.DealWithError(c, err)
return
}
//3.返回数据给前端
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, ongoingSchedule))
}
func (s *ScheduleAPI) UserRevocateTaskItemFromSchedule(c *gin.Context) {
// 1. 从请求上下文中获取用户ID
userID := c.GetInt("user_id")
// 2. 获取要撤销的任务块ID
eventID := c.Query("event_id")
intEventID, err := strconv.Atoi(eventID)
if err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
//3.调用服务层方法撤销任务块的安排
ctx, cancel := context.WithTimeout(c.Request.Context(), scheduleRequestTimeout)
defer cancel()
err = s.scheduleClient.RevokeTaskItemFromSchedule(ctx, schedulecontracts.RevokeTaskItemRequest{
UserID: userID,
EventID: intEventID,
})
if err != nil {
respond.DealWithError(c, err)
return
}
//4.返回撤销成功的响应给前端
c.JSON(http.StatusOK, respond.Ok)
}
func (s *ScheduleAPI) SmartPlanning(c *gin.Context) {
// 1. 从请求上下文中获取用户ID
userID := c.GetInt("user_id")
// 2. 从请求参数中获取智能规划的 task_class_id
taskClassID := c.Query("task_class_id")
intTaskClassID, err := strconv.Atoi(taskClassID)
if err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
//3.调用服务层方法进行智能规划
ctx, cancel := context.WithTimeout(c.Request.Context(), scheduleRequestTimeout)
defer cancel() // 记得释放资源
res, err := s.scheduleClient.SmartPlanning(ctx, schedulecontracts.SmartPlanningRequest{
UserID: userID,
TaskClassID: intTaskClassID,
})
if err != nil {
respond.DealWithError(c, err)
return
}
//4.返回智能规划成功的响应给前端
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, res))
}
// SmartPlanningMulti 处理“多任务类智能粗排”请求。
//
// 职责边界:
// 1. 只负责参数绑定、超时控制、错误透传;
// 2. 具体业务校验与排序策略由 service 层统一处理;
// 3. 保留已有单任务类接口,不与其互斥。
func (s *ScheduleAPI) SmartPlanningMulti(c *gin.Context) {
// 1. 从请求上下文中读取登录用户 ID。
userID := c.GetInt("user_id")
// 2. 绑定多任务类请求体。
var req struct {
TaskClassIDs []int `json:"task_class_ids" binding:"required,min=1,dive,min=1"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
// 3. 调用服务层执行多任务类粗排。
ctx, cancel := context.WithTimeout(c.Request.Context(), scheduleRequestTimeout)
defer cancel()
res, err := s.scheduleClient.SmartPlanningMulti(ctx, schedulecontracts.SmartPlanningMultiRequest{
UserID: userID,
TaskClassIDs: req.TaskClassIDs,
})
if err != nil {
respond.DealWithError(c, err)
return
}
// 4. 返回成功响应。
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, res))
}