sun-pc-linux 8 months ago
parent
commit
6b86a849cc
9 changed files with 128 additions and 168 deletions
  1. 39 0
      src/api/api.http
  2. 36 0
      src/api/exam.go
  3. 0 126
      src/api/learn.go
  4. 5 0
      src/api/router.go
  5. 25 0
      src/api/user.go
  6. 14 0
      src/db/model/examHistory.go
  7. 0 32
      src/db/model/learnLog.go
  8. 6 6
      src/db/model/user.go
  9. 3 4
      src/db/repo/repo.go

+ 39 - 0
src/api/api.http

@@ -0,0 +1,39 @@
+@host = 127.0.0.1:6123/crsvc
+
+
+###
+# 提交学习时长
+# 成功返回
+# {
+#     "errorNo": 200,
+#     "result": true,
+#     "errorDesc": ""
+# }
+# 失败返回
+# {
+#     "errorNo": 200,
+#     "result": false,
+#     "errorDesc": "错误描述"
+# }
+GET http://{{host}}/user/learn/inc?t=1 HTTP/1.1
+Content-Type: application/json
+Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjI4MjU4OTIsImlkIjoiNjQyYTUyNGY1ZjUwYmM5MDNmOTg2Mzk0Iiwia2V5IjoiYm94Y29zdCIsIm5hbWUiOiLlrZnog5wiLCJvcmlnX2lhdCI6MTcyMjIyMTA5MiwicGFyZW50IjoiNjQyYTUyNGY1ZjUwYmM5MDNmOTg2Mzk0IiwicGhvbmUiOiIxMzQwODU0NzgyMyIsInJvbGUiOiIiLCJzdGF0ZSI6MSwidXNlclR5cGUiOjJ9.Cz7qI3-Vah0io5ZLIFDyVOf8qsqdmbT8WHdprrjbdXw
+
+
+###
+# 创建考核记录
+POST http://{{host}}/exam/history/create HTTP/1.1
+Content-Type: application/json
+Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjI4MjU4OTIsImlkIjoiNjQyYTUyNGY1ZjUwYmM5MDNmOTg2Mzk0Iiwia2V5IjoiYm94Y29zdCIsIm5hbWUiOiLlrZnog5wiLCJvcmlnX2lhdCI6MTcyMjIyMTA5MiwicGFyZW50IjoiNjQyYTUyNGY1ZjUwYmM5MDNmOTg2Mzk0IiwicGhvbmUiOiIxMzQwODU0NzgyMyIsInJvbGUiOiIiLCJzdGF0ZSI6MSwidXNlclR5cGUiOjJ9.Cz7qI3-Vah0io5ZLIFDyVOf8qsqdmbT8WHdprrjbdXw
+
+{
+    "userId": "xxxx",
+    "content": "客户端提交的历史记录json结构字符串"
+}
+
+###
+# 查询考核历史,如果query中没有userId则查询当前登录用户的
+GET http://{{host}}/exam/history/list?page=1&size=10&query={"userId": "xxx"} HTTP/1.1
+Content-Type: application/json
+Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjI4MjU4OTIsImlkIjoiNjQyYTUyNGY1ZjUwYmM5MDNmOTg2Mzk0Iiwia2V5IjoiYm94Y29zdCIsIm5hbWUiOiLlrZnog5wiLCJvcmlnX2lhdCI6MTcyMjIyMTA5MiwicGFyZW50IjoiNjQyYTUyNGY1ZjUwYmM5MDNmOTg2Mzk0IiwicGhvbmUiOiIxMzQwODU0NzgyMyIsInJvbGUiOiIiLCJzdGF0ZSI6MSwidXNlclR5cGUiOjJ9.Cz7qI3-Vah0io5ZLIFDyVOf8qsqdmbT8WHdprrjbdXw
+

+ 36 - 0
src/api/exam.go

@@ -0,0 +1,36 @@
+package api
+
+import (
+	"cr-svc/db/model"
+	"cr-svc/db/repo"
+	"errors"
+	"time"
+
+	"github.com/gin-gonic/gin"
+)
+
+func ExamHistoryCreate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var form model.ExamHistory
+	err := c.ShouldBindJSON(&form)
+	if err != nil {
+		return nil, errors.New("参数错误")
+	}
+	form.UserId = apictx.User.ID
+	form.CreateTime = time.Now()
+	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionExamHistory, &form)
+}
+
+func ExamHistoryList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	page, size, query := UtilQueryPageSize(c)
+	// 没有userId时默认自己的
+	if _, ok := query["userId"]; !ok {
+		query["userId"] = apictx.User.ID
+	}
+
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+		CollectName: repo.CollectionExamHistory,
+		Page:        page,
+		Size:        size,
+		Query:       query,
+	})
+}

+ 0 - 126
src/api/learn.go

