This commit is contained in:
@@ -86,3 +86,22 @@ func (m *medias) GetRelations(ctx context.Context, hash string) ([]*models.Media
|
||||
tbl, query := models.MediaQuery.QueryContext(ctx)
|
||||
return query.Where(tbl.Metas.KeyEq("parent_hash", hash)).Find()
|
||||
}
|
||||
|
||||
// FindByID
|
||||
func (m *medias) FindByID(ctx context.Context, id int64) (*models.Media, error) {
|
||||
tbl, query := models.MediaQuery.QueryContext(ctx)
|
||||
item, err := query.Where(tbl.ID.Eq(id)).First()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to find media by id: %d", id)
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
// Count
|
||||
func (m *medias) Count(ctx context.Context, conds ...gen.Condition) (int64, error) {
|
||||
_, query := models.MediaQuery.QueryContext(ctx)
|
||||
if len(conds) > 0 {
|
||||
query = query.Where(conds...)
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/types"
|
||||
)
|
||||
|
||||
// @provider
|
||||
@@ -19,21 +21,12 @@ type orders struct{}
|
||||
func (m *orders) List(
|
||||
ctx context.Context,
|
||||
pagination *requests.Pagination,
|
||||
orderNumber *string,
|
||||
userID *int64,
|
||||
conds ...gen.Condition,
|
||||
) (*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()).
|
||||
@@ -124,10 +117,79 @@ func (m *orders) Refund(ctx context.Context, id int64) error {
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
func (o *orders) CreateFromUserPostID(ctx context.Context, userId, postId int64) (*models.Order, error) {
|
||||
post, err := Posts.FindByID(ctx, postId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get post")
|
||||
}
|
||||
|
||||
m := &models.Order{}
|
||||
m.Status = fields.OrderStatusPending
|
||||
m.OrderNo = time.Now().Format("20060102150405")
|
||||
m.SubOrderNo = m.OrderNo
|
||||
m.UserID = userId
|
||||
m.PostID = postId
|
||||
m.Meta = types.NewJSONType(fields.OrderMeta{})
|
||||
m.Price = post.Price
|
||||
m.Discount = post.Discount
|
||||
|
||||
if err := m.Create(ctx); err != nil {
|
||||
return m, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// FindByID
|
||||
func (m *orders) FindByID(ctx context.Context, orderID int64) (*models.Order, error) {
|
||||
return models.OrderQuery.WithContext(ctx).Where(models.OrderQuery.ID.Eq(orderID)).First()
|
||||
}
|
||||
|
||||
func (m *orders) SetMeta(ctx context.Context, orderID int64, metaFunc func(fields.OrderMeta) fields.OrderMeta) error {
|
||||
order, err := m.FindByID(ctx, orderID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
order.Meta = types.NewJSONType(metaFunc(order.Meta.Data()))
|
||||
_, err = order.Update(ctx)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// SetStatus
|
||||
func (m *orders) SetStatus(ctx context.Context, orderID int64, status fields.OrderStatus) error {
|
||||
tbl, query := models.OrderQuery.QueryContext(ctx)
|
||||
|
||||
_, err := query.Where(tbl.ID.Eq(orderID)).Update(tbl.Status, status)
|
||||
return err
|
||||
}
|
||||
|
||||
// SumAmount
|
||||
func (m *orders) SumAmount(ctx context.Context) (int64, error) {
|
||||
tbl, query := models.OrderQuery.QueryContext(ctx)
|
||||
var calc struct {
|
||||
Amount int64 `json:"amount"`
|
||||
}
|
||||
|
||||
err := query.Select(tbl.Price.Sum().As("amount")).Scan(&calc)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "failed to sum amount")
|
||||
}
|
||||
|
||||
return calc.Amount, nil
|
||||
}
|
||||
|
||||
// Count
|
||||
func (m *orders) Count(ctx context.Context, conds ...gen.Condition) (int64, error) {
|
||||
_, query := models.OrderQuery.QueryContext(ctx)
|
||||
if len(conds) > 0 {
|
||||
query = query.Where(conds...)
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
@@ -141,10 +142,28 @@ func (m *posts) GetPostsMapByIDs(ctx context.Context, ids []int64) (map[int64]*m
|
||||
}
|
||||
|
||||
// GetMediaByIds
|
||||
func (m *posts) GetMediaByIds(ctx context.Context, ids []int64) ([]*models.Media, error) {
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/atom/opt"
|
||||
"gorm.io/gorm"
|
||||
"quyun/v2/providers/wepay"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
@@ -17,10 +16,15 @@ func Provide(opts ...opt.Option) error {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(wepayClient *wepay.Client) (*orders, error) {
|
||||
obj := &orders{
|
||||
wepay: wepayClient,
|
||||
}
|
||||
if err := container.Container.Provide(func() (*orders, error) {
|
||||
obj := &orders{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*posts, error) {
|
||||
obj := &posts{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
@@ -30,11 +34,15 @@ func Provide(opts ...opt.Option) error {
|
||||
db *gorm.DB,
|
||||
medias *medias,
|
||||
orders *orders,
|
||||
posts *posts,
|
||||
users *users,
|
||||
) (contracts.Initial, error) {
|
||||
obj := &services{
|
||||
db: db,
|
||||
medias: medias,
|
||||
orders: orders,
|
||||
posts: posts,
|
||||
users: users,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
@@ -44,5 +52,12 @@ func Provide(opts ...opt.Option) error {
|
||||
}, atom.GroupInitial); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*users, error) {
|
||||
obj := &users{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ var _db *gorm.DB
|
||||
var (
|
||||
Medias *medias
|
||||
Orders *orders
|
||||
Posts *posts
|
||||
Users *users
|
||||
)
|
||||
|
||||
// @provider(model)
|
||||
@@ -18,6 +20,8 @@ type services struct {
|
||||
// define Services
|
||||
medias *medias
|
||||
orders *orders
|
||||
posts *posts
|
||||
users *users
|
||||
}
|
||||
|
||||
func (svc *services) Prepare() error {
|
||||
@@ -26,6 +30,8 @@ func (svc *services) Prepare() error {
|
||||
// set exported Services here
|
||||
Medias = svc.medias
|
||||
Orders = svc.orders
|
||||
Posts = svc.posts
|
||||
Users = svc.users
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
226
backend_v1/app/services/users.go
Normal file
226
backend_v1/app/services/users.go
Normal file
@@ -0,0 +1,226 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type users struct{}
|
||||
|
||||
// List returns a paginated list of users
|
||||
func (m *users) List(
|
||||
ctx context.Context,
|
||||
pagination *requests.Pagination,
|
||||
conds ...gen.Condition,
|
||||
) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
_, query := models.UserQuery.QueryContext(ctx)
|
||||
|
||||
items, cnt, err := query.Where(conds...).FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "query users error")
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Items: items,
|
||||
Total: cnt,
|
||||
Pagination: *pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// PostList returns a paginated list of posts for a user
|
||||
func (m *users) PostList(
|
||||
ctx context.Context,
|
||||
userId int64,
|
||||
pagination *requests.Pagination,
|
||||
conds ...gen.Condition,
|
||||
) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
// stmt := SELECT(tbl.AllColumns).
|
||||
// FROM(tbl.
|
||||
// RIGHT_JOIN(
|
||||
// tblUserPosts,
|
||||
// tblUserPosts.PostID.EQ(tbl.ID),
|
||||
// ),
|
||||
// ).
|
||||
// WHERE(CondTrue(cond...)).
|
||||
// ORDER_BY(tblUserPosts.ID.DESC()).
|
||||
// LIMIT(pagination.Limit).
|
||||
// OFFSET(pagination.Offset)
|
||||
// m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
// var posts []Posts
|
||||
// err := stmt.QueryContext(ctx, db, &posts)
|
||||
// if err != nil {
|
||||
// if errors.Is(err, qrm.ErrNoRows) {
|
||||
// return &requests.Pager{
|
||||
// Items: nil,
|
||||
// Total: 0,
|
||||
// Pagination: *pagination,
|
||||
// }, nil
|
||||
// }
|
||||
// m.log().Errorf("error querying posts: %v", err)
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// // total count
|
||||
// var cnt struct {
|
||||
// Cnt int64
|
||||
// }
|
||||
|
||||
// stmtCnt := tblUserPosts.SELECT(COUNT(tblUserPosts.ID).AS("cnt")).WHERE(tblUserPosts.UserID.EQ(Int64(userId)))
|
||||
// m.log().Infof("sql: %s", stmtCnt.DebugSql())
|
||||
|
||||
// if err := stmtCnt.QueryContext(ctx, db, &cnt); err != nil {
|
||||
// m.log().Errorf("error counting users: %v", err)
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// return &requests.Pager{
|
||||
// Items: posts,
|
||||
// Total: cnt.Cnt,
|
||||
// Pagination: *pagination,
|
||||
// }, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// GetUsersMapByIDs
|
||||
func (m *users) GetUsersMapByIDs(ctx context.Context, ids []int64) (map[int64]*models.User, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
items, err := query.Where(tbl.ID.In(ids...)).Find()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to get users by ids:%v", ids)
|
||||
}
|
||||
|
||||
return lo.KeyBy(items, func(item *models.User) int64 {
|
||||
return item.ID
|
||||
}), nil
|
||||
}
|
||||
|
||||
// BatchCheckHasBought checks if the user has bought the given post IDs
|
||||
func (m *users) BatchCheckHasBought(ctx context.Context, userId int64, postIDs []int64) (map[int64]bool, error) {
|
||||
tbl, query := models.UserPostQuery.QueryContext(ctx)
|
||||
userPosts, err := query.
|
||||
Where(
|
||||
tbl.UserID.Eq(userId),
|
||||
tbl.PostID.In(postIDs...),
|
||||
).
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "check user has bought failed, user_id: %d, post_ids: %+v", userId, postIDs)
|
||||
}
|
||||
|
||||
result := make(map[int64]bool)
|
||||
for _, postID := range postIDs {
|
||||
result[postID] = false
|
||||
}
|
||||
|
||||
for _, post := range userPosts {
|
||||
result[post.PostID] = true
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// HasBought
|
||||
func (m *users) HasBought(ctx context.Context, userID, postID int64) (bool, error) {
|
||||
tbl, query := models.UserPostQuery.QueryContext(ctx)
|
||||
cnt, err := query.
|
||||
Where(
|
||||
tbl.UserID.Eq(userID),
|
||||
tbl.PostID.Eq(postID),
|
||||
).
|
||||
Count()
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "failed to check user bought")
|
||||
}
|
||||
return cnt > 0, nil
|
||||
}
|
||||
|
||||
// SetUsername
|
||||
func (m *users) SetUsername(ctx context.Context, userID int64, username string) error {
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
_, err := query.
|
||||
Where(
|
||||
tbl.ID.Eq(userID),
|
||||
).
|
||||
Update(tbl.Username, username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BuyPosts
|
||||
func (m *users) BuyPosts(ctx context.Context, userID, postID, price int64) error {
|
||||
model := &models.UserPost{UserID: userID, PostID: postID, Price: price}
|
||||
return model.Create(ctx)
|
||||
}
|
||||
|
||||
// RevokePosts
|
||||
func (m *users) RevokeUserPosts(ctx context.Context, userID, postID int64) error {
|
||||
tbl, query := models.UserPostQuery.QueryContext(ctx)
|
||||
_, err := query.Where(
|
||||
tbl.UserID.Eq(userID),
|
||||
tbl.PostID.Eq(postID),
|
||||
).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindByID
|
||||
func (m *users) FindByID(ctx context.Context, userID int64) (*models.User, error) {
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
user, err := query.Where(tbl.ID.Eq(userID)).First()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "find by id failed, id: %d", userID)
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// SetBalance
|
||||
func (m *users) SetBalance(ctx context.Context, userID, balance int64) error {
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
_, err := query.Where(tbl.ID.Eq(userID)).Update(tbl.Balance, balance)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// AddBalance adds the given amount to the user's balance
|
||||
func (m *users) AddBalance(ctx context.Context, userID, amount int64) error {
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
_, err := query.Where(tbl.ID.Eq(userID)).Inc(tbl.Balance, amount)
|
||||
return err
|
||||
}
|
||||
|
||||
// Desc desc the given amount to the user's balance
|
||||
func (m *users) DescBalance(ctx context.Context, userID, amount int64) error {
|
||||
user, err := m.FindByID(ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if user.Balance < amount {
|
||||
return errors.New("balance not enough")
|
||||
}
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
_, err = query.Where(tbl.ID.Eq(userID)).Inc(tbl.Balance, -amount)
|
||||
return err
|
||||
}
|
||||
|
||||
// Count
|
||||
func (m *users) Count(ctx context.Context, conds ...gen.Condition) (int64, error) {
|
||||
_, query := models.UserQuery.QueryContext(ctx)
|
||||
if len(conds) > 0 {
|
||||
query = query.Where(conds...)
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
Reference in New Issue
Block a user