sunsheng hai 1 ano
pai
achega
97000be6c0
Modificáronse 7 ficheiros con 145 adicións e 34 borrados
  1. 0 22
      src/api/category.go
  2. 54 0
      src/api/exeam.go
  3. 44 0
      src/api/exeamLog.go
  4. 19 5
      src/api/router.go
  5. 19 0
      src/db/model/exeamLog.go
  6. 8 7
      src/db/model/learnLog.go
  7. 1 0
      src/db/repo/repo.go

+ 0 - 22
src/api/category.go

@@ -67,28 +67,6 @@ func CategoryList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	})
 }
 
-func CategoryDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	_id := c.Param("id")
-	id, _ := primitive.ObjectIDFromHex(_id)
-	if id.IsZero() {
-		return nil, errors.New("id错误")
-	}
-	cate := &model.Category{}
-	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-		CollectName: repo.CollectionCategory,
-		Query:       repo.Map{"_id": id},
-	}, cate)
-	if err != nil {
-		log.Error(err)
-		return nil, err
-	}
-
-	if !found {
-		return nil, errors.New("未找到该数据")
-	}
-	return cate, nil
-}
-
 func UpdateCategory(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// 验证是否为管理员
 	isAdmin, err := IsAdmin(c, apictx)

+ 54 - 0
src/api/exeam.go

@@ -0,0 +1,54 @@
+package api
+
+import (
+	"copter-train/db/repo"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/mongo"
+)
+
+// 生成考核试题
+// /exeam/generate/:scope
+type GenerateExeamReq struct {
+	ChoiceNum int `json:"choiceNum"` // 选择题生成多少道
+	JudgeNum  int `json:"judgeNum"`  // 判断题生成多少道
+}
+
+func GenerateExeam(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	db := c.Param("scope")
+	var form GenerateExeamReq
+	err := c.ShouldBindJSON(&form)
+	if err != nil {
+		return nil, err
+	}
+	// 查询所有试题
+	ctx := apictx.CreateRepoCtx().Ctx
+	mongoClient := apictx.CreateRepoCtx().Client
+	col := mongoClient.GetDbCollection(db, repo.CollectionTest)
+	pipeline := mongo.Pipeline{
+		{
+			{Key: "$facet", Value: bson.M{
+				"choiceQuestions": bson.A{
+					bson.M{"$match": bson.M{"type": "选择"}},
+					bson.M{"$sample": bson.M{"size": form.ChoiceNum}},
+				},
+				"judgeQuestions": bson.A{
+					bson.M{"$match": bson.M{"type": "判断"}},
+					bson.M{"$sample": bson.M{"size": form.JudgeNum}},
+				},
+			}},
+		},
+	}
+	cursor, err := col.Aggregate(ctx, pipeline)
+	if err != nil {
+		return nil, err
+	}
+	var result []bson.M
+	if err = cursor.All(ctx, &result); err != nil {
+		return nil, err
+	}
+	defer cursor.Close(ctx)
+
+	return result, nil
+}

+ 44 - 0
src/api/exeamLog.go

@@ -0,0 +1,44 @@
+package api
+
+import (
+	"copter-train/db/model"
+	"copter-train/db/repo"
+	"copter-train/log"
+	"errors"
+
+	"github.com/gin-gonic/gin"
+)
+
+// 提交考核试题
+func SubmitExeamLog(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	db := c.Param("scope")
+	if len(db) == 0 {
+		return nil, errors.New("scope不能为空")
+	}
+	exeamLog := &model.ExeamLog{}
+	err := c.ShouldBindJSON(exeamLog)
+	if err != nil {
+		log.Error(err)
+		return nil, err
+	}
+	exeamLog.Uid = apictx.User.ID
+	return repo.RepoAddDbDoc(apictx.CreateRepoCtx(), db, repo.CollectionExeamLog, exeamLog)
+}
+
+// 考核记录列表
+// /exeamLog/list/:scope
+func ExeamLogList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	db := c.Param("scope")
+	if len(db) == 0 {
+		return nil, errors.New("scope不能为空")
+	}
+	page, size, query := UtilQueryPageSize(c)
+	return repo.RepoDbPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+		Db:          db,
+		CollectName: repo.CollectionExeamLog,
+		Page:        page,
+		Size:        size,
+		Query:       query,
+		// Sort:        bson.D{bson.E{Key: "sort", Value: 1}, bson.E{Key: "createTime", Value: 1}},
+	})
+}

+ 19 - 5
src/api/router.go