@@ -1,126 +0,0 @@
-package api
-
-// // 具体数据库中创建
-// // 每个用户每个模块有多条记录,每次学习生成一条记录
-// func CreateLearnLog(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-// 	db := c.Param("scope")
-// 	if len(db) == 0 {
-// 		return nil, errors.New("scope不能为空")
-// 	}
-// 	learnLog := &model.LearnLog{}
-// 	err := c.ShouldBindJSON(learnLog)
-// 	if err != nil {
-// 		log.Error(err)
-// 		return nil, err
-// 	}
-// 	if len(learnLog.Cid) < 1 {
-// 		return nil, errors.New("模块id不能为空")
-// 	}
-
-// 	// 没找到数据,创建
-// 	learnLog.Uid = apictx.User.ID
-// 	zero := 0
-// 	learnLog.LearnTime = &zero
-// 	learnLog.CreateTime = time.Now()
-// 	learnLog.UpdateTime = time.Now()
-// 	return repo.RepoAddDbDoc(apictx.CreateRepoCtx(), db, repo.CollectionLearnLog, learnLog)
-// }
-
-// // 每分钟记录下学习时长
-// // sync/time/:id/:scope
-// func SyncLearnTime(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-// 	id := c.Param("id")
-// 	db := c.Param("scope")
-// 	objId, _ := primitive.ObjectIDFromHex(id)
-// 	if objId.IsZero() {
-// 		return nil, errors.New("id错误")
-// 	}
-
-// 	// 写入统计数据,每个人每个模块的总学习时长
-// 	learnLog := &model.LearnLog{}
-// 	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-// 		Db:          db,
-// 		CollectName: repo.CollectionLearnLog,
-// 		Query:       repo.Map{"_id": objId},
-// 		Project:     []string{"cid", "uid"},
-// 	}, learnLog)
-// 	if err != nil {
-// 		log.Error(err)
-// 		return nil, err
-// 	}
-// 	if !found {
-// 		return nil, errors.New("未找到记录")
-// 	}
-
-// 	update := bson.M{"$inc": bson.M{"learnTime": 1}, "$set": bson.M{"updateTime": time.Now()}}
-
-// 	_, err = repo.RepoUpdateSetDbDocs(apictx.CreateRepoCtx(), db, repo.CollectionLearnLogStatistics, bson.M{"cid": learnLog.Cid, "uid": learnLog.Uid}, update)
-// 	if err != nil {
-// 		log.Error(err)
-// 		fmt.Println(err)
-// 	}
-// 	return repo.RepoUpdateSetDbDocProps(apictx.CreateRepoCtx(), db, repo.CollectionLearnLog, id, update)
-// }
-
-// func LearnLogStatistics(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-// 	db := c.Param("scope")
-// 	if len(db) == 0 {
-// 		return nil, errors.New("scope不能为空")
-// 	}
-// 	// 学习模块进度
-// 	uid := c.Query("uid")
-// 	if len(uid) == 0 {
-// 		uid = apictx.User.ID
-// 	}
-// 	_, a, err := getLearnLogStatistics(apictx, db, uid)
-// 	return a, err
-// }
-
-// func getLearnLogStatistics(apictx *ApiSession, db string, uid string) (int, float64, error) {
-// 	learnLogStatistics := make([]*model.LearnLogStatistics, 0)
-// 	err := repo.RepoSeachDocs(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-// 		Db:          db,
-// 		CollectName: repo.CollectionLearnLogStatistics,
-// 		Query:       repo.Map{"uid": uid},
-// 		Project:     []string{"cid", "learnTime"},
-// 	}, &learnLogStatistics)
-// 	if err != nil {
-// 		log.Error(err)
-// 		return 0, 0.00, err
-// 	}
-// 	completeRate := 0.00
-// 	total := len(LearnLogSettings)
-// 	totalLearnTime := 0
-// 	if len(learnLogStatistics) > 0 {
-// 		for _, v := range learnLogStatistics {
-// 			totalLearnTime += v.LearnTime
-// 			limit, ok := LearnLogSettings[v.Cid]
-// 			if !ok {
-// 				limit = 10
-// 			}
-// 			rate := float64(v.LearnTime) / float64(limit)
-// 			if rate > 1 {
-// 				rate = 1
-// 			}
-// 			fmt.Println("pre_rate:", rate)
-// 			completeRate += rate / float64(total)
-// 		}
-// 	}
-// 	fmt.Println("completeRate:", completeRate)
-// 	fmt.Println("uid_totalLearnTime:", totalLearnTime)
-// 	fmt.Println("----------------------------------------")
-// 	return totalLearnTime, completeRate, nil
-
-// }
-
-// var LearnLogSettings = map[string]int{
-// 	"65a8edf6a7ef2b18346edd8c": 10,
-// 	"65a8ee02a7ef2b18346edd8d": 10,
-// 	"65a8ee0da7ef2b18346edd8e": 10,
-// 	"65a8ee18a7ef2b18346edd8f": 10,
-// 	"65a8ee21a7ef2b18346edd90": 10,
-// 	"65a8ee2ba7ef2b18346edd91": 60,
-// 	"65a8ee39a7ef2b18346edd92": 10,
-// 	"65a8ee39a7ef2b18346edd93": 10,
-// 	"65a8ee39a7ef2b18346edd94": 60,
-// }

