130 lines
4.3 KiB
Go
130 lines
4.3 KiB
Go
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 QuestionService struct{}
|
|
|
|
// CreateQuestion 创建题目
|
|
func (questionService *QuestionService) CreateQuestion(ctx context.Context, question *learning.Question) (err error) {
|
|
err = global.GVA_DB.WithContext(ctx).Create(question).Error
|
|
return err
|
|
}
|
|
|
|
// DeleteQuestion 删除题目
|
|
func (questionService *QuestionService) DeleteQuestion(ctx context.Context, ID string) (err error) {
|
|
err = global.GVA_DB.WithContext(ctx).Delete(&learning.Question{}, ID).Error
|
|
return err
|
|
}
|
|
|
|
// DeleteQuestionByIds 批量删除题目
|
|
func (questionService *QuestionService) DeleteQuestionByIds(ctx context.Context, IDs []string) (err error) {
|
|
err = global.GVA_DB.WithContext(ctx).Delete(&[]learning.Question{}, "id in ?", IDs).Error
|
|
return err
|
|
}
|
|
|
|
// UpdateQuestion 更新题目
|
|
func (questionService *QuestionService) UpdateQuestion(ctx context.Context, question learning.Question) (err error) {
|
|
err = global.GVA_DB.WithContext(ctx).Model(&learning.Question{}).Where("id = ?", question.ID).Updates(&question).Error
|
|
return err
|
|
}
|
|
|
|
// GetQuestion 根据ID获取题目
|
|
func (questionService *QuestionService) GetQuestion(ctx context.Context, ID string) (question learning.Question, err error) {
|
|
err = global.GVA_DB.WithContext(ctx).Where("id = ?", ID).First(&question).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
// 手动查询关联的知识点信息
|
|
if question.KnowledgePointId != 0 {
|
|
var knowledgePoint learning.KnowledgePoint
|
|
kpErr := global.GVA_DB.WithContext(ctx).Where("id = ?", question.KnowledgePointId).First(&knowledgePoint).Error
|
|
if kpErr == nil {
|
|
question.KnowledgePoint = knowledgePoint
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetQuestionInfoList 分页获取题目列表
|
|
func (questionService *QuestionService) GetQuestionInfoList(ctx context.Context, info learningReq.QuestionSearch) (list []learning.Question, total int64, err error) {
|
|
limit := info.PageSize
|
|
offset := info.PageSize * (info.Page - 1)
|
|
// 创建db
|
|
db := global.GVA_DB.WithContext(ctx).Model(&learning.Question{})
|
|
var questions []learning.Question
|
|
// 如果有条件搜索 下方会自动创建搜索语句
|
|
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.Type != "" {
|
|
db = db.Where("type = ?", info.Type)
|
|
}
|
|
if info.Difficulty != "" {
|
|
db = db.Where("difficulty = ?", info.Difficulty)
|
|
}
|
|
if info.KnowledgePointId != 0 {
|
|
db = db.Where("knowledge_point_id = ?", info.KnowledgePointId)
|
|
}
|
|
err = db.Count(&total).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if limit != 0 {
|
|
db = db.Limit(limit).Offset(offset)
|
|
}
|
|
|
|
err = db.Order("created_at desc").Find(&questions).Error
|
|
if err != nil {
|
|
return questions, total, err
|
|
}
|
|
|
|
// 手动查询关联的知识点信息
|
|
kpIds := make([]uint, 0)
|
|
kpMap := make(map[uint]learning.KnowledgePoint)
|
|
|
|
for _, q := range questions {
|
|
if q.KnowledgePointId != 0 {
|
|
kpIds = append(kpIds, q.KnowledgePointId)
|
|
}
|
|
}
|
|
|
|
if len(kpIds) > 0 {
|
|
var knowledgePoints []learning.KnowledgePoint
|
|
global.GVA_DB.WithContext(ctx).Where("id in ?", kpIds).Find(&knowledgePoints)
|
|
for _, kp := range knowledgePoints {
|
|
kpMap[kp.ID] = kp
|
|
}
|
|
|
|
// 为每个题目设置知识点信息
|
|
for i := range questions {
|
|
if kp, exists := kpMap[questions[i].KnowledgePointId]; exists {
|
|
questions[i].KnowledgePoint = kp
|
|
}
|
|
}
|
|
}
|
|
|
|
return questions, total, err
|
|
}
|
|
|
|
// GetQuestionsByKnowledgePoint 根据知识点ID获取题目列表
|
|
func (questionService *QuestionService) GetQuestionsByKnowledgePoint(ctx context.Context, knowledgePointId uint) (list []learning.Question, err error) {
|
|
err = global.GVA_DB.WithContext(ctx).Where("knowledge_point_id = ?", knowledgePointId).Order("created_at asc").Find(&list).Error
|
|
return
|
|
}
|
|
|
|
// GetRandomQuestionsByKnowledgePoint 根据知识点ID随机获取题目
|
|
func (questionService *QuestionService) GetRandomQuestionsByKnowledgePoint(ctx context.Context, knowledgePointId uint, count int) (list []learning.Question, err error) {
|
|
err = global.GVA_DB.WithContext(ctx).Where("knowledge_point_id = ?", knowledgePointId).Order("RAND()").Limit(count).Find(&list).Error
|
|
return
|
|
}
|