81 lines
2.6 KiB
Go
81 lines
2.6 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
|
)
|
|
|
|
func (r *MySQLRepository) listMongoGiftAmounts(ctx context.Context, db *mongo.Database,
|
|
start, end time.Time, statTimezone string, luckyGift bool) ([]mongoGiftAmount, error) {
|
|
filter := bson.D{
|
|
{Key: "refunded", Value: bson.D{{Key: "$ne", Value: true}}},
|
|
{Key: "userId", Value: bson.D{{Key: "$ne", Value: nil}}},
|
|
{Key: "createTime", Value: bson.D{{Key: "$gte", Value: start}, {Key: "$lt", Value: end}}},
|
|
{Key: "sysOrigin", Value: bson.D{{Key: "$in", Value: r.cfg.Dashboard.SysOrigins}}},
|
|
}
|
|
amountPath := "$giftValue.actualAmount"
|
|
unwindAcceptUsers := false
|
|
if luckyGift {
|
|
filter = append(filter, bson.E{Key: "luckyGift", Value: true})
|
|
amountPath = "$acceptUsers.targetAmount"
|
|
unwindAcceptUsers = true
|
|
} else {
|
|
filter = append(filter,
|
|
bson.E{Key: "luckyGift", Value: bson.D{{Key: "$ne", Value: true}}},
|
|
bson.E{Key: "originId", Value: bson.D{{Key: "$regex", Value: "^\\d+$"}}},
|
|
bson.E{Key: "$or", Value: bson.A{
|
|
bson.D{{Key: "dynamicContentId", Value: bson.D{{Key: "$exists", Value: false}}}},
|
|
bson.D{{Key: "dynamicContentId", Value: nil}},
|
|
bson.D{{Key: "dynamicContentId", Value: ""}},
|
|
}},
|
|
)
|
|
}
|
|
pipeline := mongo.Pipeline{
|
|
bson.D{{Key: "$match", Value: filter}},
|
|
}
|
|
if unwindAcceptUsers {
|
|
pipeline = append(pipeline, bson.D{{Key: "$unwind", Value: "$acceptUsers"}})
|
|
}
|
|
pipeline = append(pipeline,
|
|
bson.D{{Key: "$project", Value: bson.D{
|
|
{Key: "userId", Value: "$userId"},
|
|
{Key: "amount", Value: amountPath},
|
|
{Key: "day", Value: bson.D{{Key: "$dateToString", Value: bson.D{
|
|
{Key: "format", Value: "%Y-%m-%d"},
|
|
{Key: "date", Value: "$createTime"},
|
|
{Key: "timezone", Value: statTimezone},
|
|
}}}},
|
|
}}},
|
|
bson.D{{Key: "$group", Value: bson.D{
|
|
{Key: "_id", Value: bson.D{{Key: "userId", Value: "$userId"}, {Key: "day", Value: "$day"}}},
|
|
{Key: "amount", Value: bson.D{{Key: "$sum", Value: "$amount"}}},
|
|
}}},
|
|
)
|
|
cursor, err := db.Collection(giftRunningWaterCollection).Aggregate(ctx, pipeline)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
var result []mongoGiftAmount
|
|
for cursor.Next(ctx) {
|
|
var doc bson.M
|
|
if err := cursor.Decode(&doc); err != nil {
|
|
return nil, err
|
|
}
|
|
idDoc, _ := doc["_id"].(bson.M)
|
|
userID := bsonInt64(idDoc["userId"])
|
|
day := strings.TrimSpace(fmt.Sprint(idDoc["day"]))
|
|
amount := bsonDecimal(doc["amount"])
|
|
if userID == 0 || day == "" {
|
|
continue
|
|
}
|
|
result = append(result, mongoGiftAmount{userID: userID, day: day, amount: amount})
|
|
}
|
|
return result, cursor.Err()
|
|
}
|