+ 5 - 0
src/api/router.go

@@ -22,6 +22,11 @@ func RegRouters(svc *Service) {
 	crrouter.POSTJWT("/admin/user/update", UpdateUser)
 	// 获取自己的详情信息
 	crrouter.GETJWT("/user/profile", UserProfile)
+	// 增加用户学习时长
+	crrouter.GETJWT("/user/learn/inc", UserLearnInc)
+	// 考核历史
+	crrouter.POSTJWT("/exam/history/create", ExamHistoryCreate)
+	crrouter.GETJWT("/exam/history/list", ExamHistoryList)
 
 	Upload(crrouter)
 	Version(crrouter)

+ 25 - 0
src/api/user.go

@@ -3,6 +3,8 @@ package api
 import (
 	"cr-svc/log"
 	"errors"
+	"fmt"
+	"strconv"
 	"time"
 
 	"cr-svc/db/model"
@@ -227,6 +229,29 @@ func UserProfile(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	return GetUserById(apictx, apictx.User.ID)
 }
 
+func UserLearnInc(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	_time := c.Query("t")
+	increase, _ := strconv.Atoi(_time)
+	if increase < 1 {
+
+		return false, fmt.Errorf("增加学习时长小于1: %d", increase)
+	}
+	userInfo, err := GetUserById(apictx, apictx.User.ID)
+	if err != nil {
+		return false, errors.New("未找到该用户信息")
+	}
+	learnTime := userInfo.LearnTime
+	// 增加学习时长
+	newTime := learnTime + increase
+	_, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionUser, apictx.User.ID, &model.User{LearnTime: newTime})
+	if err != nil {
+		fmt.Println(err)
+		log.Error(err)
+		return false, errors.New("更新学习时长出错")
+	}
+	return true, nil
+}
+
 // 根据id获取用户信息
 func GetUserById(apictx *ApiSession, id string) (*model.User, error) {
 	user := &model.User{}

+ 14 - 0
src/db/model/examHistory.go

@@ -0,0 +1,14 @@
+package model
+
+import (
+	"time"
+
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+type ExamHistory struct {
+	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	UserId     string             `bson:"userId,omitempty" json:"userId"`
+	Content    string             `bson:"content,omitempty" json:"content"`
+	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
+}

+ 0 - 32
src/db/model/learnLog.go

@@ -1,32 +0,0 @@
-package model
-
-import (
-	"time"
-
-	"go.mongodb.org/mongo-driver/bson/primitive"
-)
-
-// 学习记录: 哪个用户 学习了哪个内容:怎么标识这个内容 学习了多少时间:每分钟请求接口同步一次
-// 当前登录用户 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:"cid,omitempty" json:"cid"` // 分类配置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"`
-}
-
-// 学习记录统计
-type LearnLogStatistics struct {
-	Id  primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
-	Uid string             `bson:"uid,omitempty" json:"uid"` // 用户id
-	Cid string             `bson:"cid,omitempty" json:"cid"` // 分类id
-	// CateName   string             `bson:"cateName,omitempty" json:"cateName"`   // 分类名称
-	LearnTime  int       `bson:"learnTime,omitempty" json:"learnTime"` // 学习时长
-	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime"`
-	UpdateTime time.Time `bson:"updateTime,omitempty" json:"updateTime"`
-}

+ 6 - 6
src/db/model/user.go

@@ -7,12 +7,12 @@ import (
 )
 
 type User struct {
-	Id primitive.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"`
-	// Nid       string             `bson:"nid,omitempty" json:"nid,omitempty"` // 编号
-	// Name      string `bson:"name,omitempty" json:"name,omitempty"`
-	LoginName string `bson:"loginName,omitempty" json:"loginName,omitempty"`
-	Password  string `bson:"password,omitempty" json:"password,omitempty"`
-	Avatar    string `bson:"avatar,omitempty" json:"avatar,omitempty"`
+	Id        primitive.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"`
+	LoginName string             `bson:"loginName,omitempty" json:"loginName,omitempty"`
+	Password  string             `bson:"password,omitempty" json:"password,omitempty"`
+	Avatar    string             `bson:"avatar,omitempty" json:"avatar,omitempty"`
+	// 学习总时长
+	LearnTime int `bson:"learnTime,omitempty" json:"learnTime,omitempty"`
 	// student,teacher,admin
 	Roles      []string  `bson:"roles,omitempty" json:"roles,omitempty"`
 	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime,omitempty"`

+ 3 - 4
src/db/repo/repo.go

@@ -18,10 +18,9 @@ type RepoSession struct {
 }
 
 const (
-	CollectionVersions           = "versions"
-	CollectionUser               = "users"
-	CollectionLearnLog           = "LearnLogs"
-	CollectionLearnLogStatistics = "LearnLogStatistics"
+	CollectionVersions    = "versions"
+	CollectionUser        = "users"
+	CollectionExamHistory = "exam-history"
 )
 
 type Map map[string]interface{}