迁移订单导入应用服务
This commit is contained in:
1 parent
607d5a4003
commit
0d0ab2aabb
5 files changed
+370
-87
No files matched your search
@@ -752,3 +752,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | P1 订单详情应用服务迁移切片 | 已完成 | 新增 `internal/application/orders.DetailService`、`DetailResult` 和窄 `DetailRepository`;订单详情读取、账号所有权校验和商品补全从 `internal/server/order_service.go` 迁入应用层,Server 仅负责兼容错误映射与 DTO 转换;新增跨用户、不存在订单、商品读取失败保留订单主体测试,订单/Server 回归、架构门禁和中文注释门禁通过。订单更新、导入、手动发货、刷新等用例仍在 Server,阶段 4/8/9 保持“进行中” |
|
||||
| 2026-08-15 | P1 订单更新应用服务迁移切片 | 已完成 | 新增 `internal/application/orders.UpdateService`、`UpdateRequest`、`UpdateRepository` 和 `ValidationError`;订单所有权、状态/金额归一化、商品标题约束及订单+商品事务写入从 `internal/server/order_service.go` 迁入纯应用层,Server 仅负责 HTTP DTO 转换和旧错误分类映射;新增更新成功、非法字段、无权/不存在/存储错误测试,订单/应用定向测试、架构门禁和中文注释门禁通过。订单删除、导入、手动发货、刷新以及其他领域应用服务仍未迁移,阶段 4/8/9 保持“进行中” |
|
||||
| 2026-08-15 | P1 订单删除应用服务迁移切片 | 已完成 | 新增 `internal/application/orders.DeleteService` 和窄 `DeleteRepository`;订单存在性、账号所有权和逻辑删除从 `internal/server/order_service.go` 迁入纯应用层,Server 仅负责兼容错误映射;新增成功删除、无权/不存在、读取/删除存储错误测试,订单/应用定向测试、架构门禁和中文注释门禁通过。订单导入、手动发货、刷新以及其他领域应用服务仍未迁移,阶段 4/8/9 保持“进行中” |
|
||||
| 2026-08-15 | P1 订单导入应用服务迁移切片 | 已完成 | 新增 `internal/application/orders.ImportService`、具名 `ImportOrder/ImportResult` 和窄 `ImportRepository`;动态文件/HTTP 行只在 Server 边界转换,账号归属、状态/金额校验、逐条结果和订单+商品事务写入迁入纯应用层;新增成功导入、混合失败、事务/商品写入失败测试,导入 HTTP 回归、全量 Go 测试、race、架构门禁和中文注释门禁通过。手动发货、刷新以及其他领域应用服务仍未迁移,阶段 4/8/9 保持“进行中” |
|
||||
@@ -0,0 +1,199 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ImportOrder 描述已经由文件/HTTP 适配层解析的订单导入行。
|
||||
type ImportOrder struct {
|
||||
// OrderID 是订单业务标识。
|
||||
OrderID string
|
||||
// CookieID 是订单所属账号标识。
|
||||
CookieID string
|
||||
// ItemID 是关联商品标识。
|
||||
ItemID string
|
||||
// ItemTitle 是商品标题。
|
||||
ItemTitle string
|
||||
// ItemPrice 是商品价格文本。
|
||||
ItemPrice string
|
||||
// ItemDetail 是商品详情 JSON 或描述文本。
|
||||
ItemDetail string
|
||||
// BuyerID 是买家标识。
|
||||
BuyerID 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
|
||||
}
|
||||
|
||||
// ImportItemResult 描述单条订单导入结果。
|
||||
type ImportItemResult struct {
|
||||
// OrderID 是导入结果对应的订单标识。
|
||||
OrderID string
|
||||
// Success 表示该订单是否导入成功。
|
||||
Success bool
|
||||
// Message 是成功或失败说明。
|
||||
Message string
|
||||
}
|
||||
|
||||
// ImportResult 描述批量订单导入统计及逐单结果。
|
||||
type ImportResult struct {
|
||||
// Total 是本次导入订单总数。
|
||||
Total int
|
||||
// SuccessCount 是成功导入数量。
|
||||
SuccessCount int
|
||||
// FailedCount 是失败导入数量。
|
||||
FailedCount int
|
||||
// Results 是逐订单导入结果。
|
||||
Results []ImportItemResult
|
||||
}
|
||||
|
||||
// ImportRepository 定义订单导入用例所需的最小持久化能力。
|
||||
type ImportRepository interface {
|
||||
// ListOwnedIDs 返回用户拥有的账号标识。
|
||||
ListOwnedIDs(ctx context.Context, userID int64) ([]string, error)
|
||||
// WithTransaction 在单条订单导入中执行原子写入。
|
||||
WithTransaction(ctx context.Context, work func(Writer) error) error
|
||||
}
|
||||
|
||||
// ImportService 承载订单导入的归属、字段校验和事务写入规则。
|
||||
type ImportService struct {
|
||||
// repository 保存订单导入用例所需的窄数据访问 Port。
|
||||
repository ImportRepository
|
||||
}
|
||||
|
||||
// NewImportService 创建订单导入应用服务。
|
||||
func NewImportService(repository ImportRepository) *ImportService {
|
||||
return &ImportService{repository: repository}
|
||||
}
|
||||
|
||||
// Import 按当前用户账号所有权逐单导入订单,并补全关联商品基础信息。
|
||||
func (s *ImportService) Import(ctx context.Context, userID int64, inputs []ImportOrder) (ImportResult, error) {
|
||||
if s == nil || s.repository == nil {
|
||||
return ImportResult{}, errors.New("订单导入 repository 未初始化")
|
||||
}
|
||||
// ownedCookieIDs 保存当前用户可操作的账号标识。
|
||||
ownedCookieIDs, err := s.repository.ListOwnedIDs(ctx, userID)
|
||||
if err != nil {
|
||||
return ImportResult{}, err
|
||||
}
|
||||
// defaultCookieID 保存仅有一个账号时使用的默认账号标识。
|
||||
defaultCookieID := ""
|
||||
if len(ownedCookieIDs) == 1 {
|
||||
defaultCookieID = ownedCookieIDs[0]
|
||||
}
|
||||
// result 保存批量导入统计和逐条结果。
|
||||
result := ImportResult{Total: len(inputs), Results: make([]ImportItemResult, 0, len(inputs))}
|
||||
// input 保存当前处理的订单导入行。
|
||||
for _, input := range inputs {
|
||||
// err 保存当前订单导入错误。
|
||||
if err := s.importOne(ctx, ownedCookieIDs, defaultCookieID, input); err != nil {
|
||||
result.FailedCount++
|
||||
result.Results = append(result.Results, ImportItemResult{OrderID: importResultOrderID(input), Message: err.Error()})
|
||||
continue
|
||||
}
|
||||
result.SuccessCount++
|
||||
result.Results = append(result.Results, ImportItemResult{OrderID: input.OrderID, Success: true, Message: "订单已导入"})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// importOne 在独立事务中写入一条订单及其商品信息。
|
||||
func (s *ImportService) importOne(ctx context.Context, ownedCookieIDs []string, defaultCookieID string, input ImportOrder) error {
|
||||
// orderID 保存规范化后的订单标识。
|
||||
orderID := strings.TrimSpace(input.OrderID)
|
||||
if orderID == "" {
|
||||
return errors.New("缺少必需字段: order_id")
|
||||
}
|
||||
// cookieID 保存解析后的订单所属账号标识。
|
||||
cookieID := strings.TrimSpace(input.CookieID)
|
||||
if cookieID == "" {
|
||||
cookieID = defaultCookieID
|
||||
}
|
||||
if cookieID == "" {
|
||||
return errors.New("缺少必需字段: cookie_id")
|
||||
}
|
||||
if !containsOwnedCookieID(ownedCookieIDs, cookieID) {
|
||||
return errors.New("无权操作此账号的订单")
|
||||
}
|
||||
// status 保存规范化后的订单状态。
|
||||
status := strings.TrimSpace(input.OrderStatus)
|
||||
if status != "" {
|
||||
status = NormalizeOrderStatus(status)
|
||||
if !ValidEditableOrderStatus(status) {
|
||||
return errors.New("不支持的订单状态")
|
||||
}
|
||||
}
|
||||
// amount 保存规范化后的订单金额。
|
||||
amount, ok := NormalizeOrderAmount(input.Amount)
|
||||
if !ok {
|
||||
return errors.New("订单金额必须是普通格式的非负有限数字")
|
||||
}
|
||||
// err 保存订单事务执行错误。
|
||||
if err := s.repository.WithTransaction(ctx, func(writer Writer) error {
|
||||
// err 保存订单主体写入错误。
|
||||
if err := writer.UpsertOrder(ctx, orderID, UpsertOptions{
|
||||
CookieID: cookieID, ItemID: input.ItemID, BuyerID: input.BuyerID,
|
||||
OrderStatus: status, SpecName: input.SpecName, SpecValue: input.SpecValue,
|
||||
Quantity: input.Quantity, Amount: amount, ReceiverName: input.ReceiverName,
|
||||
ReceiverPhone: input.ReceiverPhone, ReceiverAddress: input.ReceiverAddress,
|
||||
ReceiverCity: input.ReceiverCity, ChatID: input.ChatID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if input.ItemID == "" {
|
||||
return nil
|
||||
}
|
||||
// err 保存商品基础信息写入错误。
|
||||
if err := writer.UpsertItemBasic(ctx, ItemWrite{
|
||||
CookieID: cookieID, ItemID: input.ItemID, ItemTitle: input.ItemTitle,
|
||||
ItemPrice: input.ItemPrice, ItemDetail: input.ItemDetail,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("补全商品信息失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return errors.New("订单导入事务失败: " + err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// containsOwnedCookieID 判断账号标识是否在当前用户的授权集合中。
|
||||
func containsOwnedCookieID(cookieIDs []string, target string) bool {
|
||||
// cookieID 表示当前遍历的账号标识。
|
||||
for _, cookieID := range cookieIDs {
|
||||
if cookieID == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// importResultOrderID 返回结果行使用的稳定订单标识。
|
||||
func importResultOrderID(input ImportOrder) string {
|
||||
// orderID 保存导入结果使用的订单标识。
|
||||
orderID := strings.TrimSpace(input.OrderID)
|
||||
if orderID == "" {
|
||||
return "unknown"
|
||||
}
|
||||
return orderID
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// importRepositoryFake 是订单导入应用服务测试使用的内存 repository。
|
||||
type importRepositoryFake struct {
|
||||
// ownedIDs 保存测试用户拥有的账号标识。
|
||||
ownedIDs []string
|
||||
// listErr 保存账号列表查询错误。
|
||||
listErr error
|
||||
// txErr 保存事务创建或提交错误。
|
||||
txErr error
|
||||
// itemErr 保存商品写入错误。
|
||||
itemErr error
|
||||
// writer 保存最近一次事务写入器。
|
||||
writer *importWriterFake
|
||||
}
|
||||
|
||||
// ListOwnedIDs 返回测试用户拥有的账号标识。
|
||||
func (f *importRepositoryFake) ListOwnedIDs(context.Context, int64) ([]string, error) {
|
||||
return f.ownedIDs, f.listErr
|
||||
}
|
||||
|
||||
// WithTransaction 执行测试事务回调并记录写入。
|
||||
func (f *importRepositoryFake) WithTransaction(ctx context.Context, work func(Writer) error) error {
|
||||
if f.txErr != nil {
|
||||
return f.txErr
|
||||
}
|
||||
// writer 保存当前事务使用的测试写入器。
|
||||
f.writer = &importWriterFake{itemErr: f.itemErr}
|
||||
return work(f.writer)
|
||||
}
|
||||
|
||||
// importWriterFake 是订单导入应用服务测试使用的事务写入器。
|
||||
type importWriterFake struct {
|
||||
// orderID 保存写入订单标识。
|
||||
orderID string
|
||||
// options 保存订单写入字段。
|
||||
options UpsertOptions
|
||||
// item 保存商品基础信息写入字段。
|
||||
item ItemWrite
|
||||
// orderErr 保存订单写入错误。
|
||||
orderErr error
|
||||
// itemErr 保存商品写入错误。
|
||||
itemErr error
|
||||
}
|
||||
|
||||
// UpsertOrder 记录测试订单写入。
|
||||
func (f *importWriterFake) UpsertOrder(_ context.Context, orderID string, options UpsertOptions) error {
|
||||
f.orderID = orderID
|
||||
f.options = options
|
||||
return f.orderErr
|
||||
}
|
||||
|
||||
// UpsertItemBasic 记录测试商品写入。
|
||||
func (f *importWriterFake) UpsertItemBasic(_ context.Context, item ItemWrite) error {
|
||||
f.item = item
|
||||
return f.itemErr
|
||||
}
|
||||
|
||||
// PatchOrder 满足订单事务写入器接口的测试兼容方法。
|
||||
func (f *importWriterFake) PatchOrder(context.Context, string, OrderPatch) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestImportServiceWritesOrderAndItem 验证默认账号、状态和金额归一化及事务写入。
|
||||
func TestImportServiceWritesOrderAndItem(t *testing.T) {
|
||||
// repository 保存本用例使用的内存依赖。
|
||||
repository := &importRepositoryFake{ownedIDs: []string{"cookie-1"}}
|
||||
// service 保存待测试的订单导入服务。
|
||||
service := NewImportService(repository)
|
||||
// result、err 保存订单导入结果和错误。
|
||||
result, err := service.Import(context.Background(), 7, []ImportOrder{{
|
||||
OrderID: " order-1 ", ItemID: "item-1", ItemTitle: "商品", ItemPrice: "19.9", ItemDetail: "{}",
|
||||
OrderStatus: "2", Amount: "¥1,234.50", Quantity: "2",
|
||||
}})
|
||||
if err != nil || result.SuccessCount != 1 || result.FailedCount != 0 {
|
||||
t.Fatalf("导入结果异常: result=%+v err=%v", result, err)
|
||||
}
|
||||
if repository.writer == nil || repository.writer.orderID != "order-1" || repository.writer.options.OrderStatus != "pending_ship" || repository.writer.options.Amount != "1234.50" {
|
||||
t.Fatalf("订单写入异常: %+v", repository.writer)
|
||||
}
|
||||
if repository.writer.item.ItemID != "item-1" || repository.writer.item.ItemTitle != "商品" {
|
||||
t.Fatalf("商品写入异常: %+v", repository.writer.item)
|
||||
}
|
||||
}
|
||||
|
||||
// TestImportServiceReportsRowFailures 验证单条字段和所有权错误只影响当前结果行。
|
||||
func TestImportServiceReportsRowFailures(t *testing.T) {
|
||||
// repository 保存本用例使用的内存依赖。
|
||||
repository := &importRepositoryFake{ownedIDs: []string{"cookie-1", "cookie-2"}}
|
||||
// service 保存待测试的订单导入服务。
|
||||
service := NewImportService(repository)
|
||||
// result、err 保存混合导入结果和错误。
|
||||
result, err := service.Import(context.Background(), 7, []ImportOrder{
|
||||
{OrderID: "valid", CookieID: "cookie-1"},
|
||||
{OrderID: "bad-amount", CookieID: "cookie-1", Amount: "1e3"},
|
||||
{OrderID: "bad-status", CookieID: "cookie-1", OrderStatus: "unknown"},
|
||||
{OrderID: "forbidden", CookieID: "other"},
|
||||
{CookieID: "cookie-1"},
|
||||
})
|
||||
if err != nil || result.SuccessCount != 1 || result.FailedCount != 4 || len(result.Results) != 5 {
|
||||
t.Fatalf("逐条结果异常: result=%+v err=%v", result, err)
|
||||
}
|
||||
if result.Results[0].OrderID != "valid" || !result.Results[0].Success {
|
||||
t.Fatalf("成功结果异常: %+v", result.Results[0])
|
||||
}
|
||||
if result.Results[4].OrderID != "unknown" || result.Results[4].Success {
|
||||
t.Fatalf("缺少订单号结果异常: %+v", result.Results[4])
|
||||
}
|
||||
}
|
||||
|
||||
// TestImportServiceWrapsTransactionErrors 验证订单和商品事务错误保留兼容上下文。
|
||||
func TestImportServiceWrapsTransactionErrors(t *testing.T) {
|
||||
// txErr 保存事务失败原因。
|
||||
txErr := errors.New("事务失败")
|
||||
// txService 保存事务失败场景的应用服务。
|
||||
txService := NewImportService(&importRepositoryFake{ownedIDs: []string{"cookie-1"}, txErr: txErr})
|
||||
// result、err 保存事务失败导入结果和错误。
|
||||
result, err := txService.Import(context.Background(), 7, []ImportOrder{{OrderID: "order-1", CookieID: "cookie-1"}})
|
||||
if err != nil || result.FailedCount != 1 || !strings.Contains(result.Results[0].Message, "订单导入事务失败") {
|
||||
t.Fatalf("事务失败结果异常: result=%+v err=%v", result, err)
|
||||
}
|
||||
// itemRepository 保存商品写入失败场景的内存依赖。
|
||||
itemRepository := &importRepositoryFake{ownedIDs: []string{"cookie-1"}, itemErr: errors.New("商品写入失败")}
|
||||
// itemService 保存商品写入失败场景的应用服务。
|
||||
itemService := NewImportService(itemRepository)
|
||||
// result、err 保存商品写入失败的逐条结果和错误。
|
||||
result, err = itemService.Import(context.Background(), 7, []ImportOrder{{OrderID: "order-1", CookieID: "cookie-1", ItemID: "item-1"}})
|
||||
if err != nil || result.FailedCount != 1 || !strings.Contains(result.Results[0].Message, "补全商品信息失败") {
|
||||
t.Fatalf("商品失败场景不应中断批次: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ func newApplicationServices(server *Server) *applicationServices {
|
||||
// orderRepository 保存订单应用服务共享的基础设施适配器。
|
||||
orderRepository := newStoreOrderRepository(server.Store)
|
||||
return &applicationServices{
|
||||
orders: &orderApplicationService{server: newServerOrderRuntime(server), repository: orderRepository, list: orderapp.NewListService(orderRepository), detail: orderapp.NewDetailService(orderRepository), delete: orderapp.NewDeleteService(orderRepository), update: orderapp.NewUpdateService(orderRepository), refreshJobs: newStoreOrderRefreshJobRepository(server.Store)},
|
||||
orders: &orderApplicationService{server: newServerOrderRuntime(server), repository: orderRepository, list: orderapp.NewListService(orderRepository), detail: orderapp.NewDetailService(orderRepository), delete: orderapp.NewDeleteService(orderRepository), update: orderapp.NewUpdateService(orderRepository), importOrders: orderapp.NewImportService(orderRepository), refreshJobs: newStoreOrderRefreshJobRepository(server.Store)},
|
||||
itemPublish: &itemPublishService{server: server, repository: newStoreItemPublishRepository(server.Store)},
|
||||
accountLogin: &accountLoginService{server: server, repository: newStoreAccountLoginRepository(server.Store)},
|
||||
communication: &communicationService{server: server, repository: newStoreCommunicationRepository(server.Store)},
|
||||
|
||||
@@ -154,6 +154,8 @@ type orderApplicationService struct {
|
||||
delete *orderapp.DeleteService
|
||||
// update 负责订单更新字段校验和事务写入。
|
||||
update *orderapp.UpdateService
|
||||
// importOrders 负责订单导入的归属校验和事务写入。
|
||||
importOrders *orderapp.ImportService
|
||||
// refreshJobs 提供订单刷新后台任务的持久化 Port。
|
||||
refreshJobs orderapp.RefreshJobRepository
|
||||
}
|
||||
@@ -468,101 +470,44 @@ type orderImportResult struct {
|
||||
|
||||
// Import 按当前用户账号所有权逐单导入订单,并为订单关联商品补全基础信息。
|
||||
func (a *orderApplicationService) Import(ctx context.Context, userID int64, rawOrders []map[string]any) (orderImportResult, error) {
|
||||
// ownedCookieIDs、err 保存owned登录凭证IDs、err,供当前处理流程使用
|
||||
ownedCookieIDs, err := a.repository.ListOwnedIDs(ctx, userID)
|
||||
// inputs 保存文件/HTTP 原始数据转换后的应用导入行。
|
||||
inputs := make([]orderapp.ImportOrder, 0, len(rawOrders))
|
||||
// raw 保存当前待转换的原始导入行。
|
||||
for _, raw := range rawOrders {
|
||||
inputs = append(inputs, importOrderFromRaw(raw))
|
||||
}
|
||||
// result、err 保存应用层导入结果和错误。
|
||||
result, err := a.importOrders.Import(ctx, userID, inputs)
|
||||
if err != nil {
|
||||
return orderImportResult{}, err
|
||||
}
|
||||
// defaultCookieID 保存default登录凭证ID,供当前处理流程使用
|
||||
defaultCookieID := ""
|
||||
if len(ownedCookieIDs) == 1 {
|
||||
defaultCookieID = ownedCookieIDs[0]
|
||||
}
|
||||
// result 保存结果,供当前处理流程使用
|
||||
result := orderImportResult{Total: len(rawOrders), Results: make([]map[string]any, 0, len(rawOrders))}
|
||||
// raw 表示当前遍历过程中的原始
|
||||
for _, raw := range rawOrders {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := a.importOne(ctx, ownedCookieIDs, defaultCookieID, raw, &result); err != nil {
|
||||
result.FailedCount++
|
||||
result.Results = append(result.Results, errResult(raw, err.Error()))
|
||||
continue
|
||||
}
|
||||
result.SuccessCount++
|
||||
result.Results = append(result.Results, map[string]any{"order_id": firstImportString(raw, "order_id"), "success": true, "message": "订单已导入"})
|
||||
}
|
||||
return result, nil
|
||||
return orderImportResultFromApplication(result), nil
|
||||
}
|
||||
|
||||
// importOne 在独立事务中写入一条订单及其商品信息。
|
||||
func (a *orderApplicationService) importOne(ctx context.Context, ownedCookieIDs []string, defaultCookieID string, raw map[string]any, result *orderImportResult) error {
|
||||
// orderID 保存订单ID,供当前处理流程使用
|
||||
orderID := firstImportString(raw, "order_id")
|
||||
if orderID == "" {
|
||||
return errors.New("缺少必需字段: order_id")
|
||||
// importOrderFromRaw 将文件/HTTP 适配层的动态字段转换为应用层订单导入命令。
|
||||
func importOrderFromRaw(raw map[string]any) orderapp.ImportOrder {
|
||||
return orderapp.ImportOrder{
|
||||
OrderID: firstImportString(raw, "order_id"), CookieID: firstImportString(raw, "cookie_id"),
|
||||
ItemID: firstImportString(raw, "item_id"), ItemTitle: firstImportString(raw, "item_title"),
|
||||
ItemPrice: firstImportString(raw, "item_price"), ItemDetail: firstImportString(raw, "item_detail", "item_description"),
|
||||
BuyerID: firstImportString(raw, "buyer_id"), OrderStatus: firstImportString(raw, "order_status", "status", "status_text"),
|
||||
SpecName: firstImportString(raw, "spec_name"), SpecValue: firstImportString(raw, "spec_value"),
|
||||
Quantity: firstImportString(raw, "quantity"), Amount: firstImportString(raw, "amount"),
|
||||
ReceiverName: firstImportString(raw, "receiver_name"), ReceiverPhone: firstImportString(raw, "receiver_phone"),
|
||||
ReceiverAddress: firstImportString(raw, "receiver_address"), ReceiverCity: firstImportString(raw, "receiver_city"),
|
||||
ChatID: firstImportString(raw, "chat_id"),
|
||||
}
|
||||
// cookieID 保存登录凭证ID,供当前处理流程使用
|
||||
cookieID := firstImportString(raw, "cookie_id")
|
||||
if cookieID == "" {
|
||||
cookieID = defaultCookieID
|
||||
}
|
||||
if cookieID == "" {
|
||||
return errors.New("缺少必需字段: cookie_id")
|
||||
}
|
||||
if !containsCookieID(ownedCookieIDs, cookieID) {
|
||||
return errors.New("无权操作此账号的订单")
|
||||
}
|
||||
// status 保存状态,供当前处理流程使用
|
||||
status := firstImportString(raw, "order_status", "status", "status_text")
|
||||
if status != "" {
|
||||
status = db.NormalizeOrderStatus(status)
|
||||
if !validEditableOrderStatus(status) {
|
||||
return errors.New("不支持的订单状态")
|
||||
}
|
||||
}
|
||||
// amount、ok 保存amount、ok,供当前处理流程使用
|
||||
amount, ok := normalizeOrderAmount(firstImportString(raw, "amount"))
|
||||
if !ok {
|
||||
return errors.New("订单金额必须是普通格式的非负有限数字")
|
||||
}
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := a.repository.WithTransaction(ctx, func(writer orderapp.Writer) error {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
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"), ReceiverAddress: firstImportString(raw, "receiver_address"),
|
||||
ReceiverCity: firstImportString(raw, "receiver_city"), ChatID: firstImportString(raw, "chat_id"),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
// itemID 是导入订单中关联的商品标识。
|
||||
itemID := firstImportString(raw, "item_id")
|
||||
if itemID != "" {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
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 {
|
||||
return fmt.Errorf("补全商品信息失败: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return errors.New("订单导入事务失败: " + err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// errResult 生成导入失败的兼容结果行。
|
||||
func errResult(raw map[string]any, message string) map[string]any {
|
||||
// orderID 保存订单ID,供当前处理流程使用
|
||||
orderID := firstImportString(raw, "order_id")
|
||||
if orderID == "" {
|
||||
orderID = "unknown"
|
||||
// orderImportResultFromApplication 将应用层导入结果转换为旧 HTTP 响应兼容模型。
|
||||
func orderImportResultFromApplication(result orderapp.ImportResult) orderImportResult {
|
||||
// results 保存兼容客户端使用的逐条动态结果。
|
||||
results := make([]map[string]any, 0, len(result.Results))
|
||||
// item 保存当前应用层导入结果行。
|
||||
for _, item := range result.Results {
|
||||
results = append(results, map[string]any{"order_id": item.OrderID, "success": item.Success, "message": item.Message})
|
||||
}
|
||||
return map[string]any{"order_id": orderID, "success": false, "message": message}
|
||||
return orderImportResult{Total: result.Total, SuccessCount: result.SuccessCount, FailedCount: result.FailedCount, Results: results}
|
||||
}
|
||||
|
||||
// manualShipRequest 描述批量手动发货请求。
|
||||
|
||||
Reference in new issue
Block a user