From cf0d98422420bb4d4959e271a00acfb44c2b4923 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Sat, 7 Nov 2026 07:57:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(shop-product):=20=E3=80=8C=E5=BE=85?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E3=80=8D=E5=8F=AA=E7=BB=9F=E8=AE=A1=E5=BE=85?= =?UTF-8?q?=E5=AE=A1=E6=A0=B8/=E5=BC=82=E5=B8=B8=E7=9A=84=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=EF=BC=8C=E9=81=BF=E5=85=8D=E7=BB=91=E5=AE=9A=E5=90=8E?= =?UTF-8?q?=E6=95=B0=E5=AD=97=E6=B8=85=E4=B8=8D=E6=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现象:店铺商品已绑定系统商品,列表仍显示「N 单待绑定」。 原因:「待处理订单数」把 order_status_items 里所有 parse_status=2 的订单都算进去了, 但绑定只对「待审核 / 异常」的订单重建明细(syncOrderItemsOf → rebuildOrderItems 的状态白名单), 已流转(待推送/委外中/配送/历史/作废)的明细不会跟着变 → 数字永远清不掉。 - attachUnboundOrderCount:只统计 current_status ∈ (pending, exception) 的订单 - 「有订单未绑定」筛选(列表 + 导出)同口径,保证筛选结果与徽标一致 - 抽出 editableOrderIdsSql 复用 本地验证:店铺商品 59 的未绑定单从 8 → 7(其中 1 单已作废);其余不变。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- backend/app/controller/shopProduct.js | 25 +++++++++++++++++++++++-- backend/app/utils/exportDefs.js | 8 ++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/backend/app/controller/shopProduct.js b/backend/app/controller/shopProduct.js index 24ad840..0ea78fe 100644 --- a/backend/app/controller/shopProduct.js +++ b/backend/app/controller/shopProduct.js @@ -101,13 +101,14 @@ class ShopProductController extends Controller { } // 有订单未绑定:店铺商品自身未绑定,且被「未绑定系统商品」的订单明细引用 + // (只算待审核/异常的单:已流转的单不会因绑定而重建,算进来会让筛选结果与徽标一直清不掉) const onlyUnboundOrder = String(hasUnboundOrder) === '1'; if (onlyUnboundOrder) { where.bind_type = 0; where.id = { [Op.in]: ctx.app.Sequelize.literal( `(SELECT DISTINCT shop_product_id FROM order_status_items WHERE principal_id = ${principalId} ` + - 'AND parse_status = 2 AND shop_product_id IS NOT NULL)' + `AND parse_status = 2 AND shop_product_id IS NOT NULL AND order_id IN ${this.editableOrderIdsSql(principalId)})` ), }; } @@ -147,6 +148,20 @@ class ShopProductController extends Controller { return access.ownCompanyIds.map(Number).includes(Number(item.subsidiary_id)); } + /** + * 「还能通过绑定解决的未绑定明细」口径 + * + * 只有待审核 / 异常的单在绑定时会重建明细(见 syncOrderItemsOf → rebuildOrderItems), + * 已流转(待推送/委外中/配送/历史…)的单不会跟着变,若把它们也算进来, + * 绑定后数字永远清不掉,看起来像「绑定了还是待绑定」。 + * @param {number} principalId 主体ID + * @return {string} SQL 子查询(order_status_items.order_id 限定在这些订单上) + */ + editableOrderIdsSql(principalId) { + const statuses = EDITABLE_ORDER_STATUSES.map(status => `'${status}'`).join(', '); + return `(SELECT id FROM order_index WHERE principal_id = ${Number(principalId)} AND current_status IN (${statuses}))`; + } + /** * 回填每个店铺商品的「待处理订单数」(未绑定系统商品的订单明细按订单去重计数), * 便于列表上直接看出哪些商品还有订单等绑定 @@ -161,7 +176,13 @@ class ShopProductController extends Controller { const Op = this.app.Sequelize.Op; const rows = await ctx.model.OrderStatusItem.findAll({ - where: { principal_id: principalId, parse_status: 2, shop_product_id: { [Op.in]: ids } }, + where: { + principal_id: principalId, + parse_status: 2, + shop_product_id: { [Op.in]: ids }, + // 只算「绑定后能自动补上」的单(待审核 / 异常) + order_id: { [Op.in]: ctx.app.Sequelize.literal(this.editableOrderIdsSql(principalId)) }, + }, attributes: [ 'shop_product_id', [ ctx.app.Sequelize.fn('COUNT', ctx.app.Sequelize.fn('DISTINCT', ctx.app.Sequelize.col('order_id'))), 'order_count' ], diff --git a/backend/app/utils/exportDefs.js b/backend/app/utils/exportDefs.js index e16a766..e1ba7a2 100644 --- a/backend/app/utils/exportDefs.js +++ b/backend/app/utils/exportDefs.js @@ -1016,12 +1016,16 @@ const EXPORT_TYPES = { whereIn(wh, 't.shop_id', csvValues(f.shop_ids).map(Number).filter(id => Number.isInteger(id) && id > 0)); whereIn(wh, 't.platform_type', csvValues(f.platform_types)); whereIn(wh, 't.outer_code', csvValues(f.outer_codes)); - // 有订单未绑定:与列表接口同口径(未绑定 + 被未解析的订单明细引用) + // 有订单未绑定:与列表接口同口径(未绑定 + 被未解析的订单明细引用; + // 只算待审核/异常的单,已流转的单不会因绑定而重建) if (String(f.has_unbound_order) === '1') { wh.raw('t.bind_type = 0'); + const editableStatuses = [ ORDER_STATUS.PENDING, ORDER_STATUS.EXCEPTION ].map(status => `'${status}'`).join(', '); wh.raw( `t.id IN (SELECT DISTINCT shop_product_id FROM order_status_items WHERE principal_id = ${Number(ctx.principalId)} ` + - 'AND parse_status = 2 AND shop_product_id IS NOT NULL)' + 'AND parse_status = 2 AND shop_product_id IS NOT NULL ' + + `AND order_id IN (SELECT id FROM order_index WHERE principal_id = ${Number(ctx.principalId)} ` + + `AND current_status IN (${editableStatuses})))` ); } // 所在公司权限:只能导出自己所属公司(货主)的店铺商品