'use strict'; /** * 订单模型,对应 orders 表 * 含本系统主状态 status、外部 WMS 状态 wms_status、上交状态 submit_status * @param {Egg.Application} app egg application * @return {object} order model */ module.exports = app => { const { BIGINT, STRING, TINYINT, DECIMAL, JSON, DATE } = app.Sequelize; const Order = app.model.define('order', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, order_no: { type: STRING(50), allowNull: false, comment: '业务订单号' }, principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' }, shop_id: { type: BIGINT.UNSIGNED, comment: '所属店铺ID,为空表示未关联店铺' }, order_type: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '订单类型' }, status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '主状态:1待处理 2处理中 3已完成 4已取消' }, wms_status: { type: STRING(50), comment: '外部WMS状态' }, wms_order_no: { type: STRING(50), comment: '外部WMS单号' }, wms_synced_at: { type: DATE, comment: '最近一次WMS同步时间' }, submit_status: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '上交状态:0未上交 1上交中 2已上交 3上交失败' }, submit_order_no: { type: STRING(50), comment: '上交后返回的单号' }, submit_at: { type: DATE, comment: '上交时间' }, submit_msg: { type: STRING(255), comment: '上交结果/失败原因' }, total_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '订单金额' }, pay_amount: { type: DECIMAL(12, 2), comment: '实付金额' }, receiver: { type: JSON, comment: '收货人信息 JSON:{ name, phone, address }' }, remark: { type: STRING(255), comment: '备注' }, created_at: { type: DATE, comment: '创建时间' }, updated_at: { type: DATE, comment: '更新时间' }, }); Order.associate = () => { app.model.Order.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' }); app.model.Order.belongsTo(app.model.Shop, { foreignKey: 'shop_id', as: 'shop' }); app.model.Order.hasMany(app.model.OrderItem, { foreignKey: 'order_id', as: 'items' }); }; return Order; };