迁移订单更新应用服务
This commit is contained in:
1 parent
67a12ff53a
commit
b1e62b7bef
6 files changed
+436
-84
No files matched your search
@@ -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), refreshJobs: newStoreOrderRefreshJobRepository(server.Store)},
|
||||
orders: &orderApplicationService{server: newServerOrderRuntime(server), repository: orderRepository, list: orderapp.NewListService(orderRepository), detail: orderapp.NewDetailService(orderRepository), update: orderapp.NewUpdateService(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)},
|
||||
|
||||
@@ -6,10 +6,10 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
orderapp "xianyu-go/internal/application/orders"
|
||||
"xianyu-go/internal/auth"
|
||||
"xianyu-go/internal/db"
|
||||
"xianyu-go/internal/xianyu/mtop"
|
||||
@@ -290,20 +290,11 @@ func (s *Server) updateOrder(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
// status 保存状态,供当前处理流程使用
|
||||
// status 保存兼容字段 order_status/status 合并后的状态值。
|
||||
status := req.OrderStatus
|
||||
if status == nil {
|
||||
status = req.Status
|
||||
}
|
||||
if status != nil {
|
||||
// normalized 保存normalized,供当前处理流程使用
|
||||
normalized := db.NormalizeOrderStatus(strings.TrimSpace(*status))
|
||||
if !validEditableOrderStatus(normalized) {
|
||||
writeErr(w, http.StatusBadRequest, "不支持的订单状态")
|
||||
return
|
||||
}
|
||||
status = &normalized
|
||||
}
|
||||
// stringPtrFromAny 保存stringPtrFromAny,供当前处理流程使用
|
||||
stringPtrFromAny := func(value *any) *string {
|
||||
if value == nil {
|
||||
@@ -313,17 +304,8 @@ func (s *Server) updateOrder(w http.ResponseWriter, r *http.Request) {
|
||||
v := stringFromAny(*value)
|
||||
return &v
|
||||
}
|
||||
// amount 保存amount,供当前处理流程使用
|
||||
// amount 保存兼容 JSON 数值转换后的订单金额。
|
||||
amount := stringPtrFromAny(req.Amount)
|
||||
if amount != nil {
|
||||
// normalized、ok 保存normalized、ok,供当前处理流程使用
|
||||
normalized, ok := normalizeOrderAmount(*amount)
|
||||
if !ok {
|
||||
writeErr(w, http.StatusBadRequest, "订单金额必须是普通格式的非负有限数字")
|
||||
return
|
||||
}
|
||||
amount = &normalized
|
||||
}
|
||||
// sess 保存sess,供当前处理流程使用
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
if // err 保存err,供当前处理流程使用
|
||||
@@ -357,17 +339,12 @@ func validOrderAmount(raw string) bool {
|
||||
|
||||
// normalizeOrderAmount 负责normalize订单Amount相关处理。
|
||||
func normalizeOrderAmount(raw string) (string, bool) {
|
||||
return db.NormalizeOrderAmount(raw)
|
||||
return orderapp.NormalizeOrderAmount(raw)
|
||||
}
|
||||
|
||||
// validEditableOrderStatus 负责有效Editable订单状态相关处理。
|
||||
func validEditableOrderStatus(status string) bool {
|
||||
switch status {
|
||||
case "processing", "pending_ship", "shipped", "completed", "cancelled", "refunding":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return orderapp.ValidEditableOrderStatus(status)
|
||||
}
|
||||
|
||||
// manualShipOrders 负责manualShip订单列表相关处理。
|
||||
|
||||
@@ -150,6 +150,8 @@ type orderApplicationService struct {
|
||||
list *orderapp.ListService
|
||||
// detail 负责订单详情读取、商品补全和账号所有权规则。
|
||||
detail *orderapp.DetailService
|
||||
// update 负责订单更新字段校验和事务写入。
|
||||
update *orderapp.UpdateService
|
||||
// refreshJobs 提供订单刷新后台任务的持久化 Port。
|
||||
refreshJobs orderapp.RefreshJobRepository
|
||||
}
|
||||
@@ -423,62 +425,29 @@ type orderUpdateRequest struct {
|
||||
|
||||
// Update 在单事务内更新订单及可选的商品标题。
|
||||
func (a *orderApplicationService) Update(ctx context.Context, userID int64, orderID string, request orderUpdateRequest) error {
|
||||
// order、err 保存order、err,供当前处理流程使用
|
||||
order, err := a.Get(ctx, userID, orderID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if request.OrderStatus != nil {
|
||||
// normalized 保存normalized,供当前处理流程使用
|
||||
normalized := db.NormalizeOrderStatus(strings.TrimSpace(*request.OrderStatus))
|
||||
if !validEditableOrderStatus(normalized) {
|
||||
return newOrderBadRequest("不支持的订单状态")
|
||||
}
|
||||
request.OrderStatus = &normalized
|
||||
}
|
||||
if request.Amount != nil {
|
||||
// normalized、ok 保存normalized、ok,供当前处理流程使用
|
||||
normalized, ok := normalizeOrderAmount(*request.Amount)
|
||||
if !ok {
|
||||
return newOrderBadRequest("订单金额必须是普通格式的非负有限数字")
|
||||
}
|
||||
request.Amount = &normalized
|
||||
}
|
||||
// finalItemID 保存final商品ID,供当前处理流程使用
|
||||
finalItemID := strings.TrimSpace(order.ItemID)
|
||||
if request.ItemID != nil {
|
||||
finalItemID = strings.TrimSpace(*request.ItemID)
|
||||
request.ItemID = &finalItemID
|
||||
}
|
||||
// itemTitle 保存商品标题,供当前处理流程使用
|
||||
itemTitle := ""
|
||||
if request.ItemTitle != nil {
|
||||
itemTitle = strings.TrimSpace(*request.ItemTitle)
|
||||
if itemTitle == "" || finalItemID == "" {
|
||||
return newOrderBadRequest("商品标题不能为空且订单必须关联商品")
|
||||
}
|
||||
}
|
||||
return a.repository.WithTransaction(ctx, func(writer orderapp.Writer) error {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := writer.PatchOrder(ctx, orderID, orderapp.OrderPatch{
|
||||
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,
|
||||
ReceiverAddress: request.ReceiverAddress, ReceiverCity: request.ReceiverCity, ChatID: request.ChatID,
|
||||
SystemShipped: request.SystemShipped,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if request.ItemTitle != nil {
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := writer.UpsertItemBasic(ctx, orderapp.ItemWrite{
|
||||
CookieID: order.CookieID, ItemID: finalItemID, ItemTitle: itemTitle,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("更新商品标题失败: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
// err 保存应用层订单更新错误。
|
||||
err := a.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,
|
||||
ReceiverAddress: request.ReceiverAddress, ReceiverCity: request.ReceiverCity, ChatID: request.ChatID,
|
||||
SystemShipped: request.SystemShipped, ItemTitle: request.ItemTitle,
|
||||
})
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
// validationErr 保存应用层字段校验错误。
|
||||
var validationErr *orderapp.ValidationError
|
||||
if errors.As(err, &validationErr) {
|
||||
return newOrderBadRequest(validationErr.Error())
|
||||
}
|
||||
if errors.Is(err, orderapp.ErrForbidden) {
|
||||
return db.ErrForbidden
|
||||
}
|
||||
if errors.Is(err, orderapp.ErrNotFound) {
|
||||
return db.ErrNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// orderImportResult 描述批量导入的逐单结果和统计。
|
||||
|
||||
Reference in new issue
Block a user