146 lines
3.8 KiB
Go
146 lines
3.8 KiB
Go
package pushmod
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"time"
|
|
|
|
"91porn-server/common"
|
|
"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"
|
|
)
|
|
|
|
// initIndex 初始化索引
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "type", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
var mdb *db.MongoDB
|
|
|
|
const table = models.PushModel
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
// InsertOne 插入一条数据
|
|
func InsertOne(doc PushModelDoc) error {
|
|
_, err := coll(nil).InsertOne(doc)
|
|
return err
|
|
}
|
|
|
|
// Delete 删除数据
|
|
func Delete(cond bson.M) (int64, error) {
|
|
result, err := coll(nil).DeleteMany(cond)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.DeletedCount, err
|
|
}
|
|
|
|
// GetTotalCnt 获取标签视频总数
|
|
func GetTotalCnt(cond bson.M) (int64, error) {
|
|
total, err := coll(nil).Count(cond)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetTotalCnt", table, "Count", err), log.Any("cond", cond))
|
|
return 0, err
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
// GetSkipSize 计算跳转
|
|
func GetSkipSize(page, size uint64, cond bson.M) (int, int, int64, error) {
|
|
total, err := GetTotalCnt(cond)
|
|
if err != nil {
|
|
return 0, 0, 0, err
|
|
}
|
|
totalpages := uint64(math.Ceil(float64(total) / float64(size)))
|
|
if page > totalpages {
|
|
page = totalpages
|
|
}
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
return int((page - 1) * size), int(totalpages), total, nil
|
|
}
|
|
|
|
// GetPushList 条件获取推送视频列表
|
|
func GetPushList(page, size uint64, cond bson.M, sort bson.D) ([]*PushModel, int, int64, error) {
|
|
skip, totalPages, total, err := GetSkipSize(page, size, cond)
|
|
if err != nil {
|
|
return nil, 0, 0, err
|
|
}
|
|
opts := options.FindOptions{}
|
|
if sort != nil {
|
|
opts.SetSort(sort)
|
|
}
|
|
opts.SetSkip(int64(skip)).SetLimit(int64(size))
|
|
var back []*PushModel
|
|
if err = coll(nil).Find(&back, cond, &opts); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetPushList", table, "Find", err), log.Any("cond", cond))
|
|
return nil, totalPages, total, err
|
|
}
|
|
return back, totalPages, total, nil
|
|
}
|
|
|
|
// IsExist 判断视频是否存在
|
|
func IsExist(id primitive.ObjectID) (bool, error) {
|
|
return coll(nil).Exists(bson.M{"videoID": id})
|
|
}
|
|
|
|
// 获取短视频强推列表
|
|
func GetPushSpVidForRecommend(line time.Time, newsType string, page, size uint64) ([]string, time.Time, error) {
|
|
var maxCreateLine time.Time
|
|
cond := bson.M{"type": newsType}
|
|
pushVidInfos, err := getPushVidForRecommend(line, cond, page, size)
|
|
if err != nil {
|
|
return nil, maxCreateLine, nil
|
|
}
|
|
vids := make([]primitive.ObjectID, 0, len(pushVidInfos))
|
|
for _, i := range pushVidInfos {
|
|
if i == nil {
|
|
continue
|
|
}
|
|
if i.CreatedAt.After(maxCreateLine) {
|
|
maxCreateLine = i.CreatedAt
|
|
}
|
|
vids = append(vids, i.VideoID)
|
|
}
|
|
arr := common.ObjectIDs2String(vids)
|
|
return arr, maxCreateLine, nil
|
|
}
|
|
|
|
// getPushVidForRecommend 获取官方强推视频列表
|
|
func getPushVidForRecommend(line time.Time, cond bson.M, page, size uint64) ([]*RecoPushModel, error) {
|
|
sort := bson.D{{Key: "createdAt", Value: 1}}
|
|
cond["createdAt"] = bson.M{"$gt": line}
|
|
opt := options.FindOptions{}
|
|
opt.SetProjection(bson.M{"videoID": 1, "createdAt": 1})
|
|
opt.SetLimit(int64(size)).SetSkip(int64((page - 1) * size)).SetSort(sort)
|
|
var back []*RecoPushModel
|
|
if err := coll(nil).Find(&back, cond, &opt); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "getPushVidForRecommend", table, "Find", err), log.Any("cond", cond))
|
|
return nil, err
|
|
}
|
|
return back, nil
|
|
}
|