You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
6.2 KiB
Go
204 lines
6.2 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"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 Withdraw struct {
|
|
ID primitive.ObjectID `json:"_id" bson:"_id"`
|
|
Uid string `json:"uid" bson:"uid"`
|
|
Money float64 `json:"money" bson:"money"`
|
|
Status int64 `json:"status" bson:"status"`
|
|
Content string `json:"content" bson:"content"`
|
|
Taxes string `json:"taxes" bson:"taxes"`
|
|
Type int `json:"type" bson:"type"`
|
|
Account string `json:"account" bson:"account"`
|
|
CreatedAt string `json:"created_at" bson:"created_at"`
|
|
UpdatedAt string `json:"updated_at" bson:"updated_at"`
|
|
}
|
|
|
|
func GetWithdrawLogList(collection *mongo.Collection, agent_id string, offset int64, limit int64) ([]Withdraw, 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 agent_id != "" {
|
|
filter = bson.D{{Key: "uid", Value: agent_id}}
|
|
}
|
|
cur, err := collection.Find(ctx, filter, opts)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer cur.Close(ctx)
|
|
lists := []Withdraw{}
|
|
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 GetWithdrawToTal(collection *mongo.Collection) (int32, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
pipeline := bson.A{
|
|
bson.M{"$group": bson.M{
|
|
"_id": nil,
|
|
"total": bson.M{"$sum": "$money"},
|
|
}},
|
|
}
|
|
// 创建 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 GetWithdrawToTalByUid(collection *mongo.Collection, uid string) (float64, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
pipeline := bson.A{
|
|
bson.M{"$match": bson.M{
|
|
"uid": uid,
|
|
}},
|
|
bson.M{"$group": bson.M{
|
|
"_id": nil,
|
|
"total": bson.M{"$sum": "$money"},
|
|
}},
|
|
}
|
|
// 创建 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"].(float64), nil
|
|
} else {
|
|
return 0, nil
|
|
}
|
|
}
|
|
|
|
func CalculateThisMonthAndLastMonthAgentWithdrawPaymentTotal(collection *mongo.Collection, uid string) (thisMonth, lastMonth []float64, 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"),
|
|
},
|
|
"uid": uid,
|
|
}},
|
|
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": "$money"},
|
|
}},
|
|
}
|
|
|
|
// 创建 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([]float64, endOfThisMonth.Day())
|
|
lastMonth = make([]float64, 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"].(float64)
|
|
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] = float64(total)
|
|
} else {
|
|
lastMonth[date.Day()-1] = float64(total)
|
|
}
|
|
}
|
|
|
|
return thisMonth, lastMonth, nil
|
|
}
|
|
|
|
func GetWithdrawById(collection *mongo.Collection, id primitive.ObjectID) (Withdraw, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
filter := bson.D{{Key: "_id", Value: id}}
|
|
var result Withdraw
|
|
err := collection.FindOne(ctx, filter).Decode(&result)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func DoWithdraw(collection *mongo.Collection, doc Withdraw) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
_, err := collection.InsertOne(ctx, doc)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func DoWithdrawStatus(collection *mongo.Collection, id primitive.ObjectID, status int64, taxes string) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
filter := bson.D{{Key: "_id", Value: id}}
|
|
update := bson.D{{Key: "$set", Value: bson.D{{Key: "status", Value: status}, {Key: "taxes", Value: taxes}, {Key: "updated_at", Value: time.Now().Format("2006-01-02 15:04:05")}}}}
|
|
_, err := collection.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|