'use strict'; /** * 获取当前数据库已存在的表名集合。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise>} 表名集合。 */ 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} 无返回值。 */ const createReviewTaskTable = async (queryInterface, Sequelize) => { await queryInterface.createTable('wms_recheck_task', { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, review_no: { type: Sequelize.STRING(64), allowNull: false, comment: '复核单号', }, line_no: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 1, comment: '明细序号', }, warehouse_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', }, task_type: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '任务类型:1=散单 2=团购波次', }, source_task_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '关联 wms_wave_pick_task.id', }, wave_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '波次ID', }, order_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '订单ID', }, order_no: { type: Sequelize.STRING(64), allowNull: true, comment: '订单号', }, package_seq: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 1, comment: '包裹序号', }, expected_waybill_no: { type: Sequelize.STRING(64), allowNull: true, comment: '预期运单号', }, waybill_no: { type: Sequelize.STRING(64), allowNull: true, comment: '实扫运单号', }, task_status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '任务状态:0=待开始 1=复核中 2=完成 3=异常 9=取消', }, scan_status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '扫描状态:0=未扫 1=已扫 2=异常', }, review_emp_no: { type: Sequelize.STRING(32), allowNull: true, comment: '复核人工号', }, review_emp_name: { type: Sequelize.STRING(64), allowNull: true, comment: '复核人姓名', }, scan_emp_no: { type: Sequelize.STRING(32), allowNull: true, comment: '扫描人工号', }, scan_emp_name: { type: Sequelize.STRING(64), allowNull: true, comment: '扫描人姓名', }, device_id: { type: Sequelize.STRING(64), allowNull: true, comment: '设备ID', }, start_time: { type: Sequelize.DATE, allowNull: true, comment: '开始时间', }, finish_time: { type: Sequelize.DATE, allowNull: true, comment: '完成时间', }, scan_time: { type: Sequelize.DATE, allowNull: true, comment: '扫描时间', }, remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注', }, ext_json: { type: Sequelize.JSON, allowNull: true, comment: '扩展信息JSON', }, ...buildTimeColumns(Sequelize), }, { charset: 'utf8mb4', collate: 'utf8mb4_general_ci', comment: '单表复核任务明细表', }); await queryInterface.addIndex('wms_recheck_task', [ 'review_no', 'line_no' ], { name: 'uk_review_no_line_no', unique: true, }); await queryInterface.addIndex('wms_recheck_task', [ 'review_no', 'waybill_no' ], { name: 'uk_review_no_waybill_no', unique: true, }); await queryInterface.addIndex('wms_recheck_task', [ 'warehouse_id', 'task_status' ], { name: 'idx_warehouse_task_status', }); await queryInterface.addIndex('wms_recheck_task', [ 'warehouse_id', 'task_type' ], { name: 'idx_warehouse_task_type', }); await queryInterface.addIndex('wms_recheck_task', [ 'warehouse_id', 'review_no' ], { name: 'idx_warehouse_review_no', }); await queryInterface.addIndex('wms_recheck_task', [ 'source_task_id' ], { name: 'idx_source_task_id', }); await queryInterface.addIndex('wms_recheck_task', [ 'wave_id' ], { name: 'idx_wave_id', }); await queryInterface.addIndex('wms_recheck_task', [ 'order_id' ], { name: 'idx_order_id', }); await queryInterface.addIndex('wms_recheck_task', [ 'waybill_no' ], { name: 'idx_waybill_no', }); await queryInterface.addIndex('wms_recheck_task', [ 'review_no', 'scan_status' ], { name: 'idx_review_no_scan_status', }); }; /** * 新增单表复核任务明细表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ module.exports.up = async (queryInterface, Sequelize) => { const tableSet = await getExistingTableSet(queryInterface); if (tableSet.has('wms_recheck_task')) { return; } await createReviewTaskTable(queryInterface, Sequelize); }; /** * 回滚删除单表复核任务明细表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ module.exports.down = async queryInterface => { const tableSet = await getExistingTableSet(queryInterface); if (!tableSet.has('wms_recheck_task')) { return; } await queryInterface.dropTable('wms_recheck_task'); };