sub_project.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const rootId = '-1';
  10. module.exports = app => {
  11. class SubProject extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @param {String} tableName - 表名
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'sub_project';
  22. }
  23. async getSubProject(pid, uid, admin) {
  24. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  25. if (admin) return result;
  26. const permission = await this.ctx.service.subProjPermission.getUserPermission(pid, uid);
  27. result = result.filter(x => {
  28. if (x.is_folder) return true;
  29. const pb = permission.find(y => { return x.id === y.spid});
  30. if (!pb) return false;
  31. x.user_permission = pb;
  32. return x.user_permission.budget_permission.length > 0 || x.user_permission.file_permission.length > 0 || x.manage_permission.length > 0;
  33. });
  34. return result;
  35. }
  36. _filterEmptyFolder(data) {
  37. data.sort((a, b) => { return b.tree_level - a.tree_level});
  38. const result = [];
  39. for (const d of data) {
  40. if (!d.is_folder) result.push(d);
  41. if (result.find(x => { return x.tree_pid === d.id; })) result.push(d);
  42. }
  43. return result;
  44. }
  45. async getBudgetProject(pid, uid, admin) {
  46. const sql = `SELECT sp.*, b.std_id From ${this.tableName} sp LEFT JOIN ${this.ctx.service.budget.tableName} b ON sp.budget_id = b.id
  47. WHERE sp.project_id = ? AND sp.is_delete = 0`;
  48. let result = await this.db.query(sql, [pid]);
  49. const permission = await this.ctx.service.subProjPermission.getUserPermission(pid, uid);
  50. result = result.filter(x => {
  51. if (!x.is_folder && !x.budget_id) return false;
  52. if (x.is_folder || admin) return true;
  53. const pb = permission.find(y => { return x.id === y.spid});
  54. if (!pb) return false;
  55. x.permission = pb.budget_permission;
  56. x.manage_permission = pb.manage_permission;
  57. return x.budget_permission.length > 0;
  58. });
  59. console.log(result.length);
  60. return this._filterEmptyFolder(result);
  61. }
  62. async getFileProject(pid, uid, admin) {
  63. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  64. const permission = await this.ctx.service.subProjPermission.getUserPermission(pid, uid);
  65. result = result.filter(x => {
  66. if (x.is_folder || admin) return true;
  67. const pb = permission.find(y => { return x.id === y.spid});
  68. if (!pb) return false;
  69. x.permission = pb.file_permission;
  70. x.manage_permission = pb.manage_permission;
  71. return x.file_permission.length > 0;
  72. });
  73. return this._filterEmptyFolder(result);
  74. }
  75. async getLastChild(tree_pid) {
  76. const result = await this.getAllDataByCondition({ where: { tree_pid }, orders: [['tree_order', 'desc']], limit: 1, offset: 0 });
  77. return result[0];
  78. }
  79. async addFolder(data) {
  80. const parent = await this.getDataById(data.tree_pid);
  81. if (parent && !parent.is_folder) throw '添加数据结构错误';
  82. const lastChild = await this.getLastChild(parent ? parent.id : rootId);
  83. const conn = await this.db.beginTransaction();
  84. try {
  85. // 获取当前用户信息
  86. const sessionUser = this.ctx.session.sessionUser;
  87. // 获取当前项目信息
  88. const sessionProject = this.ctx.session.sessionProject;
  89. const insertData = {
  90. id: this.uuid.v4(), project_id: sessionProject.id, user_id: sessionUser.accountId,
  91. tree_pid: data.tree_pid,
  92. tree_level: parent ? parent.tree_level + 1 : 1,
  93. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  94. name: data.name, is_folder: 1,
  95. };
  96. const operate = await conn.insert(this.tableName, insertData);
  97. if (operate.affectedRows === 0) throw '新增文件夹失败';
  98. await conn.commit();
  99. return await this.getSubProject(sessionProject.id, sessionUser.accountId, sessionUser.is_admin);
  100. } catch (error) {
  101. await conn.rollback();
  102. throw error;
  103. }
  104. }
  105. async addSubProject(data) {
  106. const parent = await this.getDataById(data.tree_pid);
  107. if (parent && !parent.is_folder) throw '添加数据结构错误';
  108. const lastChild = await this.getLastChild(parent ? parent.id : rootId);
  109. const conn = await this.db.beginTransaction();
  110. try {
  111. // 获取当前用户信息
  112. const sessionUser = this.ctx.session.sessionUser;
  113. // 获取当前项目信息
  114. const sessionProject = this.ctx.session.sessionProject;
  115. const insertData = {
  116. id: this.uuid.v4(), project_id: sessionProject.id, user_id: sessionUser.accountId,
  117. tree_pid: data.tree_pid,
  118. tree_level: parent ? parent.tree_level + 1 : 1,
  119. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  120. name: data.name, is_folder: 0,
  121. };
  122. const operate = await conn.insert(this.tableName, insertData);
  123. // todo 根据节点新增时的其他操作
  124. if (operate.affectedRows === 0) throw '新增文件夹失败';
  125. await conn.commit();
  126. return await this.getSubProject(sessionProject.id, sessionUser.accountId, sessionUser.is_admin);
  127. } catch (error) {
  128. await conn.rollback();
  129. throw error;
  130. }
  131. }
  132. async getPosterityData(id){
  133. const result = [];
  134. let cur = await this.getAllDataByCondition({ where: { tree_pid: id } });
  135. let iLevel = 1;
  136. while (cur.length > 0 && iLevel < 6) {
  137. result.push(...cur);
  138. cur = await this.getAllDataByCondition({ where: { tree_pid: cur.map(x => { return x.id })} });
  139. iLevel += 1;
  140. }
  141. return result;
  142. }
  143. async dragTo(data) {
  144. const dragNode = await this.getDataById(data.drag_id);
  145. const dropNode = await this.getDataById(data.drop_id);
  146. if (!dragNode || !dropNode || !dropNode.is_folder) throw '拖拽数据结构错误';
  147. const lastChild = await this.getLastChild(dropNode.id);
  148. const posterity = await this.getPosterityData(dragNode.id);
  149. const conn = await this.db.beginTransaction();
  150. try {
  151. const updateData = {
  152. id: dragNode.id, tree_pid: dropNode.id, tree_level: dropNode.tree_level + 1,
  153. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  154. };
  155. await conn.update(this.tableName, updateData);
  156. if (dragNode.tree_level !== dropNode.tree_level + 1 && posterity.length > 0) {
  157. const posterityUpdateData = posterity.map(x => {
  158. return { id: x.id, tree_level: dropNode.tree_level + 1 - dragNode.tree_level + x.tree_level }
  159. });
  160. await conn.updateRows(this.tableName, posterityUpdateData);
  161. }
  162. // 升级原来的后项的order
  163. await conn.query(`UPDATE ${this.tableName} SET tree_order = tree_order-1 WHERE tree_pid = ? AND tree_order > ?`, [dragNode.tree_pid, dragNode.tree_order]);
  164. await conn.commit();
  165. } catch (error) {
  166. await conn.rollback();
  167. throw error;
  168. }
  169. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  170. }
  171. async del(id) {
  172. const node = await this.getDataById(id);
  173. if (!node) throw '删除的数据不存在';
  174. const posterity = await this.getPosterityData(node.id);
  175. const updateData = [
  176. { id: node.id, is_delete: 1 },
  177. ];
  178. posterity.forEach(x => {
  179. updateData.push({ id: x.id, is_delete: 1});
  180. });
  181. await this.db.updateRows(this.tableName, updateData);
  182. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  183. }
  184. async save(data) {
  185. const result = await this.db.update(this.tableName, data);
  186. if (result.affectedRows > 0) {
  187. return data;
  188. } else {
  189. throw '更新数据失败';
  190. }
  191. }
  192. async setBudgetStd(data) {
  193. const subProject = await this.getDataById(data.id);
  194. const budgetStd = await this.ctx.service.budgetStd.getDataById(data.std_id);
  195. if (!budgetStd) throw '选择的概算标准不存在,请刷新页面重试';
  196. const conn = await this.db.beginTransaction();
  197. try {
  198. const budget_id = await this.ctx.service.budget.add(conn, {
  199. pid: subProject.project_id, user_id: subProject.user_id, rela_tender: subProject.rela_tender
  200. }, budgetStd);
  201. const updateData = { id: data.id, std_id: budgetStd.id, std_name: budgetStd.name, budget_id };
  202. await conn.update(this.tableName, updateData);
  203. await conn.commit();
  204. return updateData;
  205. } catch (error) {
  206. await conn.rollback();
  207. throw error;
  208. }
  209. }
  210. async setRelaTender(data) {
  211. const subProject = await this.getDataById(data.id);
  212. const conn = await this.db.beginTransaction();
  213. try {
  214. await conn.update(this.tableName, data);
  215. await conn.update(this.ctx.service.budget.tableName, { id: subProject.budget_id, rela_tender: data.rela_tender });
  216. await conn.commit();
  217. return data;
  218. } catch (error) {
  219. await conn.rollback();
  220. throw error;
  221. }
  222. }
  223. }
  224. return SubProject;
  225. };