@@ -27,21 +27,35 @@ func RegRouters(svc *Service) {
 	root.POSTJWT("/category/create", CreateCategory)
 	root.POSTJWT("/category/delete/:id", DeleteCategory)
 	root.GET("/category/list", CategoryList)
-	// r.GET("/category/detail/:id", CategoryDetail)
 	root.POSTJWT("/category/update", UpdateCategory)
 
 	// 学习记录
-	// 学习记录: 哪个用户 学习了哪个内容:怎么标识这个内容 学习了多少时间:每分钟请求接口同步一次
-	// 当前登录用户 cid对应的内容,category 中type为'course' learnTime +1
 	root.POSTJWT("/learnLog/create/:scope", CreateLearnLog)
 	root.POSTJWT("/learnLog/sync/time/:id/:scope", SyncLearnTime)
 
 	// 考核试题管理
-	// 单选/判断
+	root.POSTJWT("/admin/test/create/:scope", CreateTest)
+	root.POSTJWT("/admin/test/delete/:id/:scope", DeleteTest)
+	root.GETJWT("/admin/test/list/:scope", TestList)
+	root.GETJWT("/admin/test/detail/:id/:scope", TestDetail)
+	root.POSTJWT("/admin/test/update/:scope", UpdateTest)
 
-	// todo
+	// 生成考核试题
+	root.POSTJWT("/exeam/generate/:scope", GenerateExeam)
 
 	// 考核记录
+	root.POSTJWT("/exeamLog/submit/:scope", SubmitExeamLog)
+	// 考核记录列表
+	root.GETJWT("/exeamLog/list/:scope", ExeamLogList)
+
+	// todo
+
+	// 学习记录统计 / 考核记录统计
+	// 管理员:选择学员查看,理论和实操分开
+	// 学习记录统计: x月份 y时长
+	// 考核记录统计: 历史考核记录[v]
+
+	// 考虑创建一张统计表,学习完成把当前学习时长写入统计表
 
 }
 

+ 19 - 0
src/db/model/exeamLog.go

@@ -0,0 +1,19 @@
+package model
+
+import (
+	"time"
+
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+type ExeamLog struct {
+	Id           primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	Uid          string             `bson:"uid,omitempty" json:"uid"`                   // 用户id
+	Type         string             `bson:"type,omitempty" json:"type"`                 // 考核类型: 理论/实操
+	Correct      *int               `bson:"correct,omitempty" json:"correct"`           // 正确题目数
+	Error        *int               `bson:"error,omitempty" json:"error"`               // 错误题目数
+	TotalScore   *int               `bson:"totalScore,omitempty" json:"totalScore"`     // 总分
+	CompleteRate *int               `bson:"completeRate,omitempty" json:"completeRate"` // 完成进度
+	CreateTime   time.Time          `bson:"createTime,omitempty" json:"createTime"`
+	UpdateTime   time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
+}

+ 8 - 7
src/db/model/learnLog.go

@@ -10,11 +10,12 @@ import (
 // 当前登录用户 cid对应的内容,category 中type为'course' learnTime +1
 
 type LearnLog struct {
-	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
-	Uid        string             `bson:"uid,omitempty" json:"uid"`             // 用户id
-	Cid        string             `bson:"pid,omitempty" json:"pid"`             // 分类配置id
-	Sid        string             `bson:"sid,omitempty" json:"sid"`             //subject id 学习主题id // 客户端用于标识题目的(因为服务端不存储题目)
-	LearnTime  *int               `bson:"learnTime,omitempty" json:"learnTime"` // 学习时长
-	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
-	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
+	Id  primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	Uid string             `bson:"uid,omitempty" json:"uid"` // 用户id
+	Cid string             `bson:"pid,omitempty" json:"pid"` // 分类配置id
+	// Sid        string             `bson:"sid,omitempty" json:"sid"`             //subject id 学习主题id // 客户端用于标识题目的(因为服务端不存储题目)
+	Type       string    `bson:"type,omitempty" json:"type"`           // 学习类型: 理论/实操
+	LearnTime  *int      `bson:"learnTime,omitempty" json:"learnTime"` // 学习时长
+	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime"`
+	UpdateTime time.Time `bson:"updateTime,omitempty" json:"updateTime"`
 }

+ 1 - 0
src/db/repo/repo.go

@@ -22,6 +22,7 @@ const (
 	CollectionUser     = "users"
 	CollectionLearnLog = "learn_logs"
 	CollectionTest     = "tests"
+	CollectionExeamLog = "exeam_logs"
 )
 
 type Map map[string]interface{}