|
@@ -9,11 +9,11 @@ import (
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
- "go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
)
|
|
|
|
|
|
// 具体数据库中创建
|
|
|
-func CreateLearnLog(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+// 每个用户每个模块只有一条记录
|
|
|
+func SyncLearnTime(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
db := c.Param("scope")
|
|
|
if len(db) == 0 {
|
|
|
return nil, errors.New("scope不能为空")
|
|
@@ -24,23 +24,53 @@ func CreateLearnLog(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
log.Error(err)
|
|
|
return nil, err
|
|
|
}
|
|
|
+ if len(learnLog.Cid) < 1 {
|
|
|
+ return nil, errors.New("模块id不能为空")
|
|
|
+ }
|
|
|
+ // 查询该学员的学习记录是否存在
|
|
|
+ searchLearnLog := &model.LearnLog{}
|
|
|
+ found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ Db: db,
|
|
|
+ Query: repo.Map{"uid": apictx.User.ID, "cid": learnLog.Cid},
|
|
|
+ }, searchLearnLog)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ if found {
|
|
|
+ update := bson.M{"$inc": bson.M{"learnTime": 1}, "$set": bson.M{"updateTime": time.Now()}}
|
|
|
+ res, err := repo.RepoUpdateSetDbDocProps(apictx.CreateRepoCtx(), db, repo.CollectionLearnLog, searchLearnLog.Id.Hex(), update)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ if res.ModifiedCount > 0 {
|
|
|
+ return true, nil
|
|
|
+ }
|
|
|
+ return false, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ // 没找到数据,创建
|
|
|
learnLog.Uid = apictx.User.ID
|
|
|
- zero := 0
|
|
|
+ zero := 1
|
|
|
learnLog.LearnTime = &zero
|
|
|
learnLog.CreateTime = time.Now()
|
|
|
learnLog.UpdateTime = time.Now()
|
|
|
- return repo.RepoAddDbDoc(apictx.CreateRepoCtx(), db, repo.CollectionLearnLog, learnLog)
|
|
|
+ _, err = repo.RepoAddDbDoc(apictx.CreateRepoCtx(), db, repo.CollectionLearnLog, learnLog)
|
|
|
+ if err != nil {
|
|
|
+ log.Error(err)
|
|
|
+ return false, nil
|
|
|
+ }
|
|
|
+ return true, nil
|
|
|
}
|
|
|
|
|
|
// 每分钟记录下学习时长
|
|
|
// 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错误")
|
|
|
- }
|
|
|
- update := bson.M{"$inc": bson.M{"learnTime": 1}, "$set": bson.M{"updateTime": time.Now()}}
|
|
|
- return repo.RepoUpdateSetDbDocProps(apictx.CreateRepoCtx(), db, repo.CollectionLearnLog, id, update)
|
|
|
-}
|
|
|
+// 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错误")
|
|
|
+// }
|
|
|
+// update := bson.M{"$inc": bson.M{"learnTime": 1}, "$set": bson.M{"updateTime": time.Now()}}
|
|
|
+// return repo.RepoUpdateSetDbDocProps(apictx.CreateRepoCtx(), db, repo.CollectionLearnLog, id, update)
|
|
|
+// }
|