forked from all_user/stat_center
100 lines
2.5 KiB
Go
100 lines
2.5 KiB
Go
package rechargechannelmod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"stat/common/log"
|
|
"stat/common/mongos"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func Init() {
|
|
db = mongos.New(mongos.MgoManager, CollRechargeChannel)
|
|
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "sumDate", Value: 1}},
|
|
},
|
|
}
|
|
|
|
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
|
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollRechargeChannel, err))
|
|
}
|
|
}
|
|
|
|
// UpsertOneUserRechargeStat 更新或插入一条支付渠道统计
|
|
func UpsertOneUserRechargeStat(ctx context.Context, channelName string, appId int32, sumDate time.Time, blocDeduction bool, money, paymoney, channelMoney int64) (err error) {
|
|
filter := bson.M{"channelName": channelName, "appId": appId, "sumDate": sumDate}
|
|
|
|
update := bson.M{}
|
|
|
|
update["orderCount"] = 1
|
|
update["totalOrderMoney"] = money
|
|
update["TotalOrderPayMoney"] = paymoney
|
|
update["channelMoney"] = channelMoney
|
|
if !blocDeduction {
|
|
update["blocOrderCount"] = 1
|
|
update["blocOrderMoney"] = money
|
|
update["blocOrderPayMoney"] = paymoney
|
|
update["blocChannelMoney"] = channelMoney
|
|
}
|
|
|
|
exists, err := db.Exists(ctx, filter)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if exists {
|
|
_, err = db.UpdateOne(ctx, filter, bson.M{"$inc": update})
|
|
} else {
|
|
insertDoc := update
|
|
insertDoc["sumDate"] = sumDate
|
|
insertDoc["appId"] = appId
|
|
insertDoc["channelName"] = channelName
|
|
|
|
_, err = db.InsertOne(ctx, insertDoc)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// 后台获取统计信息
|
|
func GetRechargeChannelStatList(ctx context.Context, appId *int32, channelName *string, start, end *time.Time, skip, limit int64) (count int64, data []RechargeChannelMod, err error) {
|
|
filter := bson.M{}
|
|
|
|
if appId != nil {
|
|
filter["appId"] = appId
|
|
}
|
|
|
|
if channelName != nil {
|
|
filter["channelName"] = channelName
|
|
}
|
|
|
|
if start != nil && end != nil {
|
|
filter["sumDate"] = bson.M{"$gte": start, "$lt": end}
|
|
} else if start != nil {
|
|
filter["sumDate"] = bson.M{"$gte": start}
|
|
} else if end != nil {
|
|
filter["sumDate"] = bson.M{"$lt": end}
|
|
}
|
|
|
|
sort := bson.D{{Key: "sumDate", Value: -1}, {Key: "appId", Value: 1}}
|
|
|
|
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
|
|
|
|
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
|
log.Error("channelCodeStat GetRechargeChannelStatList Find err", log.E(err))
|
|
return
|
|
}
|
|
if count, err = db.Count(ctx, filter); err != nil {
|
|
log.Error("channelCodeStat GetRechargeChannelStatList Count err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|