tender_permission.js 11 KB

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