154 lines
5.2 KiB
JavaScript
154 lines
5.2 KiB
JavaScript
'use strict';
|
||
|
||
const TABLE_NAME = 'wms_org_unit';
|
||
|
||
/**
|
||
* Migration: create wms_org_unit(组织架构表)
|
||
*
|
||
* 背景:系统多仓库多主体,各仓库自行维护自己的组织架构(部门/子部门层级),
|
||
* 用于工单派发、组级消息推送、入职默认权限等场景。本表与角色表(wms_role)
|
||
* 分离:角色管"能做什么",组织架构管"归属哪个组"。
|
||
*
|
||
* 字段说明:
|
||
* - warehouse_id:所属仓库,多主体数据隔离维度,部门树不允许跨仓挂接。
|
||
* - parent_id:父部门ID,NULL 表示根部门;挂接时必须与 warehouse_id 同仓(服务层校验)。
|
||
* - path:冗余层级路径(如 /1/5/9/),用前缀 LIKE 一次查询整棵子树,避免递归。
|
||
* - leader_id:部门/子部门管理员(关联 wms_account.id),可空。
|
||
* - admin_role_ids:管理员默认角色集(关联 wms_role.id),任命管理员时自动分配。
|
||
* - default_role_ids:部门默认角色集(关联 wms_role.id),成员入职挂本部门时自动分配。
|
||
* - code:部门编码,仓库内唯一(NULL 不参与唯一约束,允许多个未编码部门)。
|
||
*
|
||
* 索引:idx_warehouse_parent、idx_parent_id、uk_warehouse_code(唯一)、
|
||
* idx_path、idx_status。
|
||
*
|
||
* Idempotent: 表已存在时跳过创建。
|
||
*/
|
||
|
||
module.exports = {
|
||
/**
|
||
* 执行正向迁移:创建组织架构表与索引。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 实例。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
up: async (queryInterface, Sequelize) => {
|
||
const tables = await queryInterface.showAllTables();
|
||
const exists = tables.some(t => (typeof t === 'string' ? t : (t.tableName || t.name)) === TABLE_NAME);
|
||
if (exists) return;
|
||
|
||
await queryInterface.createTable(TABLE_NAME, {
|
||
id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '主键ID',
|
||
},
|
||
warehouse_id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '所属仓库ID,关联 wms_store.id',
|
||
},
|
||
parent_id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
allowNull: true,
|
||
comment: '父部门ID,NULL=根部门;必须与 warehouse_id 同仓',
|
||
},
|
||
name: {
|
||
type: Sequelize.STRING(100),
|
||
allowNull: false,
|
||
comment: '部门名称',
|
||
},
|
||
code: {
|
||
type: Sequelize.STRING(50),
|
||
allowNull: true,
|
||
comment: '部门编码(仓库内唯一)',
|
||
},
|
||
leader_id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
allowNull: true,
|
||
comment: '部门管理员账号ID,关联 wms_account.id',
|
||
},
|
||
admin_role_ids: {
|
||
type: Sequelize.JSON,
|
||
allowNull: true,
|
||
comment: '管理员默认角色集,关联 wms_role.id,如 [3,8];任命管理员时自动分配',
|
||
},
|
||
default_role_ids: {
|
||
type: Sequelize.JSON,
|
||
allowNull: true,
|
||
comment: '部门默认角色集,关联 wms_role.id,如 [3,8];成员入职挂本部门时自动分配',
|
||
},
|
||
path: {
|
||
type: Sequelize.STRING(500),
|
||
allowNull: true,
|
||
comment: '层级路径,如 /1/5/9/(根部门为 /{id}/),用于一次查询整棵子树',
|
||
},
|
||
sort: {
|
||
type: Sequelize.INTEGER,
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '同级排序,越小越靠前',
|
||
},
|
||
status: {
|
||
type: Sequelize.TINYINT,
|
||
allowNull: false,
|
||
defaultValue: 1,
|
||
comment: '状态:0-停用 1-启用',
|
||
},
|
||
created_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||
comment: '更新时间',
|
||
},
|
||
deleted_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: true,
|
||
comment: '软删除时间',
|
||
},
|
||
});
|
||
|
||
// 按仓库查部门树(取根 + 挂接校验)
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id', 'parent_id' ], {
|
||
name: 'idx_warehouse_parent',
|
||
});
|
||
|
||
// 按父部门查子部门
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'parent_id' ], {
|
||
name: 'idx_parent_id',
|
||
});
|
||
|
||
// 仓库内部门编码唯一
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id', 'code' ], {
|
||
name: 'uk_warehouse_code',
|
||
unique: true,
|
||
});
|
||
|
||
// 路径前缀查询子树
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'path' ], {
|
||
name: 'idx_path',
|
||
});
|
||
|
||
// 启用状态过滤
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'status' ], {
|
||
name: 'idx_status',
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 执行回滚迁移:删除组织架构表。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
down: async queryInterface => {
|
||
await queryInterface.dropTable(TABLE_NAME);
|
||
},
|
||
};
|