Initial commit
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package syncrecord
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
// 初始化model
|
||||
func Init() {
|
||||
// 获取数据库表
|
||||
db = mongos.New(mongos.MgoManager, CollSyncRecord)
|
||||
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "job", Value: 1}, {Key: "appid", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
||||
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollSyncRecord, err))
|
||||
}
|
||||
}
|
||||
|
||||
// LastRecordTimeMap 查询统计记录
|
||||
func LastRecordTimeMap(ctx context.Context) (list []SyncRecord, err error) {
|
||||
list = make([]SyncRecord, 0)
|
||||
err = db.Find(ctx, &list, bson.M{})
|
||||
if err != nil {
|
||||
log.Error("SyncRecord LastRecordTimeMap err", log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UpsertItemRecord 插入统计成功项记录
|
||||
func UpsertItemRecord(ctx context.Context, job string, appid int32, userid uint64, platformId string, successAt time.Time) (err error) {
|
||||
filter := bson.M{
|
||||
"job": job,
|
||||
"appid": appid,
|
||||
}
|
||||
exists, err := db.Exists(ctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if exists {
|
||||
_, err = db.UpdateOne(ctx, filter,
|
||||
bson.M{
|
||||
"$set": bson.M{
|
||||
"userId": userid,
|
||||
"successAt": successAt,
|
||||
"platformId": platformId,
|
||||
"updatedAt": time.Now(),
|
||||
}})
|
||||
} else {
|
||||
insertDoc := bson.M{
|
||||
"job": job,
|
||||
"appid": appid,
|
||||
"userId": userid,
|
||||
"platformId": platformId,
|
||||
"successAt": successAt,
|
||||
"createdAt": time.Now(),
|
||||
}
|
||||
|
||||
_, err = db.InsertOne(ctx, insertDoc)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// LastOneRecordTime 查询上一次统计时间
|
||||
func LastOneRecordTime(ctx context.Context, job string, appid int32) (record SyncRecord, err error) {
|
||||
|
||||
err = db.FindOne(ctx, &record, bson.M{"job": job, "appid": appid})
|
||||
if err != nil {
|
||||
log.Error("SyncRecord LastRecordTimeMap err", log.E(err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package syncrecord
|
||||
|
||||
import (
|
||||
"stat/common/mongos"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// 表名
|
||||
const CollSyncRecord = "sync_record"
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
type SyncRecord struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
Job string `bson:"job"` // 工作
|
||||
AppId int32 `bson:"appid"` // 应用Id
|
||||
UserId uint64 `bson:"userId,omitempty"` // 用户ID
|
||||
PlatformId string `bson:"platformId,omitempty"` // 原始平台流水Id
|
||||
SuccessAt time.Time `bson:"successAt,omitempty"` // 成功时间
|
||||
Disabled bool `bson:"disabled,omitempty"` // 是否禁用
|
||||
CreatedAt time.Time `bson:"createdAt,omitempty"` // 创建时间
|
||||
UpdatedAt time.Time `bson:"updatedAt,omitempty"` // 更新时间
|
||||
}
|
||||
|
||||
const (
|
||||
VisitJob = "visit" // 访问
|
||||
RegisterJob = "register" // 注册
|
||||
RegisterLightJob = "registerLight" // 注册轻量
|
||||
RechargeJob = "recharge" // 充值
|
||||
WithdrawJob = "withdraw" // 提现
|
||||
VipJob = "vip" // VIP
|
||||
RechargeAll = "rechargeAll" // 充值订单
|
||||
WithdrawAll = "withdrawAll" // 提现订单
|
||||
GameRechargeOrder = "gameRecharge" // 游戏充值订单
|
||||
GameRechargeOrderAll = "gameRechargeAll" // 游戏充值(全部)
|
||||
GameWithdrawOrder = "gameWithdraw" // 游戏提现订单
|
||||
GameWater = "gameWater" // 游戏流水
|
||||
)
|
||||
|
||||
var SyncJobDescribe = map[string]string{
|
||||
VisitJob: "日活记录同步",
|
||||
RegisterJob: "注册记录同步",
|
||||
RegisterLightJob: "轻量注册记录",
|
||||
RechargeJob: "充值记录同步",
|
||||
WithdrawJob: "提现记录同步",
|
||||
VipJob: "会员购买同步",
|
||||
RechargeAll: "充值全部同步",
|
||||
WithdrawAll: "订单全部同步",
|
||||
GameRechargeOrder: "游戏订单同步",
|
||||
GameRechargeOrderAll: "游戏充值全部",
|
||||
GameWithdrawOrder: "游戏提现同步",
|
||||
GameWater: "游戏流水同步",
|
||||
}
|
||||
Reference in New Issue
Block a user