sub_proj_permission.js 31 KB

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