Просмотр исходного кода

fix:培训考试试卷详情添加问题列表

liuyaqi 3 лет назад
Родитель
Сommit
f6fd045f66
3 измененных файлов с 24 добавлено и 10 удалено
  1. 1 1
      model/learning/learning_testpaper.go
  2. 10 3
      service/learning/question.go
  3. 13 6
      service/learning/testpaper.go

+ 1 - 1
model/learning/learning_testpaper.go

@@ -26,7 +26,7 @@ type LearningTestpaperGetReq struct {
 
 type LearningTestpaperGetRsp struct {
 	LearningTestpaper
-	Question   []int                       `json:"question"`   // 包含的试题
+	Question   []*LearningQuestionGetRsp   `json:"question"`   // 包含的试题
 	ExamRecord []*LearningExamRecordGetRsp `json:"examRecord"` // 考试记录
 }
 

+ 10 - 3
service/learning/question.go

@@ -98,13 +98,20 @@ func (s LearningQuestionService) List(ctx context.Context, req *learning.Learnin
 	if err != nil {
 		return 0, nil, err
 	}
+	questions, err := ConvLearningQuestionGetRsp(ent)
+	if err != nil {
+		return 0, nil, err
+	}
+	return total, questions, err
+}
 
+func ConvLearningQuestionGetRsp(ent []*learning.LearningQuestion) ([]*learning.LearningQuestionGetRsp, error) {
 	var questions []*learning.LearningQuestionGetRsp
 	for _, q := range ent {
 		content := []learning.LearningQuestionOption{}
-		err = json.Unmarshal([]byte(q.Content), &content)
+		err := json.Unmarshal([]byte(q.Content), &content)
 		if err != nil {
-			return 0, nil, err
+			return nil, err
 		}
 		answer := []string{}
 		for _, item := range content {
@@ -118,7 +125,7 @@ func (s LearningQuestionService) List(ctx context.Context, req *learning.Learnin
 			Content:          content,
 		})
 	}
-	return total, questions, err
+	return questions, nil
 }
 
 func (s LearningQuestionService) Add(ctx context.Context, req *learning.LearningQuestionAddReq) (int, error) {

+ 13 - 6
service/learning/testpaper.go

@@ -2,6 +2,7 @@ package learning
 
 import (
 	"context"
+	"database/sql"
 	"fmt"
 	"lims_adapter/dao/learning"
 	"lims_adapter/model/learning"
@@ -66,19 +67,25 @@ func (s LearningTestpaperService) Get(ctx context.Context, req *learning.Learnin
 	if err != nil {
 		return nil, err
 	}
-	bind, err := s.QuestionTestpaperDao.Where("TestpaperId", req.Id).All()
-	if err != nil {
+
+	questionEnt := []*learning.LearningQuestion{}
+	err = s.QuestionTestpaperDao.
+		LeftJoin("learning_question",
+			"learning_question_testpaper.QuestionId=learning_question.Id").
+		Where("learning_question_testpaper.TestpaperId", req.Id).
+		Fields("learning_question.*").Structs(&questionEnt)
+	if err != nil && err != sql.ErrNoRows {
 		return nil, err
 	}
-	question := []int{}
-	for _, b := range bind {
-		question = append(question, b.QuestionId)
+	quesitons, err := ConvLearningQuestionGetRsp(questionEnt)
+	if err != nil {
+		return nil, err
 	}
 
 	return &learning.LearningTestpaperGetRsp{
 		LearningTestpaper: *tp,
 		ExamRecord:        record,
-		Question:          question,
+		Question:          quesitons,
 	}, nil
 }