收口订单事务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

@@ -718,3 +718,4 @@ npm --prefix frontend run build
| 2026-08-15 | 审计重开与完成条件纠偏 | 已完成 | 已将阶段 2、4、5、8、9、10 从“已完成”改为“进行中”,建立九个可回滚 PR 切片,并明确“测试通过不等于架构完成”的新验收规则 |
| 2026-08-15 | P0 敏感系统设置与运维输出脱敏 | 已完成 | 新增 `SystemSettings.Redacted`;管理端不再返回 `ai_api_key`、SMTP 密码和验证码密钥明文;敏感设置缺省保留、显式空值清除;dbverify 改为长度+指纹;Go/React 回归、类型检查、前端构建和 `make check` 通过。阶段 2 仍保持“进行中”,因为凭证锁审计、访问审计和三库回归尚未全部完成 |
| 2026-08-15 | P1 订单读模型 Port 与架构依赖门禁 | 已完成 | 新增 `internal/application/orders` 纯业务 `OrderRow`/`ListFilter`/`Reader`;订单列表适配器负责 `db.*` 到应用模型的转换;架构门禁统一模块路径、禁止应用层依赖 `db/server/xianyu/browser/sql/http`,并对 Server 新增低层依赖要求临时白名单;新增门禁测试,`make check` 通过。订单写入事务 Port、其他应用服务和既有白名单仍未完成,阶段 4/8/9 继续保持“进行中” |
| 2026-08-15 | P1 订单事务 Writer Port | 已完成 | 新增 `orders.OrderPatch`、`ItemWrite`、`UpsertOptions`、`Writer` 和 `UnitOfWork`;订单更新、导入、手动发货和刷新统一通过应用 Writer,`*sql.Tx` 仅存在于 Server 基础设施适配器;保留现有提交/回滚语义并通过订单回归与 `make check`。订单实体读取、凭证 Port、其他应用服务和 Server 白名单仍未完成,阶段 4/8/9 继续保持“进行中” |
@@ -0,0 +1,97 @@
package orders
import "context"
// OrderPatch 是订单更新用例允许写入的可选字段集合。
type OrderPatch struct {
// OrderStatus 是订单状态补丁。
OrderStatus *string
// ItemID 是商品标识补丁。
ItemID *string
// BuyerID 是买家标识补丁。
BuyerID *string
// SpecName 是规格名称补丁。
SpecName *string
// SpecValue 是规格值补丁。
SpecValue *string
// Quantity 是购买数量补丁。
Quantity *string
// Amount 是订单金额补丁。
Amount *string
// ReceiverName 是收货人补丁。
ReceiverName *string
// ReceiverPhone 是收货电话补丁。
ReceiverPhone *string
// ReceiverAddress 是收货地址补丁。
ReceiverAddress *string
// ReceiverCity 是收货城市补丁。
ReceiverCity *string
// ChatID 是聊天会话补丁。
ChatID *string
// SystemShipped 表示是否由系统完成发货。
SystemShipped *bool
}
// ItemWrite 是订单事务需要写入的商品基础信息。
type ItemWrite struct {
// CookieID 是商品所属账号标识。
CookieID string
// ItemID 是商品标识。
ItemID string
// ItemTitle 是商品标题。
ItemTitle string
// ItemPrice 是商品价格文本。
ItemPrice string
// ItemDetail 是商品详情 JSON。
ItemDetail string
}
// UpsertOptions 是订单导入和同步使用的订单写入字段。
type UpsertOptions struct {
// ItemID 是关联商品标识。
ItemID string
// BuyerID 是买家标识。
BuyerID string
// CookieID 是订单所属账号标识。
CookieID string
// OrderStatus 是订单状态。
OrderStatus string
// SpecName 是规格名称。
SpecName string
// SpecValue 是规格值。
SpecValue string
// Quantity 是购买数量。
Quantity string
// Amount 是订单金额文本。
Amount string
// ReceiverName 是收货人姓名。
ReceiverName string
// ReceiverPhone 是收货人电话。
ReceiverPhone string
// ReceiverAddress 是收货地址。
ReceiverAddress string
// ReceiverCity 是收货城市。
ReceiverCity string
// ChatID 是聊天会话标识。
ChatID string
// IsBargain 表示是否为砍价订单。
IsBargain *bool
// SystemShipped 表示是否由系统完成发货。
SystemShipped *bool
}
// Writer 定义订单事务内的持久化写入能力,不暴露数据库事务类型。
type Writer interface {
// PatchOrder 更新订单可选字段。
PatchOrder(ctx context.Context, orderID string, patch OrderPatch) error
// UpsertItemBasic 写入商品基础信息。
UpsertItemBasic(ctx context.Context, item ItemWrite) error
// UpsertOrder 写入订单及其状态字段。
UpsertOrder(ctx context.Context, orderID string, options UpsertOptions) error
}
// UnitOfWork 定义订单用例的事务边界,由基础设施适配器实现。
type UnitOfWork interface {
// WithTransaction 在单个事务中执行订单写入,并负责提交或回滚。
WithTransaction(ctx context.Context, work func(Writer) error) error
}
+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,