明树Git Lab

Commit 384ce04a authored by zhangqi's avatar zhangqi

增加查询条件

parent dc52e81e
......@@ -108,12 +108,13 @@ async function createUser(req, res, next) {
if (!(ret && ret.id)) {
return res.sendError(errorMessage.databaseQueryError);
}
//处理部门 角色
//处理部门 角色 岗位
body.departs = body.departs || [];
body.roles = body.roles || [];
body.positions = body.positions || [];
let userDeparts = body.departs.map(o => { return { userId: ret.id, departId: o && o.id || o } });
let userRoles = body.roles.map(o => { return { userId: ret.id, roleId: o && o.id || o } });
let userPositions = body.positionss.map(o => { return { userId: ret.id, roleId: o && o.id || o } });
let userPositions = body.positions.map(o => { return { userId: ret.id, roleId: o && o.id || o } });
await DB.UserDepart.bulkCreate(userDeparts);
await DB.UserRole.bulkCreate(userRoles);
await DB.UserPosition.bulkCreate(userPositions);
......@@ -162,13 +163,16 @@ async function updateUser(req, res, next) {
{
model: DB.Role,
as: 'roles',
where: { del: 0 },
attributes: ["id"],
through: { attributes: [] },
}, {
model: DB.Depart,
as: 'departs',
where: { del: 0 },
attributes: ["id"],
through: { attributes: [] },
}, {
model: DB.Position,
as: 'positions',
attributes: ["id"],
through: { attributes: [] },
}
......@@ -182,6 +186,7 @@ async function updateUser(req, res, next) {
//跟现有情况对比 确定增删关系
const departIds = body.departs.map(o => { return o && o.id || o });
await userModule.setUserDepart(user.id, departIds, user.departs);
console.log('user.departs',user.departs);
delete body.departs;
}
if (body.roles && body.roles.length) {
......@@ -190,6 +195,12 @@ async function updateUser(req, res, next) {
await userModule.setUserRole(user.id, roleIds, user.roles);
delete body.roles;
}
if (body.positions && body.positions.length) {
//跟现有情况对比 确定增删关系
const positionIds = body.positions.map(o => { return o && o.id || o });
await userModule.setUserPosition(user.id, positionIds, user.positions);
delete body.roles;
}
const ret = await DB.User.update(body, { where: { id: body.id } });
return res.sendData(ret);
} catch (error) {
......@@ -215,6 +226,21 @@ async function listUser(req, res, next) {
}
search.limit = limit;
search.offset = offset;
search.include = [
{
model: DB.Role,
as: 'roles',
through: { attributes: [] },
}, {
model: DB.Depart,
as: 'departs',
through: { attributes: [] },
}, {
model: DB.Position,
as: 'positions',
through: { attributes: [] },
}
];
search.attributes = req.body.attributes || { exclude: ['password', 'salt'] };
let ret = await DB.User.findAndCountAll(search);
return res.sendData(ret);
......
......@@ -46,7 +46,7 @@ async function setUserDepart(userId, departIds, departs) {
dbIds.push(element.roleId);
if (!departIds.includes(element.roleId)) {
// 1. departIds里面没有 但是关系表里有的 需要删除
needDelIds.push(element.roleId);
needDelIds.push(element.roleId);
}
}
for (let index = 0; index < departIds.length; index++) {
......@@ -64,6 +64,40 @@ async function setUserDepart(userId, departIds, departs) {
}
}
/**
*
* @param {*} userId 被设置用户
* @param {*} positionIds 被设置岗位
* @param {*} positions 当前已拥有岗位
*/
async function setUserPosition(userId, positionIds, positions) {
//需新增得
let dbIds = [], needAddIds = [], needDelIds = [];
for (let index = 0; index < positions.length; index++) {
const element = positions[index];
dbIds.push(element.roleId);
if (!positionIds.includes(element.roleId)) {
// 1. departIds里面没有 但是关系表里有的 需要删除
needDelIds.push(element.roleId);
}
}
for (let index = 0; index < positionIds.length; index++) {
const element = positions[index];
if (!dbIds.includes(element)) {
needAddIds.push(element);
}
}
if (needAddIds.length) {
let objs = needAddIds.map(o => { return { userId: userId, roleId: o } });
await DB.UserRole.bulkCreate(objs);
}
if (needDelIds.length) {
await DB.UserRole.destroy({ where: { userId: userId, roleId: { [Op.in]: needDelIds } } });
}
}
/**
*
* @param {*} roleId 被设置角色
......@@ -95,6 +129,7 @@ async function setRoleMenu(roleId, menuIds, rolemenus) {
}
}
async function getProjectApprover(userId, roleCode) {
// 获取当前用户公司的 具体角色 的用户列表
let user = await DB.User.findOne({ where: { id: userId } });
......@@ -105,6 +140,7 @@ async function getProjectApprover(userId, roleCode) {
module.exports = {
setUserRole,
setUserDepart,
setUserPosition,
setRoleMenu,
getProjectApprover,
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment