oms/backend/database/migrations/20261108112000-add-unique-gift-to-products.js
liufumingren 62df580393 feat(product): 商品增加「订单唯一赠品」,明细解析整单只落一行
赠品类商品在平台上会随购买数量重复出现(数量 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>
2026-11-08 11:28:18 +08:00

28 lines
1004 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 商品增加「订单唯一赠品」标记。
*
* 置 1 后:订单明细解析到该商品时,**整单只保留一行、数量固定 1**,
* 不管平台明细带了多少数量、通过几个组合/几条明细命中(见 lib/orderItemExpand.collapseUniqueGiftRows)。
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn('products', 'is_order_unique_gift', {
type: Sequelize.TINYINT,
allowNull: false,
defaultValue: 0,
comment: '是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否',
after: 'is_batch_mgmt',
});
await queryInterface.addIndex('products', [ 'principal_id', 'is_order_unique_gift' ], {
name: 'idx_products_principal_unique_gift',
});
},
async down(queryInterface) {
await queryInterface.removeIndex('products', 'idx_products_principal_unique_gift');
await queryInterface.removeColumn('products', 'is_order_unique_gift');
},
};