// Copyright © 2023 OpenIM open source community. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package chat import ( "context" "time" "github.com/openimsdk/tools/db/mongoutil" "github.com/openimsdk/tools/db/pagination" "github.com/openimsdk/tools/errs" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" chatdb "git.imall.cloud/openim/chat/pkg/common/db/table/chat" ) func NewWithdraw(db *mongo.Database) (chatdb.WithdrawInterface, error) { coll := db.Collection("withdraws") _, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{ { Keys: bson.D{ {Key: "user_id", Value: 1}, {Key: "create_time", Value: -1}, }, }, { Keys: bson.D{ {Key: "status", Value: 1}, {Key: "create_time", Value: -1}, }, }, { Keys: bson.D{ {Key: "create_time", Value: -1}, }, }, }) if err != nil { return nil, errs.Wrap(err) } return &Withdraw{coll: coll}, nil } type Withdraw struct { coll *mongo.Collection } func (o *Withdraw) Create(ctx context.Context, withdraws ...*chatdb.Withdraw) error { return mongoutil.InsertMany(ctx, o.coll, withdraws) } func (o *Withdraw) Take(ctx context.Context, withdrawID string) (*chatdb.Withdraw, error) { return mongoutil.FindOne[*chatdb.Withdraw](ctx, o.coll, bson.M{"_id": withdrawID}) } func (o *Withdraw) FindByUserID(ctx context.Context, userID string, pagination pagination.Pagination) (int64, []*chatdb.Withdraw, error) { filter := bson.M{"user_id": userID} return mongoutil.FindPage[*chatdb.Withdraw](ctx, o.coll, filter, pagination, options.Find().SetSort(bson.D{{Key: "create_time", Value: -1}})) } func (o *Withdraw) FindByStatus(ctx context.Context, status int32, pagination pagination.Pagination) (int64, []*chatdb.Withdraw, error) { filter := bson.M{"status": status} return mongoutil.FindPage[*chatdb.Withdraw](ctx, o.coll, filter, pagination, options.Find().SetSort(bson.D{{Key: "create_time", Value: -1}})) } func (o *Withdraw) UpdateStatus(ctx context.Context, withdrawID string, status int32, auditorID string, auditRemark string) error { update := bson.M{ "$set": bson.M{ "status": status, "auditor_id": auditorID, "audit_time": time.Now(), "audit_remark": auditRemark, "update_time": time.Now(), }, } return mongoutil.UpdateOne(ctx, o.coll, bson.M{"_id": withdrawID}, update, false) } func (o *Withdraw) Page(ctx context.Context, pagination pagination.Pagination) (int64, []*chatdb.Withdraw, error) { return mongoutil.FindPage[*chatdb.Withdraw](ctx, o.coll, bson.M{}, pagination, options.Find().SetSort(bson.D{{Key: "create_time", Value: -1}})) }