payment_permission_audit.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, transaction = null) {
  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. if (pushData.length > 0) {
  34. return transaction ? await transaction.insert(this.tableName, pushData) : await this.db.insert(this.tableName, pushData);
  35. }
  36. return false;
  37. }
  38. async saveGroup(pid, groupid) {
  39. const transaction = await this.db.beginTransaction();
  40. try {
  41. // 删除所在组的用户
  42. await transaction.delete(this.tableName, { pid, groupid });
  43. const data = {
  44. pid,
  45. groupid,
  46. create_time: new Date(),
  47. };
  48. await transaction.insert(this.tableName, data);
  49. await transaction.commit();
  50. return true;
  51. } catch (err) {
  52. await transaction.rollback();
  53. throw err;
  54. }
  55. }
  56. async delAudit(id) {
  57. return await this.db.delete(this.tableName, { id });
  58. }
  59. async getOnePermission(is_admin, uid) {
  60. if (is_admin) {
  61. return paymentConst.audit_admin_permission;
  62. }
  63. const info = await this.getDataByCondition({ uid });
  64. if (!info) return false;
  65. info.permission_json = info.permission_json ? JSON.parse(info.permission_json) : paymentConst.audit_permission;
  66. return info.permission_json;
  67. }
  68. async getList(pid) {
  69. const list = await this.db.select(this.tableName, { where: { pid }, orders: [['id', 'desc']] });
  70. for (const l of list) {
  71. if (l.uid) {
  72. const accountInfo = await this.ctx.service.projectAccount.getDataById(l.uid);
  73. l.name = accountInfo.name;
  74. l.company = accountInfo.company;
  75. l.permission_json = l.permission_json ? JSON.parse(l.permission_json) : paymentConst.audit_permission;
  76. } else {
  77. l.name = accountGroup[l.groupid];
  78. }
  79. }
  80. return list;
  81. }
  82. async updateOnePermission(updateData) {
  83. if (!updateData.id || !updateData.permission_json) {
  84. return false;
  85. }
  86. updateData.permission_json = JSON.stringify(updateData.permission_json);
  87. return await this.db.update(this.tableName, updateData);
  88. }
  89. async updateAllPermission(pid, type, value) {
  90. if (this._.indexOf(paymentConst.permission_type_array, type) === -1 || this._.indexOf(paymentConst.permission_value_array, value) === -1) {
  91. return false;
  92. }
  93. const permissionAudits = await this.getList(pid);
  94. if (permissionAudits.length > 0) {
  95. const updateList = [];
  96. for (const pa of permissionAudits) {
  97. if (pa.permission_json[type] !== value) {
  98. pa.permission_json[type] = value;
  99. updateList.push({
  100. id: pa.id,
  101. permission_json: JSON.stringify(pa.permission_json),
  102. });
  103. }
  104. }
  105. if (updateList.length > 0) await this.db.updateRows(this.tableName, updateList);
  106. }
  107. return true;
  108. }
  109. }
  110. return paymentPermissionAudit;
  111. };