@@ -0,0 +1,35 @@
|
||||
package pushmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// PushModel 视频推送模型
|
||||
type PushModel struct {
|
||||
VideoID primitive.ObjectID `json:"videoID" bson:"videoID"`
|
||||
Title string `json:"title" bson:"title"`
|
||||
Type string `json:"type" bson:"type"` //视频类型 COVER-图集 SP-短视频
|
||||
PushAt1 time.Time `json:"pushAt1" bson:"pushAt1"`
|
||||
PushAt2 time.Time `json:"pushAt2" bson:"pushAt2"`
|
||||
PushAt3 time.Time `json:"pushAt3" bson:"pushAt3"`
|
||||
IsCurcle bool `json:"isCurcle" bson:"isCurcle"`
|
||||
Position int `json:"position" bson:"position"` //推送位置 0 所有 1锁屏 2官方
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"` //创建时间
|
||||
}
|
||||
|
||||
// 推荐视频推送模型
|
||||
type RecoPushModel struct {
|
||||
VideoID primitive.ObjectID `json:"videoID" bson:"videoID"`
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"` //创建时间
|
||||
}
|
||||
|
||||
type PushModelDoc = PushModel
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package pushmod
|
||||
|
||||
import "91porn-server/models/commod"
|
||||
|
||||
// AddReq 推送添加
|
||||
type AddReq struct {
|
||||
VideoID string `form:"videoID" json:"videoID"`
|
||||
Title string `form:"title" json:"title"`
|
||||
}
|
||||
|
||||
// ListReq 推送列表
|
||||
type ListReq struct {
|
||||
VideoID string `form:"videoID" json:"videoID"`
|
||||
commod.Page
|
||||
}
|
||||
|
||||
// DeleteReq 推送删除请求
|
||||
type DeleteReq struct {
|
||||
IDs []string `form:"ids" json:"ids"`
|
||||
}
|
||||
|
||||
// ListResp 推送视频列表应答
|
||||
type ListResp struct {
|
||||
VInfos []*PushModel `json:"vInfos"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
// OperateResult 更新或者删除的操作返回结果
|
||||
type OperateResult struct {
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
Reference in New Issue
Block a user