@@ -0,0 +1,71 @@
|
||||
package videoactivitymod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/models/commod"
|
||||
"91porn-server/models/v/vidmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type LeaderboardType int
|
||||
|
||||
const (
|
||||
Original LeaderboardType = iota // 原创榜
|
||||
UP // UP主榜
|
||||
)
|
||||
|
||||
type ListRequest struct {
|
||||
ActivityID string `form:"activityId" json:"activityId"` // 当前活动ID
|
||||
Type LeaderboardType `form:"type" json:"type"` // 榜单类型
|
||||
commod.Page
|
||||
}
|
||||
|
||||
type ListResponse struct {
|
||||
ActivityID primitive.ObjectID `json:"activityId"` // 当前活动ID
|
||||
ActivityBackgroundImage string `json:"activityBackgroundImage"` // 活动背景图
|
||||
ActivityEndTime time.Time `json:"activityEndTime"` // 活动结束时间
|
||||
ActivityDesc string `json:"activityDesc"` // 活动详情描述
|
||||
WorkList []*vidmod.VideoInfo `json:"workList"` // 作品列表
|
||||
HasNext bool `json:"hasNext"` // 是否有下一页
|
||||
}
|
||||
|
||||
func (receiver *ListRequest) Filter() (primitive.M, error) {
|
||||
activityId, err := primitive.ObjectIDFromHex(receiver.ActivityID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
filter := bson.M{
|
||||
"activityId": activityId,
|
||||
"status": 1,
|
||||
"newsType": vidmod.SP,
|
||||
}
|
||||
if receiver.Type == UP {
|
||||
filter["isMadou"] = true
|
||||
}
|
||||
return filter, nil
|
||||
}
|
||||
|
||||
func (receiver *ListRequest) Options() *options.FindOptions {
|
||||
return options.Find().SetSkip(int64((receiver.PageNumber - 1) * receiver.PageSize)).SetLimit(int64(receiver.PageSize + 1)).SetSort(bson.M{"fakeLikeCount": -1})
|
||||
}
|
||||
|
||||
type HistoryRecordRequest struct {
|
||||
commod.Page
|
||||
}
|
||||
|
||||
func (receiver *HistoryRecordRequest) Filter() primitive.M {
|
||||
return bson.M{}
|
||||
}
|
||||
|
||||
func (receiver *HistoryRecordRequest) Options() *options.FindOptions {
|
||||
return options.Find().SetSkip(int64((receiver.PageNumber - 1) * receiver.PageSize)).SetLimit(int64(receiver.PageSize + 1)).SetSort(bson.M{"endTime": -1})
|
||||
}
|
||||
|
||||
type HistoryRecordResponse struct {
|
||||
HasNext bool `json:"hasNext"` // 是否有下一页
|
||||
ActivityList []*VideoActivity `json:"activityList"` // 列表
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package videoactivitymod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type StatusType int
|
||||
|
||||
const (
|
||||
Enable StatusType = iota + 1 // 使用中
|
||||
Unused // 未使用
|
||||
Deprecated // 弃用
|
||||
)
|
||||
|
||||
type VideoActivity struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // ID
|
||||
BackgroundImage string `json:"backgroundImage" bson:"backgroundImage"` // 背景图
|
||||
EndTime time.Time `json:"endTime" bson:"endTime"` // 结束时间
|
||||
Desc string `json:"desc" bson:"desc"` // 详情描述
|
||||
Status StatusType `json:"status" bson:"status"` // 状态
|
||||
CreateTime time.Time `json:"createTime" bson:"createTime"` // 创建时间
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package videoactivitymod
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.VideoActivity
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
indexModels := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "status", Value: 1}, {Key: "createTime", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "status", Value: 1}, {Key: "endTime", Value: -1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(indexModels); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// SubmitDocument 提交文档
|
||||
func SubmitDocument(document VideoActivity) error {
|
||||
result, err := coll(nil).InsertOne(&document)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "SubmitDocument", table, "InsertOne", err),
|
||||
log.Any("document", document),
|
||||
)
|
||||
return err
|
||||
}
|
||||
if result.InsertedID.(primitive.ObjectID).IsZero() {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "SubmitDocument", table, "result.InsertedID", "InsertedID is null"),
|
||||
log.Any("document", document),
|
||||
)
|
||||
return errors.New("result is null")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditDocument 修改文档
|
||||
func EditDocument(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:", "EditDocument", 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:", "EditDocument", table, "result.ModifiedCount", "ModifiedCount is zero"),
|
||||
log.Any("filter", filter),
|
||||
log.Any("update", update),
|
||||
)
|
||||
return errors.New("result is null")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryAllDocument 分页查询文档
|
||||
func QueryAllDocument(filter primitive.M, opts ...*options.FindOptions) (out []*VideoActivity, err error) {
|
||||
if err = coll(nil).Find(&out, filter, opts...); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllDocument", table, "Find", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CountDocument 查询文档条目数
|
||||
func CountDocument(filter primitive.M) (int64, error) {
|
||||
count, err := coll(nil).Count(filter)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "CountDocument", table, "Count", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// QueryDocumentByStatus 根据状态获取文档
|
||||
func QueryDocumentByStatus(status StatusType) (out VideoActivity, err error) {
|
||||
if err = coll(nil).FindOne(&out, bson.M{"status": status, "endTime": bson.M{"$gte": time.Now()}}, options.FindOne().SetSort(bson.M{"endTime": -1})); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryDocumentByStatus", table, "FindOne", err),
|
||||
log.Any("status", status),
|
||||
)
|
||||
return
|
||||
}
|
||||
// 未查询到开启中的活动、则查询结束时间最晚的一条记录
|
||||
if out.ID.IsZero() {
|
||||
if err = coll(nil).FindOne(&out, bson.M{}, options.FindOne().SetSort(bson.M{"endTime": -1})); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryDocumentByStatus", table, "FindOne", err),
|
||||
log.Any("status", status),
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func QueryDocumentByID(id primitive.ObjectID) (out VideoActivity, err error) {
|
||||
if err = coll(nil).FindOne(&out, bson.M{"_id": id}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryDocumentByStatus", table, "FindOne", err),
|
||||
log.Any("id", id),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user