Skip to content

Commit

Permalink
feat: 用户department_id外键关联department表;
Browse files Browse the repository at this point in the history
用户department_id外键关联department表;
  • Loading branch information
Imfdj committed May 21, 2021
1 parent b6673bc commit 214b14d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/model/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = app => {
}
);
user.associate = function(models) {
user.hasOne(app.model.Departments, { foreignKey: 'id', sourceKey: 'department_id', as: 'department' });
app.model.Users.belongsToMany(app.model.Roles, {
through: app.model.UserRoles,
foreignKey: 'user_id',
Expand Down
17 changes: 15 additions & 2 deletions app/service/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UserService extends Service {
email ? (where.email = { [Op.like]: `%${email}%` }) : null;
phone ? (where.phone = { [Op.like]: `%${phone}%` }) : null;
!ctx.helper.tools.isParam(state) ? (where.state = state) : null;
!ctx.helper.tools.isParam(department_id) ? (where.department_id = department_id) : null;
!ctx.helper.tools.isParam(department_id) ? (where.department_id = department_id === 0 ? null : department_id) : null;
!ctx.helper.tools.isParam(project_id) ? (project_where = { id: project_id }) : null;
prop_order && order ? Order.push([prop_order, order]) : null;
return await ctx.model.Users.findAndCountAll({
Expand All @@ -39,6 +39,11 @@ class UserService extends Service {
model: ctx.model.Roles,
attributes: ['id', 'name'],
},
{
model: ctx.model.Departments,
attributes: ['id', 'name'],
as: 'department',
},
],
distinct: true,
});
Expand All @@ -49,6 +54,13 @@ class UserService extends Service {
return await ctx.model.Users.findOne({
where: { id },
attributes: { exclude: ['password', 'deleted_at'] },
include: [
{
model: ctx.model.Departments,
attributes: ['id', 'name'],
as: 'department',
},
],
});
}

Expand Down Expand Up @@ -257,7 +269,8 @@ class UserService extends Service {
async updateUserDepartment(payload) {
const { ctx } = this;
const { id, department_id } = payload;
return await ctx.model.Users.update({ department_id }, { where: { id } });
// 如果department_id为0则设为null
return await ctx.model.Users.update({ department_id: department_id === 0 ? null : department_id }, { where: { id } });
}

/**
Expand Down
10 changes: 8 additions & 2 deletions generator/config-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ module.exports = {
required: false,
description: '部门ID', // 供swagger使用
example: 0, // 供swagger使用
allowNull: false, // 是否允许为空
defaultValue: 0, // 数据库表中字段的默认值
allowNull: true, // 是否允许为空
comment: '部门ID', // 数据库表中字段的描述
// 外键设置
references: {
model: 'department', // 外键关联表
key: 'id', // 外键字段名
},
onUpdate: 'NO ACTION', // 外键更新约束 CASCADE RESTRICT SET NULL SET DEFAULT NO ACTION
onDelete: 'NO ACTION', // 外键删除约束 CASCADE RESTRICT SET NULL SET DEFAULT NO ACTION
},
{
name: 'username',
Expand Down

0 comments on commit 214b14d

Please sign in to comment.