This commit is contained in:
88
backend_v1/app/services/medias.go
Normal file
88
backend_v1/app/services/medias.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/fields"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type medias struct{}
|
||||
|
||||
func (m *medias) List(
|
||||
ctx context.Context,
|
||||
pagination *requests.Pagination,
|
||||
conds ...gen.Condition,
|
||||
) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
tbl, query := models.MediaQuery.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, "failed to list media items")
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Items: items,
|
||||
Total: cnt,
|
||||
Pagination: *pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetByIds
|
||||
func (m *medias) GetByIds(ctx context.Context, ids []int64) ([]*models.Media, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*models.Media{}, nil
|
||||
}
|
||||
|
||||
tbl, query := models.MediaQuery.QueryContext(ctx)
|
||||
|
||||
items, err := query.
|
||||
Where(tbl.ID.In(ids...)).
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get media items by ids")
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
// GetByHash
|
||||
func (m *medias) GetByHash(ctx context.Context, hash string) (*models.Media, error) {
|
||||
tbl, query := models.MediaQuery.QueryContext(ctx)
|
||||
item, err := query.
|
||||
Where(tbl.Hash.Eq(hash)).
|
||||
First()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get media item by hash")
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
// UpdateMetas
|
||||
func (m *medias) UpdateMetas(ctx context.Context, id int64, metas fields.MediaMetas) error {
|
||||
tbl, query := models.MediaQuery.QueryContext(ctx)
|
||||
_, err := query.
|
||||
Where(tbl.ID.Eq(id)).
|
||||
Update(tbl.Metas, metas)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to update media metas for id: %d", id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRelationMedias
|
||||
|
||||
func (m *medias) GetRelations(ctx context.Context, hash string) ([]*models.Media, error) {
|
||||
tbl, query := models.MediaQuery.QueryContext(ctx)
|
||||
return query.Where(tbl.Metas.KeyEq("parent_hash", hash)).Find()
|
||||
}
|
||||
133
backend_v1/app/services/orders.go
Normal file
133
backend_v1/app/services/orders.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/fields"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type orders struct{}
|
||||
|
||||
// List 订单列表(支持按订单号模糊查询、按用户过滤)。
|
||||
func (m *orders) List(
|
||||
ctx context.Context,
|
||||
pagination *requests.Pagination,
|
||||
orderNumber *string,
|
||||
userID *int64,
|
||||
) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
tbl, query := models.OrderQuery.QueryContext(ctx)
|
||||
|
||||
conds := make([]gen.Condition, 0, 2)
|
||||
if orderNumber != nil && *orderNumber != "" {
|
||||
conds = append(conds, tbl.OrderNo.Like("%"+*orderNumber+"%"))
|
||||
}
|
||||
if userID != nil {
|
||||
conds = append(conds, tbl.UserID.Eq(*userID))
|
||||
}
|
||||
|
||||
orders, cnt, err := query.
|
||||
Where(conds...).
|
||||
Order(tbl.ID.Desc()).
|
||||
FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to list orders")
|
||||
}
|
||||
|
||||
// 这里刻意使用“先查订单,再批量查关联”的方式,避免在分页时 JOIN 造成重复行/分页不稳定。
|
||||
postIDs := lo.Uniq(lo.Map(orders, func(o *models.Order, _ int) int64 { return o.PostID }))
|
||||
userIDs := lo.Uniq(lo.Map(orders, func(o *models.Order, _ int) int64 { return o.UserID }))
|
||||
|
||||
posts, err := models.PostQuery.WithContext(ctx).GetByIDs(postIDs...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get posts by ids")
|
||||
}
|
||||
users, err := models.UserQuery.WithContext(ctx).GetByIDs(userIDs...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get users by ids")
|
||||
}
|
||||
|
||||
postMap := lo.SliceToMap(posts, func(p *models.Post) (int64, *models.Post) { return p.ID, p })
|
||||
userMap := lo.SliceToMap(users, func(u *models.User) (int64, *models.User) { return u.ID, u })
|
||||
|
||||
// OrderListItem 用于订单列表展示,补充作品标题与用户名等冗余信息,避免前端二次查询。
|
||||
type orderListItem struct {
|
||||
*models.Order
|
||||
PostTitle string `json:"post_title"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
items := lo.Map(orders, func(o *models.Order, _ int) *orderListItem {
|
||||
item := &orderListItem{Order: o}
|
||||
if post, ok := postMap[o.PostID]; ok {
|
||||
item.PostTitle = post.Title
|
||||
}
|
||||
if user, ok := userMap[o.UserID]; ok {
|
||||
item.Username = user.Username
|
||||
}
|
||||
return item
|
||||
})
|
||||
|
||||
return &requests.Pager{
|
||||
Items: items,
|
||||
Total: cnt,
|
||||
Pagination: *pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Refund 订单退款(余额支付走本地退款;微信支付走微信退款并标记为退款处理中)。
|
||||
func (m *orders) Refund(ctx context.Context, id int64) error {
|
||||
// 余额支付:这里强调“状态一致性”,必须在一个事务中完成:余额退回 + 撤销购买权益 + 更新订单状态。
|
||||
return models.Q.Transaction(func(tx *models.Query) error {
|
||||
order, err := tx.Order.WithContext(ctx).GetByID(id)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get order in tx")
|
||||
}
|
||||
|
||||
// 退回余额(使用原子自增,避免并发覆盖)。
|
||||
costBalance := order.Meta.Data().CostBalance
|
||||
if costBalance > 0 {
|
||||
if _, err := tx.User.
|
||||
WithContext(ctx).
|
||||
Where(tx.User.ID.Eq(order.UserID)).
|
||||
Inc(tx.User.Balance, costBalance); err != nil {
|
||||
return errors.Wrap(err, "failed to refund balance")
|
||||
}
|
||||
}
|
||||
|
||||
// 撤销已购买的作品权限(删除 user_posts 记录)。
|
||||
if _, err := tx.UserPost.
|
||||
WithContext(ctx).
|
||||
Where(
|
||||
tx.UserPost.UserID.Eq(order.UserID),
|
||||
tx.UserPost.PostID.Eq(order.PostID),
|
||||
).
|
||||
Delete(); err != nil {
|
||||
return errors.Wrap(err, "failed to revoke user post")
|
||||
}
|
||||
|
||||
// 更新订单状态为“退款成功”。
|
||||
if _, err := tx.Order.
|
||||
WithContext(ctx).
|
||||
Where(tx.Order.ID.Eq(order.ID)).
|
||||
Update(tx.Order.Status, fields.OrderStatusRefundSuccess); err != nil {
|
||||
return errors.Wrap(err, "failed to update order status")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// GetByOrderNO
|
||||
func (m *orders) GetByOrderNO(ctx context.Context, orderNo string) (*models.Order, error) {
|
||||
return models.OrderQuery.WithContext(ctx).Where(models.OrderQuery.OrderNo.Eq(orderNo)).First()
|
||||
}
|
||||
150
backend_v1/app/services/posts.go
Normal file
150
backend_v1/app/services/posts.go
Normal file
@@ -0,0 +1,150 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
"time"
|
||||
|
||||
"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) GetMediaByIds(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()
|
||||
}
|
||||
@@ -6,16 +6,35 @@ import (
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/atom/opt"
|
||||
"gorm.io/gorm"
|
||||
"quyun/v2/providers/wepay"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func() (*medias, error) {
|
||||
obj := &medias{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(wepayClient *wepay.Client) (*orders, error) {
|
||||
obj := &orders{
|
||||
wepay: wepayClient,
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
db *gorm.DB,
|
||||
test *test,
|
||||
medias *medias,
|
||||
orders *orders,
|
||||
) (contracts.Initial, error) {
|
||||
obj := &services{
|
||||
db: db,
|
||||
test: test,
|
||||
db: db,
|
||||
medias: medias,
|
||||
orders: orders,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
@@ -25,12 +44,5 @@ func Provide(opts ...opt.Option) error {
|
||||
}, atom.GroupInitial); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*test, error) {
|
||||
obj := &test{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -8,21 +8,24 @@ var _db *gorm.DB
|
||||
|
||||
// exported CamelCase Services
|
||||
var (
|
||||
Test *test
|
||||
Medias *medias
|
||||
Orders *orders
|
||||
)
|
||||
|
||||
// @provider(model)
|
||||
type services struct {
|
||||
db *gorm.DB
|
||||
// define Services
|
||||
test *test
|
||||
medias *medias
|
||||
orders *orders
|
||||
}
|
||||
|
||||
func (svc *services) Prepare() error {
|
||||
_db = svc.db
|
||||
|
||||
// set exported Services here
|
||||
Test = svc.test
|
||||
Medias = svc.medias
|
||||
Orders = svc.orders
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
package services
|
||||
|
||||
import "context"
|
||||
|
||||
// @provider
|
||||
type test struct{}
|
||||
|
||||
func (t *test) Test(ctx context.Context) (string, error) {
|
||||
return "Test", nil
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/commands/testx"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type TestSuiteInjectParams struct {
|
||||
dig.In
|
||||
|
||||
Initials []contracts.Initial `group:"initials"` // nolint:structcheck
|
||||
}
|
||||
|
||||
type TestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
TestSuiteInjectParams
|
||||
}
|
||||
|
||||
func Test_Test(t *testing.T) {
|
||||
providers := testx.Default().With(Provide)
|
||||
|
||||
testx.Serve(providers, t, func(p TestSuiteInjectParams) {
|
||||
suite.Run(t, &TestSuite{TestSuiteInjectParams: p})
|
||||
})
|
||||
}
|
||||
|
||||
func (t *TestSuite) Test_Test() {
|
||||
Convey("test_work", t.T(), func() {
|
||||
t.T().Log("start test at", time.Now())
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user