收口订单事务Writer Port

新增订单应用层纯写入模型、Writer 与 UnitOfWork,订单更新、导入、手动发货和刷新不再向应用接口暴露 sql.Tx 或 db 写入类型。

数据库事务和 db.* 转换仅保留在 Server 基础设施适配器,保留原有提交回滚语义。

验证:make check。
This commit is contained in:
Christ committed 2026-08-15 13:12:14 +08:00
1 parent c2503645e0
commit cec9c13217
4 files changed
+161 -36

No files matched your search

+51 -23
View File
@@ -3,7 +3,6 @@ package server
import (
"context"
"database/sql"
orderapp "xianyu-go/internal/application/orders"
"xianyu-go/internal/db"
)
@@ -22,16 +21,10 @@ type orderRepository interface {
GetItem(ctx context.Context, cookieID, itemID string) (*db.ItemInfo, error)
// SoftDeleteOrder 逻辑删除订单。
SoftDeleteOrder(ctx context.Context, orderID string) (bool, error)
// WithTransaction 在一个事务中执行订单用例的持久化操作。
WithTransaction(ctx context.Context, work func(*sql.Tx) error) error
// PatchOrderTx 在事务内更新订单字段。
PatchOrderTx(ctx context.Context, tx *sql.Tx, orderID string, patch db.OrderPatch) error
// UpsertItemBasicTx 在事务内写入商品基础信息。
UpsertItemBasicTx(ctx context.Context, tx *sql.Tx, item *db.ItemInfoRow) error
// UpsertOrderTx 在事务内写入订单。
UpsertOrderTx(ctx context.Context, tx *sql.Tx, orderID string, opts db.OrderUpsertOpts) error
// WithTransaction 在应用层 Writer 中执行订单持久化操作。
WithTransaction(ctx context.Context, work func(orderapp.Writer) error) error
// UpsertOrder 写入订单。
UpsertOrder(ctx context.Context, orderID string, opts db.OrderUpsertOpts) error
UpsertOrder(ctx context.Context, orderID string, opts orderapp.UpsertOptions) error
// LockCredentials 串行化账号凭证状态变更。
LockCredentials(cookieID string) func()
// LoadCookiePlatformDetail 读取账号平台凭证详情。
@@ -107,7 +100,7 @@ func (r storeOrderRepository) SoftDeleteOrder(ctx context.Context, orderID strin
}
// WithTransaction 创建、提交或回滚订单事务。
func (r storeOrderRepository) WithTransaction(ctx context.Context, work func(*sql.Tx) error) error {
func (r storeOrderRepository) WithTransaction(ctx context.Context, work func(orderapp.Writer) error) error {
// tx 和 err 保存事务创建结果。
tx, err := r.store.DB.BeginTx(ctx, nil)
if err != nil {
@@ -120,8 +113,10 @@ func (r storeOrderRepository) WithTransaction(ctx context.Context, work func(*sq
_ = tx.Rollback()
}
}()
// writer 是隐藏数据库事务细节的订单写入适配器。
writer := storeOrderWriter{store: r.store, tx: tx}
// err 保存事务工作函数的执行错误。
if err := work(tx); err != nil {
if err := work(writer); err != nil {
return err
}
// err 保存事务提交错误。
@@ -132,24 +127,55 @@ func (r storeOrderRepository) WithTransaction(ctx context.Context, work func(*sq
return nil
}
// PatchOrderTx 委托事务内订单更新。
func (r storeOrderRepository) PatchOrderTx(ctx context.Context, tx *sql.Tx, orderID string, patch db.OrderPatch) error {
return r.store.Orders.PatchTx(ctx, tx, orderID, patch)
// storeOrderWriter 将订单应用写入模型适配为数据库事务操作。
type storeOrderWriter struct {
// store 保存数据库 repository 聚合入口。
store *db.Store
// tx 保存当前事务,仅在基础设施适配器内部可见。
tx *sql.Tx
}
// UpsertItemBasicTx 委托事务内商品基础信息写入。
func (r storeOrderRepository) UpsertItemBasicTx(ctx context.Context, tx *sql.Tx, item *db.ItemInfoRow) error {
return r.store.Items.UpsertBasicTx(ctx, tx, item)
// PatchOrder 委托事务内订单更新。
func (w storeOrderWriter) PatchOrder(ctx context.Context, orderID string, patch orderapp.OrderPatch) error {
return w.store.Orders.PatchTx(ctx, w.tx, orderID, db.OrderPatch{
OrderStatus: patch.OrderStatus, ItemID: patch.ItemID, BuyerID: patch.BuyerID,
SpecName: patch.SpecName, SpecValue: patch.SpecValue, Quantity: patch.Quantity,
Amount: patch.Amount, ReceiverName: patch.ReceiverName, ReceiverPhone: patch.ReceiverPhone,
ReceiverAddr: patch.ReceiverAddress, ReceiverCity: patch.ReceiverCity, ChatID: patch.ChatID,
SystemShipped: patch.SystemShipped,
})
}
// UpsertOrderTx 委托事务内订单写入。
func (r storeOrderRepository) UpsertOrderTx(ctx context.Context, tx *sql.Tx, orderID string, opts db.OrderUpsertOpts) error {
return r.store.Orders.UpsertTx(ctx, tx, orderID, opts)
// UpsertItemBasic 委托事务内商品基础信息写入。
func (w storeOrderWriter) UpsertItemBasic(ctx context.Context, item orderapp.ItemWrite) error {
return w.store.Items.UpsertBasicTx(ctx, w.tx, &db.ItemInfoRow{
CookieID: item.CookieID, ItemID: item.ItemID, ItemTitle: item.ItemTitle,
ItemPrice: item.ItemPrice, ItemDetail: item.ItemDetail,
})
}
// UpsertOrder 委托事务内订单写入。
func (w storeOrderWriter) UpsertOrder(ctx context.Context, orderID string, options orderapp.UpsertOptions) error {
return w.store.Orders.UpsertTx(ctx, w.tx, orderID, db.OrderUpsertOpts{
ItemID: options.ItemID, BuyerID: options.BuyerID, CookieID: options.CookieID,
OrderStatus: options.OrderStatus, SpecName: options.SpecName, SpecValue: options.SpecValue,
Quantity: options.Quantity, Amount: options.Amount, ReceiverName: options.ReceiverName,
ReceiverPhone: options.ReceiverPhone, ReceiverAddr: options.ReceiverAddress,
ReceiverCity: options.ReceiverCity, ChatID: options.ChatID,
IsBargain: options.IsBargain, SystemShipped: options.SystemShipped,
})
}
// UpsertOrder 委托订单写入。
func (r storeOrderRepository) UpsertOrder(ctx context.Context, orderID string, opts db.OrderUpsertOpts) error {
return r.store.Orders.Upsert(ctx, orderID, opts)
func (r storeOrderRepository) UpsertOrder(ctx context.Context, orderID string, opts orderapp.UpsertOptions) error {
return r.store.Orders.Upsert(ctx, orderID, db.OrderUpsertOpts{
ItemID: opts.ItemID, BuyerID: opts.BuyerID, CookieID: opts.CookieID,
OrderStatus: opts.OrderStatus, SpecName: opts.SpecName, SpecValue: opts.SpecValue,
Quantity: opts.Quantity, Amount: opts.Amount, ReceiverName: opts.ReceiverName,
ReceiverPhone: opts.ReceiverPhone, ReceiverAddr: opts.ReceiverAddress,
ReceiverCity: opts.ReceiverCity, ChatID: opts.ChatID,
IsBargain: opts.IsBargain, SystemShipped: opts.SystemShipped,
})
}
// LockCredentials 委托账号凭证锁。
@@ -192,3 +218,5 @@ func newStoreOrderRepository(store *db.Store) orderRepository {
// 确保 Store 适配器始终覆盖订单应用服务所需的全部能力。
var _ orderRepository = storeOrderRepository{}
var _ orderapp.UnitOfWork = storeOrderRepository{}
var _ orderapp.Writer = storeOrderWriter{}
+12 -13
View File
@@ -2,7 +2,6 @@ package server
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
@@ -302,20 +301,20 @@ func (a *orderApplicationService) Update(ctx context.Context, userID int64, orde
return newOrderBadRequest("商品标题不能为空且订单必须关联商品")
}
}
return a.repository.WithTransaction(ctx, func(tx *sql.Tx) error {
return a.repository.WithTransaction(ctx, func(writer orderapp.Writer) error {
if // err 保存err,供当前处理流程使用
err := a.repository.PatchOrderTx(ctx, tx, orderID, db.OrderPatch{
err := writer.PatchOrder(ctx, orderID, orderapp.OrderPatch{
OrderStatus: request.OrderStatus, ItemID: request.ItemID, BuyerID: request.BuyerID,
SpecName: request.SpecName, SpecValue: request.SpecValue, Quantity: request.Quantity,
Amount: request.Amount, ReceiverName: request.ReceiverName, ReceiverPhone: request.ReceiverPhone,
ReceiverAddr: request.ReceiverAddress, ReceiverCity: request.ReceiverCity, ChatID: request.ChatID,
ReceiverAddress: request.ReceiverAddress, ReceiverCity: request.ReceiverCity, ChatID: request.ChatID,
SystemShipped: request.SystemShipped,
}); err != nil {
return err
}
if request.ItemTitle != nil {
if // err 保存err,供当前处理流程使用
err := a.repository.UpsertItemBasicTx(ctx, tx, &db.ItemInfoRow{
err := writer.UpsertItemBasic(ctx, orderapp.ItemWrite{
CookieID: order.CookieID, ItemID: finalItemID, ItemTitle: itemTitle,
}); err != nil {
return fmt.Errorf("更新商品标题失败: %w", err)
@@ -397,13 +396,13 @@ func (a *orderApplicationService) importOne(ctx context.Context, ownedCookieIDs
return errors.New("订单金额必须是普通格式的非负有限数字")
}
if // err 保存err,供当前处理流程使用
err := a.repository.WithTransaction(ctx, func(tx *sql.Tx) error {
err := a.repository.WithTransaction(ctx, func(writer orderapp.Writer) error {
if // err 保存err,供当前处理流程使用
err := a.repository.UpsertOrderTx(ctx, tx, orderID, db.OrderUpsertOpts{
err := writer.UpsertOrder(ctx, orderID, orderapp.UpsertOptions{
CookieID: cookieID, ItemID: firstImportString(raw, "item_id"), BuyerID: firstImportString(raw, "buyer_id"),
OrderStatus: status, SpecName: firstImportString(raw, "spec_name"), SpecValue: firstImportString(raw, "spec_value"),
Quantity: firstImportString(raw, "quantity"), Amount: amount, ReceiverName: firstImportString(raw, "receiver_name"),
ReceiverPhone: firstImportString(raw, "receiver_phone"), ReceiverAddr: firstImportString(raw, "receiver_address"),
ReceiverPhone: firstImportString(raw, "receiver_phone"), ReceiverAddress: firstImportString(raw, "receiver_address"),
ReceiverCity: firstImportString(raw, "receiver_city"), ChatID: firstImportString(raw, "chat_id"),
}); err != nil {
return err
@@ -412,7 +411,7 @@ func (a *orderApplicationService) importOne(ctx context.Context, ownedCookieIDs
itemID := firstImportString(raw, "item_id")
if itemID != "" {
if // err 保存err,供当前处理流程使用
err := a.repository.UpsertItemBasicTx(ctx, tx, &db.ItemInfoRow{
err := writer.UpsertItemBasic(ctx, orderapp.ItemWrite{
CookieID: cookieID, ItemID: itemID, ItemTitle: firstImportString(raw, "item_title"),
ItemPrice: firstImportString(raw, "item_price"), ItemDetail: firstImportString(raw, "item_detail", "item_description"),
}); err != nil {
@@ -553,10 +552,10 @@ func (a *orderApplicationService) manualStatusShip(ctx context.Context, userID i
// sysShip 保存sysShip,供当前处理流程使用
sysShip := true
// upsertErr 保存upsertErr,供当前处理流程使用
upsertErr := a.repository.UpsertOrder(ctx, orderID, db.OrderUpsertOpts{
upsertErr := a.repository.UpsertOrder(ctx, orderID, orderapp.UpsertOptions{
CookieID: order.CookieID, OrderStatus: "shipped", SystemShipped: &sysShip, ItemID: order.ItemID,
BuyerID: order.BuyerID, ReceiverName: order.ReceiverName, ReceiverPhone: order.ReceiverPhone,
ReceiverAddr: order.ReceiverAddr, ReceiverCity: order.ReceiverCity, ChatID: order.ChatID,
ReceiverAddress: order.ReceiverAddr, ReceiverCity: order.ReceiverCity, ChatID: order.ChatID,
SpecName: order.SpecName, SpecValue: order.SpecValue, Quantity: order.Quantity, Amount: order.Amount,
})
if upsertErr != nil && a.server.Logger != nil {
@@ -646,7 +645,7 @@ func (a *orderApplicationService) RefreshSingle(ctx context.Context, userID int6
status = db.NormalizeOrderStatus(order.OrderStatus)
}
if // err 保存err,供当前处理流程使用
err := a.repository.UpsertOrder(ctx, orderID, db.OrderUpsertOpts{CookieID: cookieID, OrderStatus: status, SpecName: detail.SpecName, SpecValue: detail.SpecValue, Quantity: detail.Quantity, Amount: detail.Amount}); err != nil {
err := a.repository.UpsertOrder(ctx, orderID, orderapp.UpsertOptions{CookieID: cookieID, OrderStatus: status, SpecName: detail.SpecName, SpecValue: detail.SpecValue, Quantity: detail.Quantity, Amount: detail.Amount}); err != nil {
return orderSingleRefreshResponse{}, err
}
return orderSingleRefreshResponse{Success: true, Message: "订单刷新完成", Order: orderRefreshDetailResponse{Quantity: detail.Quantity, SpecName: detail.SpecName, SpecValue: detail.SpecValue, OrderStatus: db.NormalizeOrderStatus(detail.OrderStatus), Amount: detail.Amount}}, nil
@@ -867,7 +866,7 @@ func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, coo
newStatus = target.CurrentStatus
}
// err 保存err,供当前处理流程使用
err := a.repository.UpsertOrder(ctx, target.OrderID, db.OrderUpsertOpts{
err := a.repository.UpsertOrder(ctx, target.OrderID, orderapp.UpsertOptions{
CookieID: cid,
OrderStatus: newStatus,
SpecName: detail.SpecName,