154 lines
3.7 KiB
Go
154 lines
3.7 KiB
Go
package videogoldcoinmod
|
|
|
|
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.VideoGoldCoin
|
|
|
|
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 VideoGoldCoin) error {
|
|
if _, err := coll(mt).InsertOne(cfg); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// QueryAllList 分页查询文档
|
|
func QueryAllList(filter primitive.M, opts ...*options.FindOptions) (out []*VideoGoldCoin, 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
|
|
}
|
|
|
|
// GetVideoList 获取金币视频列表
|
|
func GetVideoList(skip, limit int64, cond bson.M, opts ...*options.FindOptions) (back []*APPVideoGoldCoin, total int64, hasNext bool, err error) {
|
|
// 获取总条数
|
|
total, err = coll(nil).Count(cond)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "getVideoList", table, "Find", err),
|
|
log.Any("skip", skip),
|
|
log.Any("limit", limit),
|
|
log.Any("cond", cond))
|
|
return
|
|
}
|
|
if total == 0 {
|
|
return
|
|
}
|
|
err = coll(nil).Find(&back, cond, opts...)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "getVideoList", table, "Find", err),
|
|
log.Any("skip", skip),
|
|
log.Any("limit", limit),
|
|
log.Any("cond", cond),
|
|
)
|
|
return
|
|
}
|
|
if len(back) > int(limit) {
|
|
hasNext = true
|
|
back = back[:limit]
|
|
}
|
|
return
|
|
}
|
|
|
|
func InsertMany(records []VideoGoldCoin) error {
|
|
opts := options.InsertMany().SetOrdered(false)
|
|
_, err := coll(nil).InsertMany(records, opts)
|
|
if !db.IsMongoDupKey(err) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func IsExistByVideoID(videoID primitive.ObjectID) (bool, error) {
|
|
count, err := coll(nil).Count(bson.M{"_id": videoID})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if count > 0 {
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// Remove 删除
|
|
func Remove(filter primitive.M) (err error) {
|
|
_, err = coll(nil).DeleteOne(filter)
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Remove", table, "DeleteOne", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|