package mediamod import ( "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) type moduleLatestContentAt struct { ModuleID primitive.ObjectID `bson:"_id"` LatestCreatedAt time.Time `bson:"latestCreatedAt"` LatestContentUpdated time.Time `bson:"latestContentUpdated"` } // LatestContentAtByModules 返回动漫、漫画等媒体亚模块最近一次新增或子集更新时间。 func LatestContentAtByModules(moduleIDs []primitive.ObjectID) (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": 1, "isDelete": false, "mId": bson.M{"$in": moduleIDs}, }}, {"$group": bson.M{ "_id": "$mId", "latestCreatedAt": bson.M{"$max": "$createdAt"}, "latestContentUpdated": bson.M{"$max": "$contentUpdateTime"}, }}, } var rows []moduleLatestContentAt if err := coll(nil).Aggregate(&rows, pipeline); err != nil { return nil, err } for _, row := range rows { latest := row.LatestCreatedAt if row.LatestContentUpdated.After(latest) { latest = row.LatestContentUpdated } if !row.ModuleID.IsZero() && !latest.IsZero() { result[row.ModuleID.Hex()] = latest } } return result, nil }