budget_permission.js 4.8 KB

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