81 lines
2.3 KiB
Go
81 lines
2.3 KiB
Go
package contentlibmod
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/models"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var mdb *db.MongoDB
|
|
|
|
const table = models.ContentLibrary
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
|
{
|
|
Keys: bson.D{{Key: "type", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
func Insert(doc ContentLibrary) (err error) {
|
|
if _, err = coll(nil).InsertOne(doc); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func DeleteByIDS(ids []ObjectID) (err error) {
|
|
if _, err = coll(nil).DeleteMany(bson.M{"_id": bson.M{"$in": ids}}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteByIDS", table, "DeleteMany", err), log.Any("ids", ids))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetContentList(robotType *string, skip int64, limit int64) (total int64, data []ContentLibrary, err error) {
|
|
var query = bson.M{}
|
|
if robotType != nil {
|
|
query["robotType"] = *robotType
|
|
}
|
|
var opts = options.Find()
|
|
opts.SetSort(bson.D{{Key: "createdAt", Value: -1}}).SetSkip(skip).SetLimit(limit)
|
|
if err = coll(nil).Find(&data, query, opts); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetContentList", table, "Find", err), log.Any("robotType", robotType))
|
|
return
|
|
}
|
|
if total, err = coll(nil).Count(query); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetContentList", table, "Count", err), log.Any("robotType", robotType))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetContentListByIDS(ids []ObjectID) (data []ContentLibrary, err error) {
|
|
if err = coll(nil).Find(&data, bson.M{"_id": bson.M{"$in": ids}}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetContentListByIDS", table, "Find", err), log.Any("ids", ids))
|
|
return
|
|
}
|
|
return
|
|
}
|