This commit is contained in:
2026-09-15 16:02:45 +08:00
parent 112c5f8e46
commit ade2453083
70 changed files with 1441 additions and 707 deletions
+28
View File
@@ -86,6 +86,34 @@ func (d *LikeDao) LikeCountByTarget(ctx context.Context, likeType int8, targetID
return count, nil
}
// LikeCountByTargets 批量统计多个目标的点赞数(仅 status=1),返回 targetID -> count;空入参短路。
func (d *LikeDao) LikeCountByTargets(ctx context.Context, likeType int8, targetIDs []int64) (map[int64]int64, error) {
ctx, span := global.Tracer.Start(ctx, "mysqldao.LikeCountByTargets")
defer span.End()
counts := make(map[int64]int64, len(targetIDs))
if len(targetIDs) == 0 {
return counts, nil
}
var rows []struct {
TargetID int64 `gorm:"column:target_id"`
Cnt int64 `gorm:"column:cnt"`
}
if err := d.db.WithContext(ctx).Model(&mysqlmodel.Like{}).
Select("target_id, COUNT(*) AS cnt").
Where("like_type = ? AND target_id IN ? AND status = ?", likeType, targetIDs, int8(1)).
Group("target_id").
Scan(&rows).Error; err != nil {
span.RecordError(err)
return nil, err
}
for _, r := range rows {
counts[r.TargetID] = r.Cnt
}
return counts, nil
}
// LikeListUserIDsByTarget 查询点赞用户ID列表(仅 status=1)
func (d *LikeDao) LikeListUserIDsByTarget(ctx context.Context, likeType int8, targetID int64) ([]int64, error) {
ctx, span := global.Tracer.Start(ctx, "mysqldao.LikeListUserIDsByTarget")