WMS_MAIN/database/migrations/20260519193000-create-wms-pack-task-tables.js
liufumingren 8cf2c5befa 1
2026-05-20 16:31:29 +08:00

213 lines
5.8 KiB
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';
/**
* 获取当前数据库已存在的表名集合。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<Set<string>>} 表名集合。
*/
const getExistingTableSet = async queryInterface => {
const tables = await queryInterface.showAllTables();
return new Set(
tables.map(table => (typeof table === 'string' ? table : table.tableName))
);
};
/**
* 构建通用时间字段。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {{create_time: object, update_time: object, deleted_at: object}} 时间字段定义。
*/
const buildTimeColumns = Sequelize => ({
create_time: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '创建时间',
},
update_time: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
comment: '更新时间',
},
deleted_at: {
type: Sequelize.DATE,
allowNull: true,
comment: '删除时间',
},
});
/**
* 创建打包任务主表。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
const createPackTaskTable = async (queryInterface, Sequelize) => {
await queryInterface.createTable('wms_pack_task', {
id: {
type: Sequelize.BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键ID',
},
order_id: {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: false,
comment: '订单ID',
},
pack_mode: {
type: Sequelize.TINYINT.UNSIGNED,
allowNull: false,
defaultValue: 1,
comment: '打包模式:1=散单 2=团购',
},
status: {
type: Sequelize.TINYINT.UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: '任务状态:0=待处理',
},
primary_packer_emp_id: {
type: Sequelize.INTEGER.UNSIGNED,
allowNull: true,
comment: '主打包人工号ID',
},
primary_packer_emp_no: {
type: Sequelize.STRING(64),
allowNull: true,
comment: '主打包人工号',
},
primary_packer_name: {
type: Sequelize.STRING(128),
allowNull: true,
comment: '主打包人姓名',
},
packers_snapshot: {
type: Sequelize.JSON,
allowNull: true,
comment: '打包人快照:散单对象/团购数组',
},
meta: {
type: Sequelize.JSON,
allowNull: true,
comment: '扩展信息JSON',
},
...buildTimeColumns(Sequelize),
}, {
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
comment: '打包任务主表',
});
await queryInterface.addIndex('wms_pack_task', [ 'order_id' ], {
name: 'idx_order_id',
});
await queryInterface.addIndex('wms_pack_task', [ 'primary_packer_emp_id' ], {
name: 'idx_primary_packer_emp_id',
});
await queryInterface.addIndex('wms_pack_task', [ 'pack_mode', 'status' ], {
name: 'idx_pack_mode_status',
});
};
/**
* 创建打包任务-打包人关系表。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
const createPackTaskPackerTable = async (queryInterface, Sequelize) => {
await queryInterface.createTable('wms_pack_task_packer', {
id: {
type: Sequelize.BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键ID',
},
pack_task_id: {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: false,
comment: '打包任务ID',
},
emp_id: {
type: Sequelize.INTEGER.UNSIGNED,
allowNull: false,
comment: '打包人工号ID',
},
emp_no: {
type: Sequelize.STRING(64),
allowNull: false,
comment: '打包人工号',
},
emp_name: {
type: Sequelize.STRING(128),
allowNull: false,
comment: '打包人姓名',
},
seq: {
type: Sequelize.TINYINT.UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: '排序序号',
},
...buildTimeColumns(Sequelize),
}, {
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
comment: '打包任务-打包人关系表',
});
await queryInterface.addIndex('wms_pack_task_packer', [ 'pack_task_id', 'emp_id' ], {
name: 'uix_pack_task_emp',
unique: true,
});
await queryInterface.addIndex('wms_pack_task_packer', [ 'emp_id' ], {
name: 'idx_emp_id',
});
await queryInterface.addIndex('wms_pack_task_packer', [ 'pack_task_id' ], {
name: 'idx_pack_task_id',
});
};
/**
* 创建打包任务相关表。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
module.exports.up = async (queryInterface, Sequelize) => {
const tableSet = await getExistingTableSet(queryInterface);
if (!tableSet.has('wms_pack_task')) {
await createPackTaskTable(queryInterface, Sequelize);
}
if (!tableSet.has('wms_pack_task_packer')) {
await createPackTaskPackerTable(queryInterface, Sequelize);
}
};
/**
* 回滚删除打包任务相关表。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
module.exports.down = async queryInterface => {
const tableSet = await getExistingTableSet(queryInterface);
if (tableSet.has('wms_pack_task_packer')) {
await queryInterface.dropTable('wms_pack_task_packer');
}
if (tableSet.has('wms_pack_task')) {
await queryInterface.dropTable('wms_pack_task');
}
};