sub_project.js 13 KB

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