350 lines
11 KiB
Go
350 lines
11 KiB
Go
package learning
|
||
|
||
import (
|
||
"strconv"
|
||
|
||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
|
||
"github.com/flipped-aurora/gin-vue-admin/server/model/learning"
|
||
learningReq "github.com/flipped-aurora/gin-vue-admin/server/model/learning/request"
|
||
"github.com/flipped-aurora/gin-vue-admin/server/service"
|
||
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
||
"github.com/gin-gonic/gin"
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
type UserExamApi struct{}
|
||
|
||
// CreateUserExam 创建用户考试记录
|
||
// @Tags UserExam
|
||
// @Summary 创建用户考试记录
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data body learningReq.UserExamCreate true "创建用户考试记录"
|
||
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
||
// @Router /userExam/createUserExam [post]
|
||
func (userExamApi *UserExamApi) CreateUserExam(c *gin.Context) {
|
||
var req learningReq.UserExamCreate
|
||
err := c.ShouldBindJSON(&req)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
userExam := learning.UserExam{
|
||
UserId: req.UserId,
|
||
ExamId: req.ExamId,
|
||
AttemptNumber: req.AttemptNumber,
|
||
StartTime: req.StartTime,
|
||
EndTime: req.EndTime,
|
||
Duration: req.Duration,
|
||
TotalScore: req.TotalScore,
|
||
CorrectCount: req.CorrectCount,
|
||
WrongCount: req.WrongCount,
|
||
Status: req.Status,
|
||
IsPassed: req.IsPassed,
|
||
SubmittedAt: req.SubmittedAt,
|
||
}
|
||
|
||
err = service.ServiceGroupApp.LearningServiceGroup.UserExamService.CreateUserExam(c.Request.Context(), &userExam)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("创建失败!", zap.Error(err))
|
||
response.FailWithMessage("创建失败", c)
|
||
return
|
||
}
|
||
response.OkWithMessage("创建成功", c)
|
||
}
|
||
|
||
// DeleteUserExam 删除用户考试记录
|
||
// @Tags UserExam
|
||
// @Summary 删除用户考试记录
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data body request.IdsReq true "批量删除用户考试记录"
|
||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||
// @Router /userExam/deleteUserExam [delete]
|
||
func (userExamApi *UserExamApi) DeleteUserExam(c *gin.Context) {
|
||
ID := c.Query("ID")
|
||
err := service.ServiceGroupApp.LearningServiceGroup.UserExamService.DeleteUserExam(c.Request.Context(), ID)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("删除失败!", zap.Error(err))
|
||
response.FailWithMessage("删除失败", c)
|
||
return
|
||
}
|
||
response.OkWithMessage("删除成功", c)
|
||
}
|
||
|
||
// DeleteUserExamByIds 批量删除用户考试记录
|
||
// @Tags UserExam
|
||
// @Summary 批量删除用户考试记录
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
|
||
// @Router /userExam/deleteUserExamByIds [delete]
|
||
func (userExamApi *UserExamApi) DeleteUserExamByIds(c *gin.Context) {
|
||
IDs := c.QueryArray("IDs[]")
|
||
err := service.ServiceGroupApp.LearningServiceGroup.UserExamService.DeleteUserExamByIds(c.Request.Context(), IDs)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
|
||
response.FailWithMessage("批量删除失败", c)
|
||
return
|
||
}
|
||
response.OkWithMessage("批量删除成功", c)
|
||
}
|
||
|
||
// UpdateUserExam 更新用户考试记录
|
||
// @Tags UserExam
|
||
// @Summary 更新用户考试记录
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data body learningReq.UserExamUpdate true "更新用户考试记录"
|
||
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
||
// @Router /userExam/updateUserExam [put]
|
||
func (userExamApi *UserExamApi) UpdateUserExam(c *gin.Context) {
|
||
var req learningReq.UserExamUpdate
|
||
err := c.ShouldBindJSON(&req)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
userExam := learning.UserExam{
|
||
GVA_MODEL: global.GVA_MODEL{ID: req.ID},
|
||
UserId: req.UserId,
|
||
ExamId: req.ExamId,
|
||
AttemptNumber: req.AttemptNumber,
|
||
StartTime: req.StartTime,
|
||
EndTime: req.EndTime,
|
||
Duration: req.Duration,
|
||
TotalScore: req.TotalScore,
|
||
CorrectCount: req.CorrectCount,
|
||
WrongCount: req.WrongCount,
|
||
Status: req.Status,
|
||
IsPassed: req.IsPassed,
|
||
SubmittedAt: req.SubmittedAt,
|
||
}
|
||
|
||
err = service.ServiceGroupApp.LearningServiceGroup.UserExamService.UpdateUserExam(c.Request.Context(), userExam)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("更新失败!", zap.Error(err))
|
||
response.FailWithMessage("更新失败", c)
|
||
return
|
||
}
|
||
response.OkWithMessage("更新成功", c)
|
||
}
|
||
|
||
// FindUserExam 用id查询用户考试记录
|
||
// @Tags UserExam
|
||
// @Summary 用id查询用户考试记录
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data query learning.UserExam true "用id查询用户考试记录"
|
||
// @Success 200 {object} response.Response{data=learning.UserExam,msg=string} "查询成功"
|
||
// @Router /userExam/findUserExam [get]
|
||
func (userExamApi *UserExamApi) FindUserExam(c *gin.Context) {
|
||
ID := c.Query("ID")
|
||
reuserExam, err := service.ServiceGroupApp.LearningServiceGroup.UserExamService.GetUserExam(c.Request.Context(), ID)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("查询失败!", zap.Error(err))
|
||
response.FailWithMessage("查询失败", c)
|
||
return
|
||
}
|
||
response.OkWithData(reuserExam, c)
|
||
}
|
||
|
||
// GetUserExamList 分页获取用户考试记录列表
|
||
// @Tags UserExam
|
||
// @Summary 分页获取用户考试记录列表
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data query learningReq.UserExamSearch true "分页获取用户考试记录列表"
|
||
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
|
||
// @Router /userExam/getUserExamList [get]
|
||
func (userExamApi *UserExamApi) GetUserExamList(c *gin.Context) {
|
||
var pageInfo learningReq.UserExamSearch
|
||
err := c.ShouldBindQuery(&pageInfo)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
list, total, err := service.ServiceGroupApp.LearningServiceGroup.UserExamService.GetUserExamInfoList(c.Request.Context(), pageInfo)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
||
response.FailWithMessage("获取失败", c)
|
||
return
|
||
}
|
||
response.OkWithDetailed(response.PageResult{
|
||
List: list,
|
||
Total: total,
|
||
Page: pageInfo.Page,
|
||
PageSize: pageInfo.PageSize,
|
||
}, "获取成功", c)
|
||
}
|
||
|
||
// StartExam 开始考试
|
||
// @Tags UserExam
|
||
// @Summary 开始考试
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data body learningReq.StartExamRequest true "开始考试请求"
|
||
// @Success 200 {object} response.Response{data=learning.UserExam,msg=string} "开始成功"
|
||
// @Router /userExam/startExam [post]
|
||
func (userExamApi *UserExamApi) StartExam(c *gin.Context) {
|
||
var req learningReq.StartExamRequest
|
||
err := c.ShouldBindJSON(&req)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
// 如果没有提供userId,从JWT中获取当前用户ID
|
||
if req.UserId == 0 {
|
||
req.UserId = uint(utils.GetUserID(c))
|
||
}
|
||
|
||
userExam, err := service.ServiceGroupApp.LearningServiceGroup.UserExamService.StartExam(c.Request.Context(), req.UserId, req.ExamId)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("开始考试失败!", zap.Error(err))
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithData(userExam, c)
|
||
}
|
||
|
||
// SubmitExam 提交考试
|
||
// @Tags UserExam
|
||
// @Summary 提交考试
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data body learningReq.SubmitExamRequest true "提交考试请求"
|
||
// @Success 200 {object} response.Response{msg=string} "提交成功"
|
||
// @Router /userExam/submitExam [put]
|
||
func (userExamApi *UserExamApi) SubmitExam(c *gin.Context) {
|
||
var req learningReq.SubmitExamRequest
|
||
err := c.ShouldBindJSON(&req)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
err = service.ServiceGroupApp.LearningServiceGroup.UserExamService.SubmitExam(c.Request.Context(), req.UserExamId, req.TotalScore, req.CorrectCount, req.WrongCount)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("提交考试失败!", zap.Error(err))
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithMessage("提交成功", c)
|
||
}
|
||
|
||
// GetUserExamHistory 获取用户考试历史
|
||
// @Tags UserExam
|
||
// @Summary 获取用户考试历史
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param userId query uint false "用户ID"
|
||
// @Success 200 {object} response.Response{data=[]learning.UserExam,msg=string} "获取成功"
|
||
// @Router /userExam/getUserExamHistory [get]
|
||
func (userExamApi *UserExamApi) GetUserExamHistory(c *gin.Context) {
|
||
userIdStr := c.Query("userId")
|
||
|
||
var userId uint
|
||
if userIdStr != "" {
|
||
userIdUint, err := strconv.ParseUint(userIdStr, 10, 32)
|
||
if err != nil {
|
||
response.FailWithMessage("用户ID格式错误", c)
|
||
return
|
||
}
|
||
userId = uint(userIdUint)
|
||
} else {
|
||
// 如果没有提供userId,从JWT中获取当前用户ID
|
||
userId = uint(utils.GetUserID(c))
|
||
}
|
||
|
||
list, err := service.ServiceGroupApp.LearningServiceGroup.UserExamService.GetUserExamHistory(c.Request.Context(), userId)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
||
response.FailWithMessage("获取失败", c)
|
||
return
|
||
}
|
||
response.OkWithData(list, c)
|
||
}
|
||
|
||
// GetUserExamStatistics 获取用户考试统计
|
||
// @Tags UserExam
|
||
// @Summary 获取用户考试统计
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param userId query uint false "用户ID"
|
||
// @Success 200 {object} response.Response{data=learningReq.UserExamStatistics,msg=string} "获取成功"
|
||
// @Router /userExam/getUserExamStatistics [get]
|
||
func (userExamApi *UserExamApi) GetUserExamStatistics(c *gin.Context) {
|
||
userIdStr := c.Query("userId")
|
||
|
||
var userId uint
|
||
if userIdStr != "" {
|
||
userIdUint, err := strconv.ParseUint(userIdStr, 10, 32)
|
||
if err != nil {
|
||
response.FailWithMessage("用户ID格式错误", c)
|
||
return
|
||
}
|
||
userId = uint(userIdUint)
|
||
} else {
|
||
// 如果没有提供userId,从JWT中获取当前用户ID
|
||
userId = uint(utils.GetUserID(c))
|
||
}
|
||
|
||
stats, err := service.ServiceGroupApp.LearningServiceGroup.UserExamService.GetUserExamStatistics(c.Request.Context(), userId)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
||
response.FailWithMessage("获取失败", c)
|
||
return
|
||
}
|
||
response.OkWithData(stats, c)
|
||
}
|
||
|
||
// GetExamRanking 获取考试排行榜
|
||
// @Tags UserExam
|
||
// @Summary 获取考试排行榜
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param examId query uint true "考试ID"
|
||
// @Param limit query int false "限制数量"
|
||
// @Success 200 {object} response.Response{data=learningReq.ExamRankingResponse,msg=string} "获取成功"
|
||
// @Router /userExam/getExamRanking [get]
|
||
func (userExamApi *UserExamApi) GetExamRanking(c *gin.Context) {
|
||
examIdStr := c.Query("examId")
|
||
limitStr := c.Query("limit")
|
||
|
||
examId, err := strconv.ParseUint(examIdStr, 10, 32)
|
||
if err != nil {
|
||
response.FailWithMessage("考试ID格式错误", c)
|
||
return
|
||
}
|
||
|
||
limit := 10 // 默认限制10条
|
||
if limitStr != "" {
|
||
if l, err := strconv.Atoi(limitStr); err == nil {
|
||
limit = l
|
||
}
|
||
}
|
||
|
||
ranking, err := service.ServiceGroupApp.LearningServiceGroup.UserExamService.GetExamRanking(c.Request.Context(), uint(examId), limit)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
||
response.FailWithMessage("获取失败", c)
|
||
return
|
||
}
|
||
response.OkWithData(ranking, c)
|
||
}
|