sunsheng 1 рік тому
батько
коміт
508c33fd94
6 змінених файлів з 118 додано та 3 видалено
  1. 1 1
      src/api/exeamLog.go
  2. 14 2
      src/api/learnLog.go
  3. 2 0
      src/api/router.go
  4. 87 0
      src/api/statistics.go
  5. 13 0
      src/api/user.go
  6. 1 0
      src/db/model/exeamLog.go

+ 1 - 1
src/api/exeamLog.go

@@ -30,7 +30,7 @@ func SubmitExeamLog(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 }
 
 // 考核记录列表
-// /exeamLog/list/:scope
+// /exeamLog/list/:scope?page=1&size=10&query={"uid":"xxx","type":"理论"}
 func ExeamLogList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	db := c.Param("scope")
 	if len(db) == 0 {

+ 14 - 2
src/api/learnLog.go

@@ -28,10 +28,22 @@ func SyncLearnTime(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		return nil, errors.New("模块id不能为空")
 	}
 	// 查询该学员的学习记录是否存在
+	// 一年一条记录
 	searchLearnLog := &model.LearnLog{}
+	year := time.Now().Year()                                      // 获取当前年份
+	loc, _ := time.LoadLocation("Asia/Shanghai")                   // 加载中国的时区
+	startTime := time.Date(year, time.January, 1, 0, 0, 0, 0, loc) // 使用中国的时区
+	endTime := time.Date(year+1, time.January, 1, 0, 0, 0, 0, loc) // 使用中国的时区
 	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-		Db:    db,
-		Query: repo.Map{"uid": apictx.User.ID, "cid": learnLog.Cid},
+		Db: db,
+		Query: repo.Map{
+			"uid": apictx.User.ID,
+			"cid": learnLog.Cid,
+			"createTime": repo.Map{
+				"$gte": startTime,
+				"$lt":  endTime,
+			},
+		},
 	}, searchLearnLog)
 	if err != nil {
 		return nil, err

+ 2 - 0
src/api/router.go

@@ -49,8 +49,10 @@ func RegRouters(svc *Service) {
 	root.GETJWT("/exeamLog/list/:scope", ExeamLogList)
 
 	// todo
+	root.GETJWT("/statistics/learn/process/:scope", StatisticsLearnProcess)
 
 	// 学习记录统计 / 考核记录统计
+
 	// 管理员:选择学员查看,理论和实操分开
 	// 学习记录统计: x月份 y时长/ 本年
 	// 考核记录统计: 历史考核记录[v] | 考核列表

+ 87 - 0
src/api/statistics.go

@@ -0,0 +1,87 @@
+package api
+
+import (
+	"context"
+	"copter-train/db/repo"
+	"errors"
+	"strconv"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/mongo"
+)
+
+func StatisticsLearnProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	db := c.Param("scope")
+	if len(db) == 0 {
+		return nil, errors.New("scope不能为空")
+	}
+	uid := c.Query("uid")
+	_type := c.Query("type")
+	_year := c.Query("year")
+	year, err := strconv.Atoi(_year)
+	if err != nil {
+		return nil, errors.New("year参数错误")
+	}
+
+	colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
+	return AggregateMonthlyLearnTime(colls, uid, _type, year)
+
+}
+
+func AggregateMonthlyLearnTime(colls *mongo.Collection, uid string, learnType string, year int) ([]bson.M, error) {
+	pipeline := mongo.Pipeline{
+		{
+			{"$match", bson.D{
+				{"uid", uid},
+				{"createTime", bson.D{
+					{"$gte", bson.M{
+						"$dateFromParts": bson.M{
+							"year": year, "month": 1, "day": 1,
+							"timezone": "Asia/Shanghai",
+						},
+					}},
+					{"$lt", bson.M{
+						"$dateFromParts": bson.M{
+							"year": year + 1, "month": 1, "day": 1,
+							"timezone": "Asia/Shanghai",
+						},
+					}},
+				}},
+			}},
+		},
+		{
+			{"$project", bson.D{
+				{"month", bson.D{
+					{"$month", "$createTime"},
+				}},
+				{"learnTime", 1},
+			}},
+		},
+		{
+			{"$group", bson.D{
+				{"_id", "$month"},
+				{"totalLearnTime", bson.D{
+					{"$sum", "$learnTime"},
+				}},
+			}},
+		},
+		{
+			{"$sort", bson.D{
+				{"_id", 1},
+			}},
+		},
+	}
+
+	cursor, err := colls.Aggregate(context.TODO(), pipeline)
+	if err != nil {
+		return nil, err
+	}
+
+	var results []bson.M
+	if err = cursor.All(context.TODO(), &results); err != nil {
+		return nil, err
+	}
+
+	return results, nil
+}

+ 13 - 0
src/api/user.go

@@ -75,6 +75,19 @@ func CreateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		log.Error(err)
 		return nil, err
 	}
+
+	// 验证登录名是否存在
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionUser,
+		Query:       repo.Map{"loginName": user.LoginName},
+	}, user)
+	if err != nil {
+		return nil, err
+	}
+	if found {
+		return nil, errors.New("该账号已存在")
+	}
+
 	// student,teacher,admin
 	if len(user.Roles) < 1 {
 		user.Roles = []string{"student"}

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

@@ -8,6 +8,7 @@ import (
 
 type ExeamLog struct {
 	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	Cid        string             `bson:"cid,omitempty" json:"cid"`               // 分类id
 	Uid        string             `bson:"uid,omitempty" json:"uid"`               // 用户id
 	Type       string             `bson:"type,omitempty" json:"type"`             // 考核类型: 理论/实操
 	Correct    *int               `bson:"correct,omitempty" json:"correct"`       // 正确题目数