payment_folder.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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({ spid: this.ctx.subProject.id, 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.getDataByCondition({ spid: this.ctx.subProject.id, folder_id: 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.folder_id : parentFolderInfo.folder_id;
  34. }
  35. const childrenCount = await transaction.select(this.tableName, { where: { spid: this.ctx.subProject.id, parent_id: parentId }, orders: [['order', 'desc']] });
  36. const order = childrenCount && childrenCount.length > 0 ? childrenCount[0].order + 1 : 1;
  37. const maxFolderId = await this.getMaxId(this.ctx.subProject.id);
  38. const insertData = {
  39. id: this.uuid.v4(),
  40. pid: projectId,
  41. spid: this.ctx.subProject.id,
  42. uid,
  43. name,
  44. folder_id: maxFolderId + 1,
  45. parent_id: parentId,
  46. parent_path,
  47. level,
  48. order,
  49. is_leaf: 1,
  50. in_time: new Date(),
  51. };
  52. await transaction.insert(this.tableName, insertData);
  53. await transaction.commit();
  54. } catch (err) {
  55. await transaction.rollback();
  56. throw err;
  57. }
  58. }
  59. async deleteFolder(id) {
  60. // 删除所有底下的目录
  61. const transaction = await this.db.beginTransaction();
  62. try {
  63. const info = await this.getDataById(id);
  64. if (info.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) {
  65. throw '您没有权限删除此文件夹';
  66. }
  67. let ids = [info.folder_id];
  68. if (info.parent_id === 0) {
  69. const childList = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, parent_id: info.folder_id } });
  70. if (childList.length > 0) {
  71. ids = [...ids, ...this._.map(childList, 'folder_id')];
  72. for (const c of childList) {
  73. const deleteData = await this.getDataByParentPath(this.tableName, c.parent_path + '-' + c.folder_id + '%', transaction);
  74. if (deleteData.length > 0) {
  75. const delids = this._.map(deleteData, 'folder_id');
  76. ids = [...ids, ...delids];
  77. }
  78. }
  79. }
  80. } else if (info.parent_path) {
  81. const deleteData = await this.getDataByParentPath(this.tableName, info.parent_path + '-' + info.folder_id + '%', transaction);
  82. if (deleteData.length > 0) {
  83. const delids = this._.map(deleteData, 'folder_id');
  84. ids = [...ids, ...delids];
  85. }
  86. // 父节点可能变成is_leaf为0
  87. const parentFolderInfo = await this.getDataByCondition({ spid: this.ctx.subProject.id, folder_id: info.parent_id });
  88. const childrenCount = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, parent_id: parentFolderInfo.folder_id } });
  89. if (childrenCount.length === 1) {
  90. await transaction.update(this.tableName, { id: parentFolderInfo.id, is_leaf: 1 });
  91. }
  92. }
  93. // 判断是否存在标段,有则无法删除目录
  94. const tenderCount = await this.ctx.service.paymentTender.count({ spid: this.ctx.subProject.id, folder_id: ids });
  95. if (tenderCount > 0) {
  96. throw '文件夹下存在标段,无法删除';
  97. }
  98. await transaction.delete(this.tableName, { spid: this.ctx.subProject.id, folder_id: ids });
  99. await transaction.commit();
  100. } catch (err) {
  101. await transaction.rollback();
  102. throw err;
  103. }
  104. }
  105. async getDataByParentPath(tableName, parent_path, transaction = null) {
  106. this.initSqlBuilder();
  107. this.sqlBuilder.setAndWhere('spid', {
  108. value: this.db.escape(this.ctx.subProject.id),
  109. operate: '=',
  110. });
  111. this.sqlBuilder.setAndWhere('parent_path', {
  112. value: this.db.escape(parent_path),
  113. operate: 'Like',
  114. });
  115. const [sql, sqlParam] = this.sqlBuilder.build(tableName);
  116. const resultData = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  117. return resultData;
  118. }
  119. async getList(uid, tenderList, auditPermission) {
  120. // 获取所有项目参与者
  121. const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
  122. where: { project_id: this.ctx.session.sessionProject.id, enable: 1 },
  123. columns: ['id', 'name'],
  124. });
  125. let folderList = [];
  126. if (auditPermission.view_all) {
  127. folderList = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id } });
  128. } else {
  129. folderList = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, uid } });
  130. // 再找出标段对应的目录及自建的目录下的子目录
  131. if (tenderList.length > 0) {
  132. for (const t of tenderList) {
  133. if (this._.findIndex(folderList, { folder_id: t.folder_id }) === -1) {
  134. const folderInfo = await this.getDataByCondition({ spid: this.ctx.subProject.id, folder_id: t.folder_id });
  135. folderList.push(folderInfo);
  136. }
  137. }
  138. }
  139. if (folderList.length > 0) {
  140. const leafFolderList = this._.filter(folderList, { is_leaf: 1 });
  141. const parentFolderIdList = this._.map(this._.filter(folderList, { is_leaf: 0 }), 'id');
  142. const allNotExistFolderIds = [];
  143. for (const lf of leafFolderList) {
  144. let parentPathArray = lf.parent_path !== '' ? lf.parent_path.split('-') : [];
  145. if (parentPathArray.length > 0) {
  146. parentPathArray = parentPathArray.map(function(data) {
  147. return +data;
  148. });// 字符串数组转整型数组
  149. }
  150. const notExistFolderIds = this._.difference(parentPathArray, parentFolderIdList);
  151. if (notExistFolderIds.length > 0) {
  152. for (const id of notExistFolderIds) {
  153. if (!this._.includes(allNotExistFolderIds, id)) {
  154. allNotExistFolderIds.push(id);
  155. }
  156. }
  157. }
  158. }
  159. if (allNotExistFolderIds.length > 0) {
  160. const newFolderList = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, folder_id: allNotExistFolderIds } });
  161. folderList = [...folderList, ...newFolderList];
  162. }
  163. }
  164. }
  165. if (folderList.length > 0) {
  166. // folderList = this._.uniqBy(folderList, 'id');
  167. for (const f of folderList) {
  168. const userInfo = this._.find(accountList, { id: f.uid });
  169. f.user_name = userInfo ? userInfo.name : '';
  170. }
  171. }
  172. return folderList;
  173. }
  174. async getNoSpList(pid) {
  175. // 获取所有项目参与者
  176. const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
  177. where: { project_id: this.ctx.session.sessionProject.id, enable: 1 },
  178. columns: ['id', 'name'],
  179. });
  180. const folderList = await this.getAllDataByCondition({ where: { pid, spid: '' } });
  181. if (folderList.length > 0) {
  182. // folderList = this._.uniqBy(folderList, 'id');
  183. for (const f of folderList) {
  184. const userInfo = this._.find(accountList, { id: f.uid });
  185. f.user_name = userInfo ? userInfo.name : '';
  186. }
  187. }
  188. return folderList;
  189. }
  190. async getMaxId(spid, needFlag = false) {
  191. const result = await this.db.queryOne(`select max(folder_id) as max_id from ${this.tableName} where spid = ?`, [spid]);
  192. const maxId = result ? result.max_id : 0;
  193. if (needFlag) {
  194. const oldResult = await this.db.queryOne(`select max(folder_id) as max_id from ${this.tableName} where pid = ? AND spid = ?`, [this.ctx.session.sessionProject.id, '']);
  195. const oldMaxId = oldResult ? oldResult.max_id : 0;
  196. return maxId > oldMaxId ? maxId : oldMaxId;
  197. }
  198. return maxId;
  199. }
  200. // async getChildrenByParentId(parentId, transaction = null) {
  201. // const list = await this.getAllDataByCondition({ where: { parent_id: parentId } });
  202. // }
  203. }
  204. return paymentFolder;
  205. };