145 lines
3.8 KiB
Go
145 lines
3.8 KiB
Go
package txnactmod
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/models"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func Init() {
|
|
mdb = db.Init(table)
|
|
initIndex()
|
|
}
|
|
|
|
const table = models.TransactionAct
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
// initIndex 索引设置
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
|
{
|
|
Keys: bson.D{{Key: "act", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "updatedAt", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "uid", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("user model set index err ==>[%+v]", err))
|
|
}
|
|
}
|
|
|
|
// InsertWithdrawChannel 插入一条数据
|
|
func Insert(t *TransactionAct) error {
|
|
t.UpdatedAt = time.Now()
|
|
t.CreatedAt = t.UpdatedAt
|
|
if _, err := coll(nil).InsertOne(t); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteWithdrawChannel delete
|
|
func DeleteByID(id primitive.ObjectID) (int64, error) {
|
|
result, err := coll(nil).DeleteOne(bson.M{"_id": id})
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteByID", table, "DeleteOne", err),
|
|
log.Any("id", id),
|
|
)
|
|
return 0, err
|
|
}
|
|
return result.DeletedCount, err
|
|
}
|
|
|
|
func FindOneByID(id primitive.ObjectID) (TransactionAct, error) {
|
|
ta := TransactionAct{}
|
|
if err := coll(nil).FindOne(&ta, bson.M{"_id": id}); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOneByID", table, "FindOne", err),
|
|
log.Any("id", id),
|
|
)
|
|
return ta, err
|
|
}
|
|
return ta, nil
|
|
}
|
|
|
|
// UpdateWithdrawChannel 金币配置更新
|
|
func Update(id primitive.ObjectID, set *TransactionActSelector) (int64, error) {
|
|
set.UpdatedAt = time.Now()
|
|
result, err := coll(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$set": set})
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateOne", err),
|
|
log.Any("id", id),
|
|
)
|
|
return 0, err
|
|
}
|
|
return result.ModifiedCount, nil
|
|
}
|
|
|
|
// GetWithdrawChannels 条件获取支付渠道列表
|
|
func FindManyByActType(uid uint64, actType ActType) ([]*TransactionActRes, error) {
|
|
opts := options.FindOptions{}
|
|
opts.SetSort(bson.D{{Key: "updatedAt", Value: -1}})
|
|
back := make([]*TransactionActRes, 0)
|
|
if err := coll(nil).Find(&back, bson.M{"uid": uid, "aType": actType}, &opts); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateOne", err),
|
|
log.Any("uid", uid),
|
|
log.Any("actType", actType),
|
|
)
|
|
return nil, err
|
|
}
|
|
return back, nil
|
|
}
|
|
|
|
// 条件获取支付渠道列表
|
|
func FindMany(query Query) ([]*TransactionAct, error) {
|
|
opts := options.FindOptions{}
|
|
back := make([]*TransactionAct, 0)
|
|
b, _ := bson.Marshal(query)
|
|
f := make(bson.M)
|
|
if err := bson.Unmarshal(b, &f); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := coll(nil).Find(&back, f, &opts); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindMany", table, "Find", err),
|
|
log.Any("query", query),
|
|
)
|
|
return nil, err
|
|
}
|
|
return back, nil
|
|
}
|
|
|
|
// 条件获取支付渠道通道
|
|
func FindOne(query Query) (TransactionAct, error) {
|
|
back := TransactionAct{}
|
|
b, _ := bson.Marshal(query)
|
|
f := make(bson.M)
|
|
if err := bson.Unmarshal(b, &f); err != nil {
|
|
return back, err
|
|
}
|
|
if err := coll(nil).FindOne(&back, f); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOne", table, "FindOne", err),
|
|
log.Any("query", query),
|
|
)
|
|
return back, err
|
|
}
|
|
return back, nil
|
|
}
|