@@ -0,0 +1,108 @@
|
||||
package tonerecomod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type ThemeType = string
|
||||
|
||||
const (
|
||||
MostLikes ThemeType = "mostLikes" //最多点赞
|
||||
PlayAtMost ThemeType = "playAtMost" //最多播放
|
||||
MostComments ThemeType = "mostComments" //最多评论
|
||||
OfficialRecom ThemeType = "officialRecom" //官方推荐
|
||||
GoldCoinArea ThemeType = "goldCoinArea" //金币专区
|
||||
LatestUpload ThemeType = "latestUpload" //最新上传
|
||||
FreeArea ThemeType = "freeArea" //免费专区
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.ToneRecom
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// InitIndex 设置index
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "vid", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "sortKey", 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]", models.ToneRecom, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Insert ()插入消息记录
|
||||
func InsertMany(offiRecoms []OffiRecomDoc) error {
|
||||
now := time.Now()
|
||||
docs := make([]interface{}, len(offiRecoms))
|
||||
for i, offiRecom := range offiRecoms {
|
||||
offiRecom.CreatedAt = &now
|
||||
m, err := common.ToBsonM(offiRecom)
|
||||
if err != nil {
|
||||
return fmt.Errorf("tone OffiRecom InsertMany ToBsonM fail, error:%+v ", err)
|
||||
}
|
||||
docs[i] = m
|
||||
}
|
||||
if _, err := coll(nil).InsertMany(docs); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertMany", table, "InsertMany", err),
|
||||
log.Any("offiRecoms", offiRecoms),
|
||||
)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete Delete
|
||||
func DeleteMany(idArray []ObjectID) (err error) {
|
||||
if _, err = coll(nil).DeleteMany(M{"_id": M{"$in": idArray}}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteMany", table, "DeleteMany", err),
|
||||
log.Any("idArray", idArray),
|
||||
)
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetOffiRecomList(skip int64, limit int64, enable bool) ([]ObjectID, error) {
|
||||
opt := (&options.FindOptions{}).
|
||||
SetSort(D{{Key: "sortKey", Value: -1}}).
|
||||
SetSkip(skip).
|
||||
SetLimit(limit)
|
||||
filter := M{
|
||||
"enable": enable,
|
||||
}
|
||||
offiRecomList := make([]OffiRecom, limit)
|
||||
if err := coll(nil).Find(&offiRecomList, filter, opt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
vidList := make([]ObjectID, len(offiRecomList))
|
||||
for i, v := range offiRecomList {
|
||||
vidList[i] = v.VID
|
||||
}
|
||||
return vidList, nil
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package tonerecomod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models/v/vidmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// M M from Map
|
||||
type M = bson.M
|
||||
|
||||
// D D from Doc
|
||||
type D = bson.D
|
||||
|
||||
type ObjectID = primitive.ObjectID
|
||||
|
||||
// A A from Array
|
||||
type A = bson.A
|
||||
|
||||
type VidInfo = vidmod.VideoBase
|
||||
|
||||
type OffiRecomPage struct {
|
||||
Total int `json:"total" bson:"total"`
|
||||
List []VidInfo `json:"list" bson:"list"` //封面
|
||||
}
|
||||
|
||||
type OffiRecom struct {
|
||||
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
|
||||
VID primitive.ObjectID `json:"vid,omitempty" bson:"vid,omitempty"`
|
||||
SortKey int `json:"sortKey" bson:"sortKey,omitempty"` //排序
|
||||
Enable bool `json:"enable" bson:"enable,omitempty"` //使能
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"` //创建时间
|
||||
}
|
||||
|
||||
type OffiRecomDoc struct {
|
||||
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
|
||||
VID primitive.ObjectID `json:"vid,omitempty" bson:"vid,omitempty"`
|
||||
SortKey *int `json:"sortKey" bson:"sortKey,omitempty"` //排序
|
||||
Enable *bool `json:"enable" bson:"enable,omitempty"` //使能
|
||||
CreatedAt *time.Time `json:"createdAt" bson:"createdAt,omitempty"` //创建时间
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package tonerecomod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
)
|
||||
|
||||
// OffiRecomPages OffiRecomPages
|
||||
func OffiRecomPages(sort D, skip int64, limit int64) (page OffiRecomPage, err error) {
|
||||
pipeline := []M{
|
||||
{
|
||||
"$facet": M{
|
||||
"total": A{M{"$count": "total"}},
|
||||
"list": A{
|
||||
M{"$sort": sort},
|
||||
M{"$skip": skip},
|
||||
M{"$limit": limit},
|
||||
M{
|
||||
"$lookup": M{
|
||||
"from": models.VideoInfo,
|
||||
"localField": "vid",
|
||||
"foreignField": "_id",
|
||||
"as": "video_info",
|
||||
},
|
||||
},
|
||||
M{
|
||||
"$unwind": "$video_info",
|
||||
},
|
||||
M{
|
||||
"$set": M{
|
||||
"video_info.sortKey": "$sortKey",
|
||||
"video_info.enable": "$enable",
|
||||
},
|
||||
},
|
||||
M{
|
||||
"$replaceWith": "$video_info",
|
||||
},
|
||||
M{
|
||||
"$lookup": M{
|
||||
"from": models.Tag,
|
||||
"localField": "tags",
|
||||
"foreignField": "_id",
|
||||
"as": "tags",
|
||||
},
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
"$project": M{
|
||||
"total": M{"$arrayElemAt": A{"$total.total", 0}},
|
||||
"list": 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
coll := mdb.Coll(models.ToneRecom)
|
||||
if err = coll.AggregateDecode(&page, pipeline); err != nil {
|
||||
log.ZapLog.Error(fmt.Sprintf("coll:%s Page error:%+v:", models.ToneRecom, err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user