183 lines
3.8 KiB
Go
183 lines
3.8 KiB
Go
package vidmod
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"91porn-server/common/log"
|
|
"91porn-server/common/pageopt"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type VIDSlice []VideoModel
|
|
|
|
func (v VIDSlice) ToMap() map[ObjectID]*VideoModel {
|
|
m := make(map[ObjectID]*VideoModel, len(v))
|
|
for i := 0; i < len(v); i++ {
|
|
m[v[i].ID] = &v[i]
|
|
}
|
|
return m
|
|
}
|
|
|
|
func (v VIDSlice) IDs() []ObjectID {
|
|
ids := make([]ObjectID, len(v))
|
|
for i, v := range v {
|
|
ids[i] = v.ID
|
|
}
|
|
return ids
|
|
}
|
|
|
|
type Matcher = pageopt.Matcher
|
|
|
|
// IDInMatch
|
|
type IDInMatch = pageopt.IDInMatch
|
|
|
|
// PublisherIDMatch
|
|
type PublisherIDMatch struct {
|
|
PublisherID *uint64
|
|
}
|
|
|
|
func (c *PublisherIDMatch) New() Matcher {
|
|
return pageopt.NewAssignMatch("publisherID", c.PublisherID)
|
|
}
|
|
|
|
// PublisherIDInMatch
|
|
type PublisherIDInMatch struct {
|
|
PublisherIDList []uint64
|
|
}
|
|
|
|
func (c *PublisherIDInMatch) New() Matcher {
|
|
return pageopt.NewInMatch("publisherID", c.PublisherIDList)
|
|
}
|
|
|
|
// StatusInMatch
|
|
type StatusInMatch struct {
|
|
StatusList []int
|
|
}
|
|
|
|
func (c *StatusInMatch) New() Matcher {
|
|
return pageopt.NewInMatch("status", c.StatusList)
|
|
}
|
|
|
|
// StatusInMatch
|
|
type StatusMatch struct {
|
|
Status int
|
|
}
|
|
|
|
func (c *StatusMatch) New() Matcher {
|
|
return pageopt.NewAssignMatch("status", c.Status)
|
|
}
|
|
|
|
// ReviewAtGTEAndLTMatch
|
|
type ReviewAtGTEAndLTMatch struct {
|
|
GTE *time.Time
|
|
LT *time.Time
|
|
}
|
|
|
|
func (c *ReviewAtGTEAndLTMatch) New() Matcher {
|
|
return pageopt.NewGTEAndLTMatch("reviewAt", c.GTE, c.LT)
|
|
}
|
|
|
|
// UpdatedAtGTEAndLTMatch
|
|
type UpdatedAtGTEAndLTMatch struct {
|
|
GTE *time.Time
|
|
LT *time.Time
|
|
}
|
|
|
|
func (c *UpdatedAtGTEAndLTMatch) New() Matcher {
|
|
return pageopt.NewGTEAndLTMatch("updatedAt", c.GTE, c.LT)
|
|
}
|
|
|
|
type CreatedAtGTEAndLTMatch = pageopt.CreatedAtGTEAndLTMatch
|
|
|
|
type Sort = bson.D
|
|
|
|
func List(sort Sort, skip, limit *int64, matchers ...Matcher) (VIDSlice, error) {
|
|
filter := pageopt.MergeM(matchers)
|
|
opt := &options.FindOptions{}
|
|
if len(sort) != 0 {
|
|
opt.SetSort(sort)
|
|
}
|
|
if skip != nil {
|
|
opt.SetSkip(*skip)
|
|
}
|
|
if limit != nil {
|
|
opt.SetLimit(*limit)
|
|
}
|
|
list := VIDSlice{}
|
|
if err := coll(nil).Find(&list, filter, opt); err != nil {
|
|
log.Error("vidmod List error", log.E(err))
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func Count(matchers ...Matcher) (int64, error) {
|
|
filter := pageopt.MergeM(matchers)
|
|
count, err := coll(nil).Count(filter)
|
|
if err != nil {
|
|
log.Error("vidmod Count error", log.E(err))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
func CountMapByPublisherID(matchers ...Matcher) (map[uint64]int64, error) {
|
|
filter := pageopt.MergeM(matchers)
|
|
pipeline := []M{
|
|
{"$match": filter},
|
|
{"$group": M{"_id": "$publisherID", "count": M{"$sum": 1}}},
|
|
}
|
|
var list []struct {
|
|
PublisherID uint64 `bson:"_id"`
|
|
Count int64 `bson:"count"`
|
|
}
|
|
if err := coll(nil).Aggregate(&list, pipeline); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "CountTotalLikeByUID", table, "Aggregate", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return nil, err
|
|
}
|
|
m := make(map[uint64]int64, len(list))
|
|
for _, v := range list {
|
|
m[v.PublisherID] = v.Count
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func PublisherIDList(matchers ...Matcher) (uids []uint64, err error) {
|
|
filter := pageopt.MergeM(matchers)
|
|
list, err := coll(nil).Distinct("publisherID", filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, i := range list {
|
|
id, err := strconv.Atoi(fmt.Sprintf("%v", i))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
uids = append(uids, uint64(id))
|
|
}
|
|
return uids, nil
|
|
}
|
|
|
|
func CountNum(filter interface{}) (int64, error) {
|
|
count, err := coll(nil).Count(filter)
|
|
if err != nil {
|
|
log.Error("vidmod Count error", log.E(err))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
func GetListByCond(cond bson.M, opts *options.FindOptions) ([]VideoModel, error) {
|
|
var data []VideoModel
|
|
if err := coll(nil).Find(&data, cond, opts); err != nil {
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|