diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index fd4b93a..f9044de 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -719,3 +719,4 @@ npm --prefix frontend run build | 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 继续保持“进行中” | +| 2026-08-15 | P1 订单实体读取与平台运行视图 Port | 已完成 | 新增纯应用层 `Order`、`ItemInfo`、`PlatformRuntimeData`;订单详情、商品读取、订单分页和刷新凭证读取不再通过 repository 暴露 `db.Order`、`db.ItemInfo`、`db.OrderRow` 或 `db.CookieDetail`;Server 到现有自动化/会话辅助函数保留显式边界适配;新增字段完整性转换测试,`make check` 和注释门禁通过。该三切片仅收口订单 Port,凭证协调器、生命周期、补偿、批量同步和其他领域依赖仍未完成,阶段 4/8/9 继续保持“进行中” | diff --git a/internal/application/orders/read_model.go b/internal/application/orders/read_model.go index 0f1cc7b..d43cf60 100644 --- a/internal/application/orders/read_model.go +++ b/internal/application/orders/read_model.go @@ -46,6 +46,98 @@ type OrderRow struct { UpdatedAt string } +// Order 是订单详情和发货用例使用的纯业务实体。 +type Order struct { + // OrderID 是订单稳定标识。 + OrderID string + // ItemID 是关联商品标识。 + ItemID string + // BuyerID 是买家标识。 + BuyerID string + // SpecName 是规格名称。 + SpecName string + // SpecValue 是规格值。 + SpecValue string + // Quantity 是购买数量。 + Quantity string + // Amount 是订单金额文本。 + Amount string + // OrderStatus 是持久化的订单状态。 + OrderStatus string + // CookieID 是订单所属账号标识。 + CookieID string + // IsBargain 表示订单是否为砍价订单。 + IsBargain int + // ReceiverName 是收货人姓名。 + ReceiverName string + // ReceiverPhone 是收货人电话。 + ReceiverPhone string + // ReceiverAddress 是收货地址。 + ReceiverAddress string + // ReceiverCity 是收货城市。 + ReceiverCity string + // Version 是订单乐观锁版本。 + Version int + // ChatID 是聊天会话标识。 + ChatID string + // SystemShipped 表示是否由系统确认发货。 + SystemShipped bool + // PaidAt 是订单付款时间文本。 + PaidAt string + // ShippedAt 是订单发货时间文本。 + ShippedAt string + // CompletedAt 是订单完成时间文本。 + CompletedAt string + // BuyerReviewedAt 是买家评价时间文本。 + BuyerReviewedAt string + // LastReviewRequestAt 是最近一次索评时间文本。 + LastReviewRequestAt string + // ReviewRequestCount 是索评次数。 + ReviewRequestCount int + // CreatedAt 是订单创建时间。 + CreatedAt string + // UpdatedAt 是订单更新时间。 + UpdatedAt string +} + +// ItemInfo 是订单详情需要的纯商品信息。 +type ItemInfo struct { + // ID 是商品信息记录的本地标识。 + ID int64 + // CookieID 是商品所属账号标识。 + CookieID string + // ItemID 是平台商品标识。 + ItemID string + // ItemTitle 是商品标题。 + ItemTitle string + // ItemDescription 是商品描述。 + ItemDescription string + // ItemCategory 是商品分类。 + ItemCategory string + // ItemPrice 是商品价格文本。 + ItemPrice string + // ItemDetail 是商品详情 JSON。 + ItemDetail string + // IsMultiSpec 表示商品是否启用多规格。 + IsMultiSpec bool + // MultiQuantityDelivery 表示商品是否启用多数量发货。 + MultiQuantityDelivery bool +} + +// PlatformRuntimeData 是订单刷新访问平台所需的最小账号运行视图。 +type PlatformRuntimeData struct { + // ID 是闲鱼账号的稳定标识。 + ID string + // UserID 是账号所属的本地用户标识。 + UserID int64 + // Value 是 repository 解密后的 Cookie 明文,仅在平台请求边界短暂使用。 + Value string + // MetadataJSON 是 Cookie 快照等平台请求元数据。 + MetadataJSON string + // ShowBrowser 表示风控恢复是否允许使用可视化浏览器。 + ShowBrowser bool +} + // ListFilter 是订单列表查询的纯业务筛选条件。 type ListFilter struct { // UserID 是当前用户标识。 diff --git a/internal/server/order_model_conversion_test.go b/internal/server/order_model_conversion_test.go new file mode 100644 index 0000000..5076d40 --- /dev/null +++ b/internal/server/order_model_conversion_test.go @@ -0,0 +1,110 @@ +package server + +import ( + "reflect" + "testing" + + orderapp "xianyu-go/internal/application/orders" + "xianyu-go/internal/db" +) + +// TestOrderModelConversions 保证订单边界转换完整保留业务字段并正确处理空值。 +func TestOrderModelConversions(t *testing.T) { + // databaseOrder 是模拟数据库读取到的完整订单实体。 + databaseOrder := &db.Order{ + OrderID: "order-1", ItemID: "item-1", BuyerID: "buyer-1", SpecName: "颜色", + SpecValue: "蓝", Quantity: "2", Amount: "19.90", OrderStatus: "pending_ship", + CookieID: "cookie-1", IsBargain: 1, ReceiverName: "收货人", ReceiverPhone: "13800000000", + ReceiverAddr: "上海市", ReceiverCity: "上海", Version: 3, ChatID: "chat-1", + SystemShipped: true, PaidAt: "paid", ShippedAt: "shipped", CompletedAt: "completed", + BuyerReviewedAt: "reviewed", LastReviewRequestAt: "requested", ReviewRequestCount: 2, + CreatedAt: "created", UpdatedAt: "updated", + } + // applicationOrder 是订单应用层实体的预期结果。 + applicationOrder := &orderapp.Order{ + OrderID: "order-1", ItemID: "item-1", BuyerID: "buyer-1", SpecName: "颜色", + SpecValue: "蓝", Quantity: "2", Amount: "19.90", OrderStatus: "pending_ship", + CookieID: "cookie-1", IsBargain: 1, ReceiverName: "收货人", ReceiverPhone: "13800000000", + ReceiverAddress: "上海市", ReceiverCity: "上海", Version: 3, ChatID: "chat-1", + SystemShipped: true, PaidAt: "paid", ShippedAt: "shipped", CompletedAt: "completed", + BuyerReviewedAt: "reviewed", LastReviewRequestAt: "requested", ReviewRequestCount: 2, + CreatedAt: "created", UpdatedAt: "updated", + } + // converted 是数据库订单转换后的应用订单。 + converted := orderFromDB(databaseOrder) + if !reflect.DeepEqual(converted, applicationOrder) { + t.Fatalf("订单转换结果不完整: got=%+v want=%+v", converted, applicationOrder) + } + if orderFromDB(nil) != nil { + t.Fatal("空数据库订单应转换为空应用订单") + } + + // databaseItem 是模拟数据库读取到的完整商品实体。 + databaseItem := &db.ItemInfo{ + ID: 7, CookieID: "cookie-1", ItemID: "item-1", ItemTitle: "商品", + ItemDescription: "描述", ItemCategory: "分类", ItemPrice: "9.95", ItemDetail: `{"images":["a"]}`, + IsMultiSpec: true, MultiQuantityDelivery: true, + } + // applicationItem 是商品应用层实体的预期结果。 + applicationItem := &orderapp.ItemInfo{ + ID: 7, CookieID: "cookie-1", ItemID: "item-1", ItemTitle: "商品", + ItemDescription: "描述", ItemCategory: "分类", ItemPrice: "9.95", ItemDetail: `{"images":["a"]}`, + IsMultiSpec: true, MultiQuantityDelivery: true, + } + // convertedItem 是数据库商品转换后的应用商品。 + convertedItem := itemInfoFromDB(databaseItem) + if !reflect.DeepEqual(convertedItem, applicationItem) { + t.Fatalf("商品转换结果不完整: got=%+v want=%+v", convertedItem, applicationItem) + } + if itemInfoFromDB(nil) != nil { + t.Fatal("空数据库商品应转换为空应用商品") + } + + // databaseRuntime 是模拟数据库读取到的平台运行视图。 + databaseRuntime := db.CookiePlatformRuntimeData{ID: "cookie-1", UserID: 9, Value: "cookie", MetadataJSON: "{}", ShowBrowser: true} + // applicationRuntime 是平台运行视图的预期应用层结果。 + applicationRuntime := &orderapp.PlatformRuntimeData{ID: "cookie-1", UserID: 9, Value: "cookie", MetadataJSON: "{}", ShowBrowser: true} + // convertedRuntime 是数据库平台运行视图转换后的应用模型。 + convertedRuntime := platformRuntimeDataFromDB(databaseRuntime) + if !reflect.DeepEqual(convertedRuntime, applicationRuntime) { + t.Fatalf("平台运行视图转换结果不完整: got=%+v want=%+v", convertedRuntime, applicationRuntime) + } +} + +// TestOrderForAutomation 保证未迁移的自动化边界不会丢失订单字段。 +func TestOrderForAutomation(t *testing.T) { + // applicationOrder 是传入自动化中心适配器的应用订单。 + applicationOrder := &orderapp.Order{ + OrderID: "order-2", ItemID: "item-2", BuyerID: "buyer-2", ReceiverAddress: "地址", + CookieID: "cookie-2", ChatID: "chat-2", OrderStatus: "pending_ship", Version: 4, + } + // expectedDatabaseOrder 是自动化中心当前接口仍接收的数据库订单。 + expectedDatabaseOrder := &db.Order{ + OrderID: "order-2", ItemID: "item-2", BuyerID: "buyer-2", ReceiverAddr: "地址", + CookieID: "cookie-2", ChatID: "chat-2", OrderStatus: "pending_ship", Version: 4, + } + // converted 是传给自动化中心的数据库订单。 + converted := orderForAutomation(applicationOrder) + if !reflect.DeepEqual(converted, expectedDatabaseOrder) { + t.Fatalf("自动化边界订单转换错误: got=%+v want=%+v", converted, expectedDatabaseOrder) + } + if orderForAutomation(nil) != nil { + t.Fatal("空应用订单应转换为空数据库订单") + } +} + +// TestCookieDetailForOrderPlatform 保证订单刷新访问共享会话辅助函数时只映射最小字段。 +func TestCookieDetailForOrderPlatform(t *testing.T) { + // runtimeData 是订单应用层持有的平台运行视图。 + runtimeData := &orderapp.PlatformRuntimeData{ID: "cookie-3", UserID: 11, Value: "value", MetadataJSON: "metadata", ShowBrowser: true} + // expectedDetail 是共享 Server 会话辅助函数使用的兼容详情。 + expectedDetail := &db.CookieDetail{ID: "cookie-3", UserID: 11, Value: "value", MetadataJSON: "metadata", ShowBrowser: true} + // converted 是共享 Server 会话辅助函数使用的平台详情。 + converted := cookieDetailForOrderPlatform(runtimeData) + if !reflect.DeepEqual(converted, expectedDetail) { + t.Fatalf("平台详情边界转换错误: got=%+v want=%+v", converted, expectedDetail) + } + if cookieDetailForOrderPlatform(nil) != nil { + t.Fatal("空平台运行视图应转换为空平台详情") + } +} diff --git a/internal/server/order_repository.go b/internal/server/order_repository.go index 94b4f69..6c0b164 100644 --- a/internal/server/order_repository.go +++ b/internal/server/order_repository.go @@ -16,9 +16,9 @@ type orderRepository interface { // ListOrdersForUser 查询用户范围内的订单列表。 ListOrdersForUser(ctx context.Context, filter orderapp.ListFilter) ([]orderapp.OrderRow, int, error) // GetOrder 查询单个订单。 - GetOrder(ctx context.Context, orderID string) (*db.Order, error) + GetOrder(ctx context.Context, orderID string) (*orderapp.Order, error) // GetItem 查询账号下的商品信息。 - GetItem(ctx context.Context, cookieID, itemID string) (*db.ItemInfo, error) + GetItem(ctx context.Context, cookieID, itemID string) (*orderapp.ItemInfo, error) // SoftDeleteOrder 逻辑删除订单。 SoftDeleteOrder(ctx context.Context, orderID string) (bool, error) // WithTransaction 在应用层 Writer 中执行订单持久化操作。 @@ -28,13 +28,13 @@ type orderRepository interface { // LockCredentials 串行化账号凭证状态变更。 LockCredentials(cookieID string) func() // LoadCookiePlatformDetail 读取账号平台凭证详情。 - LoadCookiePlatformDetail(ctx context.Context, cookieID string) (*db.CookieDetail, error) + LoadCookiePlatformDetail(ctx context.Context, cookieID string) (*orderapp.PlatformRuntimeData, error) // UpdateRenewalCookie 更新账号续期 Cookie 和 metadata。 UpdateRenewalCookie(ctx context.Context, cookieID, value, metadata string, at int64) error // SoftDeleteMissingOrders 删除账号下远端已不存在的订单。 SoftDeleteMissingOrders(ctx context.Context, cookieID string, activeIDs map[string]struct{}) (int, error) // ListOrdersByCookiePage 分页读取账号订单。 - ListOrdersByCookiePage(ctx context.Context, cookieID string, limit, offset int) ([]db.OrderRow, error) + ListOrdersByCookiePage(ctx context.Context, cookieID string, limit, offset int) ([]orderapp.OrderRow, error) } // storeOrderRepository 将完整 Store 适配为订单应用服务窄 repository。 @@ -84,14 +84,65 @@ func orderRowsFromDB(rows []db.OrderRow) []orderapp.OrderRow { return converted } +// orderFromDB 将数据库订单实体转换为不暴露存储层字段命名的应用实体。 +func orderFromDB(order *db.Order) *orderapp.Order { + if order == nil { + return nil + } + return &orderapp.Order{ + OrderID: order.OrderID, ItemID: order.ItemID, BuyerID: order.BuyerID, + SpecName: order.SpecName, SpecValue: order.SpecValue, Quantity: order.Quantity, + Amount: order.Amount, OrderStatus: order.OrderStatus, CookieID: order.CookieID, + IsBargain: order.IsBargain, ReceiverName: order.ReceiverName, + ReceiverPhone: order.ReceiverPhone, ReceiverAddress: order.ReceiverAddr, + ReceiverCity: order.ReceiverCity, Version: order.Version, ChatID: order.ChatID, + SystemShipped: order.SystemShipped, PaidAt: order.PaidAt, ShippedAt: order.ShippedAt, + CompletedAt: order.CompletedAt, BuyerReviewedAt: order.BuyerReviewedAt, + LastReviewRequestAt: order.LastReviewRequestAt, ReviewRequestCount: order.ReviewRequestCount, + CreatedAt: order.CreatedAt, UpdatedAt: order.UpdatedAt, + } +} + +// itemInfoFromDB 将数据库商品实体转换为订单应用层商品模型。 +func itemInfoFromDB(item *db.ItemInfo) *orderapp.ItemInfo { + if item == nil { + return nil + } + return &orderapp.ItemInfo{ + ID: item.ID, CookieID: item.CookieID, ItemID: item.ItemID, + ItemTitle: item.ItemTitle, ItemDescription: item.ItemDescription, + ItemCategory: item.ItemCategory, ItemPrice: item.ItemPrice, + ItemDetail: item.ItemDetail, IsMultiSpec: item.IsMultiSpec, + MultiQuantityDelivery: item.MultiQuantityDelivery, + } +} + +// platformRuntimeDataFromDB 将数据库平台运行视图转换为订单应用层模型。 +func platformRuntimeDataFromDB(data db.CookiePlatformRuntimeData) *orderapp.PlatformRuntimeData { + return &orderapp.PlatformRuntimeData{ + ID: data.ID, UserID: data.UserID, Value: data.Value, + MetadataJSON: data.MetadataJSON, ShowBrowser: data.ShowBrowser, + } +} + // GetOrder 委托订单详情查询。 -func (r storeOrderRepository) GetOrder(ctx context.Context, orderID string) (*db.Order, error) { - return r.store.Orders.Get(ctx, orderID) +func (r storeOrderRepository) GetOrder(ctx context.Context, orderID string) (*orderapp.Order, error) { + // order 和 err 保存数据库订单查询结果及其错误。 + order, err := r.store.Orders.Get(ctx, orderID) + if err != nil { + return nil, err + } + return orderFromDB(order), nil } // GetItem 委托商品信息查询。 -func (r storeOrderRepository) GetItem(ctx context.Context, cookieID, itemID string) (*db.ItemInfo, error) { - return r.store.Items.Get(ctx, cookieID, itemID) +func (r storeOrderRepository) GetItem(ctx context.Context, cookieID, itemID string) (*orderapp.ItemInfo, error) { + // item 和 err 保存数据库商品查询结果及其错误。 + item, err := r.store.Items.Get(ctx, cookieID, itemID) + if err != nil { + return nil, err + } + return itemInfoFromDB(item), nil } // SoftDeleteOrder 委托订单逻辑删除。 @@ -184,13 +235,13 @@ func (r storeOrderRepository) LockCredentials(cookieID string) func() { } // LoadCookiePlatformDetail 委托平台凭证详情查询。 -func (r storeOrderRepository) LoadCookiePlatformDetail(ctx context.Context, cookieID string) (*db.CookieDetail, error) { +func (r storeOrderRepository) LoadCookiePlatformDetail(ctx context.Context, cookieID string) (*orderapp.PlatformRuntimeData, error) { // data 和 err 保存平台运行视图查询结果。 data, err := r.store.Cookies.GetCookiePlatformRuntimeData(ctx, cookieID) if err != nil { return nil, err } - return &db.CookieDetail{ID: data.ID, UserID: data.UserID, Value: data.Value, MetadataJSON: data.MetadataJSON, ShowBrowser: data.ShowBrowser}, nil + return platformRuntimeDataFromDB(data), nil } // UpdateRenewalCookie 委托续期 Cookie 更新。 @@ -204,8 +255,13 @@ func (r storeOrderRepository) SoftDeleteMissingOrders(ctx context.Context, cooki } // ListOrdersByCookiePage 委托账号订单分页查询。 -func (r storeOrderRepository) ListOrdersByCookiePage(ctx context.Context, cookieID string, limit, offset int) ([]db.OrderRow, error) { - return r.store.Orders.ByCookiePage(ctx, cookieID, limit, offset) +func (r storeOrderRepository) ListOrdersByCookiePage(ctx context.Context, cookieID string, limit, offset int) ([]orderapp.OrderRow, error) { + // rows 和 err 保存数据库订单分页结果及其错误。 + rows, err := r.store.Orders.ByCookiePage(ctx, cookieID, limit, offset) + if err != nil { + return nil, err + } + return orderRowsFromDB(rows), nil } // newStoreOrderRepository 从完整 Store 构造订单应用服务窄 repository。 diff --git a/internal/server/order_service.go b/internal/server/order_service.go index a442945..607b3b2 100644 --- a/internal/server/order_service.go +++ b/internal/server/order_service.go @@ -125,7 +125,7 @@ func orderDTOFromRow(row orderapp.OrderRow) orderDTO { } // orderDTOFromOrder 把订单实体和关联商品信息转换为详情响应视图。 -func orderDTOFromOrder(order *db.Order, item *db.ItemInfo) orderDTO { +func orderDTOFromOrder(order *orderapp.Order, item *orderapp.ItemInfo) orderDTO { // itemTitle、itemImage 保存商品Title、item图片,供当前处理流程使用 itemTitle, itemImage := "", "" if item != nil { @@ -140,11 +140,41 @@ func orderDTOFromOrder(order *db.Order, item *db.ItemInfo) orderDTO { Quantity: order.Quantity, Amount: order.Amount, OrderStatus: status, Status: status, CookieID: order.CookieID, IsBargain: order.IsBargain, SystemShipped: order.SystemShipped, ReceiverName: order.ReceiverName, ReceiverPhone: order.ReceiverPhone, - ReceiverAddress: order.ReceiverAddr, ReceiverCity: order.ReceiverCity, + ReceiverAddress: order.ReceiverAddress, ReceiverCity: order.ReceiverCity, CreatedAt: order.CreatedAt, UpdatedAt: order.UpdatedAt, } } +// orderForAutomation 将订单应用实体转换为尚未迁移完成的自动化中心数据库实体。 +func orderForAutomation(order *orderapp.Order) *db.Order { + if order == nil { + return nil + } + return &db.Order{ + OrderID: order.OrderID, ItemID: order.ItemID, BuyerID: order.BuyerID, + SpecName: order.SpecName, SpecValue: order.SpecValue, Quantity: order.Quantity, + Amount: order.Amount, OrderStatus: order.OrderStatus, CookieID: order.CookieID, + IsBargain: order.IsBargain, ReceiverName: order.ReceiverName, + ReceiverPhone: order.ReceiverPhone, ReceiverAddr: order.ReceiverAddress, + ReceiverCity: order.ReceiverCity, Version: order.Version, ChatID: order.ChatID, + SystemShipped: order.SystemShipped, PaidAt: order.PaidAt, ShippedAt: order.ShippedAt, + CompletedAt: order.CompletedAt, BuyerReviewedAt: order.BuyerReviewedAt, + LastReviewRequestAt: order.LastReviewRequestAt, ReviewRequestCount: order.ReviewRequestCount, + CreatedAt: order.CreatedAt, UpdatedAt: order.UpdatedAt, + } +} + +// cookieDetailForOrderPlatform 将订单应用层平台运行视图转换为共享 Server 会话辅助函数所需的兼容详情。 +func cookieDetailForOrderPlatform(data *orderapp.PlatformRuntimeData) *db.CookieDetail { + if data == nil { + return nil + } + return &db.CookieDetail{ + ID: data.ID, UserID: data.UserID, Value: data.Value, + MetadataJSON: data.MetadataJSON, ShowBrowser: data.ShowBrowser, + } +} + // orderDetailResult 返回订单详情的统一响应视图。 type orderDetailResult struct { // Order 是已经完成商品信息补全的订单视图。 @@ -188,7 +218,7 @@ func (a *orderApplicationService) List(ctx context.Context, query orderListQuery } // Get 查询订单并校验订单绑定账号属于当前用户。 -func (a *orderApplicationService) Get(ctx context.Context, userID int64, orderID string) (*db.Order, error) { +func (a *orderApplicationService) Get(ctx context.Context, userID int64, orderID string) (*orderapp.Order, error) { // order、err 保存order、err,供当前处理流程使用 order, err := a.repository.GetOrder(ctx, orderID) if err != nil || order == nil { @@ -501,7 +531,7 @@ func (a *orderApplicationService) appendManualFailure(result *manualShipResult, } // manualFullDelivery 执行完整自动化发货分支。 -func (a *orderApplicationService) manualFullDelivery(ctx context.Context, order *db.Order, orderID string, result *manualShipResult) { +func (a *orderApplicationService) manualFullDelivery(ctx context.Context, order *orderapp.Order, orderID string, result *manualShipResult) { if a.server.Manager == nil || a.server.automation == nil { a.appendManualFailure(result, orderID, "自动化中心未初始化") return @@ -512,7 +542,7 @@ func (a *orderApplicationService) manualFullDelivery(ctx context.Context, order return } // sent、err 保存sent、err,供当前处理流程使用 - sent, err := a.server.automation.ManualFullDelivery(ctx, order) + sent, err := a.server.automation.ManualFullDelivery(ctx, orderForAutomation(order)) if err != nil { a.appendManualFailure(result, orderID, err.Error()) a.server.notifyDelivery(order.CookieID, order.BuyerID, order.ItemID, order.ChatID, "手动完整发货失败: "+err.Error()) @@ -524,7 +554,7 @@ func (a *orderApplicationService) manualFullDelivery(ctx context.Context, order } // manualStatusShip 调用平台确认发货并把成功状态写入本地订单。 -func (a *orderApplicationService) manualStatusShip(ctx context.Context, userID int64, order *db.Order, orderID string, result *manualShipResult) { +func (a *orderApplicationService) manualStatusShip(ctx context.Context, userID int64, order *orderapp.Order, orderID string, result *manualShipResult) { if a.server.MTop == nil { a.appendManualFailure(result, orderID, "mtop 客户端未初始化") return @@ -555,7 +585,7 @@ func (a *orderApplicationService) manualStatusShip(ctx context.Context, userID i 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, - ReceiverAddress: order.ReceiverAddr, ReceiverCity: order.ReceiverCity, ChatID: order.ChatID, + ReceiverAddress: order.ReceiverAddress, 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 { @@ -599,16 +629,18 @@ func (a *orderApplicationService) RefreshSingle(ctx context.Context, userID int6 }() // latest、err 保存latest、err,供当前处理流程使用 latest, err := a.repository.LoadCookiePlatformDetail(ctx, cookieID) - if err != nil || latest == nil || latest.UserID != userID || !hasStoredCookieCredential(latest) { + // latestDetail 是转换到共享会话辅助函数的最小平台详情。 + latestDetail := cookieDetailForOrderPlatform(latest) + if err != nil || latestDetail == nil || latestDetail.UserID != userID || !hasStoredCookieCredential(latestDetail) { return orderSingleRefreshResponse{}, errOrderCredentialChanged } // refreshCtx、cancel 保存refreshCtx、cancel,供当前处理流程使用 refreshCtx, cancel := context.WithTimeout(ctx, time.Minute) defer cancel() // mtopCtx、cookieSession 保存mtopCtx、cookie会话,供当前处理流程使用 - mtopCtx, cookieSession := withMTopCookieSnapshot(refreshCtx, latest) + mtopCtx, cookieSession := withMTopCookieSnapshot(refreshCtx, latestDetail) // detail、callErr 保存detail、callErr,供当前处理流程使用 - detail, callErr := detailFetcher.FetchOrderDetail(mtopCtx, latest.Value, orderID) + detail, callErr := detailFetcher.FetchOrderDetail(mtopCtx, latestDetail.Value, orderID) if callErr == nil && detail == nil { callErr = errors.New("订单详情接口未返回结果") } @@ -617,14 +649,14 @@ func (a *orderApplicationService) RefreshSingle(ctx context.Context, userID int6 // runtimeCookieChanged 保存runtime登录凭证Changed,供当前处理流程使用 runtimeCookieChanged := false // value、valueChanged、handled、persistErr 保存value、valueChanged、handled、persistErr,供当前处理流程使用 - value, valueChanged, handled, persistErr := a.server.persistMTopCookieSessionLocked(ctx, latest, cookieSession) + value, valueChanged, handled, persistErr := a.server.persistMTopCookieSessionLocked(ctx, latestDetail, cookieSession) if persistErr != nil { callErr = errors.Join(callErr, fmt.Errorf("保存订单详情响应 Cookie Jar: %w", persistErr)) } else if handled && valueChanged { runtimeCookie, runtimeCookieChanged = value, true - } else if !handled && callErr == nil && detail.UpdatedCookies != "" && detail.UpdatedCookies != latest.Value { + } else if !handled && callErr == nil && detail.UpdatedCookies != "" && detail.UpdatedCookies != latestDetail.Value { // metadata 保存metadata,供当前处理流程使用 - metadata := cookierefresh.MetadataWithoutSnapshot(latest.MetadataJSON) + metadata := cookierefresh.MetadataWithoutSnapshot(latestDetail.MetadataJSON) if // saveErr 保存saveErr,供当前处理流程使用 saveErr := a.repository.UpdateRenewalCookie(ctx, cookieID, detail.UpdatedCookies, metadata, time.Now().Unix()); saveErr == nil { runtimeCookie, runtimeCookieChanged = detail.UpdatedCookies, true @@ -680,7 +712,9 @@ func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, coo credentialUnlock := a.repository.LockCredentials(cid) // latest、latestErr 保存latest、latestErr,供当前处理流程使用 latest, latestErr := a.repository.LoadCookiePlatformDetail(ctx, cid) - if latestErr != nil || latest == nil || latest.UserID != userID || !hasStoredCookieCredential(latest) { + // latestDetail 是转换到共享会话辅助函数的最小平台详情。 + latestDetail := cookieDetailForOrderPlatform(latest) + if latestErr != nil || latestDetail == nil || latestDetail.UserID != userID || !hasStoredCookieCredential(latestDetail) { credentialUnlock() if latestErr == nil { latestErr = errors.New("账号凭证已变化") @@ -692,11 +726,11 @@ func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, coo continue } // latest.Value 仅用于当前账号的凭证调用,不需要写回账号列表。 - mtopCtx, cookieSession := withMTopCookieSnapshot(ctx, latest) + mtopCtx, cookieSession := withMTopCookieSnapshot(ctx, latestDetail) // accountDiscovered、accountUpdated、accountNewIDs、accountRemoteIDs、discoveryErr 保存账号Discovered、accountUpdated、accountNewIDs、accountRemoteIDs、discoveryErr,供当前处理流程使用 - accountDiscovered, accountUpdated, accountNewIDs, accountRemoteIDs, discoveryErr := a.server.discoverSoldOrders(mtopCtx, fetcher, cid, latest.Value) + accountDiscovered, accountUpdated, accountNewIDs, accountRemoteIDs, discoveryErr := a.server.discoverSoldOrders(mtopCtx, fetcher, cid, latestDetail.Value) // value、valueChanged、persistErr 保存value、valueChanged、persistErr,供当前处理流程使用 - value, valueChanged, _, persistErr := a.server.persistMTopCookieSessionLocked(ctx, latest, cookieSession) + value, valueChanged, _, persistErr := a.server.persistMTopCookieSessionLocked(ctx, latestDetail, cookieSession) if persistErr != nil { discoveryErr = errors.Join(discoveryErr, fmt.Errorf("保存订单列表响应 Cookie Jar: %w", persistErr)) } @@ -826,7 +860,9 @@ func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, coo credentialUnlock := a.repository.LockCredentials(cid) // latest、latestErr 保存latest、latestErr,供当前处理流程使用 latest, latestErr := a.repository.LoadCookiePlatformDetail(ctx, cid) - if latestErr != nil || latest == nil || latest.UserID != userID || !hasStoredCookieCredential(latest) { + // latestDetail 是转换到共享会话辅助函数的最小平台详情。 + latestDetail := cookieDetailForOrderPlatform(latest) + if latestErr != nil || latestDetail == nil || latestDetail.UserID != userID || !hasStoredCookieCredential(latestDetail) { credentialUnlock() failed += len(chunk) results = append(results, map[string]any{"cookie_id": cid, "success": false, "message": "账号凭证已变化"}) @@ -835,13 +871,13 @@ func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, coo // detailCtx、cancel 保存detailCtx、cancel,供当前处理流程使用 detailCtx, cancel := context.WithTimeout(ctx, 3*time.Minute) // mtopCtx、cookieSession 保存mtopCtx、cookie会话,供当前处理流程使用 - mtopCtx, cookieSession := withMTopCookieSnapshot(detailCtx, latest) + mtopCtx, cookieSession := withMTopCookieSnapshot(detailCtx, latestDetail) // sessionErr 保存会话Err,供当前处理流程使用 var sessionErr error // target 表示当前遍历过程中的target for _, target := range chunk { // detail、fetchErr 保存detail、fetchErr,供当前处理流程使用 - detail, fetchErr := detailFetcher.FetchOrderDetail(mtopCtx, latest.Value, target.OrderID) + detail, fetchErr := detailFetcher.FetchOrderDetail(mtopCtx, latestDetail.Value, target.OrderID) if fetchErr != nil || detail == nil { failed++ // message 保存消息,供当前处理流程使用 @@ -895,7 +931,7 @@ func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, coo } cancel() // value、valueChanged、persistErr 保存value、valueChanged、persistErr,供当前处理流程使用 - value, valueChanged, _, persistErr := a.server.persistMTopCookieSessionLocked(ctx, latest, cookieSession) + value, valueChanged, _, persistErr := a.server.persistMTopCookieSessionLocked(ctx, latestDetail, cookieSession) credentialUnlock() if persistErr != nil { failed++