package learning import ( "context" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/model/learning" learningReq "github.com/flipped-aurora/gin-vue-admin/server/model/learning/request" ) type ChapterService struct{} // CreateChapter 创建章节 func (chapterService *ChapterService) CreateChapter(ctx context.Context, chapter *learning.Chapter) (err error) { err = global.GVA_DB.WithContext(ctx).Create(chapter).Error return err } // DeleteChapter 删除章节 func (chapterService *ChapterService) DeleteChapter(ctx context.Context, ID string) (err error) { err = global.GVA_DB.WithContext(ctx).Delete(&learning.Chapter{}, ID).Error return err } // DeleteChapterByIds 批量删除章节 func (chapterService *ChapterService) DeleteChapterByIds(ctx context.Context, IDs []string) (err error) { err = global.GVA_DB.WithContext(ctx).Delete(&[]learning.Chapter{}, "id in ?", IDs).Error return err } // UpdateChapter 更新章节 func (chapterService *ChapterService) UpdateChapter(ctx context.Context, chapter learning.Chapter) (err error) { err = global.GVA_DB.WithContext(ctx).Model(&learning.Chapter{}).Where("id = ?", chapter.ID).Updates(&chapter).Error return err } // GetChapter 根据ID获取章节 func (chapterService *ChapterService) GetChapter(ctx context.Context, ID string) (chapter learning.Chapter, err error) { err = global.GVA_DB.WithContext(ctx).Where("id = ?", ID).First(&chapter).Error if err != nil { return } // 手动查询关联的课程信息 if chapter.CourseId != 0 { var course learning.Course courseErr := global.GVA_DB.WithContext(ctx).Where("id = ?", chapter.CourseId).First(&course).Error if courseErr == nil { chapter.Course = course } } return } // GetChapterInfoList 分页获取章节列表 func (chapterService *ChapterService) GetChapterInfoList(ctx context.Context, info learningReq.ChapterSearch) (list []learning.Chapter, total int64, err error) { limit := info.PageSize offset := info.PageSize * (info.Page - 1) // 创建db db := global.GVA_DB.WithContext(ctx).Model(&learning.Chapter{}) var chapters []learning.Chapter // 如果有条件搜索 下方会自动创建搜索语句 if info.StartCreatedAt != nil && info.EndCreatedAt != nil { db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt) } if info.Title != "" { db = db.Where("title LIKE ?", "%"+info.Title+"%") } if info.Status != "" { db = db.Where("status = ?", info.Status) } if info.CourseId != 0 { db = db.Where("course_id = ?", info.CourseId) } err = db.Count(&total).Error if err != nil { return } if limit != 0 { db = db.Limit(limit).Offset(offset) } err = db.Order("sort asc, created_at desc").Find(&chapters).Error if err != nil { return chapters, total, err } // 手动查询关联的课程信息 courseIds := make([]uint, 0) courseMap := make(map[uint]learning.Course) for _, chapter := range chapters { if chapter.CourseId != 0 { courseIds = append(courseIds, chapter.CourseId) } } if len(courseIds) > 0 { var courses []learning.Course global.GVA_DB.WithContext(ctx).Where("id in ?", courseIds).Find(&courses) for _, course := range courses { courseMap[course.ID] = course } // 为每个章节设置课程信息 for i := range chapters { if course, exists := courseMap[chapters[i].CourseId]; exists { chapters[i].Course = course } } } return chapters, total, err } // GetChaptersByCourse 根据课程ID获取章节列表 func (chapterService *ChapterService) GetChaptersByCourse(ctx context.Context, courseId uint) (list []learning.Chapter, err error) { err = global.GVA_DB.WithContext(ctx).Where("course_id = ? AND status = ?", courseId, "published").Order("sort asc, created_at asc").Find(&list).Error return } // UpdateChapterStatus 更新章节状态 func (chapterService *ChapterService) UpdateChapterStatus(ctx context.Context, ID string, status string) (err error) { err = global.GVA_DB.WithContext(ctx).Model(&learning.Chapter{}).Where("id = ?", ID).Update("status", status).Error return err }