tender_permission.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2025/7/17
  7. * @version
  8. */
  9. module.exports = app => {
  10. class tenderPermission 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 = 'tender_permission';
  21. this.PermissionConst = {
  22. quality: {
  23. view: { title: '查看', value: 1, isDefault: 1 },
  24. upload: { title: '上传文件', value: 2 },
  25. add: { title: '新增功能', value: 3 },
  26. add_inspection: { title: '添加质量巡检', value: 4 },
  27. },
  28. };
  29. this.PermissionBlock = [
  30. { key: 'quality', name: '质量管理', field: 'quality' },
  31. ];
  32. for (const p of this.PermissionBlock) {
  33. if (p.children) {
  34. for (const c of p.children) {
  35. c.permission = [];
  36. const pConst = this.PermissionConst[c.key];
  37. if (!pConst) continue;
  38. for (const prop in pConst) {
  39. c.permission.push({ key: prop, ...pConst[prop]});
  40. }
  41. }
  42. } else {
  43. p.permission = [];
  44. const pConst = this.PermissionConst[p.key];
  45. if (!pConst) continue;
  46. for (const prop in pConst) {
  47. p.permission.push({ key: prop, ...pConst[prop]});
  48. }
  49. }
  50. }
  51. this.AdminPermission = {};
  52. for (const p in this.PermissionConst) {
  53. this.AdminPermission[p] = this.ctx.helper.mapAllSubField(this.PermissionConst[p], 'value');
  54. }
  55. }
  56. partPermissionConst(part) {
  57. if (!part) return this.PermissionConst;
  58. const parts = part instanceof Array ? part : [part];
  59. const result = {};
  60. for (const p of parts) {
  61. result[p] = this.PermissionConst[p];
  62. }
  63. return result;
  64. }
  65. partPermissionBlock(part) {
  66. if (!part) return this.PermissionBlock;
  67. const parts = part instanceof Array ? part : [part];
  68. const result = this.PermissionBlock.filter(x => { return parts.indexOf(x.key) >= 0; });
  69. return result;
  70. }
  71. parsePermission(data) {
  72. const _ = this.ctx.helper._;
  73. const datas = data instanceof Array ? data : [data];
  74. datas.forEach(x => {
  75. for (const p in this.PermissionConst) {
  76. x[p] = x[p] ? _.map(x[p].split(','), _.toInteger) : [];
  77. }
  78. });
  79. }
  80. // 权限检查
  81. conversePermission(permission) {
  82. const result = {};
  83. for (const block of this.PermissionBlock) {
  84. const per = {};
  85. for(const p of block.permission) {
  86. per[p.key] = permission[block.key].indexOf(p.value) >= 0;
  87. }
  88. result[block.key] = per;
  89. }
  90. return result;
  91. }
  92. getAdminPermission() {
  93. return this.conversePermission(this.AdminPermission);
  94. }
  95. async getUserPermission(tid, uid) {
  96. const result = await this.getDataByCondition({ tid, uid });
  97. this.parsePermission(result);
  98. return this.conversePermission(result);
  99. }
  100. // 用户权限编辑
  101. async getPartsPermission(tid, parts) {
  102. if (!parts || parts.length === 0) return [];
  103. const partSql = parts.map(x => { return `${x} <> ''`}).join(' OR ');
  104. const sql = `SELECT qp.*, pa.name, pa.role FROM ${this.tableName} qp LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON qp.uid = pa.id WHERE qp.tid = ? AND (${partSql}) ORDER BY qp.create_time DESC`;
  105. const result = await this.db.query(sql, [tid]);
  106. this.parsePermission(result);
  107. return result;
  108. }
  109. async savePermission(tid, member, permissionBlock) {
  110. const orgMember = await this.getAllDataByCondition({ where: { tid } });
  111. const updateMember = [], insertMember = [];
  112. for (const om of orgMember) {
  113. const nmi = member.findIndex(x => { return om.uid == x.uid; });
  114. if (nmi < 0) {
  115. const um = { id: om.id };
  116. for (const p of permissionBlock) {
  117. um[p] = '';
  118. }
  119. updateMember.push(um);
  120. } else {
  121. const nm = member[nmi];
  122. const um = { id: om.id };
  123. for (const p in this.PermissionConst) {
  124. if (nm[p]) um[p] = nm[p].join(',');
  125. }
  126. updateMember.push(um);
  127. member.splice(nmi, 1);
  128. }
  129. }
  130. for (const m of member) {
  131. const im = { id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id, spid: this.ctx.subProject.id, tid, uid: m.uid };
  132. for (const p in this.PermissionConst) {
  133. if (m[p]) im[p] = m[p].join(',');
  134. }
  135. insertMember.push(im);
  136. }
  137. const conn = await this.db.beginTransaction();
  138. try {
  139. if (updateMember.length > 0) await conn.updateRows(this.tableName, updateMember);
  140. if (insertMember.length > 0) await conn.insert(this.tableName, insertMember);
  141. await conn.commit();
  142. } catch (err) {
  143. await conn.rollback();
  144. throw err;
  145. }
  146. }
  147. async saveOnePermission(tid, uids, member, permissionBlock, transaction = null) {
  148. const orgMember = await this.getAllDataByCondition({ where: { tid, uid: uids } });
  149. const updateMember = [], insertMember = [];
  150. for (const om of orgMember) {
  151. const nmi = member.findIndex(x => { return om.uid == x.uid; });
  152. if (nmi < 0) {
  153. const um = { id: om.id };
  154. for (const p of permissionBlock) {
  155. um[p] = '';
  156. }
  157. updateMember.push(um);
  158. } else {
  159. const nm = member[nmi];
  160. const um = { id: om.id };
  161. for (const p in this.PermissionConst) {
  162. if (nm[p]) um[p] = nm[p].join(',');
  163. }
  164. updateMember.push(um);
  165. member.splice(nmi, 1);
  166. }
  167. }
  168. for (const m of member) {
  169. const im = { id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id, spid: this.ctx.subProject.id, tid, uid: m.uid };
  170. for (const p in this.PermissionConst) {
  171. if (m[p]) im[p] = m[p].join(',');
  172. }
  173. insertMember.push(im);
  174. }
  175. if (!transaction) {
  176. const conn = await this.db.beginTransaction();
  177. try {
  178. if (updateMember.length > 0) await conn.updateRows(this.tableName, updateMember);
  179. if (insertMember.length > 0) await conn.insert(this.tableName, insertMember);
  180. await conn.commit();
  181. } catch (err) {
  182. await conn.rollback();
  183. throw err;
  184. }
  185. } else {
  186. if (updateMember.length > 0) await transaction.updateRows(this.tableName, updateMember);
  187. if (insertMember.length > 0) await transaction.insert(this.tableName, insertMember);
  188. }
  189. }
  190. async setOtherTender(tidList, userList, permissionBlock) {
  191. // 根据标段找出创建人去除,已存在的先删除再插入
  192. const transaction = await this.db.beginTransaction();
  193. try {
  194. const tenderList = await this.ctx.service.tender.getAllDataByCondition({
  195. columns: ['id', 'user_id'],
  196. where: { id: tidList.split(',') },
  197. });
  198. const oldTouristList = await this.getAllDataByCondition({ where: { tid: tidList.split(',') } });
  199. const insertData = [];
  200. const updateData = [];
  201. for (const user of userList) {
  202. for (const t of tenderList) {
  203. const updateInfo = this._.find(oldTouristList, { tid: t.id, uid: user.uid });
  204. // if (delId) deleteIdData.push(delId.id);
  205. if (updateInfo) {
  206. const um = { id: updateInfo.id };
  207. for (const p of permissionBlock) {
  208. um[p] = user.member.join(',');
  209. }
  210. updateData.push(um);
  211. } else if (user.uid !== t.user_id) {
  212. const um = { id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id, spid: this.ctx.subProject.id, tid: t.id, uid: user.uid };
  213. for (const p of permissionBlock) {
  214. um[p] = user.member.join(',');
  215. }
  216. insertData.push(um);
  217. }
  218. }
  219. }
  220. if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
  221. if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
  222. await transaction.commit();
  223. return true;
  224. } catch (err) {
  225. await transaction.rollback();
  226. throw err;
  227. }
  228. }
  229. }
  230. return tenderPermission;
  231. };