134 lines
3.9 KiB
Go
134 lines
3.9 KiB
Go
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
|
|
}
|