1
0

dept.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import BaseController from '../../base';
  2. import { AdminRoute } from '../../../decorator/router_register';
  3. import { CreateDeptDto, DeleteDeptDto, InfoDeptDto, UpdateDeptDto, TransferDeptDto } from '../../../dto/admin/sys/dept';
  4. /**
  5. * 系统部门控制器
  6. */
  7. export default class SysDeptController extends BaseController {
  8. /**
  9. * @api {get} /admin/sys/dept/list 获取部门列表
  10. * @apiGroup 系统部门
  11. * @apiUse Auth
  12. * @apiUse BaseRes
  13. */
  14. @AdminRoute('/sys/dept/list', 'get')
  15. async list() {
  16. this.res(
  17. {
  18. data: await this.service.admin.sys.dept.getDepts(this.ctx.token.uid),
  19. },
  20. );
  21. }
  22. /**
  23. * @api {post} /admin/sys/dept/add 增加部门
  24. * @apiGroup 系统部门
  25. * @apiUse Auth
  26. * @apiUse BaseRes
  27. * @apiParam {Number} parentDepartmentId 父级部门编号,没有则为-1
  28. * @apiParam {String} departmentName 部门名称
  29. */
  30. @AdminRoute('/sys/dept/add', 'post')
  31. async add() {
  32. const dto = await this.ctx.validate<CreateDeptDto>(CreateDeptDto);
  33. await this.service.admin.sys.dept.add(dto.departmentName, dto.parentDepartmentId);
  34. this.res();
  35. }
  36. /**
  37. * @api {post} /admin/sys/dept/delete 删除部门
  38. * @apiGroup 系统部门
  39. * @apiUse Auth
  40. * @apiUse BaseRes
  41. * @apiParam {Number} departmentId 部门编号
  42. */
  43. @AdminRoute('/sys/dept/delete', 'post')
  44. async delete() {
  45. const dto = await this.ctx.validate<DeleteDeptDto>(DeleteDeptDto);
  46. // 查询是否有关联用户,如果含有则无法删除
  47. const count = await this.service.admin.sys.dept.countUserByDeptId(dto.departmentId);
  48. if (count > 0) {
  49. this.res({
  50. code: 10009,
  51. });
  52. return;
  53. }
  54. // 查询是否有关联的角色,角色与部门存在数据相互绑定
  55. const count2 = await this.service.admin.sys.dept.countRoleByDeptId(dto.departmentId);
  56. if (count2) {
  57. this.res({
  58. code: 10010,
  59. });
  60. return;
  61. }
  62. await this.service.admin.sys.dept.delete(dto.departmentId);
  63. this.res();
  64. }
  65. /**
  66. * @api {get} /admin/sys/dept/info 获取部门信息
  67. * @apiGroup 系统部门
  68. * @apiUse Auth
  69. * @apiUse BaseRes
  70. * @apiParam {Number} departmentId 部门编号
  71. * @apiSuccess {SysDepartment} data 部门信息实体
  72. */
  73. @AdminRoute('/sys/dept/info', 'get')
  74. async info() {
  75. const q = await this.ctx.validate<InfoDeptDto>(InfoDeptDto, this.getQuery());
  76. this.res({
  77. data: await this.service.admin.sys.dept.info(parseInt(q.departmentId)),
  78. });
  79. }
  80. /**
  81. * @api {post} /admin/sys/dept/update 获取部门信息
  82. * @apiGroup 系统部门
  83. * @apiUse Auth
  84. * @apiUse BaseRes
  85. * @apiParam {Number} id 部门编号
  86. * @apiParam {Number} parentId 父级部门编号,没有则为-1
  87. * @apiParam {String} name 部门名称
  88. * @apiParam {Number} orderNum 排序
  89. */
  90. @AdminRoute('/sys/dept/update', 'post')
  91. async update() {
  92. const dto = await this.ctx.validate<UpdateDeptDto>(UpdateDeptDto);
  93. await this.service.admin.sys.dept.update(dto);
  94. this.res();
  95. }
  96. /**
  97. * @api {post} /admin/sys/dept/transfer 人员部门转移
  98. * @apiGroup 系统部门
  99. * @apiUse Auth
  100. * @apiUse BaseRes
  101. * @apiParam {Number[]} userIds 管理员编号列表
  102. * @apiParam {Number} departmentId 需要转移去的部门编号
  103. */
  104. @AdminRoute('/sys/dept/transfer', 'post')
  105. async transfer() {
  106. const dto = await this.ctx.validate<TransferDeptDto>(TransferDeptDto);
  107. await this.service.admin.sys.dept.transfer(dto.userIds, dto.departmentId);
  108. this.res();
  109. }
  110. }