budget_permission.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class BudgetPermission 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 = 'budget_permission';
  21. this.PermissionConst = {
  22. view: { title: '查看', value: 1 },
  23. edit: { title: '编辑', value: 2 },
  24. };
  25. }
  26. async showBudget(uid) {
  27. const count = await this.count({ pid: this.ctx.session.sessionProject.id, uid });
  28. return count > 0;
  29. }
  30. async getBudgetPermission(bid) {
  31. const _ = this.ctx.helper._;
  32. const result = await this.db.query(`SELECT bp.*, p.name, p.role
  33. FROM ${this.tableName} bp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  34. On bp.uid = p.id WHERE bid = ?`, [bid]);
  35. result.forEach(x => {
  36. x.permission = x.permission ? _.map(x.permission.split(','), _.toInteger) : []
  37. });
  38. return result;
  39. }
  40. async getUserPermission() {
  41. const _ = this.ctx.helper._;
  42. const result = await this.getAllDataByCondition({
  43. where: { uid: this.ctx.session.sessionUser.accountId, pid: this.ctx.session.sessionProject.id }
  44. });
  45. result.forEach(x => {
  46. x.permission = x.permission ? _.map(x.permission.split(','), _.toInteger) : []
  47. });
  48. return result;
  49. }
  50. async getBudgetUserPermission(bid) {
  51. const _ = this.ctx.helper._;
  52. const result = await this.getDataByCondition({uid: this.ctx.session.sessionUser.accountId, bid});
  53. if (result) result.permission = result.permission ? _.map(result.permission.split(','), _.toInteger) : [];
  54. return result;
  55. }
  56. async saveBudgetPermission(bid, member) {
  57. const orgMember = await this.getAllDataByCondition({ where: { bid } });
  58. const dm = [], um = [], im = [], cur = new Date();
  59. for (const om of orgMember) {
  60. const nm = member.find(x => { return om.uid === x.uid });
  61. if (!nm) {
  62. dm.push(om.id);
  63. } else {
  64. um.push({
  65. id: om.id,
  66. permission: nm.permission.join(','),
  67. modify_time: cur,
  68. });
  69. member.splice(member.indexOf(nm), 1);
  70. }
  71. }
  72. for (const m of member) {
  73. im.push({
  74. pid: this.ctx.session.sessionProject.id, bid, uid: m.uid,
  75. permission: m.permission.join(','), in_time: cur, modify_time: cur,
  76. })
  77. }
  78. const conn = await this.db.beginTransaction();
  79. try {
  80. if (dm.length > 0) await conn.delete(this.tableName, { id: dm });
  81. if (um.length > 0) await conn.updateRows(this.tableName, um);
  82. if (im.length > 0) await conn.insert(this.tableName, im);
  83. await conn.commit();
  84. } catch (err) {
  85. await conn.rollback();
  86. throw err;
  87. }
  88. }
  89. }
  90. return BudgetPermission;
  91. };