170 lines
4.0 KiB
Go
170 lines
4.0 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"quyun/v2/app/requests"
|
|
"quyun/v2/database/models"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/samber/lo"
|
|
"go.ipao.vip/gen"
|
|
)
|
|
|
|
// @provider
|
|
type posts struct{}
|
|
|
|
// IncrViewCount
|
|
func (m *posts) IncrViewCount(ctx context.Context, postID int64) error {
|
|
tbl, query := models.PostQuery.QueryContext(ctx)
|
|
|
|
_, err := query.Where(tbl.ID.Eq(postID)).Inc(tbl.Views, 1)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "failed to increment view count for post %d", postID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// List
|
|
func (m *posts) List(
|
|
ctx context.Context,
|
|
pagination *requests.Pagination,
|
|
conds ...gen.Condition,
|
|
) (*requests.Pager, error) {
|
|
pagination.Format()
|
|
|
|
tbl, query := models.PostQuery.QueryContext(ctx)
|
|
items, cnt, err := query.Where(conds...).
|
|
Order(tbl.ID.Desc()).
|
|
FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "list post failed")
|
|
}
|
|
|
|
return &requests.Pager{
|
|
Items: items,
|
|
Total: cnt,
|
|
Pagination: *pagination,
|
|
}, nil
|
|
}
|
|
|
|
// SendTo
|
|
func (m *posts) SendTo(ctx context.Context, postID, userID int64) error {
|
|
model := &models.UserPost{
|
|
UserID: userID,
|
|
PostID: postID,
|
|
Price: -1,
|
|
}
|
|
return model.Create(ctx)
|
|
}
|
|
|
|
// PostBoughtStatistics 获取指定文件 ID 的购买次数
|
|
func (m *posts) BoughtStatistics(ctx context.Context, postIds []int64) (map[int64]int64, error) {
|
|
tbl, query := models.UserPostQuery.QueryContext(ctx)
|
|
|
|
var items []struct {
|
|
Count int64
|
|
PostID int64
|
|
}
|
|
err := query.Select(
|
|
tbl.UserID.Count().As("count"),
|
|
tbl.PostID,
|
|
).
|
|
Where(tbl.PostID.In(postIds...)).
|
|
Group(tbl.PostID).Scan(&items)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make(map[int64]int64)
|
|
for _, item := range items {
|
|
result[item.PostID] = item.Count
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Bought 获取用户购买记录
|
|
func (m *posts) Bought(ctx context.Context, userId int64, pagination *requests.Pagination) (*requests.Pager, error) {
|
|
pagination.Format()
|
|
tbl, query := models.UserPostQuery.QueryContext(ctx)
|
|
|
|
items, cnt, err := query.
|
|
Where(tbl.UserID.Eq(userId)).
|
|
FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
postIds := lo.Map(items, func(item *models.UserPost, _ int) int64 { return item.PostID })
|
|
postInfoMap := lo.KeyBy(items, func(item *models.UserPost) int64 { return item.PostID })
|
|
|
|
postItemMap, err := m.GetPostsMapByIDs(ctx, postIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
type retItem struct {
|
|
Title string `json:"title"`
|
|
Price int64 `json:"price"`
|
|
BoughtAt time.Time `json:"bought_at"`
|
|
}
|
|
|
|
var retItems []retItem
|
|
for _, postID := range postIds {
|
|
post, ok := postItemMap[postID]
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
postInfo := postInfoMap[postID]
|
|
|
|
retItems = append(retItems, retItem{
|
|
Title: post.Title,
|
|
Price: postInfo.Price,
|
|
BoughtAt: postInfo.CreatedAt,
|
|
})
|
|
}
|
|
|
|
return &requests.Pager{
|
|
Items: retItems,
|
|
Total: cnt,
|
|
Pagination: *pagination,
|
|
}, nil
|
|
}
|
|
|
|
// GetPostsMapByIDs
|
|
func (m *posts) GetPostsMapByIDs(ctx context.Context, ids []int64) (map[int64]*models.Post, error) {
|
|
tbl, query := models.PostQuery.QueryContext(ctx)
|
|
posts, err := query.Where(tbl.ID.In(ids...)).Find()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return lo.KeyBy(posts, func(item *models.Post) int64 { return item.ID }), nil
|
|
}
|
|
|
|
// GetMediaByIds
|
|
func (m *posts) GetMediasByIds(ctx context.Context, ids []int64) ([]*models.Media, error) {
|
|
if len(ids) == 0 {
|
|
return nil, nil
|
|
}
|
|
tbl, query := models.MediaQuery.QueryContext(ctx)
|
|
return query.Where(tbl.ID.In(ids...)).Find()
|
|
}
|
|
|
|
// FindByID
|
|
func (m *posts) FindByID(ctx context.Context, id int64, conds ...gen.Condition) (*models.Post, error) {
|
|
tbl, query := models.PostQuery.QueryContext(ctx)
|
|
if len(conds) > 0 {
|
|
query = query.Where(conds...)
|
|
}
|
|
return query.Where(tbl.ID.Eq(id)).First()
|
|
}
|
|
|
|
// Count
|
|
func (m *posts) Count(ctx context.Context, conds ...gen.Condition) (int64, error) {
|
|
_, query := models.PostQuery.QueryContext(ctx)
|
|
if len(conds) > 0 {
|
|
query = query.Where(conds...)
|
|
}
|
|
return query.Count()
|
|
}
|