sub_proj_permission.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class subProjPermission 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 = 'sub_project_permission';
  21. this._definePermission();
  22. }
  23. _definePermission() {
  24. this.PermissionConst = {
  25. budget: {
  26. view: { title: '查看', value: 1 },
  27. edit: { title: '编辑', value: 2 },
  28. },
  29. file: {
  30. view: { title: '查看', value: 1 },
  31. upload: { title: '上传/引用', value: 2 },
  32. delete: { title: '删除文件', value: 4 },
  33. filing: { title: '文件类别编辑', value: 3 },
  34. },
  35. manage: {
  36. rela: { title: '关联标段', value: 1 },
  37. },
  38. info: {
  39. view: { title: '查看', value: 1},
  40. edit: { title: '编辑', value: 2 },
  41. },
  42. datacollect: {
  43. view: { title: '查看', value: 1},
  44. },
  45. contract: {
  46. edit: { title: '编辑节点', value: 1 },
  47. add: { title: '添加合同', value: 2 },
  48. node: { title: '授权节点下查看本节点合同', value: 3 },
  49. unit: { title: '授权节点下查看本单位合同', value: 4 },
  50. view: { title: '查看所有合同', value: 5 },
  51. att: { title: '上传附件', value: 6 },
  52. },
  53. fund_trans: {
  54. view: { title: '查看', value: 1 },
  55. add: { title: '新建划拨', value: 2 },
  56. att: { title: '上传附件', value: 3 },
  57. },
  58. fund_pay: {
  59. view: { title: '查看', value: 1 },
  60. att: { title: '上传附件', value: 3 },
  61. },
  62. payment: {
  63. view: { title: '查看', value: 1 },
  64. admin: { title: '模块管理员', value: 2 },
  65. view_all: { title: '查看所有标段', value: 3 },
  66. }
  67. };
  68. this.PermissionBlock = [
  69. { key: 'datacollect', name: '决策大屏', field: 'datacollect_permission' },
  70. { key: 'info', name: '项目概况', field: 'info_permission' },
  71. { key: 'contract', name: '合同管理', field: 'contract_permission', hint: ['1、编辑节点:编辑合同管理内页树结构',
  72. '2、添加合同:允许添加合同',
  73. '3、授权节点下查看本节点合同:授权节点下查看所有人上传的合同',
  74. '4、授权节点下查看本单位合同:授权节点下查看本单位人员添加的所有合同',
  75. '5、查看所有合同:未授权节点情况下可以查看包括其他单位人员添加的合同',
  76. '注:查看合同第3、4、5必须选择其一,否则无法查看本项目合同管理',
  77. ] },
  78. { key: 'file', name: '资料归集', field: 'file_permission' },
  79. { key: 'budget', name: '动态投资', field: 'budget_permission' },
  80. {
  81. key: 'financial', name: '资金监管', children: [
  82. { key: 'fund_trans', name: '资金划拨', field: 'fund_trans_permission' },
  83. { key: 'fund_pay', name: '资金支付', field: 'fund_pay_permission' },
  84. ]
  85. },
  86. { key: 'payment', name: '支付审批', field: 'payment_permission' },
  87. ];
  88. for (const p of this.PermissionBlock) {
  89. if (p.children) {
  90. for (const c of p.children) {
  91. c.permission = [];
  92. const pConst = this.PermissionConst[c.key];
  93. if (!pConst) continue;
  94. for (const prop in pConst) {
  95. c.permission.push({ key: prop, ...pConst[prop]});
  96. }
  97. }
  98. } else {
  99. p.permission = [];
  100. const pConst = this.PermissionConst[p.key];
  101. if (!pConst) continue;
  102. for (const prop in pConst) {
  103. p.permission.push({ key: prop, ...pConst[prop]});
  104. }
  105. }
  106. }
  107. }
  108. getRelaPermissionBlock(key) {
  109. for (const p of this.PermissionBlock) {
  110. if (p.key === key) return p;
  111. if (p.children) {
  112. for (const c of p.children) {
  113. if (c.key === key) return c;
  114. }
  115. }
  116. }
  117. }
  118. get adminPermission () {
  119. return {
  120. budget_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.budget, 'value'),
  121. file_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.file, 'value'),
  122. manage_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.manage, 'value'),
  123. filing_type: 'all',
  124. info_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.info, 'value'),
  125. datacollect_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.datacollect, 'value'),
  126. contract_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.contract, 'value'),
  127. fund_pay_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.fund_pay, 'value'),
  128. fund_trans_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.fund_trans, 'value'),
  129. payment_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.payment, 'value'),
  130. }
  131. }
  132. async showSubTab(uid, type, spid = '') {
  133. const spidSql = spid ? ` AND spid = "${spid}"` : '';
  134. const sql = `SELECT count(*) as count FROM ${this.tableName} WHERE ${type}_permission <> '' AND uid = ?` + spidSql;
  135. const result = await this.db.queryOne(sql, [uid]);
  136. return result.count;
  137. }
  138. async showBudget(uid) {
  139. return await this.showSubTab(uid, 'budget');
  140. }
  141. async showFile(uid) {
  142. return await this.showSubTab(uid, 'file');
  143. }
  144. async showPayment(uid, spid) {
  145. return await this.showSubTab(uid, 'payment', spid);
  146. }
  147. parsePermission(data) {
  148. const _ = this.ctx.helper._;
  149. const datas = data instanceof Array ? data : [data];
  150. datas.forEach(x => {
  151. x.budget_permission = x.budget_permission ? _.map(x.budget_permission.split(','), _.toInteger) : [];
  152. x.file_permission = x.file_permission ? _.map(x.file_permission.split(','), _.toInteger) : [];
  153. x.manage_permission = x.manage_permission ? _.map(x.manage_permission.split(','), _.toInteger) : [];
  154. x.info_permission = x.info_permission ? _.map(x.info_permission.split(','), _.toInteger) : [];
  155. x.datacollect_permission = x.datacollect_permission ? _.map(x.datacollect_permission.split(','), _.toInteger) : [];
  156. x.contract_permission = x.contract_permission ? _.map(x.contract_permission.split(','), _.toInteger) : [];
  157. x.fund_pay_permission = x.fund_pay_permission ? _.map(x.fund_pay_permission.split(','), _.toInteger) : [];
  158. x.fund_trans_permission = x.fund_trans_permission ? _.map(x.fund_trans_permission.split(','), _.toInteger) : [];
  159. x.filing_type = x.filing_type ? _.map(x.filing_type.split(','), _.toInteger): [];
  160. x.payment_permission = x.payment_permission ? _.map(x.payment_permission.split(','), _.toInteger) : [];
  161. });
  162. }
  163. async getPermission(subProjectId) {
  164. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  165. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  166. On spp.uid = p.id WHERE spp.spid = ?`, [subProjectId]);
  167. this.parsePermission(result);
  168. return result;
  169. }
  170. async getBudgetPermission(subProjectId) {
  171. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  172. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  173. On spp.uid = p.id WHERE spp.spid = ? and budget_permission <> ''`, [subProjectId]);
  174. this.parsePermission(result);
  175. return result;
  176. }
  177. async getUserPermission(pid, uid) {
  178. const result = await this.getAllDataByCondition({
  179. where: { uid: this.ctx.session.sessionUser.accountId, pid: this.ctx.session.sessionProject.id }
  180. });
  181. this.parsePermission(result);
  182. return result;
  183. }
  184. async getBudgetUserPermission(bid) {
  185. const subProj = await this.service.subProject.getDataByCondition({ budget_id: bid });
  186. const result = await this.getDataByCondition({ spid: subProj.id, uid: this.ctx.session.sessionUser.accountId });
  187. if (result) this.parsePermission(result);
  188. return result;
  189. }
  190. async getSubProjectUserPermission(spid, uid) {
  191. const result = await this.getDataByCondition({ spid, uid });
  192. if (result) this.parsePermission(result);
  193. return result;
  194. };
  195. async savePermission(subProjectId, member) {
  196. const orgMember = await this.getAllDataByCondition({ where: { spid: subProjectId } });
  197. const dm = [], um = [], im = [];
  198. for (const om of orgMember) {
  199. const nm = member.find(x => { return om.uid === x.uid; });
  200. if (!nm) {
  201. dm.push(om.id);
  202. } else {
  203. um.push({
  204. id: om.id, budget_permission: nm.budget_permission.join(','),
  205. file_permission: nm.file_permission.join(','),
  206. manage_permission: nm.manage_permission.join(',')
  207. });
  208. member.splice(member.indexOf(nm), 1);
  209. }
  210. }
  211. for (const m of member) {
  212. im.push({
  213. id: this.uuid.v4(),
  214. spid: subProjectId, pid: this.ctx.session.sessionProject.id, uid: m.uid,
  215. budget_permission: m.budget_permission.join(','),
  216. file_permission: m.file_permission.join(','),
  217. manage_permission: m.manage_permission.join(',')
  218. });
  219. }
  220. const conn = await this.db.beginTransaction();
  221. try {
  222. if (dm.length > 0) await conn.delete(this.tableName, { id: dm });
  223. if (um.length > 0) await conn.updateRows(this.tableName, um);
  224. if (im.length > 0) await conn.insert(this.tableName, im);
  225. await conn.commit();
  226. } catch (err) {
  227. await conn.rollback();
  228. throw err;
  229. }
  230. }
  231. async _addUser(subProject, data) {
  232. const ids = data instanceof Array ? data : [data];
  233. const exists = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } });
  234. if (exists.length > 0) throw '请勿重复选择账号';
  235. const insertData = ids.map(x => {
  236. return { id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: x };
  237. });
  238. await this.db.insert(this.tableName, insertData);
  239. return insertData;
  240. }
  241. async _delUser(subProject, data) {
  242. const ids = data instanceof Array ? data : [data];
  243. const permissions = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } });
  244. await this.db.delete(this.tableName, { id: permissions.map(x => { return x.id; }) });
  245. return data;
  246. }
  247. async _updateUserPermission(data) {
  248. const datas = data instanceof Array ? data : [data];
  249. const updateData = [];
  250. // const contractData = [];
  251. for (const x of datas) {
  252. const ud = { id: x.id };
  253. for (const p of this.PermissionBlock) {
  254. if (p.children) {
  255. for (const c of p.children) {
  256. if (x[c.field] !== undefined) ud[c.field] = x[c.field] || '';
  257. }
  258. } else {
  259. if (x[p.field] !== undefined) ud[p.field] = x[p.field] || '';
  260. // if (p.field === 'contract_permission') {
  261. // const spAudit = await this.getDataById(x.id);
  262. // if (spAudit) {
  263. // const contractAudit = await this.ctx.service.contractAudit.getDataByCondition({ spid: spAudit.spid, uid: spAudit.uid });
  264. // const contractPermission = x.contract_permission ? this._.map(x.contract_permission.split(','), this._.toInteger) : [];
  265. // const newContractPermission = contractPermission.length > 0 ? await this.getContractPermission(contractPermission) : [];
  266. // console.log(newContractPermission, contractAudit);
  267. // if (!contractAudit && contractPermission.length === 0) continue;
  268. // if (contractAudit) {
  269. // if (contractPermission.length === 0 || this._.intersection([3,4,5], contractPermission).length === 0) {
  270. // contractData.push({ delete: { id: contractAudit.id }});
  271. // } else if (contractAudit.permission_add !== newContractPermission.permission_add ||
  272. // contractAudit.permission_edit !== newContractPermission.permission_edit ||
  273. // contractAudit.permission_show_unit !== newContractPermission.permission_show_unit ||
  274. // contractAudit.permission_show_node !== newContractPermission.permission_show_node) {
  275. // contractData.push({ update: { id: contractAudit.id,
  276. // permission_add: newContractPermission.permission_add,
  277. // permission_edit: newContractPermission.permission_edit,
  278. // permission_show_unit: newContractPermission.permission_show_unit,
  279. // permission_show_node: newContractPermission.permission_show_node,
  280. // }});
  281. // }
  282. // } else if (!contractAudit && contractPermission.length > 0) {
  283. // contractData.push({ add: {
  284. // spid: spAudit.spid,
  285. // tid: null,
  286. // uid: spAudit.uid,
  287. // permission_add: newContractPermission.permission_add,
  288. // permission_edit: newContractPermission.permission_edit,
  289. // permission_show_unit: newContractPermission.permission_show_unit,
  290. // permission_show_node: newContractPermission.permission_show_node,
  291. // create_time: new Date(),
  292. // }});
  293. // }
  294. // }
  295. // }
  296. }
  297. }
  298. updateData.push(ud);
  299. }
  300. const conn = await this.db.beginTransaction();
  301. try {
  302. // if (contractData.length > 0) {
  303. // for (const d of contractData) {
  304. // if (d.add) {
  305. // await conn.insert(this.ctx.service.contractAudit.tableName, d.add);
  306. // } else if (d.update) {
  307. // await conn.update(this.ctx.service.contractAudit.tableName, d.update);
  308. // } else if (d.delete) {
  309. // await conn.delete(this.ctx.service.contractAudit.tableName, d.delete);
  310. // }
  311. // }
  312. // }
  313. await conn.updateRows(this.tableName, updateData);
  314. await conn.commit();
  315. } catch(err) {
  316. await conn.rollback();
  317. throw err;
  318. }
  319. return updateData;
  320. }
  321. async updatePermission(subProject, data) {
  322. const result = {};
  323. if (data.add) result.add = await this._addUser(subProject, data.add);
  324. if (data.del) result.del = await this._delUser(subProject, data.del);
  325. if (data.update) result.update = await this._updateUserPermission(data.update);
  326. return result;
  327. }
  328. async getFilingType(subProjectId) {
  329. const permissionConst = {}, prefix = 'f';
  330. for (const p in this.PermissionConst.file) {
  331. const fp = this.PermissionConst.file[p];
  332. permissionConst[prefix + fp.value] = fp.title;
  333. }
  334. const result = await this.db.query(`SELECT spp.id, p.name, p.role, p.company, p.mobile, spp.file_permission, spp.filing_type, spp.create_time
  335. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  336. On spp.uid = p.id WHERE spp.spid = ? and file_permission <> ''`, [subProjectId]);
  337. result.forEach(x => {
  338. const filePermission = x.file_permission.split(',');
  339. x.file_permission = filePermission.map(x => {
  340. return permissionConst[prefix + x] || '';
  341. }).join(',');
  342. });
  343. return result;
  344. }
  345. // 资料归集,授权固定分类
  346. async saveFilingType(data) {
  347. const updateData = [];
  348. data.forEach(x => {
  349. updateData.push({ id: x.id, filing_type: x.filing_type });
  350. });
  351. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  352. }
  353. checkViewPermission(ctx) {
  354. const permissionBlock = ctx.service.subProjPermission.PermissionBlock.find(x => { return x.key === ctx.controllerName; });
  355. if (!permissionBlock) return true;
  356. if (permissionBlock.children) {
  357. let canView = false;
  358. for (const c of permissionBlock.children) {
  359. const viewPermission = c.permission.find(x => { return x.key === 'view'; });
  360. if (!viewPermission || ctx.subProject.permission[c.field].indexOf(viewPermission.value) >= 0) canView = true;
  361. }
  362. return canView;
  363. } else if (ctx.controllerName === 'contract') {
  364. if (ctx.url.indexOf('contract/tender') >= 0) return true;
  365. const contractViewPermission = this._.map(permissionBlock.permission.filter(x => { return ['view', 'unit', 'node'].indexOf(x.key) >= 0; }), 'value');
  366. if (!contractViewPermission) return true;
  367. return this._.intersection(ctx.subProject.permission[permissionBlock.field], contractViewPermission).length > 0;
  368. } else {
  369. const viewPermission = permissionBlock.permission.find(x => { return x.key === 'view'; });
  370. if (!viewPermission) return true;
  371. return ctx.subProject.permission[permissionBlock.field].indexOf(viewPermission.value) >= 0;
  372. }
  373. };
  374. async getContractAuditList(spid, uid = null) {
  375. const uidSql = uid ? ' AND uid in (' + uid.join(',') + ')' : '';
  376. const sql = `SELECT * FROM ?? WHERE spid = ? AND contract_permission <> ''` + uidSql;
  377. const sqlParams = [this.tableName, spid];
  378. const result = await this.db.query(sql, sqlParams);
  379. const list = [];
  380. for (const r of result) {
  381. const permission = await this.getContractPermission(this._.map(r.contract_permission.split(','), this._.toInteger));
  382. list.push({
  383. id: r.id,
  384. spid: r.spid,
  385. tid: null,
  386. uid: r.uid,
  387. permission_add: permission.permission_add,
  388. permission_edit: permission.permission_edit,
  389. permission_show_node: permission.permission_show_node,
  390. permission_show_unit: permission.permission_show_unit,
  391. permission_att: permission.permission_att,
  392. });
  393. }
  394. return list;
  395. }
  396. async getNewContractPermission(permission, newContractPermission) {
  397. const oldPermission = await this.getContractPermission(this._.map(permission.split(','), this._.toInteger));
  398. if (newContractPermission.permission_add !== undefined) {
  399. oldPermission.permission_add = newContractPermission.permission_add;
  400. }
  401. if (newContractPermission.permission_edit !== undefined) {
  402. oldPermission.permission_edit = newContractPermission.permission_edit;
  403. }
  404. if (newContractPermission.permission_show_node !== undefined) {
  405. oldPermission.permission_show_node = newContractPermission.permission_show_node;
  406. }
  407. if (newContractPermission.permission_show_unit !== undefined) {
  408. oldPermission.permission_show_unit = newContractPermission.permission_show_unit;
  409. }
  410. if (newContractPermission.permission_att !== undefined) {
  411. oldPermission.permission_att = newContractPermission.permission_att;
  412. }
  413. const permissionArr = [];
  414. if (oldPermission.permission_edit) permissionArr.push(1);
  415. if (oldPermission.permission_add) permissionArr.push(2);
  416. if (oldPermission.permission_show_node) permissionArr.push(3);
  417. if (oldPermission.permission_show_unit) permissionArr.push(4);
  418. if (!oldPermission.permission_show_unit && !oldPermission.permission_show_node) permissionArr.push(5);
  419. if (oldPermission.permission_att) permissionArr.push(6);
  420. return permissionArr.join(',');
  421. }
  422. async getContractPermission(cp) {
  423. const permission = {
  424. permission_edit: cp.indexOf(1) !== -1 ? 1: 0,
  425. permission_add: cp.indexOf(2) !== -1 ? 1: 0,
  426. permission_show_node: cp.indexOf(3) !== -1 ? 1: 0,
  427. permission_show_unit: cp.indexOf(4) !== -1 ? 1: 0,
  428. permission_att: cp.indexOf(6) !== -1 ? 1: 0,
  429. };
  430. return permission;
  431. }
  432. async getFinancailPermission(trans_permission, pay_permission) {
  433. const permission = {
  434. transfer_show: trans_permission.indexOf(1) !== -1,
  435. transfer_add: trans_permission.indexOf(2) !== -1,
  436. transfer_file: trans_permission.indexOf(3) !== -1,
  437. pay_show: pay_permission.indexOf(1) !== -1,
  438. pay_file: pay_permission.indexOf(3) !== -1,
  439. };
  440. return permission;
  441. }
  442. async getPaymentAuditList(spid, uid = null) {
  443. const uidSql = uid ? ' AND uid in (' + uid.join(',') + ')' : '';
  444. const sql = `SELECT * FROM ?? WHERE spid = ? AND payment_permission <> ''` + uidSql;
  445. const sqlParams = [this.tableName, spid];
  446. const result = await this.db.query(sql, sqlParams);
  447. const list = [];
  448. for (const r of result) {
  449. const permission = await this.getPaymentPermission(this._.map(r.payment_permission.split(','), this._.toInteger));
  450. const accountInfo = await this.ctx.service.projectAccount.getDataById(r.uid);
  451. list.push({
  452. id: r.id,
  453. name: accountInfo.name,
  454. company: accountInfo.company,
  455. uid: r.uid,
  456. permission_json: permission,
  457. });
  458. }
  459. return list;
  460. }
  461. async getPaymentPermission(pp) {
  462. if (!pp || pp.length === 0) return false;
  463. const permission = {
  464. admin: pp.indexOf(2) !== -1,
  465. view_all: pp.indexOf(3) !== -1,
  466. };
  467. return permission;
  468. }
  469. async savePaymentPermissionAudits(spid, uids, operation = 'add', transaction= null) {
  470. const updateArr = [];
  471. const spAudits = await this.getAllDataByCondition({ where: { spid: spid, uid: uids } });
  472. for (const a of spAudits) {
  473. if (operation === 'add' && a.payment_permission !== '') continue;
  474. updateArr.push({
  475. id: a.id,
  476. payment_permission: operation === 'add' ? '1' : (operation === 'del' ? '' : a.payment_permission),
  477. });
  478. }
  479. if (updateArr.length > 0) transaction ? await transaction.updateRows(this.tableName, updateArr) : await this.db.updateRows(this.tableName, updateArr);
  480. }
  481. async updateOnePaymentPermission(spid, updateData) {
  482. if (!updateData.uid || !updateData.permission_json) {
  483. return false;
  484. }
  485. const spAudit = await this.getDataByCondition({ spid: spid, uid: updateData.uid });
  486. if (spAudit) {
  487. const newPermission = await this.getNewPaymentPermission(spAudit.payment_permission, updateData.permission_json);
  488. return await this.db.update(this.tableName, { id: spAudit.id, payment_permission: newPermission });
  489. }
  490. return false;
  491. }
  492. async getNewPaymentPermission(permission, newPermission) {
  493. const oldPermission = await this.getPaymentPermission(this._.map(permission.split(','), this._.toInteger));
  494. if (newPermission.admin !== undefined) {
  495. oldPermission.admin = newPermission.admin;
  496. }
  497. if (newPermission.view_all !== undefined) {
  498. oldPermission.view_all = newPermission.view_all;
  499. }
  500. const permissionArr = [1];
  501. if (oldPermission.admin) permissionArr.push(2);
  502. if (oldPermission.view_all) permissionArr.push(3);
  503. return permissionArr.join(',');
  504. }
  505. async getDatacollectAuditList(spid, uid = null) {
  506. const uidSql = uid ? ' AND uid in (' + uid.join(',') + ')' : '';
  507. const sql = `SELECT * FROM ?? WHERE spid = ? AND datacollect_permission <> ''` + uidSql;
  508. const sqlParams = [this.tableName, spid];
  509. const result = await this.db.query(sql, sqlParams);
  510. const list = [];
  511. for (const r of result) {
  512. const accountInfo = await this.ctx.service.projectAccount.getDataById(r.uid);
  513. list.push({
  514. id: r.id,
  515. name: accountInfo.name,
  516. company: accountInfo.company,
  517. company_id: accountInfo.company_id,
  518. uid: r.uid,
  519. });
  520. }
  521. return list;
  522. }
  523. async saveDatacollectPermissionAudits(spid, uids, operation = 'add', transaction= null) {
  524. const updateArr = [];
  525. const spAudits = await this.getAllDataByCondition({ where: { spid: spid, uid: uids } });
  526. for (const a of spAudits) {
  527. if (operation === 'add' && a.datacollect_permission !== '') continue;
  528. updateArr.push({
  529. id: a.id,
  530. datacollect_permission: operation === 'add' ? '1' : (operation === 'del' ? '' : a.datacollect_permission),
  531. });
  532. }
  533. if (updateArr.length > 0) transaction ? await transaction.updateRows(this.tableName, updateArr) : await this.db.updateRows(this.tableName, updateArr);
  534. }
  535. }
  536. return subProjPermission;
  537. };