'use strict'; /** * 订单明细模型,对应 order_items 表(订单的副表) * 记录平台侧商品快照,并单向关联系统商品/组合商品: * product_id 为空表示尚未解析出商品(parse_status:0未解析 1已解析 2解析失败) * @param {Egg.Application} app egg application * @return {object} orderItem model */ module.exports = app => { const { BIGINT, STRING, TINYINT, DECIMAL, JSON, DATE } = app.Sequelize; const OrderItem = app.model.define('order_item', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' }, order_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属订单ID' }, shop_id: { type: BIGINT.UNSIGNED, comment: '所属店铺ID(冗余订单的店铺)' }, product_id: { type: BIGINT.UNSIGNED, comment: '系统商品ID,为空表示未解析出商品' }, combo_product_id: { type: BIGINT.UNSIGNED, comment: '组合商品ID,为空表示非组合商品' }, shop_product_id: { type: BIGINT.UNSIGNED, comment: '命中的店铺商品ID' }, item_type: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '明细类型:0未识别 1普通商品 2组合商品' }, parse_status: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '解析状态:0未解析 1已解析 2解析失败' }, parse_msg: { type: STRING(255), comment: '解析失败原因' }, outer_item_id: { type: STRING(100), comment: '平台商品ID' }, outer_sku_id: { type: STRING(100), comment: '平台规格ID' }, sku_code: { type: STRING(100), comment: '商家编码' }, name: { type: STRING(255), comment: '商品名称(快照)' }, spec: { type: STRING(255), comment: '规格(快照)' }, image: { type: STRING(255), comment: '图片(快照)' }, quantity: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '数量' }, price: { type: DECIMAL(12, 2), comment: '单价' }, total_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '明细金额' }, raw: { type: JSON, comment: '平台返回的明细原始数据' }, created_at: { type: DATE, comment: '创建时间' }, updated_at: { type: DATE, comment: '更新时间' }, }); OrderItem.associate = () => { app.model.OrderItem.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' }); app.model.OrderItem.belongsTo(app.model.Order, { foreignKey: 'order_id', as: 'order' }); app.model.OrderItem.belongsTo(app.model.Shop, { foreignKey: 'shop_id', as: 'shop' }); app.model.OrderItem.belongsTo(app.model.Product, { foreignKey: 'product_id', as: 'product' }); app.model.OrderItem.belongsTo(app.model.ComboProduct, { foreignKey: 'combo_product_id', as: 'comboProduct' }); app.model.OrderItem.belongsTo(app.model.ShopProduct, { foreignKey: 'shop_product_id', as: 'shopProduct' }); }; return OrderItem; };