feat: 🗓️ 新增任务块排期能力并完善课程与日程模型

Version: 0.1.1.dev.260207

- 新增并测试通过将任务块排进日程接口 
- 批量导入课程接口增加单双周功能,支持只在单双周上课的课程 📚
- 任务块时间定位逻辑调整为「第几周-周几」模式 🧭

refactor: 🔨 重构时间与日程数据结构

- 完成绝对日期与相对时间的转换逻辑 🔄
  - 后续可根据需求灵活决定时间的传入与输出类型
- 再次重构 schedule 表单结构
  - 拆分为 schedule_event(单)与 schedule(多)
  - 建立前者对后者的一对多关系 🧩

fix: 🐛 大幅调整表结构与业务逻辑,修复大量历史遗留 bug 🔥
This commit is contained in:
LoveLosita
2026-02-07 16:33:30 +08:00
parent 132b7095ac
commit f4bea0576c
17 changed files with 546 additions and 40 deletions

View File

@@ -136,6 +136,30 @@ func ProcessUserGetCompleteTaskClassRequest(taskClass *model.TaskClass) (*model.
return req, nil
}
// UserInsertTaskItemRequestToModel 用于将填入空闲时段日程的请求转换为 Schedule 模型
func UserInsertTaskItemRequestToModel(req *model.UserInsertTaskClassItemToScheduleRequest, item *model.TaskClassItem, taskID, userID, startSection, endSection int) ([]model.Schedule, *model.ScheduleEvent) {
var schedules []model.Schedule
for section := startSection; section <= endSection; section++ {
req1 := &model.Schedule{
UserID: userID, // 由调用方填充
EmbeddedTaskID: &taskID,
Week: req.Week,
DayOfWeek: req.DayOfWeek,
Section: section,
Status: "normal",
}
schedules = append(schedules, *req1)
}
req2 := &model.ScheduleEvent{
UserID: userID, // 由调用方填充
Name: safeStr(item.Content), // 任务内容作为事件名称
Type: "task",
RelID: &item.ID, // 关联到 TaskClassItem 的 ID
CanBeEmbedded: false, // 任务事件允许嵌入其他任务(如果需要的话)
}
return schedules, req2
}
// --- 🛡️ 辅助工具函数:保持代码清爽并防止 Panic ---
func safeStr(s *string) string {

52
backend/conv/time.go Normal file
View File

@@ -0,0 +1,52 @@
package conv
import (
"errors"
"time"
"github.com/LoveLosita/smartflow/backend/respond"
"github.com/spf13/viper"
)
// DateFormat 此处定义基准学期的开始和结束日期
const DateFormat = "2006-01-02"
var (
SemesterStartDate = viper.GetString("semesterStartDate") // 从配置文件中读取学期开始日期
SemesterEndDate = viper.GetString("semesterEndDate") // 从配置文件中读取学期结束日期
)
// RealDateToRelativeDate 将绝对日期转换为相对日期(格式: "week-day"
func RealDateToRelativeDate(realDate string) (int, int, error) {
t, err := time.Parse(DateFormat, realDate)
if err != nil {
return 0, 0, err
}
start, _ := time.Parse(DateFormat, SemesterStartDate)
end, _ := time.Parse(DateFormat, SemesterEndDate)
// 边界校验:日期必须在学期范围内
if t.Before(start) || t.After(end) {
return 0, 0, errors.New("日期超出学期范围")
}
// 计算天数差值注意24小时为一个基准天
days := int(t.Sub(start).Hours() / 24)
// 计算周数和星期
// 假设 SemesterStartDate 对应第 1 周,周 1
week := (days / 7) + 1
dayOfWeek := (days % 7) + 1
return week, dayOfWeek, nil
}
// RelativeDateToRealDate 将相对日期转换为绝对日期(输入格式: "week-day"
func RelativeDateToRealDate(week, dayOfWeek int) (string, error) {
start, _ := time.Parse(DateFormat, SemesterStartDate)
// 核心转换逻辑:(周-1)*7 + (天-1)
offsetDays := (week-1)*7 + (dayOfWeek - 1)
targetDate := start.AddDate(0, 0, offsetDays)
// 校验计算出的日期是否超出学期结束日期
end, _ := time.Parse(DateFormat, SemesterEndDate)
if targetDate.After(end) {
return "", respond.TimeOutOfRangeOfThisSemester
}
return targetDate.Format(DateFormat), nil
}