|
@@ -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{
|