sub_proj_permission.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 _copyUserPermission(copyData, force = false) {
  326. const copyPermission = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, uid: copyData.uid }});
  327. if (copyPermission.length === 0) throw '选择的用户不存在';
  328. const insertData = [], updateData = [];
  329. for (const spid of copyData.spid) {
  330. const exist = await this.getAllDataByCondition({ columns: ['uid'], where: { spid } });
  331. copyPermission.forEach(cp => {
  332. const ecp = exist.find(x => { return x.uid === cp.uid; });
  333. if (ecp && force) {
  334. const udata = { id: ecp.id };
  335. for (const prop in cp) {
  336. if (prop.indexOf('_permission') > 1) udata[prop] = cp[prop];
  337. }
  338. updateData.push(udata)
  339. } else {
  340. const idata = { id: this.uuid.v4(), spid, pid: cp.pid, uid: cp.uid };
  341. for (const prop in cp) {
  342. if (prop.indexOf('_permission') > 1) idata[prop] = cp[prop];
  343. }
  344. insertData.push(idata);
  345. }
  346. });
  347. }
  348. if (insertData.length > 0) await this.db.insert(this.tableName, insertData);
  349. if (force && updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  350. }
  351. async updatePermission(subProject, data) {
  352. const result = {};
  353. if (data.add) result.add = await this._addUser(subProject, data.add);
  354. if (data.del) result.del = await this._delUser(subProject, data.del);
  355. if (data.update) result.update = await this._updateUserPermission(data.update);
  356. if (data.copy) await this._copyUserPermission(data.copy);
  357. return result;
  358. }
  359. async getFilingType(subProjectId) {
  360. const permissionConst = {}, prefix = 'f';
  361. for (const p in this.PermissionConst.file) {
  362. const fp = this.PermissionConst.file[p];
  363. permissionConst[prefix + fp.value] = fp.title;
  364. }
  365. 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
  366. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  367. On spp.uid = p.id WHERE spp.spid = ? and file_permission <> ''`, [subProjectId]);
  368. result.forEach(x => {
  369. const filePermission = x.file_permission.split(',');
  370. x.file_permission = filePermission.map(x => {
  371. return permissionConst[prefix + x] || '';
  372. }).join(',');
  373. });
  374. return result;
  375. }
  376. // 资料归集,授权固定分类
  377. async saveFilingType(data) {
  378. const updateData = [];
  379. data.forEach(x => {
  380. updateData.push({ id: x.id, filing_type: x.filing_type });
  381. });
  382. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  383. }
  384. checkViewPermission(ctx) {
  385. const permissionBlock = ctx.service.subProjPermission.PermissionBlock.find(x => { return x.key === ctx.controllerName; });
  386. if (!permissionBlock) return true;
  387. if (permissionBlock.children) {
  388. let canView = false;
  389. for (const c of permissionBlock.children) {
  390. const viewPermission = c.permission.find(x => { return x.key === 'view'; });
  391. if (!viewPermission || ctx.subProject.permission[c.field].indexOf(viewPermission.value) >= 0) canView = true;
  392. }
  393. return canView;
  394. } else if (ctx.controllerName === 'contract') {
  395. if (ctx.url.indexOf('contract/tender') >= 0) return true;
  396. const contractViewPermission = this._.map(permissionBlock.permission.filter(x => { return ['view', 'unit', 'node'].indexOf(x.key) >= 0; }), 'value');
  397. if (!contractViewPermission) return true;
  398. return this._.intersection(ctx.subProject.permission[permissionBlock.field], contractViewPermission).length > 0;
  399. } else {
  400. const viewPermission = permissionBlock.permission.find(x => { return x.key === 'view'; });
  401. if (!viewPermission) return true;
  402. return ctx.subProject.permission[permissionBlock.field].indexOf(viewPermission.value) >= 0;
  403. }
  404. };
  405. async getContractAuditList(spid, uid = null) {
  406. const uidSql = uid ? ' AND uid in (' + uid.join(',') + ')' : '';
  407. const sql = `SELECT * FROM ?? WHERE spid = ? AND contract_permission <> ''` + uidSql;
  408. const sqlParams = [this.tableName, spid];
  409. const result = await this.db.query(sql, sqlParams);
  410. const list = [];
  411. for (const r of result) {
  412. const permission = await this.getContractPermission(this._.map(r.contract_permission.split(','), this._.toInteger));
  413. list.push({
  414. id: r.id,
  415. spid: r.spid,
  416. tid: null,
  417. uid: r.uid,
  418. permission_add: permission.permission_add,
  419. permission_edit: permission.permission_edit,
  420. permission_edit_contract: permission.permission_edit_contract,
  421. permission_add_pay: permission.permission_add_pay,
  422. permission_show_node: permission.permission_show_node,
  423. permission_show_unit: permission.permission_show_unit,
  424. permission_att: permission.permission_att,
  425. });
  426. }
  427. return list;
  428. }
  429. async getNewContractPermission(permission, newContractPermission) {
  430. const oldPermission = await this.getContractPermission(this._.map(permission.split(','), this._.toInteger));
  431. if (newContractPermission.permission_add !== undefined) {
  432. oldPermission.permission_add = newContractPermission.permission_add;
  433. }
  434. if (newContractPermission.permission_edit !== undefined) {
  435. oldPermission.permission_edit = newContractPermission.permission_edit;
  436. }
  437. if (newContractPermission.permission_edit_contract !== undefined) {
  438. oldPermission.permission_edit_contract = newContractPermission.permission_edit_contract;
  439. }
  440. if (newContractPermission.permission_add_pay !== undefined) {
  441. oldPermission.permission_add_pay = newContractPermission.permission_add_pay;
  442. }
  443. if (newContractPermission.permission_show_node !== undefined) {
  444. oldPermission.permission_show_node = newContractPermission.permission_show_node;
  445. }
  446. if (newContractPermission.permission_show_unit !== undefined) {
  447. oldPermission.permission_show_unit = newContractPermission.permission_show_unit;
  448. }
  449. if (newContractPermission.permission_att !== undefined) {
  450. oldPermission.permission_att = newContractPermission.permission_att;
  451. }
  452. const permissionArr = [];
  453. if (oldPermission.permission_edit) permissionArr.push(1);
  454. if (oldPermission.permission_add) permissionArr.push(2);
  455. if (oldPermission.permission_edit_contract) permissionArr.push(7);
  456. if (oldPermission.permission_add_pay) permissionArr.push(8);
  457. if (oldPermission.permission_show_node) permissionArr.push(3);
  458. if (oldPermission.permission_show_unit) permissionArr.push(4);
  459. if (!oldPermission.permission_show_unit && !oldPermission.permission_show_node) permissionArr.push(5);
  460. if (oldPermission.permission_att) permissionArr.push(6);
  461. return permissionArr.join(',');
  462. }
  463. async getContractPermission(cp) {
  464. const permission = {
  465. permission_edit: cp.indexOf(1) !== -1 ? 1: 0,
  466. permission_add: cp.indexOf(2) !== -1 ? 1: 0,
  467. permission_edit_contract: cp.indexOf(7) !== -1 ? 1: 0,
  468. permission_add_pay: cp.indexOf(8) !== -1 ? 1: 0,
  469. permission_show_node: cp.indexOf(3) !== -1 ? 1: 0,
  470. permission_show_unit: cp.indexOf(4) !== -1 ? 1: 0,
  471. permission_att: cp.indexOf(6) !== -1 ? 1: 0,
  472. };
  473. return permission;
  474. }
  475. async getFinancailPermission(trans_permission, pay_permission) {
  476. const permission = {
  477. transfer_show: trans_permission.indexOf(1) !== -1,
  478. transfer_add: trans_permission.indexOf(2) !== -1,
  479. transfer_file: trans_permission.indexOf(3) !== -1,
  480. pay_show: pay_permission.indexOf(1) !== -1,
  481. pay_file: pay_permission.indexOf(3) !== -1,
  482. };
  483. return permission;
  484. }
  485. async getPaymentAuditList(spid, uid = null) {
  486. const uidSql = uid ? ' AND uid in (' + uid.join(',') + ')' : '';
  487. const sql = `SELECT * FROM ?? WHERE spid = ? AND payment_permission <> ''` + uidSql;
  488. const sqlParams = [this.tableName, spid];
  489. const result = await this.db.query(sql, sqlParams);
  490. const list = [];
  491. for (const r of result) {
  492. const permission = await this.getPaymentPermission(this._.map(r.payment_permission.split(','), this._.toInteger));
  493. const accountInfo = await this.ctx.service.projectAccount.getDataById(r.uid);
  494. list.push({
  495. id: r.id,
  496. name: accountInfo.name,
  497. company: accountInfo.company,
  498. uid: r.uid,
  499. permission_json: permission,
  500. });
  501. }
  502. return list;
  503. }
  504. async getPaymentPermission(pp) {
  505. if (!pp || pp.length === 0) return false;
  506. const permission = {
  507. admin: pp.indexOf(2) !== -1,
  508. view_all: pp.indexOf(3) !== -1,
  509. };
  510. return permission;
  511. }
  512. async savePaymentPermissionAudits(spid, uids, operation = 'add', transaction= null) {
  513. const updateArr = [];
  514. const spAudits = await this.getAllDataByCondition({ where: { spid: spid, uid: uids } });
  515. for (const a of spAudits) {
  516. if (operation === 'add' && a.payment_permission !== '') continue;
  517. updateArr.push({
  518. id: a.id,
  519. payment_permission: operation === 'add' ? '1' : (operation === 'del' ? '' : a.payment_permission),
  520. });
  521. }
  522. if (updateArr.length > 0) transaction ? await transaction.updateRows(this.tableName, updateArr) : await this.db.updateRows(this.tableName, updateArr);
  523. }
  524. async updateOnePaymentPermission(spid, updateData) {
  525. if (!updateData.uid || !updateData.permission_json) {
  526. return false;
  527. }
  528. const spAudit = await this.getDataByCondition({ spid: spid, uid: updateData.uid });
  529. if (spAudit) {
  530. const newPermission = await this.getNewPaymentPermission(spAudit.payment_permission, updateData.permission_json);
  531. return await this.db.update(this.tableName, { id: spAudit.id, payment_permission: newPermission });
  532. }
  533. return false;
  534. }
  535. async getNewPaymentPermission(permission, newPermission) {
  536. const oldPermission = await this.getPaymentPermission(this._.map(permission.split(','), this._.toInteger));
  537. if (newPermission.admin !== undefined) {
  538. oldPermission.admin = newPermission.admin;
  539. }
  540. if (newPermission.view_all !== undefined) {
  541. oldPermission.view_all = newPermission.view_all;
  542. }
  543. const permissionArr = [1];
  544. if (oldPermission.admin) permissionArr.push(2);
  545. if (oldPermission.view_all) permissionArr.push(3);
  546. return permissionArr.join(',');
  547. }
  548. async getDatacollectAuditList(spid, uid = null) {
  549. const uidSql = uid ? ' AND uid in (' + uid.join(',') + ')' : '';
  550. const sql = `SELECT * FROM ?? WHERE spid = ? AND datacollect_permission <> ''` + uidSql;
  551. const sqlParams = [this.tableName, spid];
  552. const result = await this.db.query(sql, sqlParams);
  553. const list = [];
  554. for (const r of result) {
  555. const accountInfo = await this.ctx.service.projectAccount.getDataById(r.uid);
  556. list.push({
  557. id: r.id,
  558. name: accountInfo.name,
  559. company: accountInfo.company,
  560. company_id: accountInfo.company_id,
  561. uid: r.uid,
  562. });
  563. }
  564. return list;
  565. }
  566. async saveDatacollectPermissionAudits(spid, uids, operation = 'add', transaction= null) {
  567. const updateArr = [];
  568. const spAudits = await this.getAllDataByCondition({ where: { spid: spid, uid: uids } });
  569. for (const a of spAudits) {
  570. if (operation === 'add' && a.datacollect_permission !== '') continue;
  571. updateArr.push({
  572. id: a.id,
  573. datacollect_permission: operation === 'add' ? '1' : (operation === 'del' ? '' : a.datacollect_permission),
  574. });
  575. }
  576. if (updateArr.length > 0) transaction ? await transaction.updateRows(this.tableName, updateArr) : await this.db.updateRows(this.tableName, updateArr);
  577. }
  578. async getRecentProjects(uid) {
  579. 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';
  580. const sqlParams = [this.tableName, this.ctx.service.subProject.tableName, uid];
  581. const result = await this.db.query(sql, sqlParams);
  582. return result;
  583. }
  584. }
  585. return subProjPermission;
  586. };