Selaa lähdekoodia

feature(培训考试): 考试记录显示正确率与题目总数

liuyaqi 3 vuotta sitten
vanhempi
commit
6c982e8df5

+ 30 - 24
dao/learning/internal/learning_exam_record.go

@@ -26,14 +26,16 @@ type LearningExamRecordDao struct {
 
 // LearningExamRecordColumns defines and stores column names for table learning_exam_record.
 type learningExamRecordColumns struct {
-	Id          string // Id
-	UserId      string // 用户 Id
-	SkillId     string // 技能 Id
-	TestpaperId string // 试卷 Id
-	Status      string // 状态 1 通过 2 未通过
-	CreatedAt   string // 创建时间
-	UpdatedAt   string // 更新时间
-	DeletedAt   string // 删除时间
+	Id            string // Id
+	UserId        string // 用户 Id
+	SkillId       string // 技能 Id
+	TestpaperId   string // 试卷 Id
+	QuestionCount string // 试卷问题数
+	CorrectCount  string // 答对问题数
+	Status        string // 状态 1 通过 2 未通过
+	CreatedAt     string // 创建时间
+	UpdatedAt     string // 更新时间
+	DeletedAt     string // 删除时间
 }
 
 var (
@@ -43,14 +45,16 @@ var (
 		DB:    g.DB("default"),
 		Table: "learning_exam_record",
 		Columns: learningExamRecordColumns{
-			Id:          "Id",
-			UserId:      "UserId",
-			SkillId:     "SkillId",
-			TestpaperId: "TestpaperId",
-			Status:      "Status",
-			CreatedAt:   "CreatedAt",
-			UpdatedAt:   "UpdatedAt",
-			DeletedAt:   "DeletedAt",
+			Id:            "Id",
+			UserId:        "UserId",
+			SkillId:       "SkillId",
+			TestpaperId:   "TestpaperId",
+			QuestionCount: "QuestionCount",
+			CorrectCount:  "CorrectCount",
+			Status:        "Status",
+			CreatedAt:     "CreatedAt",
+			UpdatedAt:     "UpdatedAt",
+			DeletedAt:     "DeletedAt",
 		},
 	}
 )
@@ -62,14 +66,16 @@ func NewLearningExamRecordDao(tenant string) LearningExamRecordDao {
 		DB:    g.DB(tenant),
 		Table: "learning_exam_record",
 		Columns: learningExamRecordColumns{
-			Id:          "Id",
-			UserId:      "UserId",
-			SkillId:     "SkillId",
-			TestpaperId: "TestpaperId",
-			Status:      "Status",
-			CreatedAt:   "CreatedAt",
-			UpdatedAt:   "UpdatedAt",
-			DeletedAt:   "DeletedAt",
+			Id:            "Id",
+			UserId:        "UserId",
+			SkillId:       "SkillId",
+			TestpaperId:   "TestpaperId",
+			QuestionCount: "QuestionCount",
+			CorrectCount:  "CorrectCount",
+			Status:        "Status",
+			CreatedAt:     "CreatedAt",
+			UpdatedAt:     "UpdatedAt",
+			DeletedAt:     "DeletedAt",
 		},
 	}
 	return dao

+ 10 - 8
model/learning/internal/learning_exam_record.go

@@ -10,12 +10,14 @@ import (
 
 // LearningExamRecord is the golang structure for table learning_exam_record.
 type LearningExamRecord struct {
-	Id          int         `orm:"Id,primary"  json:"id"`          // Id
-	UserId      int         `orm:"UserId"      json:"userId"`      // 用户 Id
-	SkillId     int         `orm:"SkillId"     json:"skillId"`     // 技能 Id
-	TestpaperId int         `orm:"TestpaperId" json:"testpaperId"` // 试卷 Id
-	Status      int         `orm:"Status"      json:"status"`      // 状态 1 通过 2 未通过
-	CreatedAt   *gtime.Time `orm:"CreatedAt"   json:"createdAt"`   // 创建时间
-	UpdatedAt   *gtime.Time `orm:"UpdatedAt"   json:"updatedAt"`   // 更新时间
-	DeletedAt   *gtime.Time `orm:"DeletedAt"   json:"deletedAt"`   // 删除时间
+	Id            int         `orm:"Id,primary"    json:"id"`            // Id
+	UserId        int         `orm:"UserId"        json:"userId"`        // 用户 Id
+	SkillId       int         `orm:"SkillId"       json:"skillId"`       // 技能 Id
+	TestpaperId   int         `orm:"TestpaperId"   json:"testpaperId"`   // 试卷 Id
+	QuestionCount int         `orm:"QuestionCount" json:"questionCount"` // 试卷问题数
+	CorrectCount  int         `orm:"CorrectCount"  json:"correctCount"`  // 答对问题数
+	Status        int         `orm:"Status"        json:"status"`        // 状态 1 通过 2 未通过
+	CreatedAt     *gtime.Time `orm:"CreatedAt"     json:"createdAt"`     // 创建时间
+	UpdatedAt     *gtime.Time `orm:"UpdatedAt"     json:"updatedAt"`     // 更新时间
+	DeletedAt     *gtime.Time `orm:"DeletedAt"     json:"deletedAt"`     // 删除时间
 }

+ 11 - 4
model/learning/learning_exam_record.go

@@ -27,11 +27,18 @@ type LearningExamRecordGetRsp struct {
 	UserName      string `json:"userName"`      // 用户名称
 	SkillName     string `json:"skillName"`     // 技能名称
 	TestpaperName string `json:"testpaperName"` // 试卷名称
+	CorrectRate   string `json:"correctRate"`
+}
+
+type LearningExamRecordAddAnswer struct {
+	QuestionId int      `json:"questionId"`
+	Answer     []string `json:"answer"`
 }
 
 type LearningExamRecordAddReq struct {
-	UserId      int `json:"userId" v:"required#请输入用户Id"`                     // 用户 Id
-	SkillId     int `json:"skillId" v:"required#请输入技能Id"`                    // 技能 Id
-	TestpaperId int `json:"testpaperId" v:"required#请输入试卷Id"`                // 试卷 Id
-	Status      int `json:"status" v:"required|int-in:1,2#请输入状态|请输入正确的状态类型"` // 状态 1 通过 2 未通过
+	UserId      int                           `json:"userId" v:"required#请输入用户Id"`                     // 用户 Id
+	SkillId     int                           `json:"skillId" v:"required#请输入技能Id"`                    // 技能 Id
+	TestpaperId int                           `json:"testpaperId" v:"required#请输入试卷Id"`                // 试卷 Id
+	Status      int                           `json:"status" v:"required|int-in:1,2#请输入状态|请输入正确的状态类型"` // 状态 1 通过 2 未通过
+	Detail      []LearningExamRecordAddAnswer `json:"detail"`
 }

+ 58 - 14
service/learning/exam_record.go

@@ -7,6 +7,9 @@ import (
 	"lims_adapter/dao/learning"
 	"lims_adapter/model"
 	"lims_adapter/model/learning"
+	"reflect"
+	"sort"
+	"strings"
 
 	"dashoo.cn/micro_libary/micro_srv"
 	"dashoo.cn/micro_libary/myerrors"
@@ -16,9 +19,11 @@ import (
 )
 
 type LearningExamRecordService struct {
-	Dao      *dao.LearningExamRecordDao
-	Tenant   string
-	userInfo request.UserInfo
+	Dao          *dao.LearningExamRecordDao
+	TestpaperDao *dao.LearningTestpaperDao
+	QuestionSrv  *LearningQuestionService
+	Tenant       string
+	userInfo     request.UserInfo
 }
 
 func NewLearningExamRecordService(ctx context.Context) (*LearningExamRecordService, error) {
@@ -31,10 +36,16 @@ func NewLearningExamRecordService(ctx context.Context) (*LearningExamRecordServi
 	if err != nil {
 		return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
 	}
+	tps, err := NewLearningQuestionService(ctx)
+	if err != nil {
+		return nil, err
+	}
 	return &LearningExamRecordService{
-		Dao:      dao.NewLearningExamRecordDao(tenant),
-		Tenant:   tenant,
-		userInfo: userInfo,
+		Dao:          dao.NewLearningExamRecordDao(tenant),
+		TestpaperDao: dao.NewLearningTestpaperDao(tenant),
+		QuestionSrv:  tps,
+		Tenant:       tenant,
+		userInfo:     userInfo,
 	}, nil
 }
 
@@ -95,6 +106,12 @@ func (s LearningExamRecordService) List(ctx context.Context, req *learning.Learn
 	if err != nil && err != sql.ErrNoRows {
 		return 0, nil, err
 	}
+	for i := range records {
+		if records[i].QuestionCount == 0 || records[i].CorrectCount == 0 {
+			records[i].CorrectRate = "0%"
+		}
+		records[i].CorrectRate = fmt.Sprintf("%.f%%", float64(records[i].CorrectCount)/float64(records[i].QuestionCount)*100)
+	}
 	return total, records, err
 }
 
@@ -117,21 +134,48 @@ func (s LearningExamRecordService) Add(ctx context.Context, req *learning.Learni
 	if r.IsEmpty() {
 		return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
 	}
-	r, err = s.Dao.DB.Table("learning_testpaper").Where("Id = ?", req.TestpaperId).One()
+
+	tp, err := s.TestpaperDao.Where("Id = ?", req.TestpaperId).One()
 	if err != nil {
 		return 0, err
 	}
-	if r.IsEmpty() {
+	if tp == nil {
 		return 0, myerrors.NewMsgError(nil, fmt.Sprintf("试卷不存在: %d", req.TestpaperId))
 	}
 
+	qustions, err := s.QuestionSrv.ListByTestpaperId(ctx, req.TestpaperId)
+	if err != nil {
+		return 0, err
+	}
+	questionCount := len(qustions)
+	correctCount := 0
+	userAnswerMap := map[int][]string{}
+	for _, a := range req.Detail {
+		userAnswerMap[a.QuestionId] = a.Answer
+	}
+	for _, q := range qustions {
+		answer := strings.Split(strings.ToUpper(q.Answer), " ")
+		userAnswer := userAnswerMap[q.Id]
+		if len(answer) != len(userAnswer) {
+			continue
+		}
+		sort.Strings(answer)
+		sort.Strings(userAnswer)
+		// fmt.Println(q, answer, userAnswer, reflect.DeepEqual(answer, userAnswer))
+		if reflect.DeepEqual(answer, userAnswer) {
+			correctCount++
+		}
+	}
+
 	id, err := s.Dao.InsertAndGetId(learning.LearningExamRecord{
-		UserId:      req.UserId,
-		SkillId:     req.SkillId,
-		TestpaperId: req.TestpaperId,
-		Status:      req.Status,
-		CreatedAt:   gtime.Now(),
-		UpdatedAt:   gtime.Now(),
+		UserId:        req.UserId,
+		SkillId:       req.SkillId,
+		TestpaperId:   req.TestpaperId,
+		QuestionCount: questionCount,
+		CorrectCount:  correctCount,
+		Status:        req.Status,
+		CreatedAt:     gtime.Now(),
+		UpdatedAt:     gtime.Now(),
 	})
 	return int(id), err
 }

+ 22 - 6
service/learning/question.go

@@ -3,6 +3,7 @@ package learning
 import (
 	"bytes"
 	"context"
+	"database/sql"
 	"encoding/json"
 	"fmt"
 	"io/ioutil"
@@ -21,9 +22,10 @@ import (
 )
 
 type LearningQuestionService struct {
-	Dao      *dao.LearningQuestionDao
-	Tenant   string
-	userInfo request.UserInfo
+	Dao                  *dao.LearningQuestionDao
+	QuestionTestpaperDao *dao.LearningQuestionTestpaperDao
+	Tenant               string
+	userInfo             request.UserInfo
 }
 
 func NewLearningQuestionService(ctx context.Context) (*LearningQuestionService, error) {
@@ -37,12 +39,26 @@ func NewLearningQuestionService(ctx context.Context) (*LearningQuestionService,
 		return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
 	}
 	return &LearningQuestionService{
-		Dao:      dao.NewLearningQuestionDao(tenant),
-		Tenant:   tenant,
-		userInfo: userInfo,
+		Dao:                  dao.NewLearningQuestionDao(tenant),
+		QuestionTestpaperDao: dao.NewLearningQuestionTestpaperDao(tenant),
+		Tenant:               tenant,
+		userInfo:             userInfo,
 	}, nil
 }
 
+func (s LearningQuestionService) ListByTestpaperId(ctx context.Context, testpaperId int) (ent []*learning.LearningQuestionGetRsp, err error) {
+	questionEnt := []*learning.LearningQuestion{}
+	err = s.QuestionTestpaperDao.
+		LeftJoin("learning_question",
+			"learning_question_testpaper.QuestionId=learning_question.Id").
+		Where("learning_question_testpaper.TestpaperId", testpaperId).
+		Fields("learning_question.*").Structs(&questionEnt)
+	if err != nil && err != sql.ErrNoRows {
+		return nil, err
+	}
+	return ConvLearningQuestionGetRsp(questionEnt)
+}
+
 func (s LearningQuestionService) Get(ctx context.Context, req *learning.LearningQuestionGetReq) (ent *learning.LearningQuestionGetRsp, err error) {
 	validErr := gvalid.CheckStruct(ctx, req, nil)
 	if validErr != nil {

+ 2 - 0
sql/create.sql

@@ -111,6 +111,8 @@ CREATE TABLE `learning_exam_record` (
   `UserId` int NOT NULL COMMENT '用户 Id',
   `SkillId` int NOT NULL COMMENT '技能 Id',
   `TestpaperId` int NOT NULL COMMENT '试卷 Id',
+  `QuestionCount` int NOT NULL COMMENT '试卷问题数',
+  `CorrectCount` int NOT NULL COMMENT '答对问题数',
   `Status` int NOT NULL COMMENT '状态 1 通过 2 未通过',
   `CreatedAt` datetime COMMENT '创建时间',
   `UpdatedAt` datetime COMMENT '更新时间',

+ 19 - 0
swaggerui/swagger.yml

@@ -1214,6 +1214,7 @@ components:
         - skillId
         - testpaperId
         - status
+        - detail
       properties:
         skillId:
           type: integer
@@ -1224,6 +1225,20 @@ components:
         status:
           type: integer
           description: 状态 1 通过 2 未通过
+        detail:
+          type: array
+          description: 答题详情
+          items:
+            type: object
+            properties:
+              questionId:
+                type: integer
+                description: 问题 Id
+              answer:
+                type: array
+                description: 答案
+                items:
+                  type: string
     LearningExamRecordListMy:
       type: object
       properties:
@@ -1618,6 +1633,10 @@ components:
         skillId: 2
         testpaperId: 2
         status: 2
+        detail:
+          -
+            questionId: 1
+            answer: [A]
     LearningExamRecordListMy:
       value:
         skillId: 2