payment_folder.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 paymentFolder extends app.BaseService {
  13. constructor(ctx) {
  14. super(ctx);
  15. this.tableName = 'payment_folder';
  16. }
  17. async addFolder(projectId, uid, parentId = 0, name) {
  18. const transaction = await this.db.beginTransaction();
  19. try {
  20. let level = 1;
  21. let parent_path = '';
  22. if (parentId !== 0) {
  23. const parentFolderInfo = await this.getDataById(parentId);
  24. if (!parentFolderInfo) throw '父目录不存在';
  25. level = parentFolderInfo.level + level;
  26. if (parentFolderInfo.is_leaf) {
  27. await transaction.update(this.tableName, { id: parentFolderInfo.id, is_leaf: 0 });
  28. }
  29. parent_path = parentFolderInfo.parent_path ? parentFolderInfo.parent_path + '-' + parentFolderInfo.id : parentFolderInfo.id;
  30. }
  31. const childrenCount = await transaction.count(this.tableName, { parent_id: parentId });
  32. const order = childrenCount + 1;
  33. const insertData = {
  34. pid: projectId,
  35. uid,
  36. name,
  37. parent_id: parentId,
  38. parent_path,
  39. level,
  40. order,
  41. is_leaf: 1,
  42. in_time: new Date(),
  43. };
  44. await transaction.insert(this.tableName, insertData);
  45. await transaction.commit();
  46. } catch (err) {
  47. await transaction.rollback();
  48. throw err;
  49. }
  50. }
  51. async deleteFolder(id) {
  52. // 删除所有底下的目录
  53. const transaction = await this.db.beginTransaction();
  54. try {
  55. const info = await this.getDataById(id);
  56. let ids = [info.id];
  57. if (info.parent_path) {
  58. const deleteData = await this.getDataByParentPath(this.tableName, info.parent_path + '-' + info.id + '%', transaction);
  59. if (deleteData.length > 0) {
  60. const delids = this._.map(deleteData, 'id');
  61. ids = [...ids, ...delids];
  62. }
  63. }
  64. await transaction.delete(this.tableName, { id: ids });
  65. await transaction.commit();
  66. } catch (err) {
  67. await transaction.rollback();
  68. throw err;
  69. }
  70. }
  71. async getDataByParentPath(tableName, parent_path, transaction = null) {
  72. this.initSqlBuilder();
  73. this.sqlBuilder.setAndWhere('parent_path', {
  74. value: this.db.escape(parent_path),
  75. operate: 'Like',
  76. });
  77. const [sql, sqlParam] = this.sqlBuilder.build(tableName);
  78. const resultData = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  79. return resultData;
  80. }
  81. async getList(uid, tenderList, auditPermission) {
  82. if (auditPermission.view_all) {
  83. return await this.getAllDataByCondition({ where: { pid: this.ctx.session.sessionProject.id } });
  84. }
  85. let folderList = await this.getAllDataByCondition({ where: { uid } });
  86. // 再找出标段对应的目录及自建的目录下的子目录
  87. if (tenderList.length > 0) {
  88. for (const t of tenderList) {
  89. if (this._.findIndex(folderList, { id: t.folder_id }) === -1) {
  90. const folderInfo = await this.getDataById(t.folder_id);
  91. folderList.push(folderInfo);
  92. }
  93. }
  94. }
  95. if (folderList.length > 0) {
  96. const leafFolderList = this._.filter(folderList, { is_leaf: 1 });
  97. const parentFolderIdList = this._.map(this._.filter(folderList, { is_leaf: 0 }), 'id');
  98. for (const lf of leafFolderList) {
  99. let parentPathArray = lf.parent_path !== '' ? lf.parent_path.split('-') : [];
  100. if (parentPathArray.length > 0) {
  101. parentPathArray = parentPathArray.map(function(data) {
  102. return +data;
  103. });// 字符串数组转整型数组
  104. }
  105. const notExistFolderIds = this._.difference(parentPathArray, parentFolderIdList);
  106. if (notExistFolderIds.length > 0) {
  107. const newFolderList = await this.getAllDataByCondition({ where: { id: notExistFolderIds } });
  108. console.log(newFolderList);
  109. folderList = [...folderList, ...newFolderList];
  110. }
  111. }
  112. }
  113. return folderList;
  114. }
  115. // async getChildrenByParentId(parentId, transaction = null) {
  116. // const list = await this.getAllDataByCondition({ where: { parent_id: parentId } });
  117. // }
  118. }
  119. return paymentFolder;
  120. };