From 81855f2cabe66bcc0f227e48a99f1e5a549ee4f4 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Tue, 3 Nov 2026 03:46:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(order):=20=E4=BF=AE=E5=A4=8D=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E8=A1=A8=E5=AD=98=E5=9C=A8=E6=97=A0=E7=B4=A2=E5=BC=95?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E7=9A=84=E8=AE=A2=E5=8D=95=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E3=80=8C=E5=88=97=E8=A1=A8=E5=8F=AF=E8=A7=81=E4=BD=86=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E6=8F=90=E7=A4=BA=E9=9D=9E=E6=9C=AC=E4=B8=BB=E4=BD=93?= =?UTF-8?q?=E3=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现象:异常/委外中/配送/已作废/已取消/历史等视图里能看到订单, 点「查看」却报「存在 1 条订单不存在或不属于当前主体」。 根因不是主体过滤缺失,而是数据不一致: 列表按状态表查询,详情按 order_index 校验主体; 历史迁移留下 25 条「状态表有行、order_index 无记录」的脏数据 (异常 8 / 委外中 5 / 配送 3 / 已作废 1 / 已取消 1 / 历史 7), 于是列表看得到、详情必然失败,报错文案又让人以为是跨主体。 改动: - orderStatus.list:加 EXISTS(order_index) 条件,只返回索引表存在且主体一致的订单, 杜绝同类脏数据再次「列表可见、点不开」 - 新增 scripts/repair-order-index(npm run order-index:repair):扫描各状态表补写缺失的 索引记录(支持 --dry-run),已执行修复上述 25 条 验证:7 个视图列表与详情全部一致(pending 105 / exception 8 / outsourcing 5 / delivery 3 / void 1 / canceled 1 / history 7,详情失败均为 0);principal=6 各视图均为 0 条; 人为插入孤儿行后列表不再显示该行。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- backend/app/service/orderStatus.js | 15 ++++-- backend/package.json | 1 + backend/scripts/repair-order-index.js | 72 +++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 backend/scripts/repair-order-index.js diff --git a/backend/app/service/orderStatus.js b/backend/app/service/orderStatus.js index d7e097a..60a5848 100644 --- a/backend/app/service/orderStatus.js +++ b/backend/app/service/orderStatus.js @@ -350,15 +350,24 @@ class OrderStatusService extends Service { } } + // 只列索引表里存在的订单:详情按 order_index 校验主体, + // 若状态表存在没有索引记录的历史脏数据,列表会看得到却点不开,这里直接挡掉 + where[Op.and] = [ + ctx.app.Sequelize.literal( + 'EXISTS (SELECT 1 FROM order_index oi WHERE oi.id = `' + Model.tableName + '`.id ' + + `AND oi.principal_id = ${principalId})` + ), + ]; + // exclude_unbound=1:排除「有未绑定系统商品的明细」的订单(订单页默认只看已解析订单) // 用 EXISTS 子查询而非 IN 列表,避免未绑定订单很多时拼接超长参数 if (String(p.exclude_unbound) === '1') { - where[Op.and] = [ + where[Op.and].push( ctx.app.Sequelize.literal( 'NOT EXISTS (SELECT 1 FROM order_status_items oi WHERE oi.order_id = `' + Model.tableName + '`.id ' + `AND oi.principal_id = ${principalId} AND oi.parse_status = 2)` - ), - ]; + ) + ); } const { count, rows } = await Model.findAndCountAll({ diff --git a/backend/package.json b/backend/package.json index c62f6cb..89905f5 100644 --- a/backend/package.json +++ b/backend/package.json @@ -18,6 +18,7 @@ "migration:create": "sequelize-cli migration:generate --name", "swagger:export": "node scripts/export-swagger.js", "order-items:rebuild": "node scripts/rebuild-order-items.js", + "order-index:repair": "node scripts/repair-order-index.js", "deploy": "npm run deploy:prod", "deploy:prod": "npm --prefix ../tools run deploy --", "deploy:backend": "npm --prefix ../tools run deploy:backend --", diff --git a/backend/scripts/repair-order-index.js b/backend/scripts/repair-order-index.js new file mode 100644 index 0000000..b51d0cc --- /dev/null +++ b/backend/scripts/repair-order-index.js @@ -0,0 +1,72 @@ +'use strict'; + +/** + * 修复「状态表有行、索引表(order_index)没有对应记录」的订单。 + * + * 背景:订单 ID 以 order_index 为准,列表按状态表查询、详情按索引表校验主体, + * 一旦某个状态表里存在没有索引记录的行,就会出现 + * 「列表能看到该订单,点详情却提示不存在或不属于当前主体」的现象。 + * + * 用法: + * node scripts/repair-order-index.js --dry-run # 只统计 + * node scripts/repair-order-index.js # 按状态表补写索引记录 + */ +const mm = require('egg-mock'); +const { ORDER_TABLE_BY_STATUS } = require('../app/lib/orderStatus'); + +(async () => { + const dryRun = process.argv.includes('--dry-run'); + const app = mm.app(); + await app.ready(); + const ctx = app.mockContext(); + + const summary = []; + for (const [ status, table ] of Object.entries(ORDER_TABLE_BY_STATUS)) { + const rows = await ctx.model.query( + `SELECT s.id, s.order_no, s.parent_order_no, s.principal_id, s.created_at + FROM \`${table}\` s LEFT JOIN order_index i ON i.id = s.id + WHERE i.id IS NULL`, + { type: app.Sequelize.QueryTypes.SELECT } + ); + summary.push({ status, table, orphans: rows.length, rows }); + } + + const total = summary.reduce((sum, item) => sum + item.orphans, 0); + for (const item of summary) { + console.log(`${item.table.padEnd(26)} 缺索引 ${item.orphans} 行`); + } + console.log(`合计缺索引 ${total} 行`); + + if (!total) { + await app.close(); + process.exit(0); + } + if (dryRun) { + console.log('[dry-run] 未写库'); + await app.close(); + process.exit(0); + } + + const now = new Date(); + for (const item of summary) { + if (!item.orphans) continue; + const payload = item.rows.map(row => ({ + id: row.id, + order_no: row.order_no, + parent_order_no: row.parent_order_no, + principal_id: row.principal_id, + current_table: item.status, + current_status: item.status, + created_at: row.created_at || now, + updated_at: now, + })); + await ctx.model.OrderIndex.bulkCreate(payload); + console.log(`已补写 ${item.table} 的 ${payload.length} 条索引记录(状态=${item.status})`); + } + + await app.close(); + process.exit(0); +})().catch(err => { + console.error('修复失败:', err); + process.exit(1); +});