@@ -88,6 +88,7 @@ type IPostDao interface {
8888 GetDetailByID (ctx context.Context , id bson.ObjectID ) (* PostCategoryTags , error )
8989 GetList (ctx context.Context , pagePipeline mongo.Pipeline , cond bson.D ) ([]* PostCategoryTags , int64 , error )
9090 GetListWithTagFilter (ctx context.Context , pagePipeline mongo.Pipeline , cond bson.D , hasTagFilter bool , labelName string ) ([]* PostCategoryTags , int64 , error )
91+ GetListWithFilter (ctx context.Context , pagePipeline mongo.Pipeline , cond bson.D , hasTagFilter bool , labelName string , hasCategoryFilter bool , categoryName string ) ([]* PostCategoryTags , int64 , error )
9192 GetAllPublishPost (ctx context.Context ) ([]* Post , error )
9293 FindByAlias (ctx context.Context , alias string ) (* Post , error )
9394}
@@ -209,7 +210,7 @@ func (d *PostDao) GetList(ctx context.Context, pagePipeline mongo.Pipeline, cond
209210}
210211
211212// buildCountPipeline 构建计数管道
212- func (d * PostDao ) buildCountPipeline (cond bson.D , labelName string ) mongo.Pipeline {
213+ func (d * PostDao ) buildCountPipeline (cond bson.D , labelName string , categoryName string ) mongo.Pipeline {
213214 stageBuilder := aggregation .NewStageBuilder ().Match (cond ).
214215 Lookup ("label" , "category" , & aggregation.LookUpOptions {
215216 LocalField : "category_id" ,
@@ -231,6 +232,14 @@ func (d *PostDao) buildCountPipeline(cond bson.D, labelName string) mongo.Pipeli
231232 )))
232233 }
233234
235+ // 添加分类过滤条件
236+ if categoryName != "" {
237+ stageBuilder = stageBuilder .Match (query .And (
238+ query .Eq ("category.type" , "category" ),
239+ query .Eq ("category.name" , categoryName ),
240+ ))
241+ }
242+
234243 return stageBuilder .Build ()
235244}
236245
@@ -247,7 +256,7 @@ func (d *PostDao) GetListWithTagFilter(ctx context.Context, pagePipeline mongo.P
247256 var count int64
248257 if hasTagFilter {
249258 // 有标签过滤时,使用聚合管道计数
250- countPipeline := d .buildCountPipeline (cond , labelName )
259+ countPipeline := d .buildCountPipeline (cond , labelName , "" )
251260 var countResult []bson.M
252261 err = d .coll .Aggregator ().Pipeline (countPipeline ).AggregateWithParse (ctx , & countResult )
253262 if err != nil {
@@ -265,6 +274,37 @@ func (d *PostDao) GetListWithTagFilter(ctx context.Context, pagePipeline mongo.P
265274 return utils .ValToPtrList (postResult ), count , err
266275}
267276
277+ // GetListWithFilter 获取文章列表(带标签和分类过滤)
278+ func (d * PostDao ) GetListWithFilter (ctx context.Context , pagePipeline mongo.Pipeline , cond bson.D , hasTagFilter bool , labelName string , hasCategoryFilter bool , categoryName string ) ([]* PostCategoryTags , int64 , error ) {
279+ // 执行分页查询
280+ var postResult []PostCategoryTags
281+ err := d .coll .Aggregator ().Pipeline (pagePipeline ).AggregateWithParse (ctx , & postResult )
282+ if err != nil {
283+ return nil , 0 , err
284+ }
285+
286+ // 计算总数
287+ var count int64
288+ if hasTagFilter || hasCategoryFilter {
289+ // 有过滤条件时,使用聚合管道计数
290+ countPipeline := d .buildCountPipeline (cond , labelName , categoryName )
291+ var countResult []bson.M
292+ err = d .coll .Aggregator ().Pipeline (countPipeline ).AggregateWithParse (ctx , & countResult )
293+ if err != nil {
294+ return nil , 0 , err
295+ }
296+ count = int64 (len (countResult ))
297+ } else {
298+ // 无过滤条件时,使用简单计数
299+ count , err = d .coll .Finder ().Filter (cond ).Count (ctx )
300+ if err != nil {
301+ return nil , 0 , err
302+ }
303+ }
304+
305+ return utils .ValToPtrList (postResult ), count , err
306+ }
307+
268308// SoftDeleteBatch 批量软删除文章
269309func (d * PostDao ) SoftDeleteBatch (ctx context.Context , ids []bson.ObjectID ) error {
270310 _ , err := d .coll .Updater ().Filter (query .In ("_id" , ids ... )).Updates (update .NewBuilder ().Set ("deleted_at" , time .Now ()).Set ("is_publish" , false ).Set ("is_top" , false ).Build ()).UpdateMany (ctx )
0 commit comments