|
|
|
'use strict';
|
|
|
|
const moment = require('moment')
|
|
|
|
|
|
|
|
async function allDeps (ctx) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { redis } = ctx.app
|
|
|
|
const start = moment()
|
|
|
|
let depRes = await redis.get('allDepartments')
|
|
|
|
if (depRes) {
|
|
|
|
depRes = JSON.parse(depRes)
|
|
|
|
depRes = depRes.departments
|
|
|
|
}
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = depRes || []
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function allUsers (ctx) {
|
|
|
|
try {
|
|
|
|
const { models } = ctx.fs.dc;
|
|
|
|
const { clickHouse } = ctx.app.fs
|
|
|
|
|
|
|
|
const userRes = await clickHouse.pepEmis.query(`
|
|
|
|
SELECT id, name FROM user
|
|
|
|
WHERE delete = '0'
|
|
|
|
`).toPromise()
|
|
|
|
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = userRes
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: error`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: typeof error == 'string' ? error : undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function editUser (ctx) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { pomsUserId, pepUserId, role = [], correlationProject = [] } = ctx.request.body
|
|
|
|
|
|
|
|
const existUserRes = await models.User.findOne({
|
|
|
|
where: {
|
|
|
|
pepUserId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (
|
|
|
|
existUserRes && !existUserRes.deleted && !pomsUserId &&
|
|
|
|
(
|
|
|
|
(existUserRes.role.includes('admin') && role.includes('admin'))
|
|
|
|
|| (!existUserRes.role.includes('admin') && !role.includes('admin'))
|
|
|
|
// 都有或都没有管理员
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// 新增已存在未删除
|
|
|
|
throw '人员账号已添加'
|
|
|
|
}
|
|
|
|
|
|
|
|
let storageData = {
|
|
|
|
pepUserId,
|
|
|
|
role,
|
|
|
|
correlationProject,
|
|
|
|
updateTime: moment().format()
|
|
|
|
}
|
|
|
|
if (existUserRes) {
|
|
|
|
// 存在且传递id 或者 不传id也存在
|
|
|
|
// 修改 update
|
|
|
|
storageData.deleted = false
|
|
|
|
if (
|
|
|
|
role.includes('admin')
|
|
|
|
) {
|
|
|
|
if (existUserRes.role.includes('admin')) {
|
|
|
|
// 已是管理员
|
|
|
|
throw '当前人员已是管理员'
|
|
|
|
}
|
|
|
|
// 正在修改为管理员
|
|
|
|
storageData.disabled = true
|
|
|
|
storageData.role = [...new Set([...existUserRes.role, ...role])]
|
|
|
|
} else if (existUserRes.role.includes('admin')) {
|
|
|
|
// 正在修改成员 但是此时还是管理员
|
|
|
|
storageData.disabled = true
|
|
|
|
storageData.role = [...role, 'admin']
|
|
|
|
}
|
|
|
|
await models.User.update(storageData, {
|
|
|
|
where: {
|
|
|
|
pepUserId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// 新增
|
|
|
|
await models.User.create(storageData)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.status = 204
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: typeof error == 'string' ? error : undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function putUser (ctx) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { pomsUserId } = ctx.params
|
|
|
|
const { disabled = undefined, deleted = undefined } = ctx.request.body
|
|
|
|
const existUserRes = await models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: pomsUserId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (existUserRes && existUserRes.role.includes('admin') && disabled == false) {
|
|
|
|
throw '成员不能既是管理员又是普通成员'
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateData = {
|
|
|
|
disabled,
|
|
|
|
deleted,
|
|
|
|
}
|
|
|
|
for (let k in updateData) {
|
|
|
|
if (updateData[k] == undefined) {
|
|
|
|
delete updateData[k]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await models.User.update(updateData, {
|
|
|
|
where: {
|
|
|
|
id: pomsUserId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ctx.status = 204
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: typeof error == 'string' ? error : undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function delAdmin (ctx) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { pomsUserId } = ctx.params
|
|
|
|
|
|
|
|
const existUserRes = await models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: pomsUserId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (existUserRes) {
|
|
|
|
let updateValues = existUserRes.dataValues
|
|
|
|
let adminIndex = updateValues.role.findIndex(r => r == 'admin')
|
|
|
|
if (adminIndex > -1) {
|
|
|
|
updateValues.role.splice(adminIndex, 1)
|
|
|
|
}
|
|
|
|
await models.User.update(updateValues, {
|
|
|
|
where: {
|
|
|
|
id: pomsUserId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.status = 204;
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
message: typeof error == 'string' ? error : undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function user (ctx) {
|
|
|
|
try {
|
|
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const { clickHouse } = ctx.app.fs
|
|
|
|
const sequelize = ctx.fs.dc.orm;
|
|
|
|
|
|
|
|
const { role, limit, page, } = ctx.query
|
|
|
|
|
|
|
|
const excludeField = ['lastInTime', 'inTimes', 'onlineDuration', 'lastInAddress', 'deleted', 'updateTime']
|
|
|
|
|
|
|
|
let findOption = {
|
|
|
|
attributes: {
|
|
|
|
exclude: excludeField,
|
|
|
|
// include: [[sequelize.fn('array_length', sequelize.col('role')), 'roleCount']]
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
deleted: false,
|
|
|
|
$or: [{
|
|
|
|
$not: {
|
|
|
|
role: { $contained: ['SuperAdmin', 'admin'] }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sequelize.where(sequelize.fn('cardinality', sequelize.col('role')), 0)],
|
|
|
|
// $not: {
|
|
|
|
// role: { $contained: ['SuperAdmin', 'admin'] }
|
|
|
|
// }
|
|
|
|
},
|
|
|
|
order: [['updateTime', 'DESC']]
|
|
|
|
}
|
|
|
|
if (role) {
|
|
|
|
findOption.where.role = { $contains: [role] }
|
|
|
|
}
|
|
|
|
if (limit) {
|
|
|
|
findOption.limit = limit
|
|
|
|
}
|
|
|
|
if (page && limit) {
|
|
|
|
findOption.offset = page * limit
|
|
|
|
}
|
|
|
|
|
|
|
|
const userRes = await models.User.findAndCountAll(findOption)
|
|
|
|
|
|
|
|
const adminRes = await models.User.findAll({
|
|
|
|
where: {
|
|
|
|
role: { $contains: ['admin'] }
|
|
|
|
},
|
|
|
|
attributes: {
|
|
|
|
exclude: excludeField,
|
|
|
|
},
|
|
|
|
order: [['updateTime', 'DESC']]
|
|
|
|
})
|
|
|
|
|
|
|
|
let userIds = new Set()
|
|
|
|
let pomsProjectIds = new Set()
|
|
|
|
for (let u of userRes.rows.concat(adminRes)) {
|
|
|
|
userIds.add(u.pepUserId)
|
|
|
|
for (let pid of u.correlationProject) {
|
|
|
|
pomsProjectIds.add(pid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 查用户所属的项企pep的部门、人员信息
|
|
|
|
let userPepRes = userIds.size ?
|
|
|
|
await clickHouse.pepEmis.query(`
|
|
|
|
SELECT DISTINCT
|
|
|
|
user.id AS id, "user"."name" AS name, department.name AS depName, department.id AS depId
|
|
|
|
FROM department_user
|
|
|
|
LEFT JOIN user
|
|
|
|
ON department_user.user=user.id
|
|
|
|
LEFT JOIN department
|
|
|
|
ON department.id=department_user.department
|
|
|
|
WHERE
|
|
|
|
user.id IN (${[...userIds].join(',')},-1)
|
|
|
|
AND department.delete='0'`
|
|
|
|
).toPromise() :
|
|
|
|
[]
|
|
|
|
|
|
|
|
// 查用户绑定的当前系统的项目 id
|
|
|
|
let pomsProjectRes = await models.ProjectCorrelation.findAll({
|
|
|
|
where: {
|
|
|
|
id: { $in: [...pomsProjectIds] },
|
|
|
|
// del: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// 获取响应的绑定的 项企项目的 id
|
|
|
|
let pepPojectIds = new Set()
|
|
|
|
for (let p of pomsProjectRes) {
|
|
|
|
if (p.pepProjectId) {
|
|
|
|
pepPojectIds.add(p.pepProjectId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 查对应的项企项目信息
|
|
|
|
let pepProjectRes = pepPojectIds.size ?
|
|
|
|
await clickHouse.projectManage.query(`
|
|
|
|
SELECT id, project_name, isdelete FROM t_pim_project WHERE id IN (${[...pepPojectIds]},-1)
|
|
|
|
`).toPromise() :
|
|
|
|
[]
|
|
|
|
|
|
|
|
// 遍历用户并将查到的信息拼合
|
|
|
|
for (let u of userRes.rows.concat(adminRes)) {
|
|
|
|
// 用户信息
|
|
|
|
const corUsers = userPepRes.filter(up => up.id == u.pepUserId)
|
|
|
|
u.dataValues.name = corUsers.length ? corUsers[0].name : ''
|
|
|
|
u.dataValues.departments = corUsers.length ? corUsers.map(cu => {
|
|
|
|
return {
|
|
|
|
name: cu.depName,
|
|
|
|
id: cu.depId
|
|
|
|
}
|
|
|
|
}) : []
|
|
|
|
// pep项目信息
|
|
|
|
u.dataValues.correlationProject = u.dataValues.correlationProject.map(cpid => {
|
|
|
|
let returnData = {
|
|
|
|
id: cpid,
|
|
|
|
}
|
|
|
|
const corPomsProject = pomsProjectRes.find(ppr => ppr.id == cpid)
|
|
|
|
if (corPomsProject) {
|
|
|
|
returnData.name = corPomsProject.name
|
|
|
|
returnData.del = corPomsProject.del
|
|
|
|
if (corPomsProject.pepProjectId) {
|
|
|
|
returnData.pepProjectId = corPomsProject.pepProjectId
|
|
|
|
const corPepProject = pepProjectRes.find(ppr => ppr.id == corPomsProject.pepProjectId)
|
|
|
|
if (corPepProject) {
|
|
|
|
returnData.pepProjectName = corPepProject.project_name
|
|
|
|
returnData.pepIsdelete = corPepProject.isdelete
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnData
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = {
|
|
|
|
admin: adminRes,
|
|
|
|
users: userRes
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
allDeps,
|
|
|
|
editUser,
|
|
|
|
putUser,
|
|
|
|
delAdmin,
|
|
|
|
user,
|
|
|
|
allUsers,
|
|
|
|
};
|