54 lines
1.7 KiB
Go
54 lines
1.7 KiB
Go
package prdcthsomod
|
|
|
|
import (
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
// VIPExperimentGoldOrderStat contains successful VIP card purchases paid with coins.
|
|
type VIPExperimentGoldOrderStat struct {
|
|
Variant string `json:"variant" bson:"variant"`
|
|
ProductID primitive.ObjectID `json:"productId" bson:"productId"`
|
|
GoldPaidOrders int `json:"goldPaidOrders" bson:"goldPaidOrders"`
|
|
GoldPaidAmount int64 `json:"goldPaidAmount" bson:"goldPaidAmount"`
|
|
}
|
|
|
|
func VIPExperimentGoldOrderStatistics(experimentID string) ([]VIPExperimentGoldOrderStat, error) {
|
|
var raw []struct {
|
|
ID struct {
|
|
Variant string `bson:"variant"`
|
|
ProductID primitive.ObjectID `bson:"productId"`
|
|
} `bson:"_id"`
|
|
GoldPaidOrders int `bson:"goldPaidOrders"`
|
|
GoldPaidAmount int64 `bson:"goldPaidAmount"`
|
|
}
|
|
err := coll(nil).Aggregate(&raw, []bson.M{
|
|
{"$match": bson.M{"experimentId": experimentID}},
|
|
{"$group": bson.M{
|
|
"_id": bson.M{
|
|
"variant": "$experimentVariant",
|
|
"productId": "$productID",
|
|
},
|
|
"goldPaidOrders": bson.M{"$sum": 1},
|
|
"goldPaidAmount": bson.M{"$sum": bson.M{"$add": bson.A{
|
|
bson.M{"$ifNull": bson.A{"$amount", 0}},
|
|
bson.M{"$ifNull": bson.A{"$income", 0}},
|
|
}}},
|
|
}},
|
|
{"$sort": bson.D{{Key: "_id.variant", Value: 1}, {Key: "goldPaidOrders", Value: -1}}},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]VIPExperimentGoldOrderStat, 0, len(raw))
|
|
for _, item := range raw {
|
|
result = append(result, VIPExperimentGoldOrderStat{
|
|
Variant: item.ID.Variant,
|
|
ProductID: item.ID.ProductID,
|
|
GoldPaidOrders: item.GoldPaidOrders,
|
|
GoldPaidAmount: item.GoldPaidAmount,
|
|
})
|
|
}
|
|
return result, nil
|
|
}
|