下沉订单应用服务集合装配

This commit is contained in:
Christ committed 2026-08-15 18:59:18 +08:00
1 parent 90c3b4e76a
commit eff4143fd6
7 files changed
+95 -87

No files matched your search

+5 -3
View File
@@ -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)},
@@ -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("应用服务访问器未返回统一装配实例")
}
+11 -11
View File
@@ -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)
}
+26 -72
View File
@@ -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
}