From eff4143fd66ec37d22ee7aed58c98d0e2321361b Mon Sep 17 00:00:00 2001 From: Christ Date: Sat, 15 Aug 2026 18:59:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E6=B2=89=E8=AE=A2=E5=8D=95=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E6=9C=8D=E5=8A=A1=E9=9B=86=E5=90=88=E8=A3=85=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/architecture/refactoring-master-plan.md | 3 +- internal/application/orders/service_set.go | 36 +++++++ .../application/orders/service_set_test.go | 12 +++ internal/server/application_services.go | 8 +- internal/server/application_services_test.go | 3 + internal/server/order_refresh_job_handlers.go | 22 ++--- internal/server/order_service.go | 98 +++++-------------- 7 files changed, 95 insertions(+), 87 deletions(-) create mode 100644 internal/application/orders/service_set.go create mode 100644 internal/application/orders/service_set_test.go diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index ca49915..ed7809d 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -95,7 +95,7 @@ app shell / routes | 1. PR CI 与测试基础 | 已完成 | 独立 CI、测试 DB 模板、可执行 race | CI、独立模板、server smoke race 和完整 race 均有验证 | | 2. 敏感数据访问边界 | 进行中 | 摘要、凭证、登录秘密和系统设置分离 | Cookie/平台运行视图已收口;待完成系统设置脱敏读写、运维输出脱敏、敏感字段访问审计和三库回归 | | 3. HTTP API 契约 | 已完成 | 统一错误、具名 DTO、版本化路径 | 统一错误 DTO、具名成功 DTO、所有前端业务调用方版本化、旧路径兼容和最终审计均已完成 | -| 4. Server 应用服务 | 进行中 | 订单、发布、登录、聊天纵向抽取 | 已完成文件级服务拆分;待完成应用 Port 独立包、Server 不持有业务服务定位器、handler 不依赖 db/xianyu/browser | +| 4. Server 应用服务 | 进行中 | 订单、发布、登录、聊天纵向抽取 | 订单业务服务已迁入 `internal/application/orders`,Server 仅保留订单 HTTP/基础设施适配器;商品发布、登录、聊天仍由 Server 持有,handler 的低层依赖与其他领域 Port 仍待收口 | | 5. 应用生命周期装配 | 进行中 | 消除必需依赖 setter 回填并统一关闭边界 | 已有 Server 自有 worker 等待;待完成 Context-aware Stop、账号 StopAll 超时、删除任务登记和统一生命周期清单 | | 6. Engine 与 Automation | 已完成 | facade + 独立状态组件 | Engine/Automation 组件边界、race、生命周期与冻结规范测试均已通过 | | 7. React Feature 化 | 已完成 | 页面、Hook、API、类型按领域拆分 | 领域 feature、行为测试、依赖门禁和构建门禁已完成 | @@ -758,3 +758,4 @@ npm --prefix frontend run build | 2026-08-15 | P1 订单刷新任务取消与状态 fencing 切片 | 已完成本切片 | 新增按用户归属的 `Cancel` 持久化命令,仅允许 `queued/running` 任务转为 `cancelled` 并清理旧 worker 租约;Server 在 worker 启动前登记可取消 Context,取消端点同时收束内存 worker,旧 worker 的 `Complete` 无法覆盖取消终态;新增 legacy/versioned DELETE 路由、前端 `cancelOrderRefreshJob`,`syncOrders` 在轮询取消时通知后端;覆盖 SQLite 任务生命周期、跨用户隔离、旧 worker fencing、HTTP 和前端 API 回归;`make check`、Go/React 注释门禁、Go 全量测试、前端 318 测试、类型检查和生产构建通过。刷新进度事件、三库任务迁移实测、后台运维查询、批量 UPSERT 和其他领域迁移仍未完成,阶段 4/5/8/9 保持“进行中” | | 2026-08-15 | P2 订单详情分片单条多值 UPSERT 切片 | 已完成本切片 | 新增应用层 `RefreshOrderWrite` 与 `BatchUpsertOrders` Port;详情分片先收集成功结果,再由 Server 适配器在单事务内调用数据库 `UpsertManyTx`,数据库以一条多值 UPSERT 写入整片订单,保留状态防倒退、空字段不覆盖、软删除恢复和跨账号归属检查;新增批量订单 SQLite 回归(状态、版本、金额、跨账号、重复订单)及应用层“单次批量调用”断言;定向 Go 测试、`make check`、架构门禁、中文注释门禁和 `git diff --check` 通过。当前环境未提供 `TEST_MYSQL_URL`/`TEST_POSTGRES_URL`,因此未宣称外部数据库实测完成;订单发现仍为逐账号游标、商品批量同步和其他批量写入仍未完成,阶段 4/5/8/9 保持“进行中” | | 2026-08-15 | P2 订单发现批量读写切片 | 已完成本切片 | 订单发现不再对远端订单逐条 `FindOrder` + `UpsertOrder`;新增 `FindOrdersByIDs`(按 500 个标识分片,兼容 SQLite 参数上限),远端列表先去重/归一化,再一次批量读取和一次多值 UPSERT,补齐 `is_bargain` 写入且保留已有砍价标记;未知状态不会覆盖已有进阶状态,批量失败不返回虚假发现统计;新增去重、批量调用次数、未知状态防倒退、批量读写失败和 SQLite 回归;`make check`、订单/Server 聚焦 race、架构门禁、中文注释门禁、`git diff --check` 通过。当前未配置 MySQL/Postgres 外部实例,三库 SQL 仅完成代码路径兼容设计,后续仍需外部查询计划/大数据量实测;跨账号账号级平台请求、商品/其他领域批量同步和阶段 4/5/8/9 仍未完成 | +| 2026-08-15 | P1 订单应用服务集合装配下沉切片 | 已完成本切片 | 新增 `internal/application/orders.ServiceSet` 与 `NewServiceSet`,统一构造订单列表、详情、删除、更新、导入、手动发货、刷新和刷新任务服务;`internal/server` 的订单对象重命名为 `orderHTTPAdapter`,只负责兼容 HTTP DTO/错误映射与归属适配,Server 不再分别创建订单业务服务;刷新任务 handler 改为通过应用层 ServiceSet 的任务 Port;新增应用集合构造测试与 Server 装配断言,订单/Server 定向测试和中文注释门禁通过。商品发布、登录、聊天等其他 Server 应用服务仍未迁移,低层 handler 依赖、生命周期统一清单、旧 API 兼容退场和整体阶段 4/5/8/9 仍保持“进行中” | diff --git a/internal/application/orders/service_set.go b/internal/application/orders/service_set.go new file mode 100644 index 0000000..1a5b92c --- /dev/null +++ b/internal/application/orders/service_set.go @@ -0,0 +1,36 @@ +package orders + +// ServiceSet 聚合订单领域的应用服务实例,由应用层统一负责构造。 +// HTTP/Server 适配层只持有该集合,不再分别创建业务服务实现。 +type ServiceSet struct { + // List 负责订单列表分页和筛选用例。 + List *ListService + // Detail 负责订单详情读取和商品补全用例。 + Detail *DetailService + // Delete 负责订单逻辑删除用例。 + Delete *DeleteService + // Update 负责订单字段与商品标题更新用例。 + Update *UpdateService + // Import 负责订单文件导入用例。 + Import *ImportService + // ManualShip 负责手动发货和补偿用例。 + ManualShip *ManualShipService + // Refresh 负责订单发现、详情刷新和缺失清理用例。 + Refresh *RefreshService + // RefreshJobs 负责订单刷新后台任务的持久化操作。 + RefreshJobs RefreshJobRepository +} + +// NewServiceSet 使用应用层 Port 构造完整订单服务集合。 +func NewServiceSet(repository Repository, refreshRepository RefreshRepository, manualRuntime ManualShipRuntime, refreshRuntime RefreshRuntime, refreshJobs RefreshJobRepository, detailChunkSize int) *ServiceSet { + return &ServiceSet{ + List: NewListService(repository), + Detail: NewDetailService(repository), + Delete: NewDeleteService(repository), + Update: NewUpdateService(repository), + Import: NewImportService(repository), + ManualShip: NewManualShipService(repository, manualRuntime), + Refresh: NewRefreshService(refreshRepository, refreshRuntime, detailChunkSize), + RefreshJobs: refreshJobs, + } +} diff --git a/internal/application/orders/service_set_test.go b/internal/application/orders/service_set_test.go new file mode 100644 index 0000000..9027a29 --- /dev/null +++ b/internal/application/orders/service_set_test.go @@ -0,0 +1,12 @@ +package orders + +import "testing" + +// TestNewServiceSetConstructsAllServices 验证订单应用服务集合由应用层完整构造。 +func TestNewServiceSetConstructsAllServices(t *testing.T) { + // services 保存应用层统一构造的订单服务集合。 + services := NewServiceSet(nil, nil, nil, nil, nil, 100) + if services == nil || services.List == nil || services.Detail == nil || services.Delete == nil || services.Update == nil || services.Import == nil || services.ManualShip == nil || services.Refresh == nil { + t.Fatalf("订单应用服务集合未完整构造: %+v", services) + } +} diff --git a/internal/server/application_services.go b/internal/server/application_services.go index b30cf53..df2a339 100644 --- a/internal/server/application_services.go +++ b/internal/server/application_services.go @@ -4,8 +4,8 @@ import orderapp "xianyu-go/internal/application/orders" // applicationServices 聚合 Server 使用的应用服务实例,统一管理共享基础设施依赖。 type applicationServices struct { - // orders 是订单应用服务。 - orders *orderApplicationService + // orders 是订单 HTTP 适配器;业务服务集合由应用层统一构造。 + orders *orderHTTPAdapter // itemPublish 是商品发布应用服务。 itemPublish *itemPublishService // accountLogin 是账号登录应用服务。 @@ -22,8 +22,10 @@ func newApplicationServices(server *Server) *applicationServices { orderRepository := newStoreOrderRepository(server.Store) // orderRuntime 保存订单服务共享的运行时能力适配器。 orderRuntime := newServerOrderRuntime(server) + // orderServices 保存应用层统一构造的订单业务服务集合。 + orderServices := orderapp.NewServiceSet(orderRepository, orderRepository, orderRuntime, orderRuntime, newStoreOrderRefreshJobRepository(server.Store), refreshOrderChunkSize) return &applicationServices{ - orders: &orderApplicationService{server: orderRuntime, repository: orderRepository, list: orderapp.NewListService(orderRepository), detail: orderapp.NewDetailService(orderRepository), delete: orderapp.NewDeleteService(orderRepository), update: orderapp.NewUpdateService(orderRepository), importOrders: orderapp.NewImportService(orderRepository), manualShip: orderapp.NewManualShipService(orderRepository, orderRuntime), refresh: orderapp.NewRefreshService(orderRepository, orderRuntime, refreshOrderChunkSize), refreshJobs: newStoreOrderRefreshJobRepository(server.Store)}, + orders: &orderHTTPAdapter{services: orderServices, repository: orderRepository}, itemPublish: &itemPublishService{server: server, repository: newStoreItemPublishRepository(server.Store)}, accountLogin: &accountLoginService{server: server, repository: newStoreAccountLoginRepository(server.Store)}, communication: &communicationService{server: server, repository: newStoreCommunicationRepository(server.Store)}, diff --git a/internal/server/application_services_test.go b/internal/server/application_services_test.go index 572c638..86dfe87 100644 --- a/internal/server/application_services_test.go +++ b/internal/server/application_services_test.go @@ -15,6 +15,9 @@ func TestNewServerAssemblesApplicationServices(t *testing.T) { if services.orders == nil || services.itemPublish == nil || services.accountLogin == nil || services.communication == nil || services.analytics == nil { t.Fatal("应用服务集合存在未装配的服务") } + if services.orders.services == nil || services.orders.services.List == nil || services.orders.services.Detail == nil || services.orders.services.Refresh == nil || services.orders.services.RefreshJobs == nil { + t.Fatal("订单应用服务应由应用层 ServiceSet 完整装配") + } if srv.orders() != services.orders || srv.itemPublishApplication() != services.itemPublish || srv.accountLoginApplication() != services.accountLogin || srv.communicationApplication() != services.communication || srv.analyticsApplication() != services.analytics { t.Fatal("应用服务访问器未返回统一装配实例") } diff --git a/internal/server/order_refresh_job_handlers.go b/internal/server/order_refresh_job_handlers.go index 6c91edb..41cf2c3 100644 --- a/internal/server/order_refresh_job_handlers.go +++ b/internal/server/order_refresh_job_handlers.go @@ -84,14 +84,14 @@ func (s *Server) startOrderRefreshJob(w http.ResponseWriter, r *http.Request) { CookieID: cookieID, FilterStatus: filterStatus, } // err 表示创建任务的数据库错误。 - if err := s.orders().refreshJobs.Create(r.Context(), job); err != nil { + if err := s.orders().services.RefreshJobs.Create(r.Context(), job); err != nil { writeErr(w, http.StatusInternalServerError, "创建订单刷新任务失败") return } // token 保存本次执行者的租约令牌。 token := randomHex(16) // claimed、claimErr 保存任务抢占结果及错误。 - claimed, claimErr := s.orders().refreshJobs.Claim(r.Context(), job.ID, token, time.Now().Add(orderRefreshJobLease).Unix()) + claimed, claimErr := s.orders().services.RefreshJobs.Claim(r.Context(), job.ID, token, time.Now().Add(orderRefreshJobLease).Unix()) if claimErr != nil || !claimed { writeErr(w, http.StatusInternalServerError, "启动订单刷新任务失败") return @@ -107,7 +107,7 @@ func (s *Server) getOrderRefreshJob(w http.ResponseWriter, r *http.Request) { // jobID 保存路径中的订单刷新任务标识。 jobID := chi.URLParam(r, "job_id") // job、err 保存任务读取结果及错误。 - job, err := s.orders().refreshJobs.Get(r.Context(), sess.UserID, jobID) + job, err := s.orders().services.RefreshJobs.Get(r.Context(), sess.UserID, jobID) if errors.Is(err, orderapp.ErrRefreshJobNotFound) { writeErr(w, http.StatusNotFound, "订单刷新任务不存在") return @@ -141,7 +141,7 @@ func (s *Server) cancelOrderRefreshJob(w http.ResponseWriter, r *http.Request) { // jobID 保存路径中的订单刷新任务标识。 jobID := chi.URLParam(r, "job_id") // cancelled、err 保存数据库取消结果及错误。 - cancelled, err := s.orders().refreshJobs.Cancel(r.Context(), sess.UserID, jobID) + cancelled, err := s.orders().services.RefreshJobs.Cancel(r.Context(), sess.UserID, jobID) if err != nil { writeErr(w, http.StatusInternalServerError, "取消订单刷新任务失败") return @@ -152,7 +152,7 @@ func (s *Server) cancelOrderRefreshJob(w http.ResponseWriter, r *http.Request) { return } // job、getErr 保存取消未生效时的当前任务状态及查询错误。 - job, getErr := s.orders().refreshJobs.Get(r.Context(), sess.UserID, jobID) + job, getErr := s.orders().services.RefreshJobs.Get(r.Context(), sess.UserID, jobID) if errors.Is(getErr, orderapp.ErrRefreshJobNotFound) { writeErr(w, http.StatusNotFound, "订单刷新任务不存在") return @@ -233,7 +233,7 @@ func (s *Server) runOrderRefreshJob(ctx context.Context, job *orderapp.RefreshJo result, err := s.orders().Refresh(ctx, job.UserID, job.CookieID, job.FilterStatus) if err != nil { // completeErr 表示失败终态写入错误。 - if _, completeErr := s.orders().refreshJobs.Complete(context.Background(), job.ID, token, "failed", "{}", err.Error()); completeErr != nil && s.Logger != nil { + if _, completeErr := s.orders().services.RefreshJobs.Complete(context.Background(), job.ID, token, "failed", "{}", err.Error()); completeErr != nil && s.Logger != nil { s.Logger.Warn("写入订单刷新失败终态失败", "job_id", job.ID, "err", completeErr) } return @@ -241,11 +241,11 @@ func (s *Server) runOrderRefreshJob(ctx context.Context, job *orderapp.RefreshJo // resultJSON、marshalErr 保存具名刷新结果 JSON 及序列化错误。 resultJSON, marshalErr := json.Marshal(result) if marshalErr != nil { - _, _ = s.orders().refreshJobs.Complete(context.Background(), job.ID, token, "failed", "{}", marshalErr.Error()) + _, _ = s.orders().services.RefreshJobs.Complete(context.Background(), job.ID, token, "failed", "{}", marshalErr.Error()) return } // completeErr 表示成功终态写入错误。 - if _, completeErr := s.orders().refreshJobs.Complete(context.Background(), job.ID, token, "succeeded", string(resultJSON), ""); completeErr != nil && s.Logger != nil { + if _, completeErr := s.orders().services.RefreshJobs.Complete(context.Background(), job.ID, token, "succeeded", string(resultJSON), ""); completeErr != nil && s.Logger != nil { s.Logger.Warn("写入订单刷新成功终态失败", "job_id", job.ID, "err", completeErr) } } @@ -276,7 +276,7 @@ func (s *Server) StartOrderRefreshRecovery(ctx context.Context) { // recoverOrderRefreshJobsOnce 将过期任务重新入队并启动新的 worker。 func (s *Server) recoverOrderRefreshJobsOnce(ctx context.Context) { // jobs、err 保存可恢复任务及扫描错误。 - jobs, err := s.orders().refreshJobs.Recoverable(ctx, time.Now().Unix(), 20) + jobs, err := s.orders().services.RefreshJobs.Recoverable(ctx, time.Now().Unix(), 20) if err != nil { if s.Logger != nil { s.Logger.Warn("扫描订单刷新恢复任务失败", "err", err) @@ -286,14 +286,14 @@ func (s *Server) recoverOrderRefreshJobsOnce(ctx context.Context) { // job 表示当前待恢复的订单刷新任务。 for _, job := range jobs { // requeued、requeueErr 保存重新入队结果及错误。 - requeued, requeueErr := s.orders().refreshJobs.RequeueExpired(ctx, job.ID, time.Now().Unix()) + requeued, requeueErr := s.orders().services.RefreshJobs.RequeueExpired(ctx, job.ID, time.Now().Unix()) if requeueErr != nil || !requeued { continue } // token 保存恢复 worker 的新租约令牌。 token := randomHex(16) // claimed、claimErr 保存恢复任务抢占结果及错误。 - claimed, claimErr := s.orders().refreshJobs.Claim(ctx, job.ID, token, time.Now().Add(orderRefreshJobLease).Unix()) + claimed, claimErr := s.orders().services.RefreshJobs.Claim(ctx, job.ID, token, time.Now().Add(orderRefreshJobLease).Unix()) if claimErr == nil && claimed { s.startOrderRefreshWorker(&job, token) } diff --git a/internal/server/order_service.go b/internal/server/order_service.go index 5fddf81..0232128 100644 --- a/internal/server/order_service.go +++ b/internal/server/order_service.go @@ -12,31 +12,6 @@ import ( "xianyu-go/internal/xianyu/mtop" ) -// orderRuntimePort 定义订单用例访问运行时平台、自动化和通知能力的最小 Port。 -// 该接口隔离 Server 聚合对象,避免订单应用服务持有 *Server。 -type orderRuntimePort interface { - // AccountRunning 判断账号运行时是否在线。 - AccountRunning(cookieID string) bool - // AutomationReady 判断完整发货自动化依赖是否已装配。 - AutomationReady() bool - // ManualFullDelivery 执行完整自动化发货。 - ManualFullDelivery(ctx context.Context, order *orderapp.Order) (int, error) - // MTopAvailable 判断平台客户端是否已注入。 - MTopAvailable() bool - // MTopClient 返回平台客户端。 - mtopClient() mtop.Client - // consignWithCurrentCookie 使用当前账号凭证确认发货。 - consignWithCurrentCookie(ctx context.Context, cookieID, orderID string, userID int64) (bool, []string, string, bool, error) - // updateRunningCookie 同步运行时账号 Cookie。 - updateRunningCookie(ctx context.Context, cookieID, value string) - // notifyDelivery 发送发货结果通知。 - notifyDelivery(cookieID, buyerID, itemID, chatID, message string) - // RecordOrderReconciliation 记录外部发货成功但本地状态未完成的补偿任务。 - RecordOrderReconciliation(ctx context.Context, orderID, cookieID, kind, message string) (string, error) - // recoverExpiredMTOPSession 处理平台会话过期。 - recoverExpiredMTOPSession(ctx context.Context, cookieID string, err error) bool -} - // serverOrderRuntimeAdapter 将 Server 的运行时能力适配为订单应用 Port。 // 适配器只存在于装配边界,订单应用服务本身不再依赖 *Server。 type serverOrderRuntimeAdapter struct { @@ -141,11 +116,6 @@ func (a serverOrderRuntimeAdapter) ReportPersistenceFailure(orderID string, err a.server.Logger.Error("更新订单为系统已发货失败", "order_id", orderID, "err", err) } -// recoverExpiredMTOPSession 处理平台会话过期。 -func (a serverOrderRuntimeAdapter) recoverExpiredMTOPSession(ctx context.Context, cookieID string, err error) bool { - return a.server.recoverExpiredMTOPSession(ctx, cookieID, err) -} - // RecoverExpiredSession 处理订单刷新应用服务报告的平台会话过期。 func (a serverOrderRuntimeAdapter) RecoverExpiredSession(ctx context.Context, cookieID string, err error) bool { return a.server.recoverExpiredMTOPSession(ctx, cookieID, err) @@ -263,35 +233,19 @@ func (a serverOrderRuntimeAdapter) IsSessionExpired(err error) bool { return mtop.IsSessionExpiredErr(err) } -// orderApplicationService 承载订单用例的业务编排,不依赖 HTTP 请求或响应对象。 -// HTTP handler 只负责把请求转换为这些类型,再把结果编码为兼容 DTO。 -type orderApplicationService struct { - // server 是订单运行时 Port,名称保留以降低本次迁移的调用面。 - server orderRuntimePort - // repository 提供订单用例所需的应用层持久化与凭证锁 Port。 +// orderHTTPAdapter 将 HTTP 请求模型和兼容响应模型适配到应用层订单服务。 +// 订单业务编排由 internal/application/orders.ServiceSet 负责。 +type orderHTTPAdapter struct { + // services 保存应用层统一构造的订单业务服务集合。 + services *orderapp.ServiceSet + // repository 保存 HTTP 适配器需要的账号归属查询 Port。 repository orderapp.Repository - // list 负责订单列表分页和账号所有权规则。 - list *orderapp.ListService - // detail 负责订单详情读取、商品补全和账号所有权规则。 - detail *orderapp.DetailService - // delete 负责订单删除的所有权校验和逻辑删除。 - delete *orderapp.DeleteService - // update 负责订单更新字段校验和事务写入。 - update *orderapp.UpdateService - // importOrders 负责订单导入的归属校验和事务写入。 - importOrders *orderapp.ImportService - // manualShip 负责手动发货的远端动作、本地状态和补偿规则。 - manualShip *orderapp.ManualShipService - // refresh 负责订单单笔和批量刷新应用编排。 - refresh *orderapp.RefreshService - // refreshJobs 提供订单刷新后台任务的持久化 Port。 - refreshJobs orderapp.RefreshJobRepository } // RefreshSingle 刷新单个订单详情并转换为兼容 HTTP 响应模型。 -func (a *orderApplicationService) RefreshSingle(ctx context.Context, userID int64, orderID string) (orderSingleRefreshResponse, error) { +func (a *orderHTTPAdapter) RefreshSingle(ctx context.Context, userID int64, orderID string) (orderSingleRefreshResponse, error) { // result、err 保存应用层单订单刷新结果和错误。 - result, err := a.refresh.RefreshSingle(ctx, userID, orderID) + result, err := a.services.Refresh.RefreshSingle(ctx, userID, orderID) if errors.Is(err, orderapp.ErrNotFound) { return orderSingleRefreshResponse{}, db.ErrNotFound } @@ -311,9 +265,9 @@ func (a *orderApplicationService) RefreshSingle(ctx context.Context, userID int6 } // Refresh 刷新当前用户订单并转换为兼容 HTTP 响应模型。 -func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, cookieID, status string) (orderRefreshResponse, error) { +func (a *orderHTTPAdapter) Refresh(ctx context.Context, userID int64, cookieID, status string) (orderRefreshResponse, error) { // result、err 保存应用层批量刷新结果和错误。 - result, err := a.refresh.Refresh(ctx, userID, cookieID, status) + result, err := a.services.Refresh.Refresh(ctx, userID, cookieID, status) if errors.Is(err, orderapp.ErrForbidden) { return orderRefreshResponse{}, db.ErrForbidden } @@ -401,12 +355,12 @@ func orderErrorKindOf(err error) (orderErrorKind, bool) { } // orders 返回当前 Server 绑定的订单应用服务。 -func (s *Server) orders() *orderApplicationService { +func (s *Server) orders() *orderHTTPAdapter { return s.applicationServiceSet().orders } // orderOwnedByUser 判断订单服务使用的账号是否归属于当前用户。 -func (a *orderApplicationService) orderOwnedByUser(ctx context.Context, userID int64, cookieID string) bool { +func (a *orderHTTPAdapter) orderOwnedByUser(ctx context.Context, userID int64, cookieID string) bool { // owned 和 err 保存账号归属检查结果。 owned, err := a.repository.ExistsOwned(ctx, userID, cookieID) return err == nil && owned @@ -516,9 +470,9 @@ type orderDetailResult struct { } // List 查询当前用户可见的订单,并集中处理分页和账号所有权规则。 -func (a *orderApplicationService) List(ctx context.Context, query orderListQuery) (orderListResult, error) { +func (a *orderHTTPAdapter) List(ctx context.Context, query orderListQuery) (orderListResult, error) { // result、err 保存应用层订单列表结果及错误。 - result, err := a.list.List(ctx, orderapp.ListQuery{ + result, err := a.services.List.List(ctx, orderapp.ListQuery{ UserID: query.UserID, CookieID: query.CookieID, Status: query.Status, Search: query.Search, Page: query.Page, PageSize: query.PageSize, }) @@ -541,9 +495,9 @@ func (a *orderApplicationService) List(ctx context.Context, query orderListQuery } // Get 查询订单并校验订单绑定账号属于当前用户。 -func (a *orderApplicationService) Get(ctx context.Context, userID int64, orderID string) (*orderapp.Order, error) { +func (a *orderHTTPAdapter) Get(ctx context.Context, userID int64, orderID string) (*orderapp.Order, error) { // order、err 保存应用层订单详情结果及错误。 - order, err := a.detail.Get(ctx, userID, orderID) + order, err := a.services.Detail.Get(ctx, userID, orderID) if errors.Is(err, orderapp.ErrNotFound) { return nil, db.ErrNotFound } @@ -554,9 +508,9 @@ func (a *orderApplicationService) Get(ctx context.Context, userID int64, orderID } // GetView 查询订单并补全商品标题和主图,供详情 handler 直接编码。 -func (a *orderApplicationService) GetView(ctx context.Context, userID int64, orderID string) (orderDetailResult, error) { +func (a *orderHTTPAdapter) GetView(ctx context.Context, userID int64, orderID string) (orderDetailResult, error) { // result、err 保存应用层详情结果及错误。 - result, err := a.detail.GetView(ctx, userID, orderID) + result, err := a.services.Detail.GetView(ctx, userID, orderID) if errors.Is(err, orderapp.ErrNotFound) { return orderDetailResult{}, db.ErrNotFound } @@ -570,9 +524,9 @@ func (a *orderApplicationService) GetView(ctx context.Context, userID int64, ord } // Delete 逻辑删除订单,保留历史记录供审计使用。 -func (a *orderApplicationService) Delete(ctx context.Context, userID int64, orderID string) error { +func (a *orderHTTPAdapter) Delete(ctx context.Context, userID int64, orderID string) error { // err 保存应用层订单删除错误。 - err := a.delete.Delete(ctx, userID, orderID) + err := a.services.Delete.Delete(ctx, userID, orderID) if errors.Is(err, orderapp.ErrForbidden) { return db.ErrForbidden } @@ -615,9 +569,9 @@ type orderUpdateRequest struct { } // Update 在单事务内更新订单及可选的商品标题。 -func (a *orderApplicationService) Update(ctx context.Context, userID int64, orderID string, request orderUpdateRequest) error { +func (a *orderHTTPAdapter) Update(ctx context.Context, userID int64, orderID string, request orderUpdateRequest) error { // err 保存应用层订单更新错误。 - err := a.update.Update(ctx, userID, orderID, orderapp.UpdateRequest{ + err := a.services.Update.Update(ctx, userID, orderID, orderapp.UpdateRequest{ 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, @@ -654,7 +608,7 @@ type orderImportResult struct { } // Import 按当前用户账号所有权逐单导入订单,并为订单关联商品补全基础信息。 -func (a *orderApplicationService) Import(ctx context.Context, userID int64, rawOrders []map[string]any) (orderImportResult, error) { +func (a *orderHTTPAdapter) Import(ctx context.Context, userID int64, rawOrders []map[string]any) (orderImportResult, error) { // inputs 保存文件/HTTP 原始数据转换后的应用导入行。 inputs := make([]orderapp.ImportOrder, 0, len(rawOrders)) // raw 保存当前待转换的原始导入行。 @@ -662,7 +616,7 @@ func (a *orderApplicationService) Import(ctx context.Context, userID int64, rawO inputs = append(inputs, importOrderFromRaw(raw)) } // result、err 保存应用层导入结果和错误。 - result, err := a.importOrders.Import(ctx, userID, inputs) + result, err := a.services.Import.Import(ctx, userID, inputs) if err != nil { return orderImportResult{}, err } @@ -716,9 +670,9 @@ type manualShipResult struct { } // ManualShip 执行状态确认或完整自动化发货,并集中处理逐单失败而不中断批次的规则。 -func (a *orderApplicationService) ManualShip(ctx context.Context, request manualShipRequest) (manualShipResult, error) { +func (a *orderHTTPAdapter) ManualShip(ctx context.Context, request manualShipRequest) (manualShipResult, error) { // result 保存应用层手动发货结果。 - result, err := a.manualShip.ManualShip(ctx, orderapp.ManualShipRequest{UserID: request.UserID, OrderIDs: request.OrderIDs, ShipMode: request.ShipMode}) + result, err := a.services.ManualShip.ManualShip(ctx, orderapp.ManualShipRequest{UserID: request.UserID, OrderIDs: request.OrderIDs, ShipMode: request.ShipMode}) if err != nil { return manualShipResult{}, err }