tender_permission.js 12 KB

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