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 }