sunsheng 1 tahun lalu
induk
melakukan
24be9b7d3f
2 mengubah file dengan 15 tambahan dan 55 penghapusan
  1. 15 43
      src/api/statistics.go
  2. 0 12
      src/readme.md

+ 15 - 43
src/api/statistics.go

@@ -39,6 +39,7 @@ func StatisticsLearnProcess(c *gin.Context, apictx *ApiSession) (interface{}, er
 		year = year_
 	}
 	fmt.Println("year:", year)
+	fmt.Println("type", _type)
 
 	colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
 	return AggregateMonthlyLearnTime(colls, uid, _type, year)
@@ -48,51 +49,22 @@ func StatisticsLearnProcess(c *gin.Context, apictx *ApiSession) (interface{}, er
 // 整个模块的学习时长 不需要cid约束
 func AggregateMonthlyLearnTime(colls *mongo.Collection, uid string, learnType string, year int) ([]bson.M, error) {
 	chinaTimeZone := time.FixedZone("CST", 8*60*60) // 中国时区
-	pipeline := mongo.Pipeline{
+	match := bson.D{
+		{Key: "uid", Value: uid},
+		{Key: "createTime", Value: bson.D{
+			{Key: "$gte", Value: time.Date(year, time.January, 1, 0, 0, 0, 0, chinaTimeZone)},
+			{Key: "$lt", Value: time.Date(year+1, time.January, 1, 0, 0, 0, 0, chinaTimeZone)},
+		}},
+	}
 
-		// {
-		// 	{Key: "$addFields", Value: bson.D{
-		// 		{Key: "dateParts", Value: bson.D{
-		// 			{Key: "$dateToParts", Value: bson.M{
-		// 				"date":     "$createTime",
-		// 				"timezone": "Asia/Shanghai",
-		// 			}},
-		// 		}},
-		// 	}},
-		// },
-		// {
-
-		// 	{Key: "$match", Value: bson.D{
-		// 		{Key: "uid", Value: uid},
-		// 		{Key: "dateParts.year", Value: year},
-		// 		// {Key: "dateParts.month", Value: bson.D{
-		// 		// 	{Key: "$gte", Value: 1},
-		// 		// 	{Key: "$lt", Value: 13},
-		// 		// }},
-		// 	}},
-		// },
-		// {
-		// 	{Key: "$project", Value: bson.D{
-		// 		{Key: "month", Value: bson.D{
-		// 			{Key: "$month", Value: "$createTime"},
-		// 		}},
-		// 		{Key: "learnTime", Value: 1},
-		// 	}},
-		// },
-		// {
-		// 	{Key: "$group", Value: bson.D{
-		// 		{Key: "_id", Value: "$month"},
+	// 检查 learnType 是否存在
+	if len(learnType) > 0 {
+		// 如果存在,添加到查询条件中
+		match = append(match, bson.E{Key: "type", Value: learnType})
+	}
 
-		{
-			{Key: "$match", Value: bson.D{
-				{Key: "uid", Value: uid},
-				{Key: "type", Value: learnType},
-				{Key: "createTime", Value: bson.D{
-					{Key: "$gte", Value: time.Date(year, time.January, 1, 0, 0, 0, 0, chinaTimeZone)},
-					{Key: "$lt", Value: time.Date(year+1, time.January, 1, 0, 0, 0, 0, chinaTimeZone)},
-				}},
-			}},
-		},
+	pipeline := mongo.Pipeline{
+		{bson.E{Key: "$match", Value: match}},
 		// 将 createTime 字段分解为年和月
 		{
 			{Key: "$addFields", Value: bson.D{

+ 0 - 12
src/readme.md

@@ -26,15 +26,3 @@ db.createUser({user:"root",pwd:"copter-train-8888",roles:["dbOwner"]});
 ```
 创建默认管理员账号
 MONGO_MIGRATION: mongo-migrations==mongodb://root:copter-train-8888@127.0.0.1:27017/copter?authSource=admin
-
-<!-- 
-eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDYxNTUwMzksImlkIjoiNjVhOGEwNGNhODU1NzUyYTU4YWNmYmY3Iiwib3JpZ19pYXQiOjE3MDU1NTAyMzl9.M3-RTgD65y8wg20dTCOyx79XMLfMVDoaNU_bDaHSpmY -->
-
-// 提交
-// 运行环境
-// 编译
-// docker 推送
-// 测试
-// 部署
-Administrator
-eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MDY2MDM4ODgsImlzcyI6IkdpdG5lc3MiLCJwaWQiOjMsInRrbiI6eyJ0eXAiOiJwYXQiLCJpZCI6MTJ9fQ.5VPbPLI17XB8-ApULzskKFtqQVxG-Xh8Gdqf1XnhwPA