阶段八:完成订单应用服务repository与事务收口
This commit is contained in:
1 parent
f613bb4eb8
commit
6f8356ba4c
5 files changed
+739
-562
No files matched your search
+532
-532
File diff suppressed because it is too large.
Load diff
@@ -614,3 +614,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | 完成阶段 8 第二个 PR 切片“账号任务窄 repository 与凭证门禁收口” | 新增 `automation.AccountTaskRepository`,账号任务协调器只持有账号启停/暂停、任务设置/租约、运行凭证和 Cookie 更新所需的最小接口;自动化中心保留 Store 装配适配器,Session 指纹阻断、续期恢复、任务幂等和 Cookie 同步语义保持不变;Automation 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:通知与应用服务 repository 边界 |
|
||||
| 2026-08-15 | 完成阶段 8 第三个 PR 切片“通知与通信应用服务 repository 边界” | 新增 `notify.Repository`,通知器仅持有渠道、SMTP 系统设置和 outbox 所需的最小接口,保留同步发送、异步租约、重试和测试发送语义;新增通信应用服务窄 repository,账号任务、通知渠道/绑定和聊天历史持久化不再直接访问完整 `db.Store`,实时账号、MTOP 和聊天服务仍由 Server 装配;通知/Server 定向测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:剩余应用服务与事务 repository 边界 |
|
||||
| 2026-08-15 | 完成阶段 8 第四个 PR 切片“分析应用服务只读 repository 边界” | 新增分析服务窄 repository,统一封装 Dashboard 固定统计、订单分析聚合、有效订单分页、卡密库存读取和数据库方言;应用服务不再持有完整 `Server` 或直接访问 `Store.Analytics/Cards`,查询阶段错误、金额表达式和响应聚合语义保持不变;Server 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:订单与发布应用服务 repository 边界 |
|
||||
| 2026-08-15 | 完成阶段 8 第五个 PR 切片“订单应用服务 repository 与事务边界” | 新增 `orderRepository`,订单应用服务的订单/商品读写、用户归属、事务、凭证锁、续期 Cookie 和远端缺失清理均通过窄接口;平台 MTOP、运行时 Cookie 更新、自动化发货和通知编排仍由 Server 负责;订单列表、详情、导入、手动发货、单笔/批量刷新语义保持不变;Server 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:发布应用服务 repository 与 worker 事务边界 |
|
||||
@@ -17,7 +17,7 @@ type applicationServices struct {
|
||||
// newApplicationServices 为指定 Server 装配全部应用服务实例。
|
||||
func newApplicationServices(server *Server) *applicationServices {
|
||||
return &applicationServices{
|
||||
orders: &orderApplicationService{server: server},
|
||||
orders: &orderApplicationService{server: server, repository: newStoreOrderRepository(server.Store)},
|
||||
itemPublish: &itemPublishService{server: server},
|
||||
accountLogin: &accountLoginService{server: server},
|
||||
communication: &communicationService{server: server, repository: newStoreCommunicationRepository(server.Store)},
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
// orderRepository 定义订单应用服务执行订单、商品、归属、事务和凭证锁操作所需的最小能力。
|
||||
type orderRepository interface {
|
||||
// ExistsOwned 判断账号是否归属于用户。
|
||||
ExistsOwned(ctx context.Context, userID int64, cookieID string) (bool, error)
|
||||
// ListOwnedIDs 返回用户拥有的账号 ID。
|
||||
ListOwnedIDs(ctx context.Context, userID int64) ([]string, error)
|
||||
// ListOrdersForUser 查询用户范围内的订单列表。
|
||||
ListOrdersForUser(ctx context.Context, filter db.OrderListFilter) ([]db.OrderRow, int, error)
|
||||
// GetOrder 查询单个订单。
|
||||
GetOrder(ctx context.Context, orderID string) (*db.Order, error)
|
||||
// GetItem 查询账号下的商品信息。
|
||||
GetItem(ctx context.Context, cookieID, itemID string) (*db.ItemInfo, error)
|
||||
// SoftDeleteOrder 逻辑删除订单。
|
||||
SoftDeleteOrder(ctx context.Context, orderID string) (bool, error)
|
||||
// WithTransaction 在一个事务中执行订单用例的持久化操作。
|
||||
WithTransaction(ctx context.Context, work func(*sql.Tx) error) error
|
||||
// PatchOrderTx 在事务内更新订单字段。
|
||||
PatchOrderTx(ctx context.Context, tx *sql.Tx, orderID string, patch db.OrderPatch) error
|
||||
// UpsertItemBasicTx 在事务内写入商品基础信息。
|
||||
UpsertItemBasicTx(ctx context.Context, tx *sql.Tx, item *db.ItemInfoRow) error
|
||||
// UpsertOrderTx 在事务内写入订单。
|
||||
UpsertOrderTx(ctx context.Context, tx *sql.Tx, orderID string, opts db.OrderUpsertOpts) error
|
||||
// UpsertOrder 写入订单。
|
||||
UpsertOrder(ctx context.Context, orderID string, opts db.OrderUpsertOpts) error
|
||||
// LockCredentials 串行化账号凭证状态变更。
|
||||
LockCredentials(cookieID string) func()
|
||||
// LoadCookiePlatformDetail 读取账号平台凭证详情。
|
||||
LoadCookiePlatformDetail(ctx context.Context, cookieID string) (*db.CookieDetail, error)
|
||||
// UpdateRenewalCookie 更新账号续期 Cookie 和 metadata。
|
||||
UpdateRenewalCookie(ctx context.Context, cookieID, value, metadata string, at int64) error
|
||||
// SoftDeleteMissingOrders 删除账号下远端已不存在的订单。
|
||||
SoftDeleteMissingOrders(ctx context.Context, cookieID string, activeIDs map[string]struct{}) (int, error)
|
||||
// ListOrdersByCookiePage 分页读取账号订单。
|
||||
ListOrdersByCookiePage(ctx context.Context, cookieID string, limit, offset int) ([]db.OrderRow, error)
|
||||
}
|
||||
|
||||
// storeOrderRepository 将完整 Store 适配为订单应用服务窄 repository。
|
||||
type storeOrderRepository struct {
|
||||
// store 保存数据库聚合入口,仅在适配器内部使用。
|
||||
store *db.Store
|
||||
}
|
||||
|
||||
// ExistsOwned 委托账号归属查询。
|
||||
func (r storeOrderRepository) ExistsOwned(ctx context.Context, userID int64, cookieID string) (bool, error) {
|
||||
return r.store.Cookies.ExistsOwned(ctx, userID, cookieID)
|
||||
}
|
||||
|
||||
// ListOwnedIDs 委托用户账号列表查询。
|
||||
func (r storeOrderRepository) ListOwnedIDs(ctx context.Context, userID int64) ([]string, error) {
|
||||
return r.store.Cookies.ListOwnedIDs(ctx, userID)
|
||||
}
|
||||
|
||||
// ListOrdersForUser 委托用户订单列表查询。
|
||||
func (r storeOrderRepository) ListOrdersForUser(ctx context.Context, filter db.OrderListFilter) ([]db.OrderRow, int, error) {
|
||||
return r.store.Orders.ListForUser(ctx, filter)
|
||||
}
|
||||
|
||||
// GetOrder 委托订单详情查询。
|
||||
func (r storeOrderRepository) GetOrder(ctx context.Context, orderID string) (*db.Order, error) {
|
||||
return r.store.Orders.Get(ctx, orderID)
|
||||
}
|
||||
|
||||
// GetItem 委托商品信息查询。
|
||||
func (r storeOrderRepository) GetItem(ctx context.Context, cookieID, itemID string) (*db.ItemInfo, error) {
|
||||
return r.store.Items.Get(ctx, cookieID, itemID)
|
||||
}
|
||||
|
||||
// SoftDeleteOrder 委托订单逻辑删除。
|
||||
func (r storeOrderRepository) SoftDeleteOrder(ctx context.Context, orderID string) (bool, error) {
|
||||
return r.store.Orders.SoftDelete(ctx, orderID)
|
||||
}
|
||||
|
||||
// WithTransaction 创建、提交或回滚订单事务。
|
||||
func (r storeOrderRepository) WithTransaction(ctx context.Context, work func(*sql.Tx) error) error {
|
||||
// tx 和 err 保存事务创建结果。
|
||||
tx, err := r.store.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// committed 标识事务是否已经提交成功。
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
// err 保存事务工作函数的执行错误。
|
||||
if err := work(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
// err 保存事务提交错误。
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
committed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// PatchOrderTx 委托事务内订单更新。
|
||||
func (r storeOrderRepository) PatchOrderTx(ctx context.Context, tx *sql.Tx, orderID string, patch db.OrderPatch) error {
|
||||
return r.store.Orders.PatchTx(ctx, tx, orderID, patch)
|
||||
}
|
||||
|
||||
// UpsertItemBasicTx 委托事务内商品基础信息写入。
|
||||
func (r storeOrderRepository) UpsertItemBasicTx(ctx context.Context, tx *sql.Tx, item *db.ItemInfoRow) error {
|
||||
return r.store.Items.UpsertBasicTx(ctx, tx, item)
|
||||
}
|
||||
|
||||
// UpsertOrderTx 委托事务内订单写入。
|
||||
func (r storeOrderRepository) UpsertOrderTx(ctx context.Context, tx *sql.Tx, orderID string, opts db.OrderUpsertOpts) error {
|
||||
return r.store.Orders.UpsertTx(ctx, tx, orderID, opts)
|
||||
}
|
||||
|
||||
// UpsertOrder 委托订单写入。
|
||||
func (r storeOrderRepository) UpsertOrder(ctx context.Context, orderID string, opts db.OrderUpsertOpts) error {
|
||||
return r.store.Orders.Upsert(ctx, orderID, opts)
|
||||
}
|
||||
|
||||
// LockCredentials 委托账号凭证锁。
|
||||
func (r storeOrderRepository) LockCredentials(cookieID string) func() {
|
||||
return r.store.LockAccountCredentials(cookieID)
|
||||
}
|
||||
|
||||
// LoadCookiePlatformDetail 委托平台凭证详情查询。
|
||||
func (r storeOrderRepository) LoadCookiePlatformDetail(ctx context.Context, cookieID string) (*db.CookieDetail, error) {
|
||||
// data 和 err 保存平台运行视图查询结果。
|
||||
data, err := r.store.Cookies.GetCookiePlatformRuntimeData(ctx, cookieID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &db.CookieDetail{ID: data.ID, UserID: data.UserID, Value: data.Value, MetadataJSON: data.MetadataJSON, ShowBrowser: data.ShowBrowser}, nil
|
||||
}
|
||||
|
||||
// UpdateRenewalCookie 委托续期 Cookie 更新。
|
||||
func (r storeOrderRepository) UpdateRenewalCookie(ctx context.Context, cookieID, value, metadata string, at int64) error {
|
||||
return r.store.Cookies.UpdateRenewalCookie(ctx, cookieID, value, metadata, at)
|
||||
}
|
||||
|
||||
// SoftDeleteMissingOrders 委托账号远端缺失订单清理。
|
||||
func (r storeOrderRepository) SoftDeleteMissingOrders(ctx context.Context, cookieID string, activeIDs map[string]struct{}) (int, error) {
|
||||
return r.store.Orders.SoftDeleteMissingForCookie(ctx, cookieID, activeIDs)
|
||||
}
|
||||
|
||||
// ListOrdersByCookiePage 委托账号订单分页查询。
|
||||
func (r storeOrderRepository) ListOrdersByCookiePage(ctx context.Context, cookieID string, limit, offset int) ([]db.OrderRow, error) {
|
||||
return r.store.Orders.ByCookiePage(ctx, cookieID, limit, offset)
|
||||
}
|
||||
|
||||
// newStoreOrderRepository 从完整 Store 构造订单应用服务窄 repository。
|
||||
func newStoreOrderRepository(store *db.Store) orderRepository {
|
||||
if store == nil || store.Cookies == nil || store.Orders == nil || store.Items == nil {
|
||||
return nil
|
||||
}
|
||||
return storeOrderRepository{store: store}
|
||||
}
|
||||
|
||||
// 确保 Store 适配器始终覆盖订单应用服务所需的全部能力。
|
||||
var _ orderRepository = storeOrderRepository{}
|
||||
@@ -18,6 +18,8 @@ import (
|
||||
type orderApplicationService struct {
|
||||
// server 提供订单服务访问数据库、平台客户端和运行时依赖。
|
||||
server *Server
|
||||
// repository 提供订单用例所需的最小持久化与凭证锁能力。
|
||||
repository orderRepository
|
||||
}
|
||||
|
||||
var errOrderDetailUnsupported = errors.New("当前 Go MTOP 客户端不支持订单详情接口")
|
||||
@@ -64,6 +66,13 @@ func (s *Server) orders() *orderApplicationService {
|
||||
return s.applicationServiceSet().orders
|
||||
}
|
||||
|
||||
// orderOwnedByUser 判断订单服务使用的账号是否归属于当前用户。
|
||||
func (a *orderApplicationService) orderOwnedByUser(ctx context.Context, userID int64, cookieID string) bool {
|
||||
// owned 和 err 保存账号归属检查结果。
|
||||
owned, err := a.repository.ExistsOwned(ctx, userID, cookieID)
|
||||
return err == nil && owned
|
||||
}
|
||||
|
||||
// orderListQuery 描述订单列表的业务查询条件。
|
||||
type orderListQuery struct {
|
||||
// UserID 是当前登录用户标识。
|
||||
@@ -145,11 +154,11 @@ func (a *orderApplicationService) List(ctx context.Context, query orderListQuery
|
||||
if query.PageSize > 200 {
|
||||
query.PageSize = 200
|
||||
}
|
||||
if query.CookieID != "" && !a.server.cookieOwnedByUser(ctx, query.UserID, query.CookieID) {
|
||||
if query.CookieID != "" && !a.orderOwnedByUser(ctx, query.UserID, query.CookieID) {
|
||||
return orderListResult{}, db.ErrForbidden
|
||||
}
|
||||
offset := (query.Page - 1) * query.PageSize
|
||||
rows, total, err := a.server.Store.Orders.ListForUser(ctx, db.OrderListFilter{
|
||||
rows, total, err := a.repository.ListOrdersForUser(ctx, db.OrderListFilter{
|
||||
UserID: query.UserID, CookieID: query.CookieID, Status: query.Status,
|
||||
Search: query.Search, Limit: query.PageSize, Offset: offset,
|
||||
})
|
||||
@@ -168,7 +177,7 @@ func (a *orderApplicationService) List(ctx context.Context, query orderListQuery
|
||||
|
||||
// Get 查询订单并校验订单绑定账号属于当前用户。
|
||||
func (a *orderApplicationService) Get(ctx context.Context, userID int64, orderID string) (*db.Order, error) {
|
||||
order, err := a.server.Store.Orders.Get(ctx, orderID)
|
||||
order, err := a.repository.GetOrder(ctx, orderID)
|
||||
if err != nil || order == nil {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -178,7 +187,7 @@ func (a *orderApplicationService) Get(ctx context.Context, userID int64, orderID
|
||||
if strings.TrimSpace(order.CookieID) == "" {
|
||||
return nil, db.ErrForbidden
|
||||
}
|
||||
if !a.server.cookieOwnedByUser(ctx, userID, order.CookieID) {
|
||||
if !a.orderOwnedByUser(ctx, userID, order.CookieID) {
|
||||
return nil, db.ErrForbidden
|
||||
}
|
||||
return order, nil
|
||||
@@ -190,7 +199,7 @@ func (a *orderApplicationService) GetView(ctx context.Context, userID int64, ord
|
||||
if err != nil {
|
||||
return orderDetailResult{}, err
|
||||
}
|
||||
item, itemErr := a.server.Store.Items.Get(ctx, order.CookieID, order.ItemID)
|
||||
item, itemErr := a.repository.GetItem(ctx, order.CookieID, order.ItemID)
|
||||
if itemErr != nil {
|
||||
item = nil
|
||||
}
|
||||
@@ -202,7 +211,7 @@ func (a *orderApplicationService) Delete(ctx context.Context, userID int64, orde
|
||||
if _, err := a.Get(ctx, userID, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := a.server.Store.Orders.SoftDelete(ctx, orderID)
|
||||
_, err := a.repository.SoftDeleteOrder(ctx, orderID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -270,8 +279,8 @@ func (a *orderApplicationService) Update(ctx context.Context, userID int64, orde
|
||||
return newOrderBadRequest("商品标题不能为空且订单必须关联商品")
|
||||
}
|
||||
}
|
||||
return a.server.withTransaction(ctx, func(tx *sql.Tx) error {
|
||||
if err := a.server.Store.Orders.PatchTx(ctx, tx, orderID, db.OrderPatch{
|
||||
return a.repository.WithTransaction(ctx, func(tx *sql.Tx) error {
|
||||
if err := a.repository.PatchOrderTx(ctx, tx, orderID, db.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,
|
||||
@@ -281,7 +290,7 @@ func (a *orderApplicationService) Update(ctx context.Context, userID int64, orde
|
||||
return err
|
||||
}
|
||||
if request.ItemTitle != nil {
|
||||
if err := a.server.Store.Items.UpsertBasicTx(ctx, tx, &db.ItemInfoRow{
|
||||
if err := a.repository.UpsertItemBasicTx(ctx, tx, &db.ItemInfoRow{
|
||||
CookieID: order.CookieID, ItemID: finalItemID, ItemTitle: itemTitle,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("更新商品标题失败: %w", err)
|
||||
@@ -305,7 +314,7 @@ type orderImportResult struct {
|
||||
|
||||
// Import 按当前用户账号所有权逐单导入订单,并为订单关联商品补全基础信息。
|
||||
func (a *orderApplicationService) Import(ctx context.Context, userID int64, rawOrders []map[string]any) (orderImportResult, error) {
|
||||
ownedCookieIDs, err := a.server.Store.Cookies.ListOwnedIDs(ctx, userID)
|
||||
ownedCookieIDs, err := a.repository.ListOwnedIDs(ctx, userID)
|
||||
if err != nil {
|
||||
return orderImportResult{}, err
|
||||
}
|
||||
@@ -353,8 +362,8 @@ func (a *orderApplicationService) importOne(ctx context.Context, ownedCookieIDs
|
||||
if !ok {
|
||||
return errors.New("订单金额必须是普通格式的非负有限数字")
|
||||
}
|
||||
if err := a.server.withTransaction(ctx, func(tx *sql.Tx) error {
|
||||
if err := a.server.Store.Orders.UpsertTx(ctx, tx, orderID, db.OrderUpsertOpts{
|
||||
if err := a.repository.WithTransaction(ctx, func(tx *sql.Tx) error {
|
||||
if err := a.repository.UpsertOrderTx(ctx, tx, orderID, db.OrderUpsertOpts{
|
||||
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"),
|
||||
@@ -366,7 +375,7 @@ func (a *orderApplicationService) importOne(ctx context.Context, ownedCookieIDs
|
||||
// itemID 是导入订单中关联的商品标识。
|
||||
itemID := firstImportString(raw, "item_id")
|
||||
if itemID != "" {
|
||||
if err := a.server.Store.Items.UpsertBasicTx(ctx, tx, &db.ItemInfoRow{
|
||||
if err := a.repository.UpsertItemBasicTx(ctx, tx, &db.ItemInfoRow{
|
||||
CookieID: cookieID, ItemID: itemID, ItemTitle: firstImportString(raw, "item_title"),
|
||||
ItemPrice: firstImportString(raw, "item_price"), ItemDetail: firstImportString(raw, "item_detail", "item_description"),
|
||||
}); err != nil {
|
||||
@@ -411,7 +420,7 @@ type manualShipResult struct {
|
||||
|
||||
// ManualShip 执行状态确认或完整自动化发货,并集中处理逐单失败而不中断批次的规则。
|
||||
func (a *orderApplicationService) ManualShip(ctx context.Context, request manualShipRequest) (manualShipResult, error) {
|
||||
ownedCookieIDs, err := a.server.Store.Cookies.ListOwnedIDs(ctx, request.UserID)
|
||||
ownedCookieIDs, err := a.repository.ListOwnedIDs(ctx, request.UserID)
|
||||
if err != nil {
|
||||
return manualShipResult{}, err
|
||||
}
|
||||
@@ -421,7 +430,7 @@ func (a *orderApplicationService) ManualShip(ctx context.Context, request manual
|
||||
if orderID == "" {
|
||||
continue
|
||||
}
|
||||
order, getErr := a.server.Store.Orders.Get(ctx, orderID)
|
||||
order, getErr := a.repository.GetOrder(ctx, orderID)
|
||||
if getErr != nil || order == nil {
|
||||
a.appendManualFailure(&result, orderID, "订单不存在")
|
||||
continue
|
||||
@@ -495,7 +504,7 @@ func (a *orderApplicationService) manualStatusShip(ctx context.Context, userID i
|
||||
return
|
||||
}
|
||||
sysShip := true
|
||||
upsertErr := a.server.Store.Orders.Upsert(ctx, orderID, db.OrderUpsertOpts{
|
||||
upsertErr := a.repository.UpsertOrder(ctx, orderID, db.OrderUpsertOpts{
|
||||
CookieID: order.CookieID, OrderStatus: "shipped", SystemShipped: &sysShip, ItemID: order.ItemID,
|
||||
BuyerID: order.BuyerID, ReceiverName: order.ReceiverName, ReceiverPhone: order.ReceiverPhone,
|
||||
ReceiverAddr: order.ReceiverAddr, ReceiverCity: order.ReceiverCity, ChatID: order.ChatID,
|
||||
@@ -526,14 +535,14 @@ func (a *orderApplicationService) RefreshSingle(ctx context.Context, userID int6
|
||||
return orderSingleRefreshResponse{}, errOrderDetailUnsupported
|
||||
}
|
||||
cookieID := order.CookieID
|
||||
credentialUnlock := a.server.Store.LockAccountCredentials(cookieID)
|
||||
credentialUnlock := a.repository.LockCredentials(cookieID)
|
||||
credentialLocked := true
|
||||
defer func() {
|
||||
if credentialLocked {
|
||||
credentialUnlock()
|
||||
}
|
||||
}()
|
||||
latest, err := a.server.loadCookiePlatformDetail(ctx, cookieID)
|
||||
latest, err := a.repository.LoadCookiePlatformDetail(ctx, cookieID)
|
||||
if err != nil || latest == nil || latest.UserID != userID || !hasStoredCookieCredential(latest) {
|
||||
return orderSingleRefreshResponse{}, errOrderCredentialChanged
|
||||
}
|
||||
@@ -553,7 +562,7 @@ func (a *orderApplicationService) RefreshSingle(ctx context.Context, userID int6
|
||||
runtimeCookie, runtimeCookieChanged = value, true
|
||||
} else if !handled && callErr == nil && detail.UpdatedCookies != "" && detail.UpdatedCookies != latest.Value {
|
||||
metadata := cookierefresh.MetadataWithoutSnapshot(latest.MetadataJSON)
|
||||
if saveErr := a.server.Store.Cookies.UpdateRenewalCookie(ctx, cookieID, detail.UpdatedCookies, metadata, time.Now().Unix()); saveErr == nil {
|
||||
if saveErr := a.repository.UpdateRenewalCookie(ctx, cookieID, detail.UpdatedCookies, metadata, time.Now().Unix()); saveErr == nil {
|
||||
runtimeCookie, runtimeCookieChanged = detail.UpdatedCookies, true
|
||||
}
|
||||
}
|
||||
@@ -570,19 +579,19 @@ func (a *orderApplicationService) RefreshSingle(ctx context.Context, userID int6
|
||||
if !validEditableOrderStatus(status) {
|
||||
status = db.NormalizeOrderStatus(order.OrderStatus)
|
||||
}
|
||||
if err := a.server.Store.Orders.Upsert(ctx, orderID, db.OrderUpsertOpts{CookieID: cookieID, OrderStatus: status, SpecName: detail.SpecName, SpecValue: detail.SpecValue, Quantity: detail.Quantity, Amount: detail.Amount}); err != nil {
|
||||
if err := a.repository.UpsertOrder(ctx, orderID, db.OrderUpsertOpts{CookieID: cookieID, OrderStatus: status, SpecName: detail.SpecName, SpecValue: detail.SpecValue, Quantity: detail.Quantity, Amount: detail.Amount}); err != nil {
|
||||
return orderSingleRefreshResponse{}, err
|
||||
}
|
||||
return orderSingleRefreshResponse{Success: true, Message: "订单刷新完成", Order: orderRefreshDetailResponse{Quantity: detail.Quantity, SpecName: detail.SpecName, SpecValue: detail.SpecValue, OrderStatus: db.NormalizeOrderStatus(detail.OrderStatus), Amount: detail.Amount}}, nil
|
||||
}
|
||||
func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, cookieID, status string) (orderRefreshResponse, error) {
|
||||
|
||||
cookieIDs, err := a.server.Store.Cookies.ListOwnedIDs(ctx, userID)
|
||||
cookieIDs, err := a.repository.ListOwnedIDs(ctx, userID)
|
||||
if err != nil {
|
||||
return orderRefreshResponse{}, err
|
||||
}
|
||||
if cookieID != "" {
|
||||
if !a.server.cookieOwnedByUser(ctx, userID, cookieID) {
|
||||
if !a.orderOwnedByUser(ctx, userID, cookieID) {
|
||||
return orderRefreshResponse{}, db.ErrForbidden
|
||||
}
|
||||
cookieIDs = []string{cookieID}
|
||||
@@ -594,8 +603,8 @@ func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, coo
|
||||
sessionExpiredAccounts := make(map[string]struct{})
|
||||
if fetcher, ok := a.server.mtopClient().(mtop.SoldOrderFetcher); ok {
|
||||
for _, cid := range cookieIDs { // cid 是当前待刷新的账号 ID。
|
||||
credentialUnlock := a.server.Store.LockAccountCredentials(cid)
|
||||
latest, latestErr := a.server.loadCookiePlatformDetail(ctx, cid)
|
||||
credentialUnlock := a.repository.LockCredentials(cid)
|
||||
latest, latestErr := a.repository.LoadCookiePlatformDetail(ctx, cid)
|
||||
if latestErr != nil || latest == nil || latest.UserID != userID || !hasStoredCookieCredential(latest) {
|
||||
credentialUnlock()
|
||||
if latestErr == nil {
|
||||
@@ -634,7 +643,7 @@ func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, coo
|
||||
"discovered": accountDiscovered, "updated": accountUpdated,
|
||||
}
|
||||
if discoveryErr == nil {
|
||||
deleted, deleteErr := a.server.Store.Orders.SoftDeleteMissingForCookie(ctx, cid, accountRemoteIDs)
|
||||
deleted, deleteErr := a.repository.SoftDeleteMissingOrders(ctx, cid, accountRemoteIDs)
|
||||
if deleteErr != nil {
|
||||
discoveryErr = fmt.Errorf("标记缺失订单失败: %w", deleteErr)
|
||||
result["success"] = false
|
||||
@@ -660,7 +669,7 @@ func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, coo
|
||||
continue
|
||||
}
|
||||
for offset := 0; ; offset += 500 {
|
||||
rows, err := a.server.Store.Orders.ByCookiePage(ctx, cid, 500, offset)
|
||||
rows, err := a.repository.ListOrdersByCookiePage(ctx, cid, 500, offset)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
@@ -721,8 +730,8 @@ func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, coo
|
||||
for cid, targets := range ordersByCookie {
|
||||
accountSessionExpired := false
|
||||
for _, chunk := range chunkRefreshTargets(targets, refreshOrderChunkSize) {
|
||||
credentialUnlock := a.server.Store.LockAccountCredentials(cid)
|
||||
latest, latestErr := a.server.loadCookiePlatformDetail(ctx, cid)
|
||||
credentialUnlock := a.repository.LockCredentials(cid)
|
||||
latest, latestErr := a.repository.LoadCookiePlatformDetail(ctx, cid)
|
||||
if latestErr != nil || latest == nil || latest.UserID != userID || !hasStoredCookieCredential(latest) {
|
||||
credentialUnlock()
|
||||
failed += len(chunk)
|
||||
@@ -755,7 +764,7 @@ func (a *orderApplicationService) Refresh(ctx context.Context, userID int64, coo
|
||||
if !validEditableOrderStatus(newStatus) {
|
||||
newStatus = target.CurrentStatus
|
||||
}
|
||||
err := a.server.Store.Orders.Upsert(ctx, target.OrderID, db.OrderUpsertOpts{
|
||||
err := a.repository.UpsertOrder(ctx, target.OrderID, db.OrderUpsertOpts{
|
||||
CookieID: cid,
|
||||
OrderStatus: newStatus,
|
||||
SpecName: detail.SpecName,
|
||||
|
||||
Reference in new issue
Block a user