payment_folder.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. const tenderCount = await this.ctx.service.paymentTender.count({ folder_id: parentId });
  21. if (tenderCount > 0) {
  22. throw '目录下存在标段无法创建子目录';
  23. }
  24. let level = 1;
  25. let parent_path = '';
  26. if (parentId !== 0) {
  27. const parentFolderInfo = await this.getDataById(parentId);
  28. if (!parentFolderInfo) throw '父目录不存在';
  29. level = parentFolderInfo.level + level;
  30. if (parentFolderInfo.is_leaf) {
  31. await transaction.update(this.tableName, { id: parentFolderInfo.id, is_leaf: 0 });
  32. }
  33. parent_path = parentFolderInfo.parent_path ? parentFolderInfo.parent_path + '-' + parentFolderInfo.id : parentFolderInfo.id;
  34. }
  35. const childrenCount = await transaction.count(this.tableName, { parent_id: parentId });
  36. const order = childrenCount + 1;
  37. const insertData = {
  38. pid: projectId,
  39. uid,
  40. name,
  41. parent_id: parentId,
  42. parent_path,
  43. level,
  44. order,
  45. is_leaf: 1,
  46. in_time: new Date(),
  47. };
  48. await transaction.insert(this.tableName, insertData);
  49. await transaction.commit();
  50. } catch (err) {
  51. await transaction.rollback();
  52. throw err;
  53. }
  54. }
  55. async deleteFolder(id) {
  56. // 删除所有底下的目录
  57. const transaction = await this.db.beginTransaction();
  58. try {
  59. const info = await this.getDataById(id);
  60. if (info.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) {
  61. throw '您没有权限删除此目录';
  62. }
  63. let ids = [info.id];
  64. if (info.parent_path) {
  65. const deleteData = await this.getDataByParentPath(this.tableName, info.parent_path + '-' + info.id + '%', transaction);
  66. if (deleteData.length > 0) {
  67. const delids = this._.map(deleteData, 'id');
  68. ids = [...ids, ...delids];
  69. }
  70. }
  71. // 判断是否存在标段,有则无法删除目录
  72. const tenderCount = await this.ctx.service.paymentTender.count({ folder_id: ids });
  73. if (tenderCount > 0) {
  74. throw '目录下存在标段,无法删除';
  75. }
  76. await transaction.delete(this.tableName, { id: ids });
  77. await transaction.commit();
  78. } catch (err) {
  79. await transaction.rollback();
  80. throw err;
  81. }
  82. }
  83. async getDataByParentPath(tableName, parent_path, transaction = null) {
  84. this.initSqlBuilder();
  85. this.sqlBuilder.setAndWhere('parent_path', {
  86. value: this.db.escape(parent_path),
  87. operate: 'Like',
  88. });
  89. const [sql, sqlParam] = this.sqlBuilder.build(tableName);
  90. const resultData = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  91. return resultData;
  92. }
  93. async getList(uid, tenderList, auditPermission) {
  94. // 获取所有项目参与者
  95. const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
  96. where: { project_id: this.ctx.session.sessionProject.id, enable: 1 },
  97. columns: ['id', 'name'],
  98. });
  99. let folderList = [];
  100. if (auditPermission.view_all) {
  101. folderList = await this.getAllDataByCondition({ where: { pid: this.ctx.session.sessionProject.id } });
  102. } else {
  103. folderList = await this.getAllDataByCondition({ where: { uid } });
  104. // 再找出标段对应的目录及自建的目录下的子目录
  105. if (tenderList.length > 0) {
  106. for (const t of tenderList) {
  107. if (this._.findIndex(folderList, { id: t.folder_id }) === -1) {
  108. const folderInfo = await this.getDataById(t.folder_id);
  109. folderList.push(folderInfo);
  110. }
  111. }
  112. }
  113. if (folderList.length > 0) {
  114. const leafFolderList = this._.filter(folderList, { is_leaf: 1 });
  115. const parentFolderIdList = this._.map(this._.filter(folderList, { is_leaf: 0 }), 'id');
  116. for (const lf of leafFolderList) {
  117. let parentPathArray = lf.parent_path !== '' ? lf.parent_path.split('-') : [];
  118. if (parentPathArray.length > 0) {
  119. parentPathArray = parentPathArray.map(function(data) {
  120. return +data;
  121. });// 字符串数组转整型数组
  122. }
  123. const notExistFolderIds = this._.difference(parentPathArray, parentFolderIdList);
  124. if (notExistFolderIds.length > 0) {
  125. const newFolderList = await this.getAllDataByCondition({ where: { id: notExistFolderIds } });
  126. console.log(newFolderList);
  127. folderList = [...folderList, ...newFolderList];
  128. }
  129. }
  130. }
  131. }
  132. if (folderList.length > 0) {
  133. for (const f of folderList) {
  134. const userInfo = this._.find(accountList, { id: f.uid });
  135. f.user_name = userInfo ? userInfo.name : '';
  136. }
  137. }
  138. return folderList;
  139. }
  140. // async getChildrenByParentId(parentId, transaction = null) {
  141. // const list = await this.getAllDataByCondition({ where: { parent_id: parentId } });
  142. // }
  143. }
  144. return paymentFolder;
  145. };