You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.1 KiB
71 lines
2.1 KiB
'use strict';
|
|
|
|
async function getLabels(ctx, next) {
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
|
|
const res = await models.QrcodeLabels.findAll({
|
|
order: [['id', 'DESC']],
|
|
})
|
|
|
|
ctx.status = 200;
|
|
ctx.body = res
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = {
|
|
"message": "获取标签失败"
|
|
}
|
|
}
|
|
}
|
|
|
|
async function createLabels(ctx, next) {
|
|
const models = ctx.fs.dc.models;
|
|
try {
|
|
let rslt = ctx.request.body;
|
|
await models.Department.create(Object.assign({}, rslt, { read: 1 }))
|
|
ctx.status = 204;
|
|
ctx.body = { message: '新建部门成功' }
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = { message: '新建部门失败' }
|
|
}
|
|
}
|
|
|
|
async function delLabels(ctx, next) {
|
|
let errMsg = "删除部门失败";
|
|
try {
|
|
const models = ctx.fs.dc.models;
|
|
const { id } = ctx.params;
|
|
let list = await models.Department.findAll({});
|
|
let deptIds = list.map(l => l.id);
|
|
const allUsers = await models.User.findAll({
|
|
where: {
|
|
departmentId: { $in: deptIds },
|
|
delete: false
|
|
}
|
|
})
|
|
const childrenDept = await models.Department.findAll({ where: { dependence: id } })
|
|
const childrenUser = allUsers.filter(au => au.departmentId == id);
|
|
if (childrenUser.length || childrenDept.length) {
|
|
errMsg = '请先删除部门下的用户或子部门';
|
|
throw errMsg;
|
|
}
|
|
await models.Department.destroy({ where: { id: id } });
|
|
await models.Department.destroy({ where: { dependence: id } });
|
|
ctx.status = 204;
|
|
ctx.body = { message: '删除部门成功' }
|
|
} catch (error) {
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
|
|
ctx.status = 400;
|
|
ctx.body = { message: error }
|
|
}
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
getLabels,
|
|
createLabels,
|
|
delLabels,
|
|
}
|