package model import ( "context" "mygo/utils" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) type OrderRebateLog struct { Uid int `json:"uid" bson:"uid"` OrderNo string `json:"order_no" bson:"order_no"` AgentID int `json:"agent_id" bson:"agent_id"` Money float64 `json:"money" bson:"money"` Payment int `json:"payment" bson:"payment"` Description string `json:"description" bson:"description"` CreatedAt string `json:"created_at" bson:"created_at"` } func GetRebateLogList(collection *mongo.Collection, agent_id string, uid int64, order_no string, created_at_start string, created_at_end string, offset int64, limit int64) ([]OrderRebateLog, int64, error) { query := bson.M{} if agent_id != "" { query["agent_id"] = agent_id } if order_no != "" { query["order_no"] = order_no } if uid > 0 { query["uid"] = uid } if created_at_start != "" { startTime, err := utils.ParseTimeString(created_at_start) if err != nil { return nil, 0, err } query["created_at"] = bson.M{"$gte": startTime} } if created_at_end != "" { endTime, err := utils.ParseTimeString(created_at_end) if err != nil { return nil, 0, err } if _, ok := query["created_at"]; !ok { query["created_at"] = bson.M{} } query["created_at"].(bson.M)["$lte"] = endTime } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() opts := options.Find().SetLimit(limit).SetSkip(offset).SetSort(bson.D{{Key: "created_at", Value: -1}}) cur, err := collection.Find(ctx, query, opts) if err != nil { return nil, 0, err } defer cur.Close(ctx) users := []OrderRebateLog{} err = cur.All(ctx, &users) if err != nil { return nil, 0, err } count, err := collection.CountDocuments(ctx, query) if err != nil { return nil, 0, err } return users, count, nil } func GetAgentChildRechargeToTal(collection *mongo.Collection, agent_id string) (int32, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() pipeline := bson.A{ bson.M{"$match": bson.M{ "agent_id": agent_id, }}, bson.M{"$group": bson.M{ "_id": nil, "total": bson.M{"$sum": "$payment"}, }}, } // 创建 options 参数,指定聚合管道和超时时间 options := options.Aggregate().SetMaxTime(10 * time.Second) cur, err := collection.Aggregate(ctx, pipeline, options) if err != nil { return 0, err } defer cur.Close(ctx) // 迭代游标并提取结果 var result []bson.M if err := cur.All(ctx, &result); err != nil { return 0, err } // 返回 payment 总数 if len(result) > 0 { return result[0]["total"].(int32), nil } else { return 0, nil } } func CalculateThisMonthAndLastMonthAgentChildPaymentTotal(collection *mongo.Collection, agent_id string) (thisMonth, lastMonth []int, err error) { now := time.Now() startOfThisMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local) endOfLastMonth := startOfThisMonth.Add(-time.Second) startOfLastMonth := time.Date(endOfLastMonth.Year(), endOfLastMonth.Month(), 1, 0, 0, 0, 0, time.Local) endOfThisMonth := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.Local).Add(-time.Second) // 定义聚合管道,包含 $match、$group 和 $sort 操作符 pipeline := bson.A{ bson.M{"$match": bson.M{ "created_at": bson.M{ "$gte": startOfLastMonth.Format("2006-01-02 15:04:05"), "$lt": endOfThisMonth.Format("2006-01-02 15:04:05"), }, "agent_id": agent_id, }}, bson.M{"$group": bson.M{ "_id": bson.M{"date": bson.M{"$substr": []interface{}{"$created_at", 0, 10}}, "is_this": bson.M{"$gte": []interface{}{"$created_at", startOfThisMonth.Format("2006-01-02 15:04:05")}}}, "total": bson.M{"$sum": "$payment"}, }}, } // 创建 options 参数,指定聚合管道和超时时间 options := options.Aggregate().SetMaxTime(30 * time.Second) cursor, err := collection.Aggregate(context.Background(), pipeline, options) if err != nil { return nil, nil, err } defer cursor.Close(context.Background()) // 遍历游标并填充 thisWeek 和 lastWeek 数组 thisMonth = make([]int, endOfThisMonth.Day()) lastMonth = make([]int, endOfLastMonth.Day()) for cursor.Next(context.Background()) { var result bson.M if err := cursor.Decode(&result); err != nil { return nil, nil, err } dateStr := result["_id"].(bson.M)["date"].(string) total := result["total"].(int32) isThisMonth := result["_id"].(bson.M)["is_this"].(bool) date, err := time.Parse("2006-01-02", dateStr) if err != nil { continue } if isThisMonth { thisMonth[date.Day()-1] = int(total) } else { lastMonth[date.Day()-1] = int(total) } } return thisMonth, lastMonth, nil }