sub_proj_permission.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. delete: { title: '删除文件', value: 4 },
  30. filing: { title: '文件类别编辑', value: 3 },
  31. },
  32. manage: {
  33. rela: { title: '关联标段', value: 1 },
  34. }
  35. };
  36. }
  37. get adminPermission () {
  38. return {
  39. budget_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.budget, 'value'),
  40. file_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.file, 'value'),
  41. manage_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.manage, 'value'),
  42. filing_type: 'all',
  43. }
  44. }
  45. async showSubTab(uid, type) {
  46. const sql = `SELECT count(*) as count FROM ${this.tableName} WHERE ${type}_permission <> '' AND uid = ?`;
  47. const result = await this.db.queryOne(sql, [uid]);
  48. return result.count;
  49. }
  50. async showBudget(uid) {
  51. return await this.showSubTab(uid, 'budget');
  52. }
  53. async showFile(uid) {
  54. return await this.showSubTab(uid, 'file');
  55. }
  56. parsePermission(data) {
  57. const _ = this.ctx.helper._;
  58. const datas = data instanceof Array ? data : [data];
  59. datas.forEach(x => {
  60. x.budget_permission = x.budget_permission ? _.map(x.budget_permission.split(','), _.toInteger) : [];
  61. x.file_permission = x.file_permission ? _.map(x.file_permission.split(','), _.toInteger) : [];
  62. x.manage_permission = x.manage_permission ? _.map(x.manage_permission.split(','), _.toInteger) : [];
  63. x.filing_type = x.filing_type ? _.map(x.filing_type.split(','), _.toInteger): [];
  64. });
  65. }
  66. async getPermission(subProjectId) {
  67. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  68. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  69. On spp.uid = p.id WHERE spp.spid = ?`, [subProjectId]);
  70. this.parsePermission(result);
  71. return result;
  72. }
  73. async getBudgetPermission(subProjectId) {
  74. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  75. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  76. On spp.uid = p.id WHERE spp.spid = ? and budget_permission <> ''`, [subProjectId]);
  77. this.parsePermission(result);
  78. return result;
  79. }
  80. async getUserPermission(pid, uid) {
  81. const result = await this.getAllDataByCondition({
  82. where: { uid: this.ctx.session.sessionUser.accountId, pid: this.ctx.session.sessionProject.id }
  83. });
  84. this.parsePermission(result);
  85. return result;
  86. }
  87. async getBudgetUserPermission(bid) {
  88. const subProj = await this.service.subProject.getDataByCondition({ budget_id: bid });
  89. const result = await this.getDataByCondition({ spid: subProj.id, uid: this.ctx.session.sessionUser.accountId });
  90. if (result) this.parsePermission(result);
  91. return result;
  92. }
  93. async getSubProjectUserPermission(spid, uid) {
  94. const result = await this.getDataByCondition({ spid, uid });
  95. if (result) this.parsePermission(result);
  96. return result;
  97. };
  98. async savePermission(subProjectId, member) {
  99. const orgMember = await this.getAllDataByCondition({ where: { spid: subProjectId } });
  100. const dm = [], um = [], im = [];
  101. for (const om of orgMember) {
  102. const nm = member.find(x => { return om.uid === x.uid; });
  103. if (!nm) {
  104. dm.push(om.id);
  105. } else {
  106. um.push({
  107. id: om.id, budget_permission: nm.budget_permission.join(','),
  108. file_permission: nm.file_permission.join(','),
  109. manage_permission: nm.manage_permission.join(',')
  110. });
  111. member.splice(member.indexOf(nm), 1);
  112. }
  113. }
  114. for (const m of member) {
  115. im.push({
  116. id: this.uuid.v4(),
  117. spid: subProjectId, pid: this.ctx.session.sessionProject.id, uid: m.uid,
  118. budget_permission: m.budget_permission.join(','),
  119. file_permission: m.file_permission.join(','),
  120. manage_permission: m.manage_permission.join(',')
  121. });
  122. }
  123. const conn = await this.db.beginTransaction();
  124. try {
  125. if (dm.length > 0) await conn.delete(this.tableName, { id: dm });
  126. if (um.length > 0) await conn.updateRows(this.tableName, um);
  127. if (im.length > 0) await conn.insert(this.tableName, im);
  128. await conn.commit();
  129. } catch (err) {
  130. await conn.rollback();
  131. throw err;
  132. }
  133. }
  134. async getFilingType(subProjectId) {
  135. const permissionConst = {}, prefix = 'f';
  136. for (const p in this.PermissionConst.file) {
  137. const fp = this.PermissionConst.file[p];
  138. permissionConst[prefix + fp.value] = fp.title;
  139. }
  140. const result = await this.db.query(`SELECT spp.id, p.name, p.role, p.company, p.mobile, spp.file_permission, spp.filing_type, spp.create_time
  141. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  142. On spp.uid = p.id WHERE spp.spid = ? and file_permission <> ''`, [subProjectId]);
  143. result.forEach(x => {
  144. const filePermission = x.file_permission.split(',');
  145. x.file_permission = filePermission.map(x => {
  146. return permissionConst[prefix + x] || '';
  147. }).join(',');
  148. });
  149. return result;
  150. }
  151. // 资料归集,授权固定分类
  152. async saveFilingType(data) {
  153. const updateData = [];
  154. data.forEach(x => {
  155. updateData.push({ id: x.id, filing_type: x.filing_type });
  156. });
  157. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  158. }
  159. }
  160. return subProjPermission;
  161. };