sub_project.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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, project_id: this.ctx.session.sessionProject.id }, 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, project_id: this.ctx.session.sessionProject.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, project_id: this.ctx.session.sessionProject.id }, orders: [['tree_order', 'asc']]});
  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 _siblingMoveForce(node, step) {
  212. const sibling = await this.getAllDataByCondition({ where: { tree_pid: node.tree_pid, project_id: this.ctx.session.sessionProject.id }, orders: [['tree_order', 'asc']] });
  213. const nodeIndex = sibling.findIndex(x => { return x.id === node.id });
  214. if (nodeIndex + step < 0) throw '移动数据结构错误';
  215. if (nodeIndex + step > sibling.length - 1) throw '移动数据结构错误';
  216. const conn = await this.db.beginTransaction();
  217. try {
  218. const updateData = [];
  219. updateData.push({ id: node.id, tree_order: sibling[nodeIndex + step].tree_order });
  220. while(step) {
  221. const stepNode = sibling[nodeIndex + step];
  222. if (step > 0) {
  223. updateData.push({ id: stepNode.id, tree_order: sibling[nodeIndex + step - 1].tree_order });
  224. step = step - 1;
  225. } else {
  226. updateData.push({ id: stepNode.id, tree_order: sibling[nodeIndex + step + 1].tree_order});
  227. step = step + 1;
  228. }
  229. }
  230. await conn.updateRows(this.tableName, updateData);
  231. await conn.commit();
  232. } catch (error) {
  233. await conn.rollback();
  234. throw error;
  235. }
  236. }
  237. async _topMove(node) {
  238. const lastChild = await this.getLastChild(rootId);
  239. const posterity = await this.getPosterityData(node.id);
  240. const conn = await this.db.beginTransaction();
  241. try {
  242. const updateData = { id: node.id, tree_pid: rootId, tree_level: 1, tree_order: lastChild ? lastChild.tree_order + 1 : 1 };
  243. await conn.update(this.tableName, updateData);
  244. if (node.tree_level !== 1 && posterity.length > 0) {
  245. const posterityUpdateData = posterity.map(x => {
  246. return { id: x.id, tree_level: x.tree_level - node.tree_level + 1 }
  247. });
  248. await conn.updateRows(this.tableName, posterityUpdateData);
  249. }
  250. // 升级原来的后项的order
  251. await conn.query(`UPDATE ${this.tableName} SET tree_order = tree_order-1 WHERE tree_pid = ? AND tree_order > ?`, [node.tree_pid, node.tree_order]);
  252. await conn.commit();
  253. } catch (error) {
  254. await conn.rollback();
  255. throw error;
  256. }
  257. }
  258. async move(data) {
  259. const node = await this.getDataById(data.id);
  260. if (!node) throw '移动数据结构错误';
  261. switch(data.type) {
  262. case 'up': await this._siblingMoveForce(node, -1); break;
  263. case 'down': await this._siblingMoveForce(node, 1); break;
  264. case 'top': await this._topMove(node); break;
  265. default: throw '未知移动类型';
  266. }
  267. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  268. }
  269. async del(id) {
  270. const node = await this.getDataById(id);
  271. if (!node) throw '删除的数据不存在';
  272. const posterity = await this.getPosterityData(node.id);
  273. const updateData = [
  274. { id: node.id, is_delete: 1 },
  275. ];
  276. posterity.forEach(x => {
  277. updateData.push({ id: x.id, is_delete: 1});
  278. });
  279. await this.db.updateRows(this.tableName, updateData);
  280. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  281. }
  282. async save(data) {
  283. const result = await this.db.update(this.tableName, data);
  284. if (result.affectedRows > 0) {
  285. return data;
  286. } else {
  287. throw '更新数据失败';
  288. }
  289. }
  290. async setBudgetStd(data) {
  291. const subProject = await this.getDataById(data.id);
  292. const budgetStd = await this.ctx.service.budgetStd.getDataById(data.std_id);
  293. if (!budgetStd) throw '选择的概算标准不存在,请刷新页面重试';
  294. const conn = await this.db.beginTransaction();
  295. try {
  296. const budget_id = await this.ctx.service.budget.add(conn, {
  297. pid: subProject.project_id, user_id: subProject.user_id, rela_tender: subProject.rela_tender
  298. }, budgetStd);
  299. const updateData = { id: data.id, std_id: budgetStd.id, std_name: budgetStd.name, budget_id };
  300. await conn.update(this.tableName, updateData);
  301. await conn.commit();
  302. return updateData;
  303. } catch (error) {
  304. await conn.rollback();
  305. throw error;
  306. }
  307. }
  308. async setRelaTender(data) {
  309. const subProject = await this.getDataById(data.id);
  310. const conn = await this.db.beginTransaction();
  311. try {
  312. await conn.update(this.tableName, data);
  313. await conn.update(this.ctx.service.budget.tableName, { id: subProject.budget_id, rela_tender: data.rela_tender });
  314. await conn.commit();
  315. return data;
  316. } catch (error) {
  317. await conn.rollback();
  318. throw error;
  319. }
  320. }
  321. async setManagement(data) {
  322. const subProject = await this.getDataById(data.id);
  323. if (subProject.management === data.management) return data;
  324. const users = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: subProject.project_id, company: data.management }});
  325. const orgMember = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid: subProject.id } });
  326. const dm = [], um = [], im = [];
  327. const filing_type = this.ctx.service.filing.allFilingType.join(','), file_permission = '1,2';
  328. for (const u of users) {
  329. const nm = orgMember.find(x => { return u.id === x.uid; });
  330. if (nm) {
  331. if (!nm.file_permission) um.push({ id: nm.id, file_permission, filing_type });
  332. } else {
  333. im.push({ id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: u.id, file_permission, filing_type });
  334. }
  335. }
  336. const conn = await this.db.beginTransaction();
  337. try {
  338. await conn.update(this.tableName, { id: subProject.id, management: data.management });
  339. await this.ctx.service.filing.initFiling(subProject.id, conn);
  340. if (dm.length > 0) await conn.delete(this.ctx.service.subProjPermission.tableName, { id: dm });
  341. if (um.length > 0) await conn.updateRows(this.ctx.service.subProjPermission.tableName, um);
  342. if (im.length > 0) await conn.insert(this.ctx.service.subProjPermission.tableName, im);
  343. await conn.commit();
  344. return data;
  345. } catch (error) {
  346. await conn.rollback();
  347. throw error;
  348. }
  349. }
  350. async refreshManagementPermission(data) {
  351. const subProject = await this.getDataById(data.id);
  352. const users = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: subProject.project_id, company: subProject.management }});
  353. const orgMember = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid: subProject.id } });
  354. const dm = [], um = [], im = [];
  355. const filing_type = this.ctx.service.filing.allFilingType.join(','), file_permission = '1,2';
  356. for (const u of users) {
  357. const nm = orgMember.find(x => { return u.id === x.uid; });
  358. if (nm) {
  359. if (!nm.file_permission) um.push({ id: nm.id, file_permission, filing_type });
  360. } else {
  361. im.push({ id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: u.id, file_permission, filing_type });
  362. }
  363. }
  364. const conn = await this.db.beginTransaction();
  365. try {
  366. if (dm.length > 0) await conn.delete(this.ctx.service.subProjPermission.tableName, { id: dm });
  367. if (um.length > 0) await conn.updateRows(this.ctx.service.subProjPermission.tableName, um);
  368. if (im.length > 0) await conn.insert(this.ctx.service.subProjPermission.tableName, im);
  369. await conn.commit();
  370. return { dm: dm.length, um: um.length, im: im.length };
  371. } catch (error) {
  372. await conn.rollback();
  373. throw error;
  374. }
  375. }
  376. }
  377. return SubProject;
  378. };