This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user