sub_project.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.user_permission.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. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  47. const permission = await this.ctx.service.subProjPermission.getUserPermission(pid, uid);
  48. result = result.filter(x => {
  49. if (!x.is_folder && !x.budget_id) return false;
  50. if (x.is_folder || admin) return true;
  51. const pb = permission.find(y => { return x.id === y.spid});
  52. if (!pb) return false;
  53. x.permission = pb.budget_permission;
  54. x.manage_permission = pb.manage_permission;
  55. return x.budget_permission.length > 0;
  56. });
  57. return this._filterEmptyFolder(result);
  58. }
  59. async getFileProject(pid, uid, admin) {
  60. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  61. const permission = await this.ctx.service.subProjPermission.getUserPermission(pid, uid);
  62. result = result.filter(x => {
  63. if (!x.is_folder && !x.management) return false;
  64. if (x.is_folder || admin) return true;
  65. const pb = permission.find(y => { return x.id === y.spid});
  66. if (!pb) return false;
  67. x.permission = pb.file_permission;
  68. x.manage_permission = pb.manage_permission;
  69. return x.permission.length > 0;
  70. });
  71. return this._filterEmptyFolder(result);
  72. }
  73. async getLastChild(tree_pid) {
  74. const result = await this.getAllDataByCondition({ where: { tree_pid }, orders: [['tree_order', 'desc']], limit: 1, offset: 0 });
  75. return result[0];
  76. }
  77. async addFolder(data) {
  78. const parent = await this.getDataById(data.tree_pid);
  79. if (parent && !parent.is_folder) throw '添加数据结构错误';
  80. const lastChild = await this.getLastChild(parent ? parent.id : rootId);
  81. const conn = await this.db.beginTransaction();
  82. try {
  83. // 获取当前用户信息
  84. const sessionUser = this.ctx.session.sessionUser;
  85. // 获取当前项目信息
  86. const sessionProject = this.ctx.session.sessionProject;
  87. const insertData = {
  88. id: this.uuid.v4(), project_id: sessionProject.id, user_id: sessionUser.accountId,
  89. tree_pid: data.tree_pid,
  90. tree_level: parent ? parent.tree_level + 1 : 1,
  91. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  92. name: data.name, is_folder: 1,
  93. };
  94. const operate = await conn.insert(this.tableName, insertData);
  95. if (operate.affectedRows === 0) throw '新增文件夹失败';
  96. await conn.commit();
  97. return await this.getSubProject(sessionProject.id, sessionUser.accountId, sessionUser.is_admin);
  98. } catch (error) {
  99. await conn.rollback();
  100. throw error;
  101. }
  102. }
  103. async addSubProject(data) {
  104. const parent = await this.getDataById(data.tree_pid);
  105. if (parent && !parent.is_folder) throw '添加数据结构错误';
  106. const lastChild = await this.getLastChild(parent ? parent.id : rootId);
  107. const conn = await this.db.beginTransaction();
  108. try {
  109. // 获取当前用户信息
  110. const sessionUser = this.ctx.session.sessionUser;
  111. // 获取当前项目信息
  112. const sessionProject = this.ctx.session.sessionProject;
  113. const insertData = {
  114. id: this.uuid.v4(), project_id: sessionProject.id, user_id: sessionUser.accountId,
  115. tree_pid: data.tree_pid,
  116. tree_level: parent ? parent.tree_level + 1 : 1,
  117. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  118. name: data.name, is_folder: 0,
  119. };
  120. const operate = await conn.insert(this.tableName, insertData);
  121. // todo 根据节点新增时的其他操作
  122. if (operate.affectedRows === 0) throw '新增文件夹失败';
  123. await conn.commit();
  124. return await this.getSubProject(sessionProject.id, sessionUser.accountId, sessionUser.is_admin);
  125. } catch (error) {
  126. await conn.rollback();
  127. throw error;
  128. }
  129. }
  130. async getPosterityData(id){
  131. const result = [];
  132. let cur = await this.getAllDataByCondition({ where: { tree_pid: id } });
  133. let iLevel = 1;
  134. while (cur.length > 0 && iLevel < 6) {
  135. result.push(...cur);
  136. cur = await this.getAllDataByCondition({ where: { tree_pid: cur.map(x => { return x.id })} });
  137. iLevel += 1;
  138. }
  139. return result;
  140. }
  141. async dragTo(data) {
  142. const dragNode = await this.getDataById(data.drag_id);
  143. const dropNode = await this.getDataById(data.drop_id);
  144. if (!dragNode || !dropNode || !dropNode.is_folder) throw '拖拽数据结构错误';
  145. const lastChild = await this.getLastChild(dropNode.id);
  146. const posterity = await this.getPosterityData(dragNode.id);
  147. const conn = await this.db.beginTransaction();
  148. try {
  149. const updateData = {
  150. id: dragNode.id, tree_pid: dropNode.id, tree_level: dropNode.tree_level + 1,
  151. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  152. };
  153. await conn.update(this.tableName, updateData);
  154. if (dragNode.tree_level !== dropNode.tree_level + 1 && posterity.length > 0) {
  155. const posterityUpdateData = posterity.map(x => {
  156. return { id: x.id, tree_level: dropNode.tree_level + 1 - dragNode.tree_level + x.tree_level }
  157. });
  158. await conn.updateRows(this.tableName, posterityUpdateData);
  159. }
  160. // 升级原来的后项的order
  161. await conn.query(`UPDATE ${this.tableName} SET tree_order = tree_order-1 WHERE tree_pid = ? AND tree_order > ?`, [dragNode.tree_pid, dragNode.tree_order]);
  162. await conn.commit();
  163. } catch (error) {
  164. await conn.rollback();
  165. throw error;
  166. }
  167. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  168. }
  169. async del(id) {
  170. const node = await this.getDataById(id);
  171. if (!node) throw '删除的数据不存在';
  172. const posterity = await this.getPosterityData(node.id);
  173. const updateData = [
  174. { id: node.id, is_delete: 1 },
  175. ];
  176. posterity.forEach(x => {
  177. updateData.push({ id: x.id, is_delete: 1});
  178. });
  179. await this.db.updateRows(this.tableName, updateData);
  180. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  181. }
  182. async save(data) {
  183. const result = await this.db.update(this.tableName, data);
  184. if (result.affectedRows > 0) {
  185. return data;
  186. } else {
  187. throw '更新数据失败';
  188. }
  189. }
  190. async setBudgetStd(data) {
  191. const subProject = await this.getDataById(data.id);
  192. const budgetStd = await this.ctx.service.budgetStd.getDataById(data.std_id);
  193. if (!budgetStd) throw '选择的概算标准不存在,请刷新页面重试';
  194. const conn = await this.db.beginTransaction();
  195. try {
  196. const budget_id = await this.ctx.service.budget.add(conn, {
  197. pid: subProject.project_id, user_id: subProject.user_id, rela_tender: subProject.rela_tender
  198. }, budgetStd);
  199. const updateData = { id: data.id, std_id: budgetStd.id, std_name: budgetStd.name, budget_id };
  200. await conn.update(this.tableName, updateData);
  201. await conn.commit();
  202. return updateData;
  203. } catch (error) {
  204. await conn.rollback();
  205. throw error;
  206. }
  207. }
  208. async setRelaTender(data) {
  209. const subProject = await this.getDataById(data.id);
  210. const conn = await this.db.beginTransaction();
  211. try {
  212. await conn.update(this.tableName, data);
  213. await conn.update(this.ctx.service.budget.tableName, { id: subProject.budget_id, rela_tender: data.rela_tender });
  214. await conn.commit();
  215. return data;
  216. } catch (error) {
  217. await conn.rollback();
  218. throw error;
  219. }
  220. }
  221. async setManagement(data) {
  222. const subProject = await this.getDataById(data.id);
  223. if (subProject.management === data.management) return data;
  224. const users = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: subProject.project_id, company: data.management }});
  225. const orgMember = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid: subProject.id } });
  226. const dm = [], um = [], im = [];
  227. const filing_type = this.ctx.service.filing.allFilingType, file_permission = '1,2';
  228. for (const u of users) {
  229. const nm = orgMember.find(x => { return u.id === x.uid; });
  230. if (nm) {
  231. if (!nm.file_permission) um.push({ id: nm.id, file_permission, filing_type });
  232. } else {
  233. im.push({ id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: u.id, file_permission, filing_type });
  234. }
  235. }
  236. const conn = await this.db.beginTransaction();
  237. try {
  238. await conn.update(this.tableName, { id: subProject.id, management: data.management });
  239. await this.ctx.service.filing.initFiling(subProject.id, conn);
  240. if (dm.length > 0) await conn.delete(this.ctx.service.subProjPermission.tableName, { id: dm });
  241. if (um.length > 0) await conn.updateRows(this.ctx.service.subProjPermission.tableName, um);
  242. if (im.length > 0) await conn.insert(this.ctx.service.subProjPermission.tableName, im);
  243. await conn.commit();
  244. return data;
  245. } catch (error) {
  246. await conn.rollback();
  247. throw error;
  248. }
  249. }
  250. }
  251. return SubProject;
  252. };