diff --git a/api/controller/question.go b/api/controller/question.go index 4eb5259..effdd75 100644 --- a/api/controller/question.go +++ b/api/controller/question.go @@ -45,13 +45,39 @@ func (r *Question) GetQuestionPage(c *gin.Context) { } // 处理返回值 - GetQuestionPageResponses := dto.GetQuestionListDto(question) + g := dto.GetQuestionListDto(question) + + for _, v := range g { + // 加载一级标签 + if v.FirstLabelId != nil { + labelDao := dao.LabelDao{} + firstLabel, err := labelDao.GetLabelFirstById(*v.FirstLabelId) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + v.LoadFirstLabel(firstLabel) + } + + // 加载二级标签 + if v.SecondLabelId != nil { + labelDao := dao.LabelDao{} + secondLabel, err := labelDao.GetLabelFirstById(*v.SecondLabelId) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + v.LoadSecondLabel(secondLabel) + } + } result := make(map[string]interface{}) result["page"] = req.Page result["page_size"] = req.PageSize result["total"] = total - result["data"] = GetQuestionPageResponses + result["data"] = g responses.OkWithData(result, c) } @@ -72,13 +98,13 @@ func (r *Question) GetQuestion(c *gin.Context) { // 业务处理 questionService := service.QuestionService{} - getQuestionResponses, err := questionService.GetQuestion(questionId) + g, err := questionService.GetQuestion(questionId) if err != nil { responses.FailWithMessage(err.Error(), c) return } - responses.OkWithData(getQuestionResponses, c) + responses.OkWithData(g, c) } // AddQuestion 新增题目 diff --git a/api/controller/questionQaItem.go b/api/controller/questionQaItem.go index 222b7ff..d67073a 100644 --- a/api/controller/questionQaItem.go +++ b/api/controller/questionQaItem.go @@ -45,17 +45,39 @@ func (r *QuestionQaItem) GetQuestionQaItemPage(c *gin.Context) { } // 处理返回值 - questionQaItemPageResponses := dto.GetQuestionQaItemListDto(questionQaItem) - if err != nil { - responses.FailWithMessage(err.Error(), c) - return + g := dto.GetQuestionQaItemListDto(questionQaItem) + + for _, v := range g { + // 加载一级标签 + if v.Question.FirstLabelId != nil { + labelDao := dao.LabelDao{} + firstLabel, err := labelDao.GetLabelFirstById(*v.Question.FirstLabelId) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + v.LoadFirstLabel(firstLabel) + } + + // 加载二级标签 + if v.Question.SecondLabelId != nil { + labelDao := dao.LabelDao{} + secondLabel, err := labelDao.GetLabelFirstById(*v.Question.SecondLabelId) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + v.LoadSecondLabel(secondLabel) + } } result := make(map[string]interface{}) result["page"] = req.Page result["page_size"] = req.PageSize result["total"] = total - result["data"] = questionQaItemPageResponses + result["data"] = g responses.OkWithData(result, c) } diff --git a/api/dao/Question.go b/api/dao/Question.go index 1e9545d..8e89d66 100644 --- a/api/dao/Question.go +++ b/api/dao/Question.go @@ -118,16 +118,6 @@ func (r *QuestionDao) GetQuestionPageSearch(req requests.GetQuestionPage, page, // 构建查询条件 query := global.Db.Model(&model.Question{}) - // 一级标签 - query = query.Preload("FirstLabel", func(db *gorm.DB) *gorm.DB { - return db.Select("label_id", "label_name") - }) - - // 二级标签 - query = query.Preload("SecondLabel", func(db *gorm.DB) *gorm.DB { - return db.Select("label_id", "label_name") - }) - // 主键id if req.QuestionId != "" { query = query.Where("question_id = ?", req.QuestionId) diff --git a/api/dao/QuestionQaItem.go b/api/dao/QuestionQaItem.go index 6f12970..d2b7c81 100644 --- a/api/dao/QuestionQaItem.go +++ b/api/dao/QuestionQaItem.go @@ -129,16 +129,6 @@ func (r *QuestionQaItemDao) GetQuestionQaItemPageSearch(req requests.GetQuestion return db.Select("question_id", "question_name", "question_type", "question_source", "difficulty", "first_label_id", "second_label_id") }) - // 一级标签 - query = query.Preload("Question.FirstLabel", func(db *gorm.DB) *gorm.DB { - return db.Select("label_id", "label_name") - }) - - // 二级标签 - query = query.Preload("Question.SecondLabel", func(db *gorm.DB) *gorm.DB { - return db.Select("label_id", "label_name") - }) - // 题库id query = query.Where("qa_id = ?", req.QaId) diff --git a/api/dto/Question.go b/api/dto/Question.go index 0fc4014..cd57f7c 100644 --- a/api/dto/Question.go +++ b/api/dto/Question.go @@ -19,6 +19,8 @@ type QuestionDto struct { QuestionAnswer []string `json:"question_answer"` // 答案 QuestionAnalysis string `json:"question_analysis"` // 解析 Difficulty int `json:"difficulty"` // 难度(0:未知 1:低 2:中 3:高) + FirstLabelId *int64 `json:"first_label_id"` // 一级标签id + SecondLabelId *int64 `json:"second_label_id"` // 二级标签id CreatedAt model.LocalTime `json:"created_at"` // 创建时间 UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间 FirstLabel *LabelDto `json:"first_label"` // 一级标签 @@ -39,6 +41,8 @@ func GetQuestionDto(m *model.Question) *QuestionDto { QuestionAnswer: []string{}, QuestionAnalysis: m.QuestionAnalysis, Difficulty: m.Difficulty, + FirstLabelId: m.FirstLabelId, + SecondLabelId: m.SecondLabelId, CreatedAt: m.CreatedAt, UpdatedAt: m.UpdatedAt, } @@ -62,20 +66,12 @@ func GetQuestionListDto(m []*model.Question) []*QuestionDto { QuestionAnswer: []string{}, QuestionAnalysis: v.QuestionAnalysis, Difficulty: v.Difficulty, + FirstLabelId: v.FirstLabelId, + SecondLabelId: v.SecondLabelId, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, } - // 加载一级标签 - if v.FirstLabel != nil { - response = response.LoadFirstLabel(v.FirstLabel) - } - - // 加载二级标签 - if v.SecondLabel != nil { - response = response.LoadSecondLabel(v.SecondLabel) - } - // 加载图片 if v.QuestionImage != "" { response = response.LoadQuestionImage(v.QuestionImage) diff --git a/api/dto/QuestionQaItem.go b/api/dto/QuestionQaItem.go index d1df31a..d6ee762 100644 --- a/api/dto/QuestionQaItem.go +++ b/api/dto/QuestionQaItem.go @@ -51,15 +51,15 @@ func GetQuestionQaItemListDto(m []*model.QuestionQaItem) []*QuestionQaItemDto { response = response.LoadQuestion(v.Question) } - // 加载一级标签 - if v.Question.FirstLabel != nil { - response = response.LoadFirstLabel(v.Question.FirstLabel) - } - - // 加载二级标签 - if v.Question.SecondLabel != nil { - response = response.LoadSecondLabel(v.Question.SecondLabel) - } + //// 加载一级标签 + //if v.Question.FirstLabel != nil { + // response = response.LoadFirstLabel(v.Question.FirstLabel) + //} + // + //// 加载二级标签 + //if v.Question.SecondLabel != nil { + // response = response.LoadSecondLabel(v.Question.SecondLabel) + //} // 将转换后的结构体添加到新切片中 responses[i] = response diff --git a/api/model/Question.go b/api/model/Question.go index 8234c2d..1bd82e1 100644 --- a/api/model/Question.go +++ b/api/model/Question.go @@ -21,8 +21,6 @@ type Question struct { FirstLabelId *int64 `gorm:"column:first_label_id;type:bigint(19);comment:一级标签id" json:"first_label_id"` SecondLabelId *int64 `gorm:"column:second_label_id;type:bigint(19);comment:二级标签id" json:"second_label_id"` Model - FirstLabel *Label `gorm:"foreignKey:FirstLabelId;references:label_id" json:"first_label"` - SecondLabel *Label `gorm:"foreignKey:SecondLabelId;references:label_id" json:"second_label"` QuestionOption []*QuestionOption `gorm:"foreignKey:QuestionId;references:question_id" json:"question_option"` } diff --git a/api/service/Question.go b/api/service/Question.go index 3ada9b9..449e8e9 100644 --- a/api/service/Question.go +++ b/api/service/Question.go @@ -807,10 +807,26 @@ func (r *QuestionService) GetQuestion(questionId int64) (g *dto.QuestionDto, err g = dto.GetQuestionDto(question) // 加载一级标签 - g.LoadFirstLabel(question.FirstLabel) + if question.FirstLabelId != nil { + labelDao := dao.LabelDao{} + firstLabel, err := labelDao.GetLabelFirstById(*question.FirstLabelId) + if err != nil { + return nil, errors.New("题目标签存在错误") + } + + g.LoadFirstLabel(firstLabel) + } // 加载二级标签 - g.LoadSecondLabel(question.SecondLabel) + if question.SecondLabelId != nil { + labelDao := dao.LabelDao{} + secondLabel, err := labelDao.GetLabelFirstById(*question.SecondLabelId) + if err != nil { + return nil, errors.New("题目标签存在错误") + } + + g.LoadSecondLabel(secondLabel) + } // 加载选项 g.LoadQuestionOption(question.QuestionOption)