60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package vidmod
|
|
|
|
import (
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type ModuleLatestAt struct {
|
|
ModuleID string `bson:"_id"`
|
|
LatestAt time.Time `bson:"latestAt"`
|
|
}
|
|
|
|
// LatestReviewAt 返回过滤条件下最近一次审核通过时间。
|
|
func LatestReviewAt(filter bson.M) (*time.Time, error) {
|
|
var list []VideoModel
|
|
opts := options.Find().
|
|
SetSort(bson.D{{Key: "reviewAt", Value: -1}, {Key: "_id", Value: -1}}).
|
|
SetLimit(1).
|
|
SetProjection(bson.M{"reviewAt": 1})
|
|
if err := coll(nil).Find(&list, filter, opts); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(list) == 0 || list[0].ReviewAt.IsZero() {
|
|
return nil, nil
|
|
}
|
|
latest := list[0].ReviewAt
|
|
return &latest, nil
|
|
}
|
|
|
|
// LatestReviewAtByModules 批量返回各亚模块最近一次内容审核通过时间。
|
|
func LatestReviewAtByModules(moduleIDs []string) (map[string]time.Time, error) {
|
|
result := make(map[string]time.Time, len(moduleIDs))
|
|
if len(moduleIDs) == 0 {
|
|
return result, nil
|
|
}
|
|
pipeline := []bson.M{
|
|
{"$match": bson.M{
|
|
"status": CheckPass,
|
|
"mId": bson.M{"$in": moduleIDs},
|
|
"reviewAt": bson.M{"$gt": time.Time{}},
|
|
}},
|
|
{"$group": bson.M{
|
|
"_id": "$mId",
|
|
"latestAt": bson.M{"$max": "$reviewAt"},
|
|
}},
|
|
}
|
|
var rows []ModuleLatestAt
|
|
if err := coll(nil).Aggregate(&rows, pipeline); err != nil {
|
|
return nil, err
|
|
}
|
|
for _, row := range rows {
|
|
if row.ModuleID != "" && !row.LatestAt.IsZero() {
|
|
result[row.ModuleID] = row.LatestAt
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|