payment_folder.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_id === 0) {
  65. const childList = await this.getAllDataByCondition({ where: { parent_id: info.id } });
  66. if (childList.length > 0) {
  67. ids = [...ids, ...this._.map(childList, 'id')];
  68. for (const c of childList) {
  69. const deleteData = await this.getDataByParentPath(this.tableName, c.parent_path + '-' + c.id + '%', transaction);
  70. if (deleteData.length > 0) {
  71. const delids = this._.map(deleteData, 'id');
  72. ids = [...ids, ...delids];
  73. }
  74. }
  75. }
  76. } else if (info.parent_path) {
  77. const deleteData = await this.getDataByParentPath(this.tableName, info.parent_path + '-' + info.id + '%', transaction);
  78. if (deleteData.length > 0) {
  79. const delids = this._.map(deleteData, 'id');
  80. ids = [...ids, ...delids];
  81. }
  82. }
  83. // 判断是否存在标段,有则无法删除目录
  84. const tenderCount = await this.ctx.service.paymentTender.count({ folder_id: ids });
  85. if (tenderCount > 0) {
  86. throw '文件夹下存在标段,无法删除';
  87. }
  88. await transaction.delete(this.tableName, { id: ids });
  89. await transaction.commit();
  90. } catch (err) {
  91. await transaction.rollback();
  92. throw err;
  93. }
  94. }
  95. async getDataByParentPath(tableName, parent_path, transaction = null) {
  96. this.initSqlBuilder();
  97. this.sqlBuilder.setAndWhere('parent_path', {
  98. value: this.db.escape(parent_path),
  99. operate: 'Like',
  100. });
  101. const [sql, sqlParam] = this.sqlBuilder.build(tableName);
  102. const resultData = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  103. return resultData;
  104. }
  105. async getList(uid, tenderList, auditPermission) {
  106. // 获取所有项目参与者
  107. const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
  108. where: { project_id: this.ctx.session.sessionProject.id, enable: 1 },
  109. columns: ['id', 'name'],
  110. });
  111. let folderList = [];
  112. if (auditPermission.view_all) {
  113. folderList = await this.getAllDataByCondition({ where: { pid: this.ctx.session.sessionProject.id } });
  114. } else {
  115. folderList = await this.getAllDataByCondition({ where: { uid } });
  116. // 再找出标段对应的目录及自建的目录下的子目录
  117. if (tenderList.length > 0) {
  118. for (const t of tenderList) {
  119. if (this._.findIndex(folderList, { id: t.folder_id }) === -1) {
  120. const folderInfo = await this.getDataById(t.folder_id);
  121. folderList.push(folderInfo);
  122. }
  123. }
  124. }
  125. if (folderList.length > 0) {
  126. const leafFolderList = this._.filter(folderList, { is_leaf: 1 });
  127. const parentFolderIdList = this._.map(this._.filter(folderList, { is_leaf: 0 }), 'id');
  128. const allNotExistFolderIds = [];
  129. for (const lf of leafFolderList) {
  130. let parentPathArray = lf.parent_path !== '' ? lf.parent_path.split('-') : [];
  131. if (parentPathArray.length > 0) {
  132. parentPathArray = parentPathArray.map(function(data) {
  133. return +data;
  134. });// 字符串数组转整型数组
  135. }
  136. const notExistFolderIds = this._.difference(parentPathArray, parentFolderIdList);
  137. if (notExistFolderIds.length > 0) {
  138. for (const id of notExistFolderIds) {
  139. if (!this._.includes(allNotExistFolderIds, id)) {
  140. allNotExistFolderIds.push(id);
  141. }
  142. }
  143. }
  144. }
  145. if (allNotExistFolderIds.length > 0) {
  146. const newFolderList = await this.getAllDataByCondition({ where: { id: allNotExistFolderIds } });
  147. folderList = [...folderList, ...newFolderList];
  148. }
  149. }
  150. }
  151. if (folderList.length > 0) {
  152. // folderList = this._.uniqBy(folderList, 'id');
  153. for (const f of folderList) {
  154. const userInfo = this._.find(accountList, { id: f.uid });
  155. f.user_name = userInfo ? userInfo.name : '';
  156. }
  157. }
  158. return folderList;
  159. }
  160. // async getChildrenByParentId(parentId, transaction = null) {
  161. // const list = await this.getAllDataByCondition({ where: { parent_id: parentId } });
  162. // }
  163. }
  164. return paymentFolder;
  165. };