tender_permission.js 10 KB

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