feat: 🚀 新增智能编排日程接口与算法模块 * 新增智能编排日程接口,实现自动生成周维度课程安排 * 抽离核心算法至 `Logic` 包,统一存放调度与排课相关算法逻辑,优化项目结构分层 * 大多数用例测试通过,当前存在少量边界用例下“排课时间是否充足”的误判问题 * 返回的周视图数据在极端场景下存在数量偏差,待进一步完善边界控制 fix: 🐛 修复批量导入课程接口 500 错误 * 修复批量导入课程接口中未在 `event` 结构体填写时间字段的问题 * 解决因时间字段为空导致的服务端 500 错误,保证数据完整性 refactor: ♻️ 新增入参校验逻辑保障调度稳定性 * 在添加任务类时新增入参校验逻辑 * 避免非法数据进入调度流程,确保自动编排日程接口执行稳定 docs: 📚 更新 README 智能编排算法说明 * 补充智能编排日程算法的设计思路与实现说明 undo: ⚠️ 追加导入课程后缓存未自动失效 * 追加导入课程后未自动删除对应周安排缓存,存在数据不一致风险 * 当前未能稳定复现,计划后续定位缓存失效时序与触发条件问题
169 lines
5.4 KiB
Go
169 lines
5.4 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"net/http"
|
||
"strconv"
|
||
"time"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/model"
|
||
"github.com/LoveLosita/smartflow/backend/respond"
|
||
"github.com/LoveLosita/smartflow/backend/service"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type ScheduleAPI struct {
|
||
scheduleService *service.ScheduleService
|
||
}
|
||
|
||
func NewScheduleAPI(scheduleService *service.ScheduleService) *ScheduleAPI {
|
||
return &ScheduleAPI{
|
||
scheduleService: scheduleService,
|
||
}
|
||
}
|
||
|
||
func (s *ScheduleAPI) GetUserTodaySchedule(c *gin.Context) {
|
||
// 1. 从请求上下文中获取用户ID
|
||
userID := c.GetInt("user_id")
|
||
//2.调用服务层方法获取用户当天的日程安排
|
||
// 创建一个带 1 秒超时的上下文
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
|
||
defer cancel() // 记得释放资源
|
||
todaySchedules, err := s.scheduleService.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(), 1*time.Second)
|
||
defer cancel() // 记得释放资源
|
||
weeklySchedules, err := s.scheduleService.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 []model.UserDeleteScheduleEvent
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
//3.调用服务层方法删除指定的日程事件
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
|
||
defer cancel() // 记得释放资源
|
||
err := s.scheduleService.DeleteScheduleEvent(ctx, req, userID)
|
||
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(), 1*time.Second)
|
||
defer cancel() // 记得释放资源
|
||
completedSchedules, err := s.scheduleService.GetUserRecentCompletedSchedules(ctx, userID, intIndex, 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(), 1*time.Second)
|
||
defer cancel() // 记得释放资源
|
||
ongoingSchedule, err := s.scheduleService.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(), 1*time.Second)
|
||
defer cancel() // 记得释放资源*/
|
||
err = s.scheduleService.RevocateUserTaskClassItem(context.Background(), userID, 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. 从请求体中获取智能规划的参数
|
||
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(), 1*time.Second)*/
|
||
ctx := context.Background()
|
||
/*defer cancel() // 记得释放资源*/
|
||
res, err := s.scheduleService.SmartPlanning(ctx, userID, intTaskClassID)
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
//4.返回智能规划成功的响应给前端
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, res))
|
||
}
|