102 lines
2.4 KiB
Go
102 lines
2.4 KiB
Go
package integralexcangemod
|
|
|
|
import (
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/models"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"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"
|
|
)
|
|
|
|
const table = models.IntegralExchange
|
|
|
|
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{{"createdAt", -1}},
|
|
},
|
|
}
|
|
_, err := coll(nil).CreateIndex(many)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("user model set index err ==>[%+v]", err))
|
|
}
|
|
}
|
|
|
|
func Init() {
|
|
mdb = db.Init(table)
|
|
initIndex()
|
|
}
|
|
|
|
func InsertOne(mt *db.MongoTool, cfg IntegralExchange) error {
|
|
if _, err := coll(mt).InsertOne(cfg); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FindOneById(id primitive.ObjectID) (IntegralExchange, error) {
|
|
cfg := IntegralExchange{}
|
|
err := coll(nil).FindOne(&cfg, bson.M{"_id": id})
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
return cfg, err
|
|
}
|
|
|
|
// QueryAllList 分页查询文档
|
|
func QueryAllList(filter primitive.M, opts ...*options.FindOptions) (out []*IntegralExchange, err error) {
|
|
if err = coll(nil).Find(&out, filter, opts...); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllList", table, "Find", err),
|
|
log.Any("filter", filter),
|
|
log.Any("opts", opts),
|
|
)
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
// QueryAllCount 查询文档条目数
|
|
func QueryAllCount(filter primitive.M) (int64, error) {
|
|
if count, err := coll(nil).Count(filter); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllCount", table, "Count", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return 0, err
|
|
} else {
|
|
return count, nil
|
|
}
|
|
}
|
|
|
|
// Edit 修改文档
|
|
func Edit(filter, update primitive.M) error {
|
|
result, err := coll(nil).UpdateOne(filter, update)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Edit", table, "UpdateOne", err),
|
|
log.Any("filter", filter),
|
|
log.Any("update", update),
|
|
)
|
|
return err
|
|
}
|
|
if result.ModifiedCount == 0 {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Edit", table, "result.ModifiedCount", "ModifiedCount is zero"),
|
|
log.Any("filter", filter),
|
|
log.Any("update", update),
|
|
)
|
|
return errors.New("result is null")
|
|
}
|
|
return nil
|
|
}
|