payment_folder.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.id : parentFolderInfo.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.id];
  68. if (info.parent_id === 0) {
  69. const childList = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, parent_id: info.id } });
  70. if (childList.length > 0) {
  71. ids = [...ids, ...this._.map(childList, 'id')];
  72. for (const c of childList) {
  73. const deleteData = await this.getDataByParentPath(this.tableName, c.parent_path + '-' + c.id + '%', transaction);
  74. if (deleteData.length > 0) {
  75. const delids = this._.map(deleteData, '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.id + '%', transaction);
  82. if (deleteData.length > 0) {
  83. const delids = this._.map(deleteData, 'id');
  84. ids = [...ids, ...delids];
  85. }
  86. }
  87. // 判断是否存在标段,有则无法删除目录
  88. const tenderCount = await this.ctx.service.paymentTender.count({ spid: this.ctx.subProject.id, folder_id: ids });
  89. if (tenderCount > 0) {
  90. throw '文件夹下存在标段,无法删除';
  91. }
  92. await transaction.delete(this.tableName, { id: ids });
  93. await transaction.commit();
  94. } catch (err) {
  95. await transaction.rollback();
  96. throw err;
  97. }
  98. }
  99. async getDataByParentPath(tableName, parent_path, transaction = null) {
  100. this.initSqlBuilder();
  101. this.sqlBuilder.setAndWhere('spid', {
  102. value: this.ctx.subProject.id,
  103. operate: '=',
  104. });
  105. this.sqlBuilder.setAndWhere('parent_path', {
  106. value: this.db.escape(parent_path),
  107. operate: 'Like',
  108. });
  109. const [sql, sqlParam] = this.sqlBuilder.build(tableName);
  110. const resultData = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  111. return resultData;
  112. }
  113. async getList(uid, tenderList, auditPermission) {
  114. // 获取所有项目参与者
  115. const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
  116. where: { project_id: this.ctx.session.sessionProject.id, enable: 1 },
  117. columns: ['id', 'name'],
  118. });
  119. let folderList = [];
  120. if (auditPermission.view_all) {
  121. folderList = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id } });
  122. } else {
  123. folderList = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, uid } });
  124. // 再找出标段对应的目录及自建的目录下的子目录
  125. if (tenderList.length > 0) {
  126. for (const t of tenderList) {
  127. if (this._.findIndex(folderList, { folder_id: t.folder_id }) === -1) {
  128. const folderInfo = await this.getDataByCondition({ spid: this.ctx.subProject.id, folder_id: t.folder_id });
  129. folderList.push(folderInfo);
  130. }
  131. }
  132. }
  133. if (folderList.length > 0) {
  134. const leafFolderList = this._.filter(folderList, { is_leaf: 1 });
  135. const parentFolderIdList = this._.map(this._.filter(folderList, { is_leaf: 0 }), 'id');
  136. const allNotExistFolderIds = [];
  137. for (const lf of leafFolderList) {
  138. let parentPathArray = lf.parent_path !== '' ? lf.parent_path.split('-') : [];
  139. if (parentPathArray.length > 0) {
  140. parentPathArray = parentPathArray.map(function(data) {
  141. return +data;
  142. });// 字符串数组转整型数组
  143. }
  144. const notExistFolderIds = this._.difference(parentPathArray, parentFolderIdList);
  145. if (notExistFolderIds.length > 0) {
  146. for (const id of notExistFolderIds) {
  147. if (!this._.includes(allNotExistFolderIds, id)) {
  148. allNotExistFolderIds.push(id);
  149. }
  150. }
  151. }
  152. }
  153. if (allNotExistFolderIds.length > 0) {
  154. const newFolderList = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, folder_id: allNotExistFolderIds } });
  155. folderList = [...folderList, ...newFolderList];
  156. }
  157. }
  158. }
  159. if (folderList.length > 0) {
  160. // folderList = this._.uniqBy(folderList, 'id');
  161. for (const f of folderList) {
  162. const userInfo = this._.find(accountList, { id: f.uid });
  163. f.user_name = userInfo ? userInfo.name : '';
  164. }
  165. }
  166. return folderList;
  167. }
  168. async getNoSpList(pid) {
  169. // 获取所有项目参与者
  170. const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
  171. where: { project_id: this.ctx.session.sessionProject.id, enable: 1 },
  172. columns: ['id', 'name'],
  173. });
  174. const folderList = await this.getAllDataByCondition({ where: { pid, spid: '' } });
  175. if (folderList.length > 0) {
  176. // folderList = this._.uniqBy(folderList, 'id');
  177. for (const f of folderList) {
  178. const userInfo = this._.find(accountList, { id: f.uid });
  179. f.user_name = userInfo ? userInfo.name : '';
  180. }
  181. }
  182. return folderList;
  183. }
  184. async getMaxId(spid, needFlag = false) {
  185. const result = await this.db.queryOne(`select max(folder_id) as max_id from ${this.tableName} where spid = ?`, [spid]);
  186. const maxId = result ? result.max_id : 0;
  187. if (needFlag) {
  188. 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, '']);
  189. const oldMaxId = oldResult ? oldResult.max_id : 0;
  190. return maxId > oldMaxId ? maxId : oldMaxId;
  191. }
  192. return maxId;
  193. }
  194. // async getChildrenByParentId(parentId, transaction = null) {
  195. // const list = await this.getAllDataByCondition({ where: { parent_id: parentId } });
  196. // }
  197. }
  198. return paymentFolder;
  199. };