payment_permission_audit.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. /**
  3. * 决策大屏用户查看权限-数据模型
  4. *
  5. * @author ellisran
  6. * @date 2021/09/23
  7. * @version
  8. */
  9. const accountGroup = require('../const/account_group').group;
  10. const paymentConst = require('../const/payment');
  11. module.exports = app => {
  12. class paymentPermissionAudit extends app.BaseService {
  13. constructor(ctx) {
  14. super(ctx);
  15. this.tableName = 'payment_permission_audit';
  16. }
  17. async getGroupInfo(pid, groupid) {
  18. const sql = 'SELECT * FROM ?? WHERE pid = ? AND groupid = ? AND uid is NULL';
  19. const sqlParam = [this.tableName, pid, groupid];
  20. return await this.db.queryOne(sql, sqlParam);
  21. }
  22. async saveAudits(pid, accountList) {
  23. const pushData = [];
  24. for (const a of accountList) {
  25. const data = {
  26. pid,
  27. groupid: a.account_group,
  28. uid: a.id,
  29. create_time: new Date(),
  30. };
  31. pushData.push(data);
  32. }
  33. return pushData.length > 0 ? await this.db.insert(this.tableName, pushData) : false;
  34. }
  35. async saveGroup(pid, groupid) {
  36. const transaction = await this.db.beginTransaction();
  37. try {
  38. // 删除所在组的用户
  39. await transaction.delete(this.tableName, { pid, groupid });
  40. const data = {
  41. pid,
  42. groupid,
  43. create_time: new Date(),
  44. };
  45. await transaction.insert(this.tableName, data);
  46. await transaction.commit();
  47. return true;
  48. } catch (err) {
  49. await transaction.rollback();
  50. throw err;
  51. }
  52. }
  53. async delAudit(id) {
  54. return await this.db.delete(this.tableName, { id });
  55. }
  56. async getOnePermission(is_admin, uid) {
  57. if (is_admin) {
  58. return paymentConst.audit_admin_permission;
  59. }
  60. const info = await this.getDataByCondition({ uid });
  61. if (!info) return false;
  62. info.permission_json = info.permission_json ? JSON.parse(info.permission_json) : paymentConst.audit_permission;
  63. return info.permission_json;
  64. }
  65. async getList(pid) {
  66. const list = await this.db.select(this.tableName, { where: { pid }, orders: [['id', 'desc']] });
  67. for (const l of list) {
  68. if (l.uid) {
  69. const accountInfo = await this.ctx.service.projectAccount.getDataById(l.uid);
  70. l.name = accountInfo.name;
  71. l.company = accountInfo.company;
  72. l.permission_json = l.permission_json ? JSON.parse(l.permission_json) : paymentConst.audit_permission;
  73. } else {
  74. l.name = accountGroup[l.groupid];
  75. }
  76. }
  77. return list;
  78. }
  79. async updateOnePermission(updateData) {
  80. if (!updateData.id || !updateData.permission_json) {
  81. return false;
  82. }
  83. updateData.permission_json = JSON.stringify(updateData.permission_json);
  84. return await this.db.update(this.tableName, updateData);
  85. }
  86. async updateAllPermission(pid, type, value) {
  87. if (this._.indexOf(paymentConst.permission_type_array, type) === -1 || this._.indexOf(paymentConst.permission_value_array, value) === -1) {
  88. return false;
  89. }
  90. const permissionAudits = await this.getList(pid);
  91. if (permissionAudits.length > 0) {
  92. const updateList = [];
  93. for (const pa of permissionAudits) {
  94. if (pa.permission_json[type] !== value) {
  95. pa.permission_json[type] = value;
  96. updateList.push({
  97. id: pa.id,
  98. permission_json: JSON.stringify(pa.permission_json),
  99. });
  100. }
  101. }
  102. if (updateList.length > 0) await this.db.updateRows(this.tableName, updateList);
  103. }
  104. return true;
  105. }
  106. }
  107. return paymentPermissionAudit;
  108. };