赠品类商品在平台上会随购买数量重复出现(数量 3 就是 3 件),也可能同时出现在多个组合里 (两份组合、不同组合都含同一赠品)。业务上赠品每单只送一份,需要按商品维度收敛。 - 迁移 add-unique-gift-to-products:products.is_order_unique_gift TINYINT 默认 0 + 索引 (已在本机执行) - 模型/契约/列表接口:字段透传;列表支持 is_order_unique_gift=1/0 筛选 - 明细解析:rebuildOrderItems 展开成最小单位后调用 collapseUniqueGifts → collapseUniqueGiftRows 收敛:同一赠品只保留第一行、数量固定 1 (多数量 / 同一组合多份 / 不同组合都含该赠品,均只落一行);重复重建不会涨回数量 - 系统商品页:新增「订单唯一赠品」开关(带说明)、列表「唯一赠品」标记、筛选下拉; 导出(exportDefs.product)同步加筛选与列 - 单测:orderItemUniqueGift.test.js(纯函数 5 例)+ orderItemUniqueGiftRebuild.test.js (真实库事务内验证两条明细数量 3 → 1 行数量 1,跑完回滚) - 顺带修 model/product.js 里重复定义的 brand_name(eslint no-dupe-keys,两行内容相同) - 文档 §6.1.1 补「组合拆分 + 订单唯一赠品」口径 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
90 lines
3.1 KiB
JavaScript
90 lines
3.1 KiB
JavaScript
'use strict';
|
|
const { app, assert } = require('egg-mock/bootstrap');
|
|
|
|
describe('service/orderItemResolve 订单唯一赠品收敛(真实库 + 回滚)', () => {
|
|
it('同一赠品的多条明细只落一行、数量 1', async () => {
|
|
const ctx = app.mockContext({ state: { principalId: 3 } });
|
|
const t = await ctx.model.transaction();
|
|
try {
|
|
const principalId = 3;
|
|
const now = new Date();
|
|
const product = await ctx.model.Product.create({
|
|
principal_id: principalId,
|
|
item_code: `TMP-GIFT-${Date.now()}`,
|
|
item_name: 'TMP gift',
|
|
retail_price: 0,
|
|
is_order_unique_gift: 1,
|
|
}, { transaction: t });
|
|
// 店铺商品绑定到该赠品:这样展开才会落到 product_id
|
|
const shopProduct = await ctx.model.ShopProduct.create({
|
|
principal_id: principalId,
|
|
shop_id: 18,
|
|
outer_item_id: 'tmp-item',
|
|
outer_sku_id: 'tmp-sku',
|
|
title: 'TMP gift shop product',
|
|
stock: 0,
|
|
status: 1,
|
|
bind_type: 1,
|
|
product_id: product.id,
|
|
}, { transaction: t });
|
|
|
|
const order = await ctx.model.Order.create({
|
|
principal_id: principalId,
|
|
order_no: `TMP-${Date.now()}`,
|
|
order_type: 1,
|
|
total_amount: 0,
|
|
pay_amount: 0,
|
|
receiver: 'TMP',
|
|
}, { transaction: t });
|
|
await ctx.model.OrderIndex.create({
|
|
id: order.id,
|
|
principal_id: principalId,
|
|
order_no: order.order_no,
|
|
parent_order_no: order.order_no,
|
|
current_status: 'pending',
|
|
current_table: 'pending',
|
|
created_at: now,
|
|
updated_at: now,
|
|
}, { transaction: t });
|
|
|
|
// 两条平台明细都命中同一赠品(模拟两份组合/多数量)
|
|
for (const idx of [ 1, 2 ]) {
|
|
await ctx.model.OrderStatusItem.create({
|
|
principal_id: principalId,
|
|
order_id: order.id,
|
|
shop_id: 18,
|
|
order_line_no: String(idx),
|
|
item_code: shopProduct.outer_item_id,
|
|
item_name: 'TMP line',
|
|
plan_qty: 3,
|
|
quantity: 3,
|
|
actual_price: 0,
|
|
shop_product_id: shopProduct.id,
|
|
product_id: product.id,
|
|
item_type: 1,
|
|
parse_status: 1,
|
|
outer_item_id: 'tmp-item',
|
|
outer_sku_id: 'tmp-sku',
|
|
platform_snapshot: { line_no: String(idx), item_code: 'tmp-item', plan_qty: 3, actual_price: 0 },
|
|
}, { transaction: t });
|
|
}
|
|
|
|
await ctx.service.orderItemResolve.rebuildOrderItems([ order.id ], { transaction: t });
|
|
|
|
const after = await ctx.model.OrderStatusItem.findAll({
|
|
where: { order_id: order.id },
|
|
attributes: [ 'product_id', 'quantity', 'plan_qty' ],
|
|
raw: true,
|
|
transaction: t,
|
|
});
|
|
console.log('rows after rebuild:', JSON.stringify(after));
|
|
assert.strictEqual(after.length, 1, 'two sources of the same gift collapse into one row');
|
|
assert.strictEqual(Number(after[0].quantity), 1);
|
|
assert.strictEqual(Number(after[0].plan_qty), 1);
|
|
assert.strictEqual(Number(after[0].product_id), Number(product.id));
|
|
} finally {
|
|
await t.rollback();
|
|
}
|
|
});
|
|
});
|