sub_proj_permission.js 28 KB

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