Go + PostgreSQL + Valkey backend (cmd/internal/migrations), uni-app mobile frontend, PC web (web/), admin console (admin/), Android WebView container (android_project/). Build artifacts, node_modules, local env and debug screenshots excluded.
546 lines
18 KiB
Go
546 lines
18 KiB
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/yourorg/article-platform/internal/domain"
|
|
)
|
|
|
|
// ── 话题 (TopicRepo) ──
|
|
|
|
type TopicRepo struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewTopicRepo(p *pgxpool.Pool) *TopicRepo {
|
|
return &TopicRepo{pool: p}
|
|
}
|
|
|
|
func (r *TopicRepo) List(ctx context.Context, q Querier, sort, categoryID, keyword string, currentUserID string, limit, offset int) ([]domain.Topic, int64, error) {
|
|
where := "t.status = 'active'"
|
|
var args []any
|
|
|
|
if categoryID != "" {
|
|
args = append(args, categoryID)
|
|
where += fmt.Sprintf(" AND t.category_id = $%d", len(args))
|
|
}
|
|
if keyword != "" {
|
|
kw := "%" + escapeLike(keyword) + "%"
|
|
args = append(args, kw)
|
|
where += fmt.Sprintf(" AND (t.name ILIKE $%d ESCAPE '\\' OR t.description ILIKE $%d ESCAPE '\\')", len(args), len(args))
|
|
}
|
|
|
|
var total int64
|
|
countSQL := "SELECT count(*) FROM topics t WHERE " + where
|
|
if err := q.QueryRow(ctx, countSQL, args...).Scan(&total); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
orderBy := "t.created_at DESC"
|
|
if sort == "hot" {
|
|
orderBy = "(t.article_count + t.follow_count) DESC, t.updated_at DESC"
|
|
}
|
|
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 20
|
|
}
|
|
args = append(args, limit, offset)
|
|
limitArg := len(args) - 1
|
|
offsetArg := len(args)
|
|
|
|
isFollowedExpr := "false"
|
|
if currentUserID != "" {
|
|
args = append(args, currentUserID)
|
|
userArg := len(args)
|
|
isFollowedExpr = fmt.Sprintf("EXISTS(SELECT 1 FROM user_topic_follows WHERE topic_id = t.id AND user_id = $%d)", userArg)
|
|
}
|
|
|
|
sql := fmt.Sprintf(`
|
|
SELECT t.id, t.name, t.description, t.cover_url, t.category_id, c.name,
|
|
t.follow_count, t.article_count, t.status, t.creator_id,
|
|
%s as is_followed, t.created_at, t.updated_at
|
|
FROM topics t
|
|
LEFT JOIN categories c ON c.id = t.category_id
|
|
WHERE %s
|
|
ORDER BY %s
|
|
LIMIT $%d OFFSET $%d
|
|
`, isFollowedExpr, where, orderBy, limitArg, offsetArg)
|
|
|
|
rows, err := q.Query(ctx, sql, args...)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []domain.Topic
|
|
for rows.Next() {
|
|
var t domain.Topic
|
|
err := rows.Scan(
|
|
&t.ID, &t.Name, &t.Description, &t.CoverURL, &t.CategoryID, &t.CategoryName,
|
|
&t.FollowCount, &t.ArticleCount, &t.Status, &t.CreatorID,
|
|
&t.IsFollowed, &t.CreatedAt, &t.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out = append(out, t)
|
|
}
|
|
return out, total, rows.Err()
|
|
}
|
|
|
|
func (r *TopicRepo) GetByID(ctx context.Context, q Querier, id string, currentUserID string) (*domain.Topic, error) {
|
|
var args []any
|
|
args = append(args, id)
|
|
|
|
isFollowedExpr := "false"
|
|
if currentUserID != "" {
|
|
args = append(args, currentUserID)
|
|
isFollowedExpr = "EXISTS(SELECT 1 FROM user_topic_follows WHERE topic_id = t.id AND user_id = $2)"
|
|
}
|
|
|
|
sql := fmt.Sprintf(`
|
|
SELECT t.id, t.name, t.description, t.cover_url, t.category_id, c.name,
|
|
t.follow_count, t.article_count, t.status, t.creator_id,
|
|
%s as is_followed, t.created_at, t.updated_at
|
|
FROM topics t
|
|
LEFT JOIN categories c ON c.id = t.category_id
|
|
WHERE t.id = $1
|
|
`, isFollowedExpr)
|
|
|
|
var t domain.Topic
|
|
err := q.QueryRow(ctx, sql, args...).Scan(
|
|
&t.ID, &t.Name, &t.Description, &t.CoverURL, &t.CategoryID, &t.CategoryName,
|
|
&t.FollowCount, &t.ArticleCount, &t.Status, &t.CreatorID,
|
|
&t.IsFollowed, &t.CreatedAt, &t.UpdatedAt,
|
|
)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return &t, err
|
|
}
|
|
|
|
func (r *TopicRepo) Insert(ctx context.Context, q Querier, t *domain.Topic) error {
|
|
sql := `INSERT INTO topics (id, name, description, cover_url, category_id, status, creator_id, created_at, updated_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, now(), now())`
|
|
_, err := q.Exec(ctx, sql, t.ID, t.Name, t.Description, t.CoverURL, t.CategoryID, t.Status, t.CreatorID)
|
|
return err
|
|
}
|
|
|
|
func (r *TopicRepo) Update(ctx context.Context, q Querier, t *domain.Topic) error {
|
|
sql := `UPDATE topics SET name = $2, description = $3, cover_url = $4, category_id = $5, status = $6, updated_at = now()
|
|
WHERE id = $1`
|
|
_, err := q.Exec(ctx, sql, t.ID, t.Name, t.Description, t.CoverURL, t.CategoryID, t.Status)
|
|
return err
|
|
}
|
|
|
|
func (r *TopicRepo) Delete(ctx context.Context, q Querier, id string) error {
|
|
_, err := q.Exec(ctx, `DELETE FROM topics WHERE id = $1`, id)
|
|
return err
|
|
}
|
|
|
|
// ── 模板 (TemplateRepo) ──
|
|
|
|
type TemplateRepo struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewTemplateRepo(p *pgxpool.Pool) *TemplateRepo {
|
|
return &TemplateRepo{pool: p}
|
|
}
|
|
|
|
func (r *TemplateRepo) List(ctx context.Context, q Querier, category, targetPlatform string, limit, offset int) ([]domain.Template, int64, error) {
|
|
where := "1=1"
|
|
var args []any
|
|
if category != "" && category != "all" {
|
|
args = append(args, category)
|
|
where += fmt.Sprintf(" AND category = $%d", len(args))
|
|
}
|
|
if targetPlatform != "" && targetPlatform != "all" {
|
|
args = append(args, targetPlatform)
|
|
where += fmt.Sprintf(" AND (target_platform = $%d OR target_platform = 'all')", len(args))
|
|
}
|
|
|
|
var total int64
|
|
if err := q.QueryRow(ctx, "SELECT count(*) FROM templates WHERE "+where, args...).Scan(&total); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 20
|
|
}
|
|
args = append(args, limit, offset)
|
|
sql := fmt.Sprintf(`SELECT id, title, description, category, cover_url, target_platform, sections_json, created_at, updated_at
|
|
FROM templates WHERE %s ORDER BY updated_at DESC LIMIT $%d OFFSET $%d`, where, len(args)-1, len(args))
|
|
|
|
rows, err := q.Query(ctx, sql, args...)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []domain.Template
|
|
for rows.Next() {
|
|
var t domain.Template
|
|
err := rows.Scan(&t.ID, &t.Title, &t.Description, &t.Category, &t.CoverURL, &t.TargetPlatform, &t.SectionsJSON, &t.CreatedAt, &t.UpdatedAt)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out = append(out, t)
|
|
}
|
|
return out, total, rows.Err()
|
|
}
|
|
|
|
func (r *TemplateRepo) GetByID(ctx context.Context, q Querier, id string) (*domain.Template, error) {
|
|
var t domain.Template
|
|
err := q.QueryRow(ctx, `SELECT id, title, description, category, cover_url, target_platform, sections_json, created_at, updated_at
|
|
FROM templates WHERE id = $1`, id).Scan(
|
|
&t.ID, &t.Title, &t.Description, &t.Category, &t.CoverURL, &t.TargetPlatform, &t.SectionsJSON, &t.CreatedAt, &t.UpdatedAt,
|
|
)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return &t, err
|
|
}
|
|
|
|
func (r *TemplateRepo) Insert(ctx context.Context, q Querier, t *domain.Template) error {
|
|
_, err := q.Exec(ctx, `INSERT INTO templates (id, title, description, category, cover_url, target_platform, sections_json, created_at, updated_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, now(), now())`,
|
|
t.ID, t.Title, t.Description, t.Category, t.CoverURL, t.TargetPlatform, t.SectionsJSON)
|
|
return err
|
|
}
|
|
|
|
func (r *TemplateRepo) Update(ctx context.Context, q Querier, t *domain.Template) error {
|
|
_, err := q.Exec(ctx, `UPDATE templates SET title = $2, description = $3, category = $4, cover_url = $5, target_platform = $6, sections_json = $7, updated_at = now()
|
|
WHERE id = $1`,
|
|
t.ID, t.Title, t.Description, t.Category, t.CoverURL, t.TargetPlatform, t.SectionsJSON)
|
|
return err
|
|
}
|
|
|
|
func (r *TemplateRepo) Delete(ctx context.Context, q Querier, id string) error {
|
|
_, err := q.Exec(ctx, `DELETE FROM templates WHERE id = $1`, id)
|
|
return err
|
|
}
|
|
|
|
// ── 个人中心列表与互动增强 (InteractionRepo 扩展) ──
|
|
|
|
func (r *InteractionRepo) ListUserFavorites(ctx context.Context, q Querier, userID string, limit, offset int) ([]domain.Article, int64, error) {
|
|
var total int64
|
|
if err := q.QueryRow(ctx, `SELECT count(*) FROM article_favorites f JOIN articles a ON a.id = f.article_id WHERE f.user_id = $1 AND a.deleted_at IS NULL`, userID).Scan(&total); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 20
|
|
}
|
|
sql := `SELECT ` + articleCols + `, u.nickname, u.avatar_url, c.name
|
|
FROM article_favorites f
|
|
JOIN articles a ON a.id = f.article_id
|
|
LEFT JOIN users u ON u.id = a.owner_id
|
|
LEFT JOIN categories c ON c.id = a.category_id
|
|
WHERE f.user_id = $1 AND a.deleted_at IS NULL
|
|
ORDER BY f.created_at DESC LIMIT $2 OFFSET $3`
|
|
|
|
rows, err := q.Query(ctx, sql, userID, limit, offset)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []domain.Article
|
|
for rows.Next() {
|
|
a, err := scanArticleWithJoins(rows)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out = append(out, *a)
|
|
}
|
|
return out, total, rows.Err()
|
|
}
|
|
|
|
func (r *InteractionRepo) ListUserLikes(ctx context.Context, q Querier, userID string, limit, offset int) ([]domain.Article, int64, error) {
|
|
var total int64
|
|
if err := q.QueryRow(ctx, `SELECT count(*) FROM article_likes l JOIN articles a ON a.id = l.article_id WHERE l.user_id = $1 AND a.deleted_at IS NULL`, userID).Scan(&total); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 20
|
|
}
|
|
sql := `SELECT ` + articleCols + `, u.nickname, u.avatar_url, c.name
|
|
FROM article_likes l
|
|
JOIN articles a ON a.id = l.article_id
|
|
LEFT JOIN users u ON u.id = a.owner_id
|
|
LEFT JOIN categories c ON c.id = a.category_id
|
|
WHERE l.user_id = $1 AND a.deleted_at IS NULL
|
|
ORDER BY l.created_at DESC LIMIT $2 OFFSET $3`
|
|
|
|
rows, err := q.Query(ctx, sql, userID, limit, offset)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []domain.Article
|
|
for rows.Next() {
|
|
a, err := scanArticleWithJoins(rows)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out = append(out, *a)
|
|
}
|
|
return out, total, rows.Err()
|
|
}
|
|
|
|
func (r *InteractionRepo) ListUserFollowings(ctx context.Context, q Querier, userID string, limit, offset int) ([]domain.User, int64, error) {
|
|
var total int64
|
|
if err := q.QueryRow(ctx, `SELECT count(*) FROM user_follows uf JOIN users u ON u.id = uf.following_id WHERE uf.follower_id = $1 AND u.deleted_at IS NULL`, userID).Scan(&total); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 20
|
|
}
|
|
sql := `SELECT u.id, u.account, u.nickname, u.email, u.avatar_url, u.role, u.status, u.bio, u.gender, u.region, u.last_login_at, u.created_at
|
|
FROM user_follows uf
|
|
JOIN users u ON u.id = uf.following_id
|
|
WHERE uf.follower_id = $1 AND u.deleted_at IS NULL
|
|
ORDER BY uf.created_at DESC LIMIT $2 OFFSET $3`
|
|
|
|
rows, err := q.Query(ctx, sql, userID, limit, offset)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []domain.User
|
|
for rows.Next() {
|
|
u, err := scanUser(rows)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out = append(out, *u)
|
|
}
|
|
return out, total, rows.Err()
|
|
}
|
|
|
|
func (r *InteractionRepo) ListUserFollowers(ctx context.Context, q Querier, userID string, limit, offset int) ([]domain.User, int64, error) {
|
|
var total int64
|
|
if err := q.QueryRow(ctx, `SELECT count(*) FROM user_follows uf JOIN users u ON u.id = uf.follower_id WHERE uf.following_id = $1 AND u.deleted_at IS NULL`, userID).Scan(&total); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 20
|
|
}
|
|
sql := `SELECT u.id, u.account, u.nickname, u.email, u.avatar_url, u.role, u.status, u.bio, u.gender, u.region, u.last_login_at, u.created_at
|
|
FROM user_follows uf
|
|
JOIN users u ON u.id = uf.follower_id
|
|
WHERE uf.following_id = $1 AND u.deleted_at IS NULL
|
|
ORDER BY uf.created_at DESC LIMIT $2 OFFSET $3`
|
|
|
|
rows, err := q.Query(ctx, sql, userID, limit, offset)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []domain.User
|
|
for rows.Next() {
|
|
u, err := scanUser(rows)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out = append(out, *u)
|
|
}
|
|
return out, total, rows.Err()
|
|
}
|
|
|
|
// 话题关注
|
|
func (r *InteractionRepo) FollowTopic(ctx context.Context, q Querier, userID, topicID string) error {
|
|
_, err := q.Exec(ctx, `INSERT INTO user_topic_follows (user_id, topic_id, created_at)
|
|
VALUES ($1, $2, now()) ON CONFLICT (user_id, topic_id) DO NOTHING`, userID, topicID)
|
|
if err == nil {
|
|
_, _ = q.Exec(ctx, `UPDATE topics SET follow_count = follow_count + 1 WHERE id = $1`, topicID)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// IsFollowingTopic 当前用户是否已关注话题
|
|
func (r *InteractionRepo) IsFollowingTopic(ctx context.Context, q Querier, userID, topicID string) (bool, error) {
|
|
var exists bool
|
|
err := q.QueryRow(ctx,
|
|
`SELECT EXISTS(SELECT 1 FROM user_topic_follows WHERE user_id=$1 AND topic_id=$2)`,
|
|
userID, topicID).Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
func (r *InteractionRepo) UnfollowTopic(ctx context.Context, q Querier, userID, topicID string) error {
|
|
tag, err := q.Exec(ctx, `DELETE FROM user_topic_follows WHERE user_id = $1 AND topic_id = $2`, userID, topicID)
|
|
if err == nil && tag.RowsAffected() > 0 {
|
|
_, _ = q.Exec(ctx, `UPDATE topics SET follow_count = GREATEST(0, follow_count - 1) WHERE id = $1`, topicID)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (r *InteractionRepo) ListFollowedTopics(ctx context.Context, q Querier, userID string, limit, offset int) ([]domain.Topic, int64, error) {
|
|
var total int64
|
|
if err := q.QueryRow(ctx, `SELECT count(*) FROM user_topic_follows utf JOIN topics t ON t.id = utf.topic_id WHERE utf.user_id = $1 AND t.status = 'active'`, userID).Scan(&total); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 20
|
|
}
|
|
sql := `SELECT t.id, t.name, t.description, t.cover_url, t.category_id, c.name,
|
|
t.follow_count, t.article_count, t.status, t.creator_id,
|
|
true as is_followed, t.created_at, t.updated_at
|
|
FROM user_topic_follows utf
|
|
JOIN topics t ON t.id = utf.topic_id
|
|
LEFT JOIN categories c ON c.id = t.category_id
|
|
WHERE utf.user_id = $1 AND t.status = 'active'
|
|
ORDER BY utf.created_at DESC LIMIT $2 OFFSET $3`
|
|
|
|
rows, err := q.Query(ctx, sql, userID, limit, offset)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []domain.Topic
|
|
for rows.Next() {
|
|
var t domain.Topic
|
|
err := rows.Scan(
|
|
&t.ID, &t.Name, &t.Description, &t.CoverURL, &t.CategoryID, &t.CategoryName,
|
|
&t.FollowCount, &t.ArticleCount, &t.Status, &t.CreatorID,
|
|
&t.IsFollowed, &t.CreatedAt, &t.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out = append(out, t)
|
|
}
|
|
return out, total, rows.Err()
|
|
}
|
|
|
|
// 浏览历史
|
|
func (r *InteractionRepo) RecordView(ctx context.Context, q Querier, userID, articleID string) error {
|
|
_, err := q.Exec(ctx, `
|
|
INSERT INTO article_view_history (user_id, article_id, viewed_at)
|
|
VALUES ($1, $2, now())
|
|
ON CONFLICT (user_id, article_id)
|
|
DO UPDATE SET viewed_at = now()
|
|
`, userID, articleID)
|
|
return err
|
|
}
|
|
|
|
func (r *InteractionRepo) ListViewHistory(ctx context.Context, q Querier, userID string, limit, offset int) ([]domain.Article, int64, error) {
|
|
var total int64
|
|
if err := q.QueryRow(ctx, `SELECT count(*) FROM article_view_history v JOIN articles a ON a.id = v.article_id WHERE v.user_id = $1 AND a.deleted_at IS NULL`, userID).Scan(&total); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 20
|
|
}
|
|
sql := `SELECT ` + articleCols + `, u.nickname, u.avatar_url, c.name, v.viewed_at
|
|
FROM article_view_history v
|
|
JOIN articles a ON a.id = v.article_id
|
|
LEFT JOIN users u ON u.id = a.owner_id
|
|
LEFT JOIN categories c ON c.id = a.category_id
|
|
WHERE v.user_id = $1 AND a.deleted_at IS NULL
|
|
ORDER BY v.viewed_at DESC LIMIT $2 OFFSET $3`
|
|
|
|
rows, err := q.Query(ctx, sql, userID, limit, offset)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []domain.Article
|
|
for rows.Next() {
|
|
var a domain.Article
|
|
var viewedAt time.Time
|
|
err := rows.Scan(
|
|
&a.ID, &a.MaskID, &a.Title, &a.RichTextTitle, &a.Subtitle, &a.Abstract,
|
|
&a.CoverImgURL, &a.CoverCrop, &a.VisualConfig, &a.InteractionConfig,
|
|
&a.BgStyle, &a.BgImgURL, &a.FontID, &a.MusicID, &a.TextPos,
|
|
&a.EnableWatermark, &a.EnableComment, &a.Privacy, &a.HasReward,
|
|
&a.OwnerID, &a.CategoryID, &a.Status, &a.ReviewStatus, &a.ReviewNote,
|
|
&a.ReviewedBy, &a.ReviewedAt, &a.PublishedAt,
|
|
&a.ViewCount, &a.LikeCount, &a.FavCount, &a.CommentCount, &a.ShareCount,
|
|
&a.SchemaVersion, &a.CreatedAt, &a.UpdatedAt,
|
|
&a.AuthorNickname, &a.AuthorAvatar, &a.CategoryName,
|
|
&viewedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
a.ViewedAt = &viewedAt
|
|
out = append(out, a)
|
|
}
|
|
return out, total, rows.Err()
|
|
}
|
|
|
|
func (r *InteractionRepo) DeleteViewHistory(ctx context.Context, q Querier, userID, articleID string) error {
|
|
_, err := q.Exec(ctx, `DELETE FROM article_view_history
|
|
WHERE user_id = $1 AND (article_id::text = $2 OR article_id IN (SELECT id FROM articles WHERE mask_id = $2))`, userID, articleID)
|
|
return err
|
|
}
|
|
|
|
func (r *InteractionRepo) ClearViewHistory(ctx context.Context, q Querier, userID string) error {
|
|
_, err := q.Exec(ctx, `DELETE FROM article_view_history WHERE user_id = $1`, userID)
|
|
return err
|
|
}
|
|
|
|
// 单篇数据看板 (Daily stats trend)
|
|
type ArticleDailyPoint struct {
|
|
Date string `json:"date"`
|
|
Views int64 `json:"views"`
|
|
Likes int64 `json:"likes"`
|
|
Comments int64 `json:"comments"`
|
|
Shares int64 `json:"shares"`
|
|
}
|
|
|
|
func (r *StatsRepo) GetArticleDailyTrend(ctx context.Context, q Querier, articleID string, days int) ([]ArticleDailyPoint, error) {
|
|
if days <= 0 || days > 90 {
|
|
days = 7
|
|
}
|
|
sql := `SELECT to_char(stat_date, 'YYYY-MM-DD') as d, pv_count, like_inc, comment_inc, share_inc
|
|
FROM article_stats_daily
|
|
WHERE article_id = $1::uuid AND stat_date >= CURRENT_DATE - ($2 * INTERVAL '1 day')
|
|
ORDER BY stat_date ASC`
|
|
|
|
rows, err := q.Query(ctx, sql, articleID, days)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var points []ArticleDailyPoint
|
|
for rows.Next() {
|
|
var p ArticleDailyPoint
|
|
if err := rows.Scan(&p.Date, &p.Views, &p.Likes, &p.Comments, &p.Shares); err != nil {
|
|
return nil, err
|
|
}
|
|
points = append(points, p)
|
|
}
|
|
|
|
// 如果没有历史分区数据,填充连续日期点
|
|
if len(points) == 0 {
|
|
now := time.Now()
|
|
for i := days - 1; i >= 0; i-- {
|
|
d := now.AddDate(0, 0, -i).Format("2006-01-02")
|
|
points = append(points, ArticleDailyPoint{
|
|
Date: d,
|
|
})
|
|
}
|
|
}
|
|
return points, rows.Err()
|
|
}
|