forked from all_user/stat_center
129 lines
3.5 KiB
Go
129 lines
3.5 KiB
Go
package syncvip
|
|
|
|
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, TN.SyncVip)
|
|
// 初始化索引
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.SyncVip.UID, Value: 1},
|
|
{Key: FN.SyncVip.AppId, Value: 1},
|
|
},
|
|
},
|
|
}
|
|
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(SyncVip:initSyncVipIndex) Create SyncVip Index failed! err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
//InsertOneSyncVip 插入一个VIP购买记录
|
|
func InsertOneSyncVip(ctx context.Context, SyncVip *SyncVip) error {
|
|
if _, err := db.InsertOne(ctx, SyncVip); err != nil {
|
|
log.Warn("SyncVip:InsertOneSyncVip db.InsertOne failed", log.E(err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//FindOneSyncVip 查找一个VIP购买记录
|
|
func FindOneSyncVip(ctx context.Context, filter bson.M) (
|
|
*SyncVip, error) {
|
|
var SyncVip SyncVip
|
|
if err := db.FindOne(ctx, &SyncVip, filter); err != nil {
|
|
return nil, err
|
|
}
|
|
return &SyncVip, nil
|
|
}
|
|
|
|
//DelOneSyncVip 删除一个VIP购买记录
|
|
func DelOneSyncVip(ctx context.Context, filter bson.M) (
|
|
int64, error) {
|
|
count, err := db.DeleteOne(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("SyncVip:DelOneSyncVip db.DeleteOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
//UpdateOneSyncVip 更新一个VIP购买记录
|
|
func UpdateOneSyncVip(ctx context.Context, filter bson.M, update bson.M) error {
|
|
_, err := db.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("user:UpdateOneSyncVip db.UpdateOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//CountSyncVips 统计VIP购买记录数量
|
|
func CountSyncVips(ctx context.Context, filter bson.M) (
|
|
int64, error) {
|
|
count, err := db.Count(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("SyncVip:CountSyncVips db.Count failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
//UpdateManySyncVip 更新满足条件的VIP购买记录
|
|
func UpdateManySyncVip(ctx context.Context, filter bson.M, update bson.M) error {
|
|
_, err := db.UpdateMany(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("user:UpdateManySyncVip db.UpdateMany failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//FindManySyncVip 查找一批VIP购买记录数据
|
|
func FindManySyncVip(ctx context.Context, filter bson.M,
|
|
opts ...*options.FindOptions) (SyncVipList, error) {
|
|
data := make(SyncVipList, 0)
|
|
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
|
log.Warn("SyncVip:FindManySyncVip db.Find failed", log.E(err), log.Any("filter", filter))
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
//FindManySyncVipWithBind 用自己定义结构查找一批VIP购买记录数据
|
|
func FindManySyncVipWithBind(ctx context.Context, bind interface{}, filter bson.M,
|
|
opts ...*options.FindOptions) error {
|
|
if err := db.Find(ctx, bind, filter, opts...); err != nil {
|
|
log.Warn("SyncVip:FindManySyncVipWithBind db.Find failed", log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//AggregateSyncVips 聚合查找一批VIP购买记录信息
|
|
func AggregateSyncVips(ctx context.Context,
|
|
bind interface{}, pipeline []bson.M) error {
|
|
if err := db.Aggregate(ctx, bind, pipeline); err != nil {
|
|
log.Warn("feedback:AggregateSyncVips db.Aggregate failed",
|
|
log.E(err), log.Any("pipeline", pipeline))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|