oms/backend/app/model/product.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

70 lines
4.3 KiB
JavaScript
Raw Permalink 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';
/**
* 商品模型,对应 products 表(字段命名与外部商品表对齐)
* @param {Egg.Application} app egg application
* @return {object} product model
*/
module.exports = app => {
const { BIGINT, STRING, TINYINT, INTEGER, DECIMAL, DATE, ENUM, JSON } = app.Sequelize;
const Product = app.model.define('product', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
subsidiary_id: { type: BIGINT.UNSIGNED, comment: '所属货主(下属公司ID)' },
item_code: { type: STRING(50), allowNull: false, comment: '商品编码' },
goods_code: { type: STRING(100), comment: '货物编码' },
item_name: { type: STRING(255), allowNull: false, comment: '商品名称' },
sku_property: { type: STRING(100), comment: 'SKU规格' },
stock_unit: { type: STRING(20), comment: '库存单位' },
length: { type: DECIMAL(10, 2), defaultValue: 0, comment: '长度(cm)' },
width: { type: DECIMAL(10, 2), defaultValue: 0, comment: '宽度(cm)' },
height: { type: DECIMAL(10, 2), defaultValue: 0, comment: '高度(cm)' },
volume: { type: DECIMAL(10, 2), defaultValue: 0, comment: '体积(cm³)' },
gross_weight: { type: INTEGER, defaultValue: 0, comment: '毛重(g)' },
net_weight: { type: INTEGER, defaultValue: 0, comment: '净重(g)' },
category_id: { type: INTEGER, comment: '分类ID(关联 product_categories)' },
category_name: { type: STRING(100), comment: '分类名称(冗余文本)' },
brand_id: { type: INTEGER, comment: '品牌ID(关联 product_brands)' },
brand_name: { type: STRING(100), comment: '品牌名称(冗余文本)' },
unit_id: { type: INTEGER, comment: '单位ID(关联 product_units)' },
item_type: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '商品类型' },
tag_price: { type: DECIMAL(10, 2), defaultValue: 0, comment: '标价' },
retail_price: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '零售价' },
cost_price: { type: DECIMAL(12, 2), comment: '成本价' },
is_area_sale: { type: STRING(1), defaultValue: 'N', comment: '是否区域销售' },
is_sn_mgmt: { type: STRING(1), defaultValue: 'N', comment: '是否序列号管理' },
is_shelf_life_mgmt: { type: STRING(1), defaultValue: 'N', comment: '是否保质期管理' },
is_batch_mgmt: { type: STRING(1), defaultValue: 'N', comment: '是否批次管理' },
is_order_unique_gift: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '是否订单唯一赠品:1 是(整单只落一行、数量 1)0 否' },
barcode: { type: STRING(100), comment: '条码' },
image: { type: STRING(255), comment: '图片(主图,取 images 首张)' },
images: { type: JSON, comment: '图片组(URL 数组,首张为主图)' },
status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
remark: { type: STRING(255), comment: '备注' },
create_from: { type: ENUM('self', 'qimen', 'api'), comment: '数据来源' },
stock_id: { type: BIGINT.UNSIGNED, comment: '库存ID' },
warehouse_id: { type: BIGINT.UNSIGNED, comment: '仓库ID' },
store_id: { type: BIGINT.UNSIGNED, comment: '门店ID' },
created_at: { type: DATE, comment: '创建时间' },
updated_at: { type: DATE, comment: '更新时间' },
deleted_at: { type: DATE, comment: '软删除时间' },
}, {
paranoid: true,
deletedAt: 'deleted_at',
});
Product.associate = () => {
app.model.Product.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' });
// 所属货主(下属公司):为空表示未指定
app.model.Product.belongsTo(app.model.PrincipalSubsidiary, { foreignKey: 'subsidiary_id', as: 'subsidiary' });
// 反向关联:店铺商品单向绑定到该商品、订单明细解析到该商品
app.model.Product.hasMany(app.model.ShopProduct, { foreignKey: 'product_id', as: 'shopProducts' });
app.model.Product.hasMany(app.model.OrderItem, { foreignKey: 'product_id', as: 'orderItems' });
// 仓库-商品关联:该商品在各仓库的推送状态
app.model.Product.hasMany(app.model.WarehouseProduct, { foreignKey: 'product_id', as: 'warehouseProducts' });
};
return Product;
};