package model import ( "context" "mygo/utils" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) type RechargeOrder struct { ID primitive.ObjectID `json:"_id" bson:"_id"` Uid int `json:"uid" bson:"uid"` OrderNo string `json:"order_no" bson:"order_no"` RegionId int `json:"regionid" bson:"regionid"` ServerId int `json:"serverid" bson:"serverid"` Payment int `json:"payment" bson:"payment"` Description string `json:"description" bson:"description"` PayStatus int `json:"pay_status" bson:"pay_status"` PayAt string `json:"pay_at" bson:"pay_at"` Complete int `json:"complete" bson:"complete"` CompletedAt string `json:"completed_at" bson:"completed_at"` CreatedAt string `json:"created_at" bson:"created_at"` } func GetRechargeOrderList(collection *mongo.Collection, pay_status int64, offset int64, limit int64) ([]RechargeOrder, int64, error) { 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}}) var filter = bson.D{} if pay_status > 0 { filter = bson.D{{Key: "pay_status", Value: pay_status}} } cur, err := collection.Find(ctx, filter, opts) if err != nil { return nil, 0, err } defer cur.Close(ctx) lists := []RechargeOrder{} err = cur.All(ctx, &lists) if err != nil { return nil, 0, err } count, err := collection.CountDocuments(ctx, filter) if err != nil { return nil, 0, err } return lists, count, nil } func GetTodayRechargeToTal(collection *mongo.Collection) (int32, error) { now := time.Now() startOfToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local) endOfToday := startOfToday.Add(time.Hour * 24) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() // 定义聚合管道,包含 $match、$group 和 $sum 操作符 pipeline := bson.A{ bson.M{"$match": bson.M{ "created_at": bson.M{ "$gte": startOfToday.Format("2006-01-02 15:04:05"), "$lt": endOfToday.Format("2006-01-02 15:04:05"), }, "pay_status": 1, }}, 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 GetRechargeToTal(collection *mongo.Collection) (int32, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() pipeline := bson.A{ bson.M{"$match": bson.M{ "pay_status": 1, }}, 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(context.Background(), &result); err != nil { return 0, err } // 返回 payment 总数 if len(result) > 0 { return result[0]["total"].(int32), nil } else { return 0, nil } } func CalculateThisWeekAndLastWeekPaymentTotal(collection *mongo.Collection) (thisWeek, lastWeek []int, err error) { // 计算本周和上周的起始时间 now := time.Now() startOfThisWeek := time.Date(now.Year(), now.Month(), now.Day()-int(now.Weekday())+1, 0, 0, 0, 0, time.Local) endOfThisWeek := startOfThisWeek.Add(time.Hour * 24 * 7) startOfLastWeek := startOfThisWeek.Add(-time.Hour * 24 * 7) // endOfLastWeek := startOfThisWeek // 定义聚合管道,包含 $match、$group 和 $sort 操作符 pipeline := bson.A{ bson.M{"$match": bson.M{ "created_at": bson.M{ "$gte": startOfLastWeek.Format("2006-01-02 15:04:05"), "$lt": endOfThisWeek.Format("2006-01-02 15:04:05"), }, "pay_status": 1, }}, 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", startOfThisWeek.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 数组 thisWeek = make([]int, 7) lastWeek = make([]int, 7) 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) isThisWeek := result["_id"].(bson.M)["is_this"].(bool) date, err := time.Parse("2006-01-02", dateStr) if err != nil { continue } weekday := int(date.Weekday()) if isThisWeek && weekday >= 0 && weekday < 7 { thisWeek[weekday] = int(total) } else if !isThisWeek && weekday >= 0 && weekday < 7 { lastWeek[weekday] = int(total) } } return thisWeek, lastWeek, nil } func CalculateThisMonthAndLastMonthPaymentTotal(collection *mongo.Collection) (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"), }, "pay_status": 1, }}, 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 } func GetRechargeList(collection *mongo.Collection, limit int64, offset int64, order_no string, uid int64, regionid int64, serverid int64, pay_status int64, pay_at_start string, pay_at_end string, created_at_start string, created_at_end string) ([]RechargeOrder, int64, error) { // Parse query parameters query := bson.M{} if order_no != "" { query["order_no"] = order_no } if uid > 0 { query["uid"] = uid } if regionid > 0 { query["regionid"] = regionid } if serverid > 0 { query["serverid"] = serverid } if pay_status > 0 { query["pay_status"] = pay_status } if pay_at_start != "" { startTime, err := utils.ParseTimeString(pay_at_start) if err != nil { return nil, 0, err } query["pay_at"] = bson.M{"$gte": startTime} } if pay_at_end != "" { endTime, err := utils.ParseTimeString(pay_at_end) if err != nil { return nil, 0, err } if _, ok := query["pay_at"]; !ok { query["pay_at"] = bson.M{} } query["pay_at"].(bson.M)["$lte"] = endTime } 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 } lists := []RechargeOrder{} err = cur.All(ctx, &lists) if err != nil { return nil, 0, err } count, err := collection.CountDocuments(ctx, query) if err != nil { return nil, 0, err } return lists, count, nil }