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})))` ); } // 所在公司权限:只能导出自己所属公司(货主)的店铺商品