263 lines
8.9 KiB
Go
263 lines
8.9 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 UserLearningApi struct{}
|
||
|
||
// CreateUserLearning 创建用户学习记录
|
||
// @Tags UserLearning
|
||
// @Summary 创建用户学习记录
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data body learningReq.UserLearningCreate true "创建用户学习记录"
|
||
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
||
// @Router /userLearning/createUserLearning [post]
|
||
func (userLearningApi *UserLearningApi) CreateUserLearning(c *gin.Context) {
|
||
var req learningReq.UserLearningCreate
|
||
err := c.ShouldBindJSON(&req)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
userLearning := learning.UserLearning{
|
||
UserId: req.UserId,
|
||
CourseId: req.CourseId,
|
||
ChapterId: req.ChapterId,
|
||
KnowledgePointId: req.KnowledgePointId,
|
||
Progress: req.Progress,
|
||
StudyTime: req.StudyTime,
|
||
LastStudyAt: req.LastStudyAt,
|
||
Status: req.Status,
|
||
Score: req.Score,
|
||
CompletedAt: req.CompletedAt,
|
||
}
|
||
|
||
err = service.ServiceGroupApp.LearningServiceGroup.UserLearningService.CreateUserLearning(c.Request.Context(), &userLearning)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("创建失败!", zap.Error(err))
|
||
response.FailWithMessage("创建失败", c)
|
||
return
|
||
}
|
||
response.OkWithMessage("创建成功", c)
|
||
}
|
||
|
||
// DeleteUserLearning 删除用户学习记录
|
||
// @Tags UserLearning
|
||
// @Summary 删除用户学习记录
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data body request.IdsReq true "批量删除用户学习记录"
|
||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||
// @Router /userLearning/deleteUserLearning [delete]
|
||
func (userLearningApi *UserLearningApi) DeleteUserLearning(c *gin.Context) {
|
||
ID := c.Query("ID")
|
||
err := service.ServiceGroupApp.LearningServiceGroup.UserLearningService.DeleteUserLearning(c.Request.Context(), ID)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("删除失败!", zap.Error(err))
|
||
response.FailWithMessage("删除失败", c)
|
||
return
|
||
}
|
||
response.OkWithMessage("删除成功", c)
|
||
}
|
||
|
||
// DeleteUserLearningByIds 批量删除用户学习记录
|
||
// @Tags UserLearning
|
||
// @Summary 批量删除用户学习记录
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
|
||
// @Router /userLearning/deleteUserLearningByIds [delete]
|
||
func (userLearningApi *UserLearningApi) DeleteUserLearningByIds(c *gin.Context) {
|
||
IDs := c.QueryArray("IDs[]")
|
||
err := service.ServiceGroupApp.LearningServiceGroup.UserLearningService.DeleteUserLearningByIds(c.Request.Context(), IDs)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
|
||
response.FailWithMessage("批量删除失败", c)
|
||
return
|
||
}
|
||
response.OkWithMessage("批量删除成功", c)
|
||
}
|
||
|
||
// UpdateUserLearning 更新用户学习记录
|
||
// @Tags UserLearning
|
||
// @Summary 更新用户学习记录
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data body learningReq.UserLearningUpdate true "更新用户学习记录"
|
||
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
||
// @Router /userLearning/updateUserLearning [put]
|
||
func (userLearningApi *UserLearningApi) UpdateUserLearning(c *gin.Context) {
|
||
var req learningReq.UserLearningUpdate
|
||
err := c.ShouldBindJSON(&req)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
userLearning := learning.UserLearning{
|
||
GVA_MODEL: global.GVA_MODEL{ID: req.ID},
|
||
UserId: req.UserId,
|
||
CourseId: req.CourseId,
|
||
ChapterId: req.ChapterId,
|
||
KnowledgePointId: req.KnowledgePointId,
|
||
Progress: req.Progress,
|
||
StudyTime: req.StudyTime,
|
||
LastStudyAt: req.LastStudyAt,
|
||
Status: req.Status,
|
||
Score: req.Score,
|
||
CompletedAt: req.CompletedAt,
|
||
}
|
||
|
||
err = service.ServiceGroupApp.LearningServiceGroup.UserLearningService.UpdateUserLearning(c.Request.Context(), userLearning)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("更新失败!", zap.Error(err))
|
||
response.FailWithMessage("更新失败", c)
|
||
return
|
||
}
|
||
response.OkWithMessage("更新成功", c)
|
||
}
|
||
|
||
// FindUserLearning 用id查询用户学习记录
|
||
// @Tags UserLearning
|
||
// @Summary 用id查询用户学习记录
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data query learning.UserLearning true "用id查询用户学习记录"
|
||
// @Success 200 {object} response.Response{data=learning.UserLearning,msg=string} "查询成功"
|
||
// @Router /userLearning/findUserLearning [get]
|
||
func (userLearningApi *UserLearningApi) FindUserLearning(c *gin.Context) {
|
||
ID := c.Query("ID")
|
||
reuserLearning, err := service.ServiceGroupApp.LearningServiceGroup.UserLearningService.GetUserLearning(c.Request.Context(), ID)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("查询失败!", zap.Error(err))
|
||
response.FailWithMessage("查询失败", c)
|
||
return
|
||
}
|
||
response.OkWithData(reuserLearning, c)
|
||
}
|
||
|
||
// GetUserLearningList 分页获取用户学习记录列表
|
||
// @Tags UserLearning
|
||
// @Summary 分页获取用户学习记录列表
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data query learningReq.UserLearningSearch true "分页获取用户学习记录列表"
|
||
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
|
||
// @Router /userLearning/getUserLearningList [get]
|
||
func (userLearningApi *UserLearningApi) GetUserLearningList(c *gin.Context) {
|
||
var pageInfo learningReq.UserLearningSearch
|
||
err := c.ShouldBindQuery(&pageInfo)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
list, total, err := service.ServiceGroupApp.LearningServiceGroup.UserLearningService.GetUserLearningInfoList(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)
|
||
}
|
||
|
||
// GetUserProgress 获取用户学习进度
|
||
// @Tags UserLearning
|
||
// @Summary 获取用户学习进度
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param userId query uint true "用户ID"
|
||
// @Param courseId query uint false "课程ID"
|
||
// @Success 200 {object} response.Response{data=[]learning.UserLearning,msg=string} "获取成功"
|
||
// @Router /userLearning/getUserProgress [get]
|
||
func (userLearningApi *UserLearningApi) GetUserProgress(c *gin.Context) {
|
||
userIdStr := c.Query("userId")
|
||
courseIdStr := c.Query("courseId")
|
||
|
||
userId, err := strconv.ParseUint(userIdStr, 10, 32)
|
||
if err != nil {
|
||
response.FailWithMessage("用户ID格式错误", c)
|
||
return
|
||
}
|
||
|
||
var courseId uint = 0
|
||
if courseIdStr != "" {
|
||
courseIdUint, err := strconv.ParseUint(courseIdStr, 10, 32)
|
||
if err != nil {
|
||
response.FailWithMessage("课程ID格式错误", c)
|
||
return
|
||
}
|
||
courseId = uint(courseIdUint)
|
||
}
|
||
|
||
// 如果没有提供userId,尝试从JWT中获取当前用户ID
|
||
if userId == 0 {
|
||
userId = uint64(utils.GetUserID(c))
|
||
}
|
||
|
||
list, err := service.ServiceGroupApp.LearningServiceGroup.UserLearningService.GetUserProgress(c.Request.Context(), uint(userId), courseId)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
||
response.FailWithMessage("获取失败", c)
|
||
return
|
||
}
|
||
response.OkWithData(list, c)
|
||
}
|
||
|
||
// GetLearningStatistics 获取用户学习统计
|
||
// @Tags UserLearning
|
||
// @Summary 获取用户学习统计
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param userId query uint false "用户ID"
|
||
// @Success 200 {object} response.Response{data=learningReq.LearningStatistics,msg=string} "获取成功"
|
||
// @Router /userLearning/getLearningStatistics [get]
|
||
func (userLearningApi *UserLearningApi) GetLearningStatistics(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.UserLearningService.GetLearningStatistics(c.Request.Context(), userId)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
||
response.FailWithMessage("获取失败", c)
|
||
return
|
||
}
|
||
response.OkWithData(stats, c)
|
||
}
|