fix: 完善同步生命周期并统一前端视觉
- 同步商品与订单时处理线上删除状态并联动自动化规则\n- 过滤卖家系统中的购买订单,修复订单逻辑删除与数据库迁移\n- 修复聊天新消息自动滚动,并统一前端颜色令牌与硬编码颜色检查
This commit is contained in:
1 parent
cf76b8898b
commit
526ac44453
40 files changed
+802
-242
No files matched your search
@@ -1006,7 +1006,8 @@ SELECT order_id,item_id,buyer_id,spec_name,spec_value,quantity,amount,order_stat
|
||||
COALESCE(buyer_reviewed_at,''),COALESCE(last_review_request_at,''),review_request_count,
|
||||
created_at,updated_at
|
||||
FROM orders o
|
||||
WHERE o.system_shipped=1
|
||||
WHERE o.system_shipped=1
|
||||
AND o.deleted_at IS NULL
|
||||
AND COALESCE(o.buyer_reviewed_at,'')=''
|
||||
AND COALESCE(o.chat_id,'')<>''
|
||||
AND EXISTS (SELECT 1 FROM automation_rules r
|
||||
|
||||
@@ -29,6 +29,7 @@ func TestMigrate_AppliesCleanSchema(t *testing.T) {
|
||||
{"orders", "system_shipped"},
|
||||
{"orders", "receiver_city"},
|
||||
{"orders", "version"},
|
||||
{"orders", "deleted_at"},
|
||||
{"cards", "image_url"},
|
||||
{"cards", "delay_seconds"},
|
||||
{"keywords", "item_id"},
|
||||
@@ -128,7 +129,7 @@ func TestLatestMigrationsDownUpSQLite(t *testing.T) {
|
||||
}
|
||||
goose.SetBaseFS(migrationsFS)
|
||||
// 依次回滚最新版本到 13,再整体升级。
|
||||
for i := 0; i < 13; i++ {
|
||||
for i := 0; i < 14; i++ {
|
||||
if err := goose.Down(d, "migrations/sqlite"); err != nil {
|
||||
t.Fatalf("down migration #%d: %v", i+1, err)
|
||||
}
|
||||
@@ -151,6 +152,9 @@ func TestLatestMigrationsDownUpSQLite(t *testing.T) {
|
||||
if columnExists(t, d, "item_info", "deleted_at") || columnExists(t, d, "automation_rules", "deleted_at") {
|
||||
t.Fatal("soft-delete columns should be removed after migration 26 down")
|
||||
}
|
||||
if columnExists(t, d, "orders", "deleted_at") {
|
||||
t.Fatal("orders.deleted_at should be removed after migration 27 down")
|
||||
}
|
||||
for _, table := range []string{"account_task_settings", "account_task_runs", "chat_sessions", "chat_messages"} {
|
||||
if tableExists(t, d, table) {
|
||||
t.Fatalf("table should be removed after migration 24 down: %s", table)
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE orders ADD COLUMN deleted_at TIMESTAMP NULL DEFAULT NULL;
|
||||
CREATE INDEX idx_orders_active ON orders(cookie_id, deleted_at);
|
||||
|
||||
-- +goose Down
|
||||
DROP INDEX idx_orders_active ON orders;
|
||||
ALTER TABLE orders DROP COLUMN deleted_at;
|
||||
@@ -0,0 +1,7 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE orders ADD COLUMN deleted_at TIMESTAMP NULL DEFAULT NULL;
|
||||
CREATE INDEX idx_orders_active ON orders(cookie_id, deleted_at);
|
||||
|
||||
-- +goose Down
|
||||
DROP INDEX IF EXISTS idx_orders_active;
|
||||
ALTER TABLE orders DROP COLUMN deleted_at;
|
||||
@@ -0,0 +1,7 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE orders ADD COLUMN deleted_at TIMESTAMP NULL;
|
||||
CREATE INDEX idx_orders_active ON orders(cookie_id, deleted_at);
|
||||
|
||||
-- +goose Down
|
||||
DROP INDEX IF EXISTS idx_orders_active;
|
||||
ALTER TABLE orders DROP COLUMN deleted_at;
|
||||
@@ -771,7 +771,7 @@ func TestMultiDB_LatestMigrationsDownUp(t *testing.T) {
|
||||
t.Fatalf("set goose dialect: %v", err)
|
||||
}
|
||||
goose.SetBaseFS(migrationsFS)
|
||||
for i := 0; i < 13; i++ {
|
||||
for i := 0; i < 14; i++ {
|
||||
if err := goose.Down(tg.store.DB, "migrations/"+subdir); err != nil {
|
||||
t.Fatalf("migration down #%d: %v", i+1, err)
|
||||
}
|
||||
@@ -818,6 +818,7 @@ func TestMultiDB_LatestMigrationsDownUp(t *testing.T) {
|
||||
{"item_publish_batch_rows", "category_json"},
|
||||
{"item_info", "deleted_at"},
|
||||
{"automation_rules", "deleted_at"},
|
||||
{"orders", "deleted_at"},
|
||||
{"account_task_settings", "auto_rate_enabled"},
|
||||
{"account_task_runs", "run_key"},
|
||||
{"chat_sessions", "unread_count"},
|
||||
|
||||
+19
-4
@@ -130,6 +130,18 @@ func (o *Orders) UpsertTx(ctx context.Context, tx *sql.Tx, orderID string, opts
|
||||
return upsertOrder(ctx, tx, o.Dialect, orderID, opts)
|
||||
}
|
||||
|
||||
// SoftDelete 将订单标记为逻辑删除,保留历史数据供审计和后续恢复。
|
||||
func (o *Orders) SoftDelete(ctx context.Context, orderID string) (bool, error) {
|
||||
result, err := o.DB.ExecContext(ctx,
|
||||
`UPDATE orders SET deleted_at=CURRENT_TIMESTAMP, updated_at=CURRENT_TIMESTAMP
|
||||
WHERE order_id=? AND deleted_at IS NULL`, orderID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
changed, err := result.RowsAffected()
|
||||
return changed > 0, err
|
||||
}
|
||||
|
||||
func upsertOrder(ctx context.Context, execer sqlQueryExecer, dialect Dialect, orderID string, opts OrderUpsertOpts) error {
|
||||
if orderID == "" {
|
||||
return errors.New("order_id 不能为空")
|
||||
@@ -152,11 +164,11 @@ func upsertOrder(ctx context.Context, execer sqlQueryExecer, dialect Dialect, or
|
||||
}
|
||||
|
||||
for attempt := 0; attempt < maxOrderUpsertRetries; attempt++ {
|
||||
var existingCookie, existingStatus sql.NullString
|
||||
var existingCookie, existingStatus, deletedAt sql.NullString
|
||||
var version int
|
||||
if err := execer.QueryRowContext(ctx,
|
||||
`SELECT cookie_id,order_status,version FROM orders WHERE order_id=?`, orderID).
|
||||
Scan(&existingCookie, &existingStatus, &version); err != nil {
|
||||
`SELECT cookie_id,order_status,version,deleted_at FROM orders WHERE order_id=?`, orderID).
|
||||
Scan(&existingCookie, &existingStatus, &version, &deletedAt); err != nil {
|
||||
return err
|
||||
}
|
||||
if opts.CookieID != "" && existingCookie.Valid && existingCookie.String != "" && existingCookie.String != opts.CookieID {
|
||||
@@ -168,6 +180,9 @@ func upsertOrder(ctx context.Context, execer sqlQueryExecer, dialect Dialect, or
|
||||
current.OrderStatus = ""
|
||||
}
|
||||
set, args := orderUpsertAssignments(current)
|
||||
if deletedAt.Valid && deletedAt.String != "" {
|
||||
set = append(set, "deleted_at=NULL")
|
||||
}
|
||||
if len(set) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -296,7 +311,7 @@ func (o *Orders) Get(ctx context.Context, orderID string) (*Order, error) {
|
||||
COALESCE(paid_at,''),COALESCE(shipped_at,''),COALESCE(completed_at,''),
|
||||
COALESCE(buyer_reviewed_at,''),COALESCE(last_review_request_at,''),review_request_count,
|
||||
created_at, updated_at
|
||||
FROM orders WHERE order_id=?`, orderID).Scan(
|
||||
FROM orders WHERE order_id=? AND deleted_at IS NULL`, orderID).Scan(
|
||||
&ord.OrderID, &itemID, &buyerID, &specName, &specValue, &qty, &amount,
|
||||
&status, &cookieID, &isBargain, &receiverName, &receiverPhone, &receiverAddr,
|
||||
&receiverCity, &version, &chatID, &sysShipped, &paidAt, &shippedAt, &completedAt,
|
||||
|
||||
@@ -30,6 +30,31 @@ func TestOrdersByCookiePageScansBeyondLegacyLimit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrdersSoftDeleteMissingForCookie(t *testing.T) {
|
||||
s, cleanup := newTestDB(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
_, cid := seedAccount(t, s)
|
||||
for _, orderID := range []string{"seller-order", "buyer-order"} {
|
||||
if err := s.Orders.Upsert(ctx, orderID, OrderUpsertOpts{CookieID: cid, OrderStatus: "pending_ship"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
deleted, err := s.Orders.SoftDeleteMissingForCookie(ctx, cid, map[string]struct{}{"seller-order": {}})
|
||||
if err != nil || deleted != 1 {
|
||||
t.Fatalf("deleted=%d err=%v", deleted, err)
|
||||
}
|
||||
if _, err := s.Orders.Get(ctx, "buyer-order"); !errors.Is(err, ErrNotFound) {
|
||||
t.Fatalf("missing order should be hidden, err=%v", err)
|
||||
}
|
||||
if err := s.Orders.Upsert(ctx, "buyer-order", OrderUpsertOpts{CookieID: cid, OrderStatus: "pending_ship"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Orders.Get(ctx, "buyer-order"); err != nil {
|
||||
t.Fatalf("reappeared order should be restored, err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// seedAccount 在临时库里建好 admin 用户 + 一个账号(cookie),返回 (userID, cookieID)。
|
||||
// 多数订单/自动化/卡券测试都需要这两层外键先就位。
|
||||
func seedAccount(t *testing.T, s *Store) (int64, string) {
|
||||
|
||||
@@ -45,7 +45,7 @@ func (o *Orders) ListForUser(ctx context.Context, f OrderListFilter) ([]OrderRow
|
||||
if f.Limit <= 0 {
|
||||
f.Limit = 20
|
||||
}
|
||||
where := []string{"c.user_id=?"}
|
||||
where := []string{"c.user_id=?", "o.deleted_at IS NULL"}
|
||||
args := []any{f.UserID}
|
||||
if f.CookieID != "" {
|
||||
where = append(where, "o.cookie_id=?")
|
||||
@@ -172,7 +172,7 @@ func (o *Orders) ByCookiePage(ctx context.Context, cookieID string, limit, offse
|
||||
`SELECT order_id, item_id, buyer_id, spec_name, spec_value, quantity, amount,
|
||||
order_status, is_bargain, system_shipped, receiver_name, receiver_phone,
|
||||
receiver_address, receiver_city, created_at, updated_at
|
||||
FROM orders WHERE cookie_id=? ORDER BY created_at DESC,order_id DESC LIMIT ? OFFSET ?`, cookieID, limit, offset)
|
||||
FROM orders WHERE cookie_id=? AND deleted_at IS NULL ORDER BY created_at DESC,order_id DESC LIMIT ? OFFSET ?`, cookieID, limit, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -205,6 +205,54 @@ func (o *Orders) ByCookiePage(ctx context.Context, cookieID string, limit, offse
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// SoftDeleteMissingForCookie 将本次完整卖家订单同步中未出现的本地订单逻辑删除。
|
||||
// activeIDs 为空表示线上已确认没有任何卖家订单;调用方必须确保同步完整成功后再调用。
|
||||
func (o *Orders) SoftDeleteMissingForCookie(ctx context.Context, cookieID string, activeIDs map[string]struct{}) (int, error) {
|
||||
if strings.TrimSpace(cookieID) == "" {
|
||||
return 0, errors.New("cookie_id 不能为空")
|
||||
}
|
||||
rows, err := o.DB.QueryContext(ctx,
|
||||
`SELECT order_id FROM orders WHERE cookie_id=? AND deleted_at IS NULL`, cookieID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
orderIDs := make([]string, 0)
|
||||
for rows.Next() {
|
||||
var orderID string
|
||||
if err := rows.Scan(&orderID); err != nil {
|
||||
_ = rows.Close()
|
||||
return 0, err
|
||||
}
|
||||
orderIDs = append(orderIDs, orderID)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
_ = rows.Close()
|
||||
return 0, err
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
deleted := 0
|
||||
for _, orderID := range orderIDs {
|
||||
if _, ok := activeIDs[orderID]; ok {
|
||||
continue
|
||||
}
|
||||
result, err := o.DB.ExecContext(ctx, `UPDATE orders
|
||||
SET deleted_at=CURRENT_TIMESTAMP, updated_at=CURRENT_TIMESTAMP
|
||||
WHERE cookie_id=? AND order_id=? AND deleted_at IS NULL`, cookieID, orderID)
|
||||
if err != nil {
|
||||
return deleted, err
|
||||
}
|
||||
changed, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return deleted, err
|
||||
}
|
||||
deleted += int(changed)
|
||||
}
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
// OrderStatusMap 将数字状态码转换为文本状态。
|
||||
var OrderStatusMap = map[string]string{
|
||||
"paid": "pending_ship",
|
||||
|
||||
Reference in new issue
Block a user