161 lines
4.6 KiB
JavaScript
161 lines
4.6 KiB
JavaScript
'use strict';
|
||
|
||
module.exports = {
|
||
async up(queryInterface, Sequelize) {
|
||
const { BIGINT, STRING, TEXT, DATE } = Sequelize;
|
||
|
||
const [ tables ] = await queryInterface.sequelize.query(
|
||
"SHOW TABLES LIKE 'wms_store_warehouse_print_quantity_log'"
|
||
);
|
||
|
||
if (tables.length > 0) {
|
||
console.log('Table wms_store_warehouse_print_quantity_log already exists, skipping creation');
|
||
return;
|
||
}
|
||
|
||
await queryInterface.createTable('wms_store_warehouse_print_quantity_log', {
|
||
id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '主键ID',
|
||
},
|
||
contract_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
references: {
|
||
model: 'wms_store_warehouse',
|
||
key: 'id',
|
||
},
|
||
onUpdate: 'CASCADE',
|
||
onDelete: 'NO ACTION',
|
||
comment: '签约关系ID,对应 wms_store_warehouse.id',
|
||
},
|
||
store_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
references: {
|
||
model: 'wms_store',
|
||
key: 'id',
|
||
},
|
||
onUpdate: 'CASCADE',
|
||
onDelete: 'NO ACTION',
|
||
comment: '货主ID',
|
||
},
|
||
warehouse_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
references: {
|
||
model: 'wms_store',
|
||
key: 'id',
|
||
},
|
||
onUpdate: 'CASCADE',
|
||
onDelete: 'NO ACTION',
|
||
comment: '仓库ID',
|
||
},
|
||
change_type: {
|
||
type: STRING(50),
|
||
allowNull: false,
|
||
comment: '变更类型,如 manual_adjust / order_deduct',
|
||
},
|
||
business_type: {
|
||
type: STRING(50),
|
||
allowNull: true,
|
||
comment: '业务来源类型,如 order_express',
|
||
},
|
||
business_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: true,
|
||
comment: '业务关联ID,预留关联 wms_order_express.id',
|
||
},
|
||
change_quantity: {
|
||
type: BIGINT(20),
|
||
allowNull: false,
|
||
comment: '本次变更数量,整数,可正可负',
|
||
},
|
||
balance_before: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '变更前余额',
|
||
},
|
||
balance_after: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '变更后余额',
|
||
},
|
||
reason: {
|
||
type: TEXT,
|
||
allowNull: true,
|
||
comment: '变更缘由;人工调整时必填',
|
||
},
|
||
operator_id: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: true,
|
||
comment: '操作人账号ID',
|
||
},
|
||
created_by: {
|
||
type: BIGINT(20).UNSIGNED,
|
||
allowNull: true,
|
||
comment: '创建人账号ID',
|
||
},
|
||
created_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||
comment: '更新时间',
|
||
},
|
||
}, {
|
||
engine: 'InnoDB',
|
||
charset: 'utf8mb4',
|
||
collate: 'utf8mb4_general_ci',
|
||
comment: '货主-仓库签约面单余额变更记录表',
|
||
});
|
||
|
||
const safeCreateIndex = async (tableName, fields, options) => {
|
||
const [ existingIndexes ] = await queryInterface.sequelize.query(
|
||
`SHOW INDEX FROM ${tableName} WHERE Key_name = '${options.name}'`
|
||
);
|
||
if (existingIndexes.length === 0) {
|
||
await queryInterface.addIndex(tableName, fields, options);
|
||
}
|
||
};
|
||
|
||
await safeCreateIndex('wms_store_warehouse_print_quantity_log', [ 'contract_id', 'id' ], {
|
||
name: 'idx_sw_print_qty_log_contract_id',
|
||
});
|
||
|
||
await safeCreateIndex('wms_store_warehouse_print_quantity_log', [ 'store_id', 'warehouse_id' ], {
|
||
name: 'idx_sw_print_qty_log_store_warehouse',
|
||
});
|
||
|
||
await safeCreateIndex('wms_store_warehouse_print_quantity_log', [ 'business_type', 'business_id' ], {
|
||
name: 'idx_sw_print_qty_log_business',
|
||
});
|
||
|
||
await safeCreateIndex('wms_store_warehouse_print_quantity_log', [ 'change_type' ], {
|
||
name: 'idx_sw_print_qty_log_change_type',
|
||
});
|
||
|
||
await safeCreateIndex('wms_store_warehouse_print_quantity_log', [ 'created_at' ], {
|
||
name: 'idx_sw_print_qty_log_created_at',
|
||
});
|
||
},
|
||
|
||
async down(queryInterface) {
|
||
const [ tables ] = await queryInterface.sequelize.query(
|
||
"SHOW TABLES LIKE 'wms_store_warehouse_print_quantity_log'"
|
||
);
|
||
|
||
if (tables.length > 0) {
|
||
await queryInterface.dropTable('wms_store_warehouse_print_quantity_log');
|
||
}
|
||
},
|
||
};
|