156 lines
4.8 KiB
Go
156 lines
4.8 KiB
Go
package service
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"dd_fiber_api/internal/question"
|
||
"dd_fiber_api/internal/question/dao"
|
||
)
|
||
|
||
// QuestionService 题目服务
|
||
type QuestionService struct {
|
||
questionDAO dao.QuestionDAOInterface
|
||
}
|
||
|
||
// NewQuestionService 创建题目服务
|
||
func NewQuestionService(questionDAO dao.QuestionDAOInterface) *QuestionService {
|
||
return &QuestionService{
|
||
questionDAO: questionDAO,
|
||
}
|
||
}
|
||
|
||
// CreateQuestion 创建题目
|
||
func (s *QuestionService) CreateQuestion(req *CreateQuestionRequest) (*question.Question, error) {
|
||
questionID := question.GenerateID()
|
||
|
||
// 确保 KnowledgeTreeIds 不为 nil
|
||
knowledgeTreeIds := req.KnowledgeTreeIds
|
||
if knowledgeTreeIds == nil {
|
||
knowledgeTreeIds = []string{}
|
||
}
|
||
|
||
questionObj := &question.Question{
|
||
ID: questionID,
|
||
Type: question.QuestionType(req.Type),
|
||
Name: req.Name,
|
||
Source: req.Source,
|
||
MaterialID: req.MaterialID,
|
||
Content: req.Content,
|
||
Options: req.Options,
|
||
Answer: req.Answer,
|
||
Explanation: req.Explanation,
|
||
KnowledgeTreeIDs: knowledgeTreeIds,
|
||
CreatedAt: question.GetCurrentTimestamp(),
|
||
UpdatedAt: question.GetCurrentTimestamp(),
|
||
}
|
||
|
||
err := s.questionDAO.Create(questionObj)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("创建题目失败: %v", err)
|
||
}
|
||
|
||
return questionObj, nil
|
||
}
|
||
|
||
// GetQuestion 获取题目
|
||
func (s *QuestionService) GetQuestion(id string) (*question.Question, error) {
|
||
questionObj, err := s.questionDAO.GetByID(id)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("题目不存在: %v", err)
|
||
}
|
||
|
||
return questionObj, nil
|
||
}
|
||
|
||
// SearchQuestions 搜索题目
|
||
func (s *QuestionService) SearchQuestions(query string, qType question.QuestionType, knowledgeTreeIds []string, page, pageSize int32) ([]*question.Question, int32, error) {
|
||
questions, total, err := s.questionDAO.Search(query, qType, knowledgeTreeIds, page, pageSize)
|
||
if err != nil {
|
||
return nil, 0, fmt.Errorf("搜索题目失败: %v", err)
|
||
}
|
||
|
||
return questions, total, nil
|
||
}
|
||
|
||
// UpdateQuestion 更新题目
|
||
func (s *QuestionService) UpdateQuestion(req *UpdateQuestionRequest) error {
|
||
// 获取现有题目
|
||
questionObj, err := s.questionDAO.GetByID(req.Id)
|
||
if err != nil {
|
||
return fmt.Errorf("题目不存在: %v", err)
|
||
}
|
||
|
||
// 确保 KnowledgeTreeIds 不为 nil
|
||
knowledgeTreeIds := req.KnowledgeTreeIds
|
||
if knowledgeTreeIds == nil {
|
||
knowledgeTreeIds = []string{}
|
||
}
|
||
|
||
// 更新字段
|
||
questionObj.Type = question.QuestionType(req.Type)
|
||
questionObj.Name = req.Name
|
||
questionObj.Source = req.Source
|
||
questionObj.MaterialID = req.MaterialID
|
||
questionObj.Content = req.Content
|
||
questionObj.Options = req.Options
|
||
questionObj.Answer = req.Answer
|
||
questionObj.Explanation = req.Explanation
|
||
questionObj.KnowledgeTreeIDs = knowledgeTreeIds
|
||
questionObj.UpdatedAt = question.GetCurrentTimestamp()
|
||
|
||
// 保存
|
||
err = s.questionDAO.Update(questionObj)
|
||
if err != nil {
|
||
return fmt.Errorf("更新题目失败: %v", err)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// DeleteQuestion 删除题目
|
||
func (s *QuestionService) DeleteQuestion(id string) error {
|
||
err := s.questionDAO.Delete(id)
|
||
if err != nil {
|
||
return fmt.Errorf("删除题目失败: %v", err)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// BatchDeleteQuestions 批量删除题目
|
||
func (s *QuestionService) BatchDeleteQuestions(ids []string) (int, []string, error) {
|
||
deletedCount, failedIDs, err := s.questionDAO.BatchDelete(ids)
|
||
if err != nil {
|
||
return 0, nil, fmt.Errorf("批量删除失败: %v", err)
|
||
}
|
||
|
||
return deletedCount, failedIDs, nil
|
||
}
|
||
|
||
// CreateQuestionRequest 创建题目请求
|
||
type CreateQuestionRequest struct {
|
||
Type int32 `json:"type"`
|
||
Name string `json:"name,omitempty"` // 题目名称(可选)
|
||
Source string `json:"source,omitempty"` // 题目出处(可选)
|
||
MaterialID string `json:"material_id,omitempty"` // 关联材料ID(可选)
|
||
Content string `json:"content"`
|
||
Options []string `json:"options"`
|
||
Answer string `json:"answer"`
|
||
Explanation string `json:"explanation"`
|
||
KnowledgeTreeIds []string `json:"knowledge_tree_ids"` // 关联的知识树ID列表(替代原来的tags)
|
||
}
|
||
|
||
// UpdateQuestionRequest 更新题目请求
|
||
type UpdateQuestionRequest struct {
|
||
Id string `json:"id"`
|
||
Type int32 `json:"type"`
|
||
Name string `json:"name,omitempty"` // 题目名称(可选)
|
||
Source string `json:"source,omitempty"` // 题目出处(可选)
|
||
MaterialID string `json:"material_id,omitempty"` // 关联材料ID(可选)
|
||
Content string `json:"content"`
|
||
Options []string `json:"options"`
|
||
Answer string `json:"answer"`
|
||
Explanation string `json:"explanation"`
|
||
KnowledgeTreeIds []string `json:"knowledge_tree_ids"` // 关联的知识树ID列表(替代原来的tags)
|
||
}
|