@@ -0,0 +1,74 @@
|
||||
package fundtransferlogmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "uid", Value: 1},
|
||||
{Key: "category", Value: 1},
|
||||
{Key: "createdAt", Value: -1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(indexes); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>%+v", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
func Insert(t *db.MongoTool, record *FundTransferLog) error {
|
||||
if _, err := coll(t).InsertOne(record); err != nil {
|
||||
log.Warn("insert fund transfer log failed", log.Any("uid", record.UID), log.E(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindLatestByUIDAndCategory(uid uint64, category Category) (*FundTransferLog, error) {
|
||||
record := &FundTransferLog{}
|
||||
filter := bson.M{"uid": uid, "category": category}
|
||||
opts := options.FindOne().SetSort(bson.D{{Key: "createdAt", Value: -1}, {Key: "_id", Value: -1}})
|
||||
if err := coll(nil).FindOne(record, filter, opts); err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if record.ID.IsZero() {
|
||||
return nil, nil
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
|
||||
func FindLatestOutByUIDAndCategory(uid uint64, category Category) (*FundTransferLog, error) {
|
||||
record := &FundTransferLog{}
|
||||
filter := bson.M{"uid": uid, "category": category, "fundType": FundTypeOut}
|
||||
opts := options.FindOne().SetSort(bson.D{{Key: "createdAt", Value: -1}, {Key: "_id", Value: -1}})
|
||||
if err := coll(nil).FindOne(record, filter, opts); err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if record.ID.IsZero() {
|
||||
return nil, nil
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package fundtransferlogmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
const table = models.FundTransferLog
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
type FundType int
|
||||
|
||||
const (
|
||||
FundTypeIn FundType = 1 // 上分
|
||||
FundTypeOut FundType = 2 // 下分
|
||||
)
|
||||
|
||||
type Category int
|
||||
|
||||
const (
|
||||
CategoryAiGirlfriend Category = iota
|
||||
)
|
||||
|
||||
type FundTransferLog struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
UID uint64 `json:"uid" bson:"uid"`
|
||||
Category Category `json:"category" bson:"category"`
|
||||
FundType FundType `json:"fundType" bson:"fundType"`
|
||||
Amount int64 `json:"amount" bson:"amount"` // 金币
|
||||
Balance int64 `json:"balance" bson:"balance"` // 操作后主钱包金币余额
|
||||
Remainder float64 `json:"remainder" bson:"remainder,omitempty"` // 不足1金币的人民币余数
|
||||
Desc string `json:"desc" bson:"desc"`
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
Reference in New Issue
Block a user