sub_proj_permission.js 7.0 KB

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