sub_project.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const rootId = '-1';
  10. const imType = require('../const/tender').imType;
  11. const defaultFunRela = {
  12. banOver: true,
  13. hintOver: true,
  14. banMinusChangeBills: true,
  15. minusNoValue: true,
  16. lockPayExpr: false,
  17. showMinusCol: true,
  18. imType: imType.zl.value,
  19. needGcl: false,
  20. };
  21. const funSet = require('../const/fun_set');
  22. const defaultFunSet = funSet.defaultInfo;
  23. module.exports = app => {
  24. class SubProject extends app.BaseService {
  25. /**
  26. * 构造函数
  27. *
  28. * @param {Object} ctx - egg全局变量
  29. * @param {String} tableName - 表名
  30. * @return {void}
  31. */
  32. constructor(ctx) {
  33. super(ctx);
  34. this.tableName = 'sub_project';
  35. }
  36. async getSubProject(pid, uid, admin, filterFolder = false) {
  37. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  38. const permission = await this.ctx.service.subProjPermission.getUserPermission(pid, uid);
  39. result = result.filter(x => {
  40. if (x.is_folder) return !filterFolder;
  41. if (admin) return true;
  42. const pb = permission.find(y => { return x.id === y.spid});
  43. if (!pb) return false;
  44. x.user_permission = pb;
  45. return x.user_permission.budget_permission.length > 0 || x.user_permission.file_permission.length > 0 || x.user_permission.manage_permission.length > 0;
  46. });
  47. return result;
  48. }
  49. _filterEmptyFolder(data) {
  50. data.sort((a, b) => { return b.tree_level - a.tree_level});
  51. const result = [];
  52. for (const d of data) {
  53. if (!d.is_folder) result.push(d);
  54. if (result.find(x => { return x.tree_pid === d.id; })) result.push(d);
  55. }
  56. return result;
  57. }
  58. async getBudgetProject(pid, uid, admin) {
  59. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  60. const adminPermission = this.ctx.service.subProjPermission.adminPermission;
  61. const permission = admin ? [] : await this.ctx.service.subProjPermission.getUserPermission(pid, uid);
  62. result = result.filter(x => {
  63. if (!x.is_folder && !x.budget_id) return false;
  64. if (x.is_folder) return true;
  65. if (admin) {
  66. x.permission = adminPermission.budget_permission;
  67. x.manage_permission = adminPermission.manage_permission;
  68. return true;
  69. } else {
  70. const pb = permission.find(y => { return x.id === y.spid});
  71. if (!pb) return false;
  72. x.permission = pb.budget_permission;
  73. x.manage_permission = pb.manage_permission;
  74. return x.permission.length > 0;
  75. }
  76. });
  77. return this._filterEmptyFolder(result);
  78. }
  79. async getFileProject(pid, uid, admin) {
  80. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  81. const adminPermission = this.ctx.service.subProjPermission.adminPermission;
  82. const permission = await this.ctx.service.subProjPermission.getUserPermission(pid, uid);
  83. result = result.filter(x => {
  84. if (!x.is_folder && !x.management) return false;
  85. if (x.is_folder) return true;
  86. if (admin) {
  87. x.permission = adminPermission.file_permission;
  88. x.manage_permission = adminPermission.manage_permission;
  89. return true;
  90. } else {
  91. const pb = permission.find(y => { return x.id === y.spid});
  92. if (!pb) return false;
  93. x.permission = pb.file_permission;
  94. x.manage_permission = pb.manage_permission;
  95. return x.permission.length > 0;
  96. }
  97. });
  98. return this._filterEmptyFolder(result);
  99. }
  100. async getLastChild(tree_pid) {
  101. const result = await this.getAllDataByCondition({ where: { tree_pid, project_id: this.ctx.session.sessionProject.id }, orders: [['tree_order', 'desc']], limit: 1, offset: 0 });
  102. return result[0];
  103. }
  104. async getPosterityData(id){
  105. const result = [];
  106. let cur = await this.getAllDataByCondition({ where: { tree_pid: id, project_id: this.ctx.session.sessionProject.id } });
  107. let iLevel = 1;
  108. while (cur.length > 0 && iLevel < 6) {
  109. result.push(...cur);
  110. cur = await this.getAllDataByCondition({ where: { tree_pid: cur.map(x => { return x.id })} });
  111. iLevel += 1;
  112. }
  113. return result;
  114. }
  115. async getStepNode(node, step) {
  116. const tree_order = [];
  117. while(step) {
  118. tree_order.push(node.tree_order + step);
  119. if (step > 0) {
  120. step = step - 1;
  121. } else {
  122. step = step + 1;
  123. }
  124. }
  125. return await this.getAllDataByCondition({ where: { tree_pid: node.tree_pid, tree_order, project_id: this.ctx.session.sessionProject.id }, orders: [['tree_order', 'asc']]});
  126. }
  127. async addFolder(data) {
  128. const parent = await this.getDataById(data.tree_pid);
  129. if (parent && !parent.is_folder) throw '添加数据结构错误';
  130. const lastChild = await this.getLastChild(parent ? parent.id : rootId);
  131. const conn = await this.db.beginTransaction();
  132. try {
  133. // 获取当前用户信息
  134. const sessionUser = this.ctx.session.sessionUser;
  135. // 获取当前项目信息
  136. const sessionProject = this.ctx.session.sessionProject;
  137. const insertData = {
  138. id: this.uuid.v4(), project_id: sessionProject.id, user_id: sessionUser.accountId,
  139. tree_pid: data.tree_pid,
  140. tree_level: parent ? parent.tree_level + 1 : 1,
  141. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  142. name: data.name, is_folder: 1,
  143. };
  144. const operate = await conn.insert(this.tableName, insertData);
  145. if (operate.affectedRows === 0) throw '新增文件夹失败';
  146. await conn.commit();
  147. return await this.getSubProject(sessionProject.id, sessionUser.accountId, sessionUser.is_admin);
  148. } catch (error) {
  149. await conn.rollback();
  150. throw error;
  151. }
  152. }
  153. async addSubProject(data) {
  154. const parent = await this.getDataById(data.tree_pid);
  155. if (parent && !parent.is_folder) throw '添加数据结构错误';
  156. const lastChild = await this.getLastChild(parent ? parent.id : rootId);
  157. const conn = await this.db.beginTransaction();
  158. try {
  159. // 获取当前用户信息
  160. const sessionUser = this.ctx.session.sessionUser;
  161. // 获取当前项目信息
  162. const sessionProject = this.ctx.session.sessionProject;
  163. const insertData = {
  164. id: this.uuid.v4(), project_id: sessionProject.id, user_id: sessionUser.accountId,
  165. tree_pid: data.tree_pid,
  166. tree_level: parent ? parent.tree_level + 1 : 1,
  167. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  168. name: data.name, is_folder: 0,
  169. };
  170. const operate = await conn.insert(this.tableName, insertData);
  171. // todo 根据节点新增时的其他操作
  172. if (operate.affectedRows === 0) throw '新增文件夹失败';
  173. await conn.commit();
  174. return await this.getSubProject(sessionProject.id, sessionUser.accountId, sessionUser.is_admin);
  175. } catch (error) {
  176. await conn.rollback();
  177. throw error;
  178. }
  179. }
  180. async dragTo(data) {
  181. const dragNode = await this.getDataById(data.drag_id);
  182. const dropNode = await this.getDataById(data.drop_id);
  183. if (!dragNode || !dropNode || !dropNode.is_folder) throw '拖拽数据结构错误';
  184. const lastChild = await this.getLastChild(dropNode.id);
  185. const posterity = await this.getPosterityData(dragNode.id);
  186. const conn = await this.db.beginTransaction();
  187. try {
  188. const updateData = {
  189. id: dragNode.id, tree_pid: dropNode.id, tree_level: dropNode.tree_level + 1,
  190. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  191. };
  192. await conn.update(this.tableName, updateData);
  193. if (dragNode.tree_level !== dropNode.tree_level + 1 && posterity.length > 0) {
  194. const posterityUpdateData = posterity.map(x => {
  195. return { id: x.id, tree_level: dropNode.tree_level + 1 - dragNode.tree_level + x.tree_level }
  196. });
  197. await conn.updateRows(this.tableName, posterityUpdateData);
  198. }
  199. // 升级原来的后项的order
  200. await conn.query(`UPDATE ${this.tableName} SET tree_order = tree_order-1 WHERE tree_pid = ? AND tree_order > ?`, [dragNode.tree_pid, dragNode.tree_order]);
  201. await conn.commit();
  202. } catch (error) {
  203. await conn.rollback();
  204. throw error;
  205. }
  206. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  207. }
  208. async _siblingMove(node, step) {
  209. const stepNode = await this.getStepNode(node, step);
  210. const conn = await this.db.beginTransaction();
  211. try {
  212. const updateData = [];
  213. updateData.push({ id: node.id, tree_order: node.tree_order + step });
  214. for (const sn of stepNode) {
  215. updateData.push({ id: node.id, tree_order: step > 0 ? sn.tree_order - 1 : sn.tree_order + 1 });
  216. }
  217. await conn.updateRows(this.tableName, updateData);
  218. await conn.commit();
  219. } catch (error) {
  220. await conn.rollback();
  221. throw error;
  222. }
  223. }
  224. async _siblingMoveForce(node, step) {
  225. const sibling = await this.getAllDataByCondition({ where: { tree_pid: node.tree_pid, project_id: this.ctx.session.sessionProject.id }, orders: [['tree_order', 'asc']] });
  226. const nodeIndex = sibling.findIndex(x => { return x.id === node.id });
  227. if (nodeIndex + step < 0) throw '移动数据结构错误';
  228. if (nodeIndex + step > sibling.length - 1) throw '移动数据结构错误';
  229. const conn = await this.db.beginTransaction();
  230. try {
  231. const updateData = [];
  232. updateData.push({ id: node.id, tree_order: sibling[nodeIndex + step].tree_order });
  233. while(step) {
  234. const stepNode = sibling[nodeIndex + step];
  235. if (step > 0) {
  236. updateData.push({ id: stepNode.id, tree_order: sibling[nodeIndex + step - 1].tree_order });
  237. step = step - 1;
  238. } else {
  239. updateData.push({ id: stepNode.id, tree_order: sibling[nodeIndex + step + 1].tree_order});
  240. step = step + 1;
  241. }
  242. }
  243. await conn.updateRows(this.tableName, updateData);
  244. await conn.commit();
  245. } catch (error) {
  246. await conn.rollback();
  247. throw error;
  248. }
  249. }
  250. async _topMove(node) {
  251. const lastChild = await this.getLastChild(rootId);
  252. const posterity = await this.getPosterityData(node.id);
  253. const conn = await this.db.beginTransaction();
  254. try {
  255. const updateData = { id: node.id, tree_pid: rootId, tree_level: 1, tree_order: lastChild ? lastChild.tree_order + 1 : 1 };
  256. await conn.update(this.tableName, updateData);
  257. if (node.tree_level !== 1 && posterity.length > 0) {
  258. const posterityUpdateData = posterity.map(x => {
  259. return { id: x.id, tree_level: x.tree_level - node.tree_level + 1 }
  260. });
  261. await conn.updateRows(this.tableName, posterityUpdateData);
  262. }
  263. // 升级原来的后项的order
  264. await conn.query(`UPDATE ${this.tableName} SET tree_order = tree_order-1 WHERE tree_pid = ? AND tree_order > ?`, [node.tree_pid, node.tree_order]);
  265. await conn.commit();
  266. } catch (error) {
  267. await conn.rollback();
  268. throw error;
  269. }
  270. }
  271. async move(data) {
  272. const node = await this.getDataById(data.id);
  273. if (!node) throw '移动数据结构错误';
  274. switch(data.type) {
  275. case 'up': await this._siblingMoveForce(node, -1); break;
  276. case 'down': await this._siblingMoveForce(node, 1); break;
  277. case 'top': await this._topMove(node); break;
  278. default: throw '未知移动类型';
  279. }
  280. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  281. }
  282. async del(id) {
  283. const node = await this.getDataById(id);
  284. if (!node) throw '删除的数据不存在';
  285. const posterity = await this.getPosterityData(node.id);
  286. const updateData = [
  287. { id: node.id, is_delete: 1 },
  288. ];
  289. posterity.forEach(x => {
  290. updateData.push({ id: x.id, is_delete: 1});
  291. });
  292. await this.db.updateRows(this.tableName, updateData);
  293. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  294. }
  295. async save(data) {
  296. const result = await this.db.update(this.tableName, data);
  297. if (result.affectedRows > 0) {
  298. return data;
  299. } else {
  300. throw '更新数据失败';
  301. }
  302. }
  303. async setBudgetStd(data) {
  304. const subProject = await this.getDataById(data.id);
  305. const budgetStd = await this.ctx.service.budgetStd.getDataById(data.std_id);
  306. if (!budgetStd) throw '选择的概算标准不存在,请刷新页面重试';
  307. const conn = await this.db.beginTransaction();
  308. try {
  309. const budget_id = await this.ctx.service.budget.add(conn, {
  310. pid: subProject.project_id, user_id: subProject.user_id, rela_tender: subProject.rela_tender
  311. }, budgetStd);
  312. const updateData = { id: data.id, std_id: budgetStd.id, std_name: budgetStd.name, budget_id };
  313. await conn.update(this.tableName, updateData);
  314. await conn.commit();
  315. return updateData;
  316. } catch (error) {
  317. await conn.rollback();
  318. throw error;
  319. }
  320. }
  321. async setRelaTender(data) {
  322. const subProject = await this.getDataById(data.id);
  323. const orgRelaTenderId = subProject.rela_tender.split(',');
  324. const conn = await this.db.beginTransaction();
  325. try {
  326. await conn.update(this.tableName, data);
  327. await conn.update(this.ctx.service.budget.tableName, { id: subProject.budget_id, rela_tender: data.rela_tender });
  328. const relaTenderId = data.rela_tender.split(',');
  329. const removeTenderId = orgRelaTenderId.filter(x => { return relaTenderId.indexOf(x) < 0});
  330. const addTenderId = relaTenderId.filter(x => { return orgRelaTenderId.indexOf(x) < 0});
  331. if (removeTenderId.length > 0) await conn.update(this.ctx.service.tender.tableName, { spid: '' }, { where: { id: removeTenderId }});
  332. if (addTenderId.length > 0) await conn.update(this.ctx.service.tender.tableName, { spid: data.id }, { where: { id: addTenderId }});
  333. await conn.commit();
  334. return data;
  335. } catch (error) {
  336. await conn.rollback();
  337. throw error;
  338. }
  339. }
  340. async addRelaTender(transaction, spid, tid) {
  341. if (!transaction) throw '未定义事务';
  342. const subProject = await this.getDataById(spid);
  343. if (!subProject) throw '所属项目不存在';
  344. const rela = subProject.rela_tender.split(',');
  345. if (rela.indexOf(tid + '') >= 0) return;
  346. rela.push(tid + '');
  347. const rela_tender = rela.join(',');
  348. await transaction.update(this.tableName, { id: spid, rela_tender});
  349. await transaction.update(this.ctx.service.budget.tableName, { id: subProject.budget_id, rela_tender});
  350. }
  351. async removeRelaTender(transaction, spid, tid) {
  352. if (!transaction) throw '未定义事务';
  353. const subProject = await this.getDataById(spid);
  354. if (!subProject) throw '所属项目不存在';
  355. const rela = subProject.rela_tender.split(',');
  356. if (rela.indexOf(tid + '') < 0) return;
  357. const rela_tender = rela.filter(x => { return x === tid + ''}).join(',');
  358. await transaction.update(this.tableName, { id: spid, rela_tender});
  359. await transaction.update(this.ctx.service.budget.tableName, { id: subProject.budget_id, rela_tender});
  360. }
  361. async setManagement(data) {
  362. const subProject = await this.getDataById(data.id);
  363. if (subProject.management === data.management) return data;
  364. const users = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: subProject.project_id, company: data.management }});
  365. const orgMember = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid: subProject.id } });
  366. const dm = [], um = [], im = [];
  367. const template = await this.ctx.service.filingTemplateList.getDataById(data.filingTemplate);
  368. if (!template) throw '选择的文件类别不存在';
  369. const templateFiling = await this.ctx.service.filingTemplate.getAllDataByCondition({
  370. where: { temp_id: template.id, is_fixed: 1 },
  371. });
  372. const filing_type = this.ctx.service.filing.analysisFilingType(templateFiling).map(x => { return x.value; }).join(','), file_permission = '1,2';
  373. for (const u of users) {
  374. const nm = orgMember.find(x => { return u.id === x.uid; });
  375. if (nm) {
  376. if (!nm.file_permission) um.push({ id: nm.id, file_permission, filing_type });
  377. } else {
  378. im.push({ id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: u.id, file_permission, filing_type });
  379. }
  380. }
  381. const conn = await this.db.beginTransaction();
  382. try {
  383. await conn.update(this.tableName, { id: subProject.id, management: data.management, filing_template_id: template.id, filing_template_name: template.name });
  384. await this.ctx.service.filing.initFiling(subProject.id, data.filingTemplate, conn);
  385. if (dm.length > 0) await conn.delete(this.ctx.service.subProjPermission.tableName, { id: dm });
  386. if (um.length > 0) await conn.updateRows(this.ctx.service.subProjPermission.tableName, um);
  387. if (im.length > 0) await conn.insert(this.ctx.service.subProjPermission.tableName, im);
  388. await conn.commit();
  389. return data;
  390. } catch (error) {
  391. await conn.rollback();
  392. throw error;
  393. }
  394. }
  395. async refreshManagementPermission(data) {
  396. const subProject = await this.getDataById(data.id);
  397. const users = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: subProject.project_id, company: subProject.management }});
  398. const orgMember = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid: subProject.id } });
  399. const dm = [], um = [], im = [];
  400. const filing_type = this.ctx.service.filing.allFilingType.join(','), file_permission = '1,2';
  401. for (const u of users) {
  402. const nm = orgMember.find(x => { return u.id === x.uid; });
  403. if (nm) {
  404. if (!nm.file_permission) um.push({ id: nm.id, file_permission, filing_type });
  405. } else {
  406. im.push({ id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: u.id, file_permission, filing_type });
  407. }
  408. }
  409. const conn = await this.db.beginTransaction();
  410. try {
  411. if (dm.length > 0) await conn.delete(this.ctx.service.subProjPermission.tableName, { id: dm });
  412. if (um.length > 0) await conn.updateRows(this.ctx.service.subProjPermission.tableName, um);
  413. if (im.length > 0) await conn.insert(this.ctx.service.subProjPermission.tableName, im);
  414. await conn.commit();
  415. return { dm: dm.length, um: um.length, im: im.length };
  416. } catch (error) {
  417. await conn.rollback();
  418. throw error;
  419. }
  420. }
  421. // 合同管理获取项目列表
  422. async getSubProjectByContract(pid, uid, admin, filterFolder = false) {
  423. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  424. if (admin) return this._filterEmptyFolder(result);
  425. const permission = await this.ctx.service.contractAudit.getAllDataByCondition({ where: { uid } });
  426. result = result.filter(x => {
  427. if (x.is_folder) return !filterFolder;
  428. const pb = permission.find(y => { return x.id === y.spid; });
  429. if (!pb) return false;
  430. return true;
  431. });
  432. return this._filterEmptyFolder(result);
  433. }
  434. async getSubProjectByTender(pid, tenders, filterFolder = false) {
  435. if (tenders.length === 0) return [];
  436. const spids = this._.uniq(this._.map(tenders, 'spid'));
  437. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  438. result = result.filter(x => {
  439. if (x.is_folder) return !filterFolder;
  440. if (!x.rela_tender) return false;
  441. return this._.includes(spids, x.id);
  442. });
  443. return this._filterEmptyFolder(result);
  444. }
  445. // 合同管理获取项目列表
  446. async getSubProjectByFinancial(pid, uid, admin, filterFolder = false) {
  447. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  448. if (admin) return this._filterEmptyFolder(result);
  449. const permission = await this.ctx.service.financialAudit.getAllDataByCondition({ where: { uid } });
  450. result = result.filter(x => {
  451. if (x.is_folder) return !filterFolder;
  452. const pb = permission.find(y => { return x.id === y.spid; });
  453. if (!pb) return false;
  454. return true;
  455. });
  456. return this._filterEmptyFolder(result);
  457. }
  458. async getFileReference(subProject) {
  459. return await this.db.query(`SELECT id, name FROM zh_file_reference_list`);
  460. };
  461. /**
  462. * 功能设置
  463. * @param id
  464. * @returns {Promise<null>}
  465. */
  466. async getFunRela(subProject) {
  467. const result = subProject.fun_rela ? JSON.parse(subProject.fun_rela) : {};
  468. this.ctx.helper._.defaults(result, defaultFunRela);
  469. return result;
  470. }
  471. async updateFunRela(id, data) {
  472. const result = await this.db.update(this.tableName, {
  473. id: id, fun_rela: JSON.stringify({
  474. banOver: data.banOver, hintOver: data.hintOver, banMinusChangeBills: data.banMinusChangeBills,
  475. imType: data.imType, needGcl: data.needGcl, minusNoValue: data.minusNoValue,
  476. lockPayExpr: data.lockPayExpr, showMinusCol: data.showMinusCol,
  477. }),
  478. });
  479. return result.affectedRows === 1;
  480. }
  481. async getFunSet(fun_set = null) {
  482. const result = fun_set ? JSON.parse(fun_set) : {};
  483. this.ctx.helper._.defaults(result, defaultFunSet);
  484. return result;
  485. }
  486. async updateFunSet(id, funSet) {
  487. const result = await this.db.update(this.tableName, {
  488. id, fun_set: JSON.stringify(funSet),
  489. });
  490. return result.affectedRows === 1;
  491. }
  492. }
  493. return SubProject;
  494. };