init: web_hgdjc_server
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package cointransmod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/mongos"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var coll *mongos.Collection
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
coll = mongos.Shop.Coll(TN.CoinTrans)
|
||||
initIndex(ctx)
|
||||
}
|
||||
|
||||
func initIndex(ctx context.Context) {
|
||||
indexes := []mongo.IndexModel{
|
||||
// 按用户+时间倒序查流水(最常用)
|
||||
{Keys: bson.D{
|
||||
{Key: FN.CoinTrans.UserId, Value: 1},
|
||||
{Key: FN.CoinTrans.CreatedAt, Value: -1},
|
||||
}},
|
||||
// 按交易类型筛选
|
||||
{Keys: bson.D{{Key: FN.CoinTrans.TranType, Value: 1}}},
|
||||
// 按订单号查(幂等校验)
|
||||
{Keys: bson.D{{Key: FN.CoinTrans.OrderNo, Value: 1}}},
|
||||
}
|
||||
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(cointransmod:initIndex) Create Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOneCoinTrans 写入一条流水记录
|
||||
func InsertOneCoinTrans(ctx context.Context, doc *CoinTrans) *srvdef.SrvErr {
|
||||
if _, err := coll.InsertOne(ctx, doc); err != nil {
|
||||
log.Error("cointransmod:InsertOneCoinTrans failed", log.Any("doc", doc), log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManyCoinTrans 分页查询流水列表
|
||||
func FindManyCoinTrans(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (CoinTransList, *srvdef.SrvErr) {
|
||||
var data CoinTransList
|
||||
if err := coll.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("cointransmod:FindManyCoinTrans failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// CountCoinTrans 统计流水条数
|
||||
func CountCoinTrans(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := coll.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("cointransmod:CountCoinTrans failed", log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// StatSyncConsumeList 按 ID 游标增量拉取金币消费流水(统计中心)
|
||||
func StatSyncConsumeList(ctx context.Context, startId int64, size int64) (CoinTransList, error) {
|
||||
opt := (&options.FindOptions{}).SetLimit(size).SetSort(bson.D{{Key: FN.CoinTrans.Id, Value: 1}})
|
||||
filter := bson.M{
|
||||
FN.CoinTrans.Id: bson.M{"$gt": startId},
|
||||
FN.CoinTrans.FlowDir: FlowOut,
|
||||
}
|
||||
list := CoinTransList{}
|
||||
if err := coll.Find(ctx, &list, filter, opt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
Reference in New Issue
Block a user