sub_proj_permission.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class subProjPermission extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @param {String} tableName - 表名
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'sub_project_permission';
  21. this.PermissionConst = {
  22. budget: {
  23. view: { title: '查看', value: 1 },
  24. edit: { title: '编辑', value: 2 },
  25. },
  26. file: {
  27. view: { title: '查看', value: 1 },
  28. upload: { title: '上传文件', value: 2 },
  29. edit: { title: '文件类别编辑', value: 3 },
  30. },
  31. manage: {
  32. rela: { title: '关联标段', value: 1 },
  33. }
  34. };
  35. }
  36. async showSubTab(uid, type) {
  37. const sql = `SELECT count(*) FROM ${this.tableName} WHERE ${type}_permission <> '' AND uid = ?`;
  38. const result = await this.db.queryOne(sql, [uid]);
  39. return result.count;
  40. }
  41. async showBudget(uid) {
  42. return await this.showSubTab(uid, 'budget');
  43. }
  44. async showFile(uid) {
  45. return await this.showSubTab(uid, 'file');
  46. }
  47. parsePermission(data) {
  48. const _ = this.ctx.helper._;
  49. const datas = data instanceof Array ? data : [data];
  50. datas.forEach(x => {
  51. x.budget_permission = x.budget_permission ? _.map(x.budget_permission.split(','), _.toInteger) : [];
  52. x.file_permission = x.file_permission ? _.map(x.file_permission.split(','), _.toInteger) : [];
  53. x.manage_permission = x.manage_permission ? _.map(x.manage_permission.split(','), _.toInteger) : [];
  54. });
  55. }
  56. async getPermission(subProjectId) {
  57. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  58. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  59. On spp.uid = p.id WHERE spp.spid = ?`, [subProjectId]);
  60. this.parsePermission(result);
  61. return result;
  62. }
  63. async getBudgetPermission(subProjectId) {
  64. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  65. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  66. On spp.uid = p.id WHERE spp.spid = ? and budget_permission <> ''`, [subProjectId]);
  67. this.parsePermission(result);
  68. return result;
  69. }
  70. async getUserPermission(pid, uid) {
  71. const result = await this.getAllDataByCondition({
  72. where: { uid: this.ctx.session.sessionUser.accountId, pid: this.ctx.session.sessionProject.id }
  73. });
  74. this.parsePermission(result);
  75. return result;
  76. }
  77. async getBudgetUserPermission(bid) {
  78. const _ = this.ctx.helper._;
  79. const result = await this.getDataByCondition({uid: this.ctx.session.sessionUser.accountId, bid});
  80. if (result) this.parsePermission(result);
  81. return result;
  82. }
  83. async savePermission(subProjectId, member) {
  84. const orgMember = await this.getAllDataByCondition({ where: { spid: subProjectId } });
  85. const dm = [], um = [], im = [];
  86. for (const om of orgMember) {
  87. const nm = member.find(x => { return om.uid === x.uid; });
  88. if (!nm) {
  89. dm.push(om.id);
  90. } else {
  91. um.push({
  92. id: om.id, budget_permission: nm.budget_permission.join(','),
  93. file_permission: nm.file_permission.join(','),
  94. manage_permission: nm.manage_permission.join(',')
  95. });
  96. member.splice(member.indexOf(nm), 1);
  97. }
  98. }
  99. for (const m of member) {
  100. im.push({
  101. spid: subProjectId, pid: this.ctx.session.sessionProject.id, uid: m.uid,
  102. budget_permission: m.budget_permission.join(','),
  103. file_permission: m.file_permission.join(','),
  104. manage_permission: m.manage_permission.join(',')
  105. })
  106. }
  107. const conn = await this.db.beginTransaction();
  108. try {
  109. if (dm.length > 0) await conn.delete(this.tableName, { id: dm });
  110. if (um.length > 0) await conn.updateRows(this.tableName, um);
  111. if (im.length > 0) await conn.insert(this.tableName, im);
  112. await conn.commit();
  113. } catch (err) {
  114. await conn.rollback();
  115. throw err;
  116. }
  117. }
  118. }
  119. return subProjPermission;
  120. };