payment_permission_audit.js 4.7 KB

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