WMS_MAIN/app/model/org_unit.js
2026-09-29 03:50:34 +08:00

171 lines
4.0 KiB
JavaScript
Raw Permalink 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';
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;
};