forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
package botdailyhourstat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
// 获取数据库表
|
||||
db = mongos.New(mongos.MgoManager, CollName)
|
||||
// 初始化索引
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "appId", Value: 1},
|
||||
{Key: "sumDate", Value: 1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "appId", Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "sumDate", Value: 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(BotHourStat:Init) Create BotHourStat Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOneBotHourStat 插入一个机器人小时统计
|
||||
func InsertOneBotHourStat(ctx context.Context, botHourStat *BotHourStat) error {
|
||||
if _, err := db.InsertOne(ctx, botHourStat); err != nil {
|
||||
log.Warn("botHourStat:InsertOneBotHourStat db.InsertOne failed", log.E(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindOneBotHourStat 查找一个机器人小时统计
|
||||
func FindOneBotHourStat(ctx context.Context, filter bson.M) (*BotHourStat, error) {
|
||||
var botHourStat BotHourStat
|
||||
if err := db.FindOne(ctx, &botHourStat, filter); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &botHourStat, nil
|
||||
}
|
||||
|
||||
// FindOneBotHourStatEx 查找一个机器人小时统计
|
||||
func FindOneBotHourStatEx(ctx context.Context, filter bson.M) (*BotHourStat, error) {
|
||||
var botHourStat BotHourStat
|
||||
if err := db.FindOneEx(ctx, &botHourStat, filter); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &botHourStat, nil
|
||||
}
|
||||
|
||||
// DelOneBotHourStat 删除一个机器人小时统计
|
||||
func DelOneBotHourStat(ctx context.Context, filter bson.M) (
|
||||
int64, error) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("BotHourStat:DelOneBotHourStat db.DeleteOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateOneBotHourStat 更新一个机器人小时统计
|
||||
func UpdateOneBotHourStat(ctx context.Context, filter bson.M, update bson.M) error {
|
||||
_, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("user:UpdateOneBotHourStat db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CountBotHourStats 统计机器人小时统计数量
|
||||
func CountBotHourStats(ctx context.Context, filter bson.M) (
|
||||
int64, error) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("BotHourStat:CountBotHourStats db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateManyBotHourStat 更新满足条件的机器人小时统计
|
||||
func UpdateManyBotHourStat(ctx context.Context, filter bson.M, update bson.M) error {
|
||||
_, err := db.UpdateMany(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("user:UpdateManyBotHourStat db.UpdateMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManyBotHourStat 查找一批机器人小时统计数据
|
||||
func FindManyBotHourStat(ctx context.Context, filter bson.M,
|
||||
opts ...*options.FindOptions) (BotHourStatList, error) {
|
||||
data := make(BotHourStatList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("BotHourStat:FindManyBotHourStat db.Find failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// FindManyBotHourStatWithBind 用自己定义结构查找一批机器人小时统计数据
|
||||
func FindManyBotHourStatWithBind(ctx context.Context, bind interface{}, filter bson.M,
|
||||
opts ...*options.FindOptions) error {
|
||||
if err := db.Find(ctx, bind, filter, opts...); err != nil {
|
||||
log.Warn("BotHourStat:FindManyBotHourStatWithBind db.Find failed", log.E(err), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AggregateBotHourStats 聚合查找一批机器人小时统计信息
|
||||
func AggregateBotHourStats(ctx context.Context, bind interface{}, pipeline []bson.M) error {
|
||||
if err := db.Aggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("feedback:AggregateBotHourStats db.Aggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user