148 lines
3.4 KiB
Go
148 lines
3.4 KiB
Go
package vidtimeonlinemod
|
|
|
|
import (
|
|
"91porn-server/common/db"
|
|
"91porn-server/models"
|
|
"91porn-server/models/commod"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
var mdb *db.MongoDB
|
|
|
|
const table = models.VidTimeOnline
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
// initIndex 初始化索引
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{"onlineTime", -1}},
|
|
},
|
|
}
|
|
_, err := coll(nil).CreateIndex(many)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
// GetAllByPage 分页获取列表
|
|
func GetAllByPage(cond bson.M, page commod.Page) ([]VidTimeOnlineModel, int64, error) {
|
|
skip := int64(page.Skip())
|
|
limit := int64(page.Limit())
|
|
opt := &options.FindOptions{}
|
|
opt.SetSkip(skip)
|
|
opt.SetLimit(limit)
|
|
vidTimeOnlineList := make([]VidTimeOnlineModel, 0)
|
|
err := coll(nil).Find(&vidTimeOnlineList, cond, opt)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
total, err := coll(nil).Count(bson.M{})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return vidTimeOnlineList, total, nil
|
|
}
|
|
|
|
// GetByPage 获取可以执行的列表
|
|
func GetByPage(page commod.Page) ([]VidTimeOnlineModel, error) {
|
|
skip := int64(page.Skip())
|
|
limit := int64(page.Limit())
|
|
sort := bson.D{bson.E{"onlineTime", 1}}
|
|
opt := &options.FindOptions{}
|
|
opt.SetSkip(skip).SetLimit(limit).SetSort(sort)
|
|
vidTimeOnlineList := make([]VidTimeOnlineModel, 0)
|
|
err := coll(nil).Find(&vidTimeOnlineList, bson.M{
|
|
"onlineTime": bson.M{
|
|
"$lte": time.Now(),
|
|
},
|
|
}, opt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return vidTimeOnlineList, nil
|
|
}
|
|
|
|
// GetByVidIds 根据vid获取列表
|
|
func GetByVidIds(vidIds []primitive.ObjectID) ([]VidTimeOnlineModel, error) {
|
|
vidTimeOnlineList := make([]VidTimeOnlineModel, 0)
|
|
err := coll(nil).Find(&vidTimeOnlineList, bson.M{"vidId": bson.M{"$in": vidIds}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return vidTimeOnlineList, nil
|
|
}
|
|
|
|
// GetByVidId 根据id获取
|
|
func GetByVidId(vid primitive.ObjectID) (VidTimeOnlineModel, error) {
|
|
var res VidTimeOnlineModel
|
|
err := coll(nil).FindOne(&res, bson.M{"vidId": vid})
|
|
return res, err
|
|
}
|
|
|
|
// DeleteByIds 删除
|
|
func DeleteByIds(ids []primitive.ObjectID) error {
|
|
_, err := coll(nil).DeleteMany(bson.M{"_id": bson.M{"$in": ids}})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteByVids 根据帖子id删除数据
|
|
func DeleteByVids(vids []primitive.ObjectID) error {
|
|
_, err := coll(nil).DeleteMany(bson.M{"vidId": bson.M{"$in": vids}})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Add 新增
|
|
func Add(prams VidTimeOnlineModel) error {
|
|
_, err := coll(nil).InsertOne(prams)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// InsertMany 批量新增
|
|
func InsertMany(records []VidTimeOnlineModel) error {
|
|
opts := options.InsertMany().SetOrdered(false)
|
|
_, err := coll(nil).InsertMany(records, opts)
|
|
if !db.IsMongoDupKey(err) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UpdateMany 通过帖子id修改定时上架信息
|
|
func UpdateMany(vids []primitive.ObjectID, update bson.M) (err error) {
|
|
if len(vids) == 0 {
|
|
return
|
|
}
|
|
_, err = coll(nil).UpdateMany(
|
|
bson.M{"vidId": bson.M{"$in": vids}},
|
|
bson.M{"$set": update},
|
|
)
|
|
return
|
|
}
|
|
|
|
func UpdateById(id primitive.ObjectID, update bson.M) (err error) {
|
|
_, err = coll(nil).UpdateOne(bson.M{"_id": id}, update)
|
|
return
|
|
}
|