diff --git a/app/model/org_unit.js b/app/model/org_unit.js new file mode 100644 index 0000000..05d3a51 --- /dev/null +++ b/app/model/org_unit.js @@ -0,0 +1,170 @@ +'use strict'; + +module.exports = app => { + const { BIGINT, STRING, DATE, TINYINT, INTEGER, JSON: JSON_TYPE } = app.Sequelize; + + const OrgUnit = app.model.define('org_unit', { + id: { + type: BIGINT(20).UNSIGNED, + primaryKey: true, + autoIncrement: true, + allowNull: false, + comment: '主键ID', + }, + warehouse_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '所属仓库ID,关联 wms_store.id', + }, + parent_id: { + type: BIGINT(20).UNSIGNED, + allowNull: true, + comment: '父部门ID,NULL=根部门;必须与 warehouse_id 同仓', + }, + name: { + type: STRING(100), + allowNull: false, + comment: '部门名称', + }, + code: { + type: STRING(50), + allowNull: true, + comment: '部门编码(仓库内唯一)', + }, + leader_id: { + type: BIGINT(20).UNSIGNED, + allowNull: true, + comment: '部门管理员账号ID,关联 wms_account.id', + }, + admin_role_ids: { + type: JSON_TYPE, + allowNull: true, + comment: '管理员默认角色集,关联 wms_role.id,如 [3,8];任命管理员时自动分配', + }, + default_role_ids: { + type: JSON_TYPE, + allowNull: true, + comment: '部门默认角色集,关联 wms_role.id,如 [3,8];成员入职挂本部门时自动分配', + }, + path: { + type: STRING(500), + allowNull: true, + comment: '层级路径,如 /1/5/9/(根部门为 /{id}/),用于一次查询整棵子树', + }, + sort: { + type: INTEGER, + allowNull: false, + defaultValue: 0, + comment: '同级排序,越小越靠前', + }, + status: { + type: TINYINT, + allowNull: false, + defaultValue: 1, + comment: '状态:0-停用 1-启用', + }, + created_at: { + type: DATE, + allowNull: false, + comment: '创建时间', + }, + updated_at: { + type: DATE, + allowNull: false, + comment: '更新时间', + }, + deleted_at: { + type: DATE, + allowNull: true, + comment: '软删除时间', + }, + }, { + tableName: 'wms_org_unit', + freezeTableName: true, + underscored: true, + timestamps: true, + createdAt: 'created_at', + updatedAt: 'updated_at', + paranoid: true, + deletedAt: 'deleted_at', + comment: '组织架构表(部门/子部门层级、部门管理员、管理员与成员默认角色)', + indexes: [ + { + name: 'idx_warehouse_parent', + fields: [ 'warehouse_id', 'parent_id' ], + }, + { + name: 'idx_parent_id', + fields: [ 'parent_id' ], + }, + { + name: 'uk_warehouse_code', + unique: true, + fields: [ 'warehouse_id', 'code' ], + }, + { + name: 'idx_path', + fields: [ 'path' ], + }, + { + name: 'idx_status', + fields: [ 'status' ], + }, + ], + }); + + // 关联关系 + OrgUnit.associate = function() { + const { Account, Store } = app.model; + + // 所属仓库 + OrgUnit.belongsTo(Store, { + foreignKey: 'warehouse_id', + targetKey: 'id', + as: 'warehouse', + }); + + // 部门管理员 + OrgUnit.belongsTo(Account, { + foreignKey: 'leader_id', + targetKey: 'id', + as: 'leader', + }); + + // 上级部门 + OrgUnit.belongsTo(OrgUnit, { + foreignKey: 'parent_id', + targetKey: 'id', + as: 'parent', + }); + + // 下级部门 + OrgUnit.hasMany(OrgUnit, { + foreignKey: 'parent_id', + sourceKey: 'id', + as: 'children', + }); + }; + + // 实例方法:是否根部门 + OrgUnit.prototype.isRoot = function() { + return !this.parent_id; + }; + + // 实例方法:是否启用 + OrgUnit.prototype.isEnabled = function() { + return this.status === 1; + }; + + // 实例方法:管理员默认角色ID列表 + OrgUnit.prototype.getAdminRoleIds = function() { + return this.admin_role_ids || []; + }; + + // 实例方法:部门默认角色ID列表(成员入职自动分配) + OrgUnit.prototype.getDefaultRoleIds = function() { + return this.default_role_ids || []; + }; + + return OrgUnit; +}; diff --git a/database/migrations/20260928120000-create-wms-org-unit.js b/database/migrations/20260928120000-create-wms-org-unit.js new file mode 100644 index 0000000..79f7174 --- /dev/null +++ b/database/migrations/20260928120000-create-wms-org-unit.js @@ -0,0 +1,153 @@ +'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} 无返回值。 + */ + 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} 无返回值。 + */ + down: async queryInterface => { + await queryInterface.dropTable(TABLE_NAME); + }, +};