|
@@ -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
|
|
|
+}
|