推进P1应用端口生命周期与发货补偿

This commit is contained in:
Christ committed 2026-08-15 22:44:16 +08:00
1 parent 2cd3c946fc
commit bcdc163306
11 files changed
+534 -120

No files matched your search

@@ -0,0 +1,176 @@
// Package items 提供商品发布相关的应用用例与消费者定义的端口。
package items
import (
"context"
"encoding/json"
"errors"
"strings"
)
// Image 是商品发布所需的图片内容;图片字节只在当前用例生命周期内由端口消费。
type Image struct {
// Filename 是上传到平台时使用的原始文件名。
Filename string
// ContentType 是经过请求校验的 MIME 类型。
ContentType string
// Data 是图片二进制内容,禁止写入日志或响应。
Data []byte
}
// Location 是平台发布所需的发货地信息,不依赖外部平台包的 DTO。
type Location struct {
// Area 是发货地的区县名称。
Area string
// City 是发货地的城市名称。
City string
// DivisionID 是平台行政区划标识。
DivisionID string
// Longitude 是发货地经度。
Longitude float64
// Latitude 是发货地纬度。
Latitude float64
// POIID 是地图服务返回的兴趣点标识。
POIID string
// POIName 是地图服务返回的兴趣点名称。
POIName string
// Province 是发货地的省份名称。
Province string
}
// PublishInput 是单商品发布用例输入,携带已由 HTTP 层验证的业务字段。
type PublishInput struct {
// UserID 是发起发布操作的用户标识。
UserID int64
// CookieID 是执行发布的账号标识。
CookieID string
// Title 是商品标题。
Title string
// Description 是商品描述。
Description string
// PriceCents 是商品售价,单位为分。
PriceCents int64
// OriginalPriceCents 是商品原价,单位为分。
OriginalPriceCents int64
// Quantity 是商品库存数量。
Quantity int
// PostageMode 是邮费模式,例如 free 或 fixed。
PostageMode string
// PostageCents 是邮费金额,单位为分。
PostageCents int64
// Location 是可选的发货地。
Location *Location
// Images 是待上传的商品图片。
Images []Image
}
// PublishResult 是平台返回的商品结果,经过端口转换后不暴露平台 DTO。
type PublishResult struct {
// ItemID 是平台商品标识。
ItemID string
// ItemURL 是平台商品详情地址。
ItemURL string
// Title 是平台最终确认的商品标题。
Title string
// PriceText 是平台格式化后的商品价格。
PriceText string
// CategoryID 是平台商品类目标识。
CategoryID string
// CategoryName 是平台商品类目名称。
CategoryName string
// ImageURL 是平台主图地址。
ImageURL string
// Quantity 是平台确认的库存数量。
Quantity int
// RawData 是平台原始结果的结构化副本,仅供本地详情持久化。
RawData map[string]any
}
// PublishOutcome 是发布端口结果及响应 Cookie 持久化风险。
type PublishOutcome struct {
// Result 是平台返回的商品结果;平台未返回结果时为空。
Result *PublishResult
// ResponseCookieErr 是平台响应 Cookie 未能持久化时的错误。
ResponseCookieErr error
// LocalSaveErr 是本地商品落库失败时的错误,由应用服务补充。
LocalSaveErr error
}
// PublishPort 是商品发布基础设施端口;实现方负责平台会话和凭证更新。
type PublishPort interface {
// Publish 调用外部平台并返回已脱离平台 DTO 的结果。
Publish(context.Context, PublishInput) (PublishOutcome, error)
}
// ItemRecord 是商品发布成功后写入本地商品表的最小模型。
type ItemRecord struct {
// CookieID 是发布所用账号标识。
CookieID string
// ItemID 是平台商品标识。
ItemID string
// ItemTitle 是商品标题。
ItemTitle string
// ItemDescription 是商品描述。
ItemDescription string
// ItemCategory 是平台类目标识。
ItemCategory string
// ItemPrice 是平台格式化后的价格文本。
ItemPrice string
// ItemDetail 是供后续详情页使用的 JSON 字符串。
ItemDetail string
// MultiQuantityDelivery 表示该商品是否启用多库存交付。
MultiQuantityDelivery bool
}
// ItemRepository 是商品本地持久化端口。
type ItemRepository interface {
// Upsert 保存或更新已在平台发布成功的商品。
Upsert(context.Context, ItemRecord) error
}
// Service 是单商品发布应用服务,负责平台结果与本地商品状态的编排。
type Service struct {
// publisher 执行平台发布及响应凭证持久化。
publisher PublishPort
// repository 保存平台成功后的本地商品记录。
repository ItemRepository
}
// NewService 创建单商品发布应用服务,并校验必需端口。
func NewService(publisher PublishPort, repository ItemRepository) (*Service, error) {
if publisher == nil {
return nil, errors.New("商品发布端口不能为空")
}
if repository == nil {
return nil, errors.New("商品仓储端口不能为空")
}
return &Service{publisher: publisher, repository: repository}, nil
}
// PublishSingle 执行发布用例;平台成功但本地保存失败时保留远端结果供 HTTP 层提示补偿。
func (svc *Service) PublishSingle(ctx context.Context, input PublishInput) (PublishOutcome, error) {
// outcome 保存平台端口返回的远端结果;err 保存平台调用或凭证持久化错误。
outcome, err := svc.publisher.Publish(ctx, input)
if err != nil || outcome.Result == nil || strings.TrimSpace(outcome.Result.ItemID) == "" {
return outcome, err
}
// detail 是供商品详情页使用的本地扩展信息;其中 RawData 仅进入受控数据库字段。
detail := map[string]any{
"item_image": outcome.Result.ImageURL,
"web_url": outcome.Result.ItemURL,
"category_name": outcome.Result.CategoryName,
"quantity": outcome.Result.Quantity,
"publish_raw": outcome.Result.RawData,
}
// detailJSON 是商品扩展信息的 JSON 表示;序列化失败不应阻断远端结果回传。
detailJSON, _ := json.Marshal(detail)
// localErr 是平台成功后本地商品落库错误,交由调用方呈现补偿提示。
localErr := svc.repository.Upsert(ctx, ItemRecord{
CookieID: input.CookieID, ItemID: outcome.Result.ItemID, ItemTitle: outcome.Result.Title,
ItemDescription: input.Description, ItemCategory: outcome.Result.CategoryID,
ItemPrice: outcome.Result.PriceText, ItemDetail: string(detailJSON),
MultiQuantityDelivery: input.Quantity > 1,
})
outcome.LocalSaveErr = localErr
return outcome, nil
}
@@ -0,0 +1,89 @@
package items
import (
"context"
"errors"
"testing"
)
// fakePublisher 是可控的平台端口替身,用于验证应用服务不会泄露平台 DTO。
type fakePublisher struct {
// outcome 保存桩平台返回的业务结果。
outcome PublishOutcome
// err 保存桩平台返回的调用错误。
err error
}
// Publish 返回预设的平台结果或错误。
func (p fakePublisher) Publish(_ context.Context, _ PublishInput) (PublishOutcome, error) {
return p.outcome, p.err
}
// fakeItemRepository 是记录最后一次商品写入的本地仓储替身。
type fakeItemRepository struct {
// record 保存最后一次收到的商品记录。
record ItemRecord
// err 保存桩仓储返回的写入错误。
err error
}
// Upsert 保存记录并返回预设错误。
func (r *fakeItemRepository) Upsert(_ context.Context, record ItemRecord) error {
r.record = record
return r.err
}
// TestServicePublishSinglePersistsRemoteResult 验证平台成功后应用层写入本地商品。
func TestServicePublishSinglePersistsRemoteResult(t *testing.T) {
// repository 是记录写入内容的本地仓储桩。
repository := &fakeItemRepository{}
// service 是使用平台和仓储替身构造的应用服务。
service, err := NewService(fakePublisher{outcome: PublishOutcome{Result: &PublishResult{
ItemID: "item-1", ItemURL: "https://example.invalid/item-1", Title: "标题",
PriceText: "12.00", CategoryID: "cat-1", CategoryName: "资料", ImageURL: "image",
Quantity: 2, RawData: map[string]any{"trace": "opaque"},
}}}, repository)
if err != nil {
t.Fatalf("NewService error: %v", err)
}
// outcome 是应用服务返回的远端结果及本地状态。
outcome, err := service.PublishSingle(context.Background(), PublishInput{CookieID: "acc-1", Description: "描述", Quantity: 2})
if err != nil || outcome.Result == nil || outcome.Result.ItemID != "item-1" {
t.Fatalf("unexpected outcome: %+v, err=%v", outcome, err)
}
if repository.record.ItemID != "item-1" || !repository.record.MultiQuantityDelivery {
t.Fatalf("unexpected local record: %+v", repository.record)
}
}
// TestServicePublishSingleKeepsRemoteError 验证平台失败时不写入本地商品。
func TestServicePublishSingleKeepsRemoteError(t *testing.T) {
// repository 是记录是否被调用的本地仓储桩。
repository := &fakeItemRepository{}
// remoteErr 是平台调用失败的代表性错误。
remoteErr := errors.New("platform unavailable")
// service 是配置平台错误桩的应用服务。
service, err := NewService(fakePublisher{err: remoteErr}, repository)
if err != nil {
t.Fatalf("NewService error: %v", err)
}
// outcome 是平台失败后应用服务保留的空结果。
outcome, callErr := service.PublishSingle(context.Background(), PublishInput{})
if !errors.Is(callErr, remoteErr) || outcome.Result != nil || repository.record.ItemID != "" {
t.Fatalf("unexpected failure outcome: %+v, err=%v, record=%+v", outcome, callErr, repository.record)
}
}
// TestNewServiceRejectsMissingPort 验证必需应用端口不能在运行时缺失。
func TestNewServiceRejectsMissingPort(t *testing.T) {
// repository 是满足仓储端口的最小替身。
repository := &fakeItemRepository{}
// err 保存缺少发布端口时的构造错误。
if _, err := NewService(nil, repository); err == nil {
t.Fatal("缺少发布端口时应返回错误")
}
// err 保存缺少仓储端口时的构造错误。
if _, err := NewService(fakePublisher{}, nil); err == nil {
t.Fatal("缺少仓储端口时应返回错误")
}
}