sub_proj_permission.js 30 KB

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