sub_project.js 16 KB

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