tender_permission.js 12 KB

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