feat: update toolchain
This commit is contained in:
@@ -2,45 +2,28 @@ package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/table"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
var ordersUpdateExcludeColumns = []Column{
|
||||
table.Orders.OrderNo,
|
||||
table.Orders.Price,
|
||||
table.Orders.Discount,
|
||||
table.Orders.SubOrderNo,
|
||||
table.Orders.PostID,
|
||||
table.Orders.UserID,
|
||||
}
|
||||
|
||||
func (m *Orders) Update(ctx context.Context) error {
|
||||
m.UpdatedAt = time.Now()
|
||||
|
||||
stmt := table.Orders.UPDATE(table.Orders.MutableColumns.Except(table.Orders.OrderNo, table.Orders.Price, table.Orders.Discount, table.Orders.SubOrderNo, table.Orders.PostID, table.Orders.UserID)).SET(m).WHERE(table.Orders.ID.EQ(Int(m.ID))).RETURNING(table.Orders.AllColumns)
|
||||
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
m.log().WithField("func", "Update").Errorf("error updating Orders item: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
m.log().WithField("func", "Update").Infof("Orders item updated successfully")
|
||||
return nil
|
||||
}
|
||||
var tblOrdersUpdateMutableColumns = tblOrders.MutableColumns.Except(
|
||||
tblOrders.OrderNo,
|
||||
tblOrders.Price,
|
||||
tblOrders.Discount,
|
||||
tblOrders.SubOrderNo,
|
||||
tblOrders.PostID,
|
||||
tblOrders.UserID,
|
||||
)
|
||||
|
||||
// BuildConditionWithKey builds the WHERE clause for order queries
|
||||
func (m *Orders) BuildConditionWithKey(orderNumber *string, userID *int64) BoolExpression {
|
||||
tbl := table.Orders
|
||||
tbl := tblOrders
|
||||
|
||||
cond := Bool(true)
|
||||
|
||||
@@ -65,7 +48,7 @@ func (m *Orders) countByCondition(ctx context.Context, expr BoolExpression) (int
|
||||
Cnt int64
|
||||
}
|
||||
|
||||
tbl := table.Orders
|
||||
tbl := tblOrders
|
||||
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(expr)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
@@ -79,10 +62,14 @@ func (m *Orders) countByCondition(ctx context.Context, expr BoolExpression) (int
|
||||
}
|
||||
|
||||
// List returns a paginated list of orders
|
||||
func (m *Orders) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression) (*requests.Pager, error) {
|
||||
func (m *Orders) List(
|
||||
ctx context.Context,
|
||||
pagination *requests.Pagination,
|
||||
cond BoolExpression,
|
||||
) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
tbl := table.Orders
|
||||
tbl := tblOrders
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(cond).
|
||||
@@ -158,7 +145,7 @@ func (o *Orders) CreateFromUserPostID(ctx context.Context, userId, postId int64)
|
||||
m.CreatedAt = time.Now()
|
||||
m.UpdatedAt = time.Now()
|
||||
m.Status = fields.OrderStatusPending
|
||||
m.OrderNo = fmt.Sprintf("%s", time.Now().Format("20060102150405"))
|
||||
m.OrderNo = time.Now().Format("20060102150405")
|
||||
m.SubOrderNo = m.OrderNo
|
||||
m.UserID = userId
|
||||
m.PostID = postId
|
||||
@@ -166,7 +153,7 @@ func (o *Orders) CreateFromUserPostID(ctx context.Context, userId, postId int64)
|
||||
m.Price = post.Price
|
||||
m.Discount = post.Discount
|
||||
|
||||
tbl := table.Orders
|
||||
tbl := tblOrders
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(m).RETURNING(tbl.AllColumns)
|
||||
o.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
@@ -178,7 +165,7 @@ func (o *Orders) CreateFromUserPostID(ctx context.Context, userId, postId int64)
|
||||
}
|
||||
|
||||
func (m *Orders) SetMeta(ctx context.Context, metaFunc func(fields.OrderMeta) fields.OrderMeta) error {
|
||||
tbl := table.Orders
|
||||
tbl := tblOrders
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.Meta).
|
||||
SET(fields.ToJson(metaFunc(m.Meta.Data))).
|
||||
@@ -196,7 +183,7 @@ func (m *Orders) SetMeta(ctx context.Context, metaFunc func(fields.OrderMeta) fi
|
||||
}
|
||||
|
||||
func (m *Orders) SetStatus(ctx context.Context, status fields.OrderStatus) error {
|
||||
tbl := table.Orders
|
||||
tbl := tblOrders
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.Status).
|
||||
SET(status).
|
||||
@@ -215,7 +202,7 @@ func (m *Orders) SetStatus(ctx context.Context, status fields.OrderStatus) error
|
||||
|
||||
// SumAmount
|
||||
func (m *Orders) SumAmount(ctx context.Context) (int64, error) {
|
||||
tbl := table.Orders
|
||||
tbl := tblOrders
|
||||
stmt := SELECT(SUM(tbl.Price).AS("cnt")).FROM(tbl).WHERE(
|
||||
tbl.Status.EQ(Int(int64(fields.OrderStatusCompleted))),
|
||||
)
|
||||
@@ -236,7 +223,7 @@ func (m *Orders) SumAmount(ctx context.Context) (int64, error) {
|
||||
|
||||
// GetByOrderNo
|
||||
func (m *Orders) GetByOrderNo(ctx context.Context, orderNo string) (*Orders, error) {
|
||||
tbl := table.Orders
|
||||
tbl := tblOrders
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
@@ -256,7 +243,7 @@ func (m *Orders) GetByOrderNo(ctx context.Context, orderNo string) (*Orders, err
|
||||
|
||||
// SetTransactionID
|
||||
func (m *Orders) SetTransactionID(ctx context.Context, transactionID string) error {
|
||||
tbl := table.Orders
|
||||
tbl := tblOrders
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.TransactionID).
|
||||
SET(String(transactionID)).
|
||||
|
||||
Reference in New Issue
Block a user