unit_compare_controller.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const libConst = require('../const/lib');
  10. module.exports = app => {
  11. class UnitCompareController extends app.BaseController {
  12. /**
  13. * 指标对比页面
  14. *
  15. * @param {object} ctx - egg全局context
  16. * @return {void}
  17. */
  18. async index (ctx) {
  19. try {
  20. const libList = await ctx.service.quotaLib.getList(libConst.status.enter);
  21. const dyNames = await ctx.service.templateNode.getDanYuanNames();
  22. const xMatchDyNames = await ctx.service.templateNode.getXMatchDanYuanNames();
  23. for (const dn of dyNames) {
  24. dn.indexClass = this.app.nodeConst.indexClass.dy;
  25. const x = xMatchDyNames.find(function (a) {
  26. return a.name === dn.name;
  27. });
  28. dn.xMatch = false || (x !== null && x !== undefined);
  29. }
  30. const renderData = {
  31. libList,
  32. nodeConst: this.app.nodeConst,
  33. dyNames,
  34. };
  35. await this.layout('unit_compare/index.ejs', renderData, 'unit_compare/modal.ejs');
  36. } catch(err) {
  37. console.log(err);
  38. }
  39. }
  40. async getParent(ctx) {
  41. try {
  42. const data = JSON.parse(ctx.request.body.data);
  43. if (!data.name) throw '选择单元指标错误';
  44. if (!data.xMatch) throw '该单元指标无可对比项,请刷新页面重新';
  45. if (!data.lib_id && data.lib_id < 0) throw '选择造价文件错误';
  46. const parent = await this.ctx.service.tenderNode.searchParent(data.lib_id, data.name);
  47. ctx.body = {err: 0, msg: '', data: parent}
  48. } catch (err) {
  49. console.log(err);
  50. if (err.stack) {
  51. ctx.body = {err: 1, msg: '获取对比项失败,请重试', data: []};
  52. } else {
  53. ctx.body = {err: 1, msg: err, data: []};
  54. }
  55. }
  56. }
  57. async search(ctx) {
  58. try {
  59. const data = JSON.parse(ctx.request.body.data);
  60. console.log(data);
  61. if (!data.lib_id) {
  62. throw '查询造价文件数据错误';
  63. }
  64. if (!data.name) {
  65. throw '查询的指标分类错误';
  66. }
  67. const condition = {lib_id: data.lib_id, class_name: data.name};
  68. if (data.xMatch) {
  69. if (!data.selects || data.selects.length === 0) {
  70. throw '查询的对比项错误';
  71. }
  72. condition.bills_xid = [];
  73. for (const s of data.selects) {
  74. condition.bills_xid.push(s.id);
  75. }
  76. }
  77. const nodes = await ctx.service.tenderNode.getAllDataByCondition({ where: condition });
  78. for (const n of nodes) {
  79. const condition = {
  80. lib_id: n.lib_id,
  81. node_id: n.node_id,
  82. };
  83. n.children = await ctx.service.tenderIndex.getAllDataByCondition({where: condition});
  84. }
  85. ctx.body = {err: 0, msg: '', data: nodes};
  86. } catch (err) {
  87. ctx.body = {err: 1, msg: err.toString(), data: []};
  88. console.log(err);
  89. }
  90. }
  91. }
  92. return UnitCompareController;
  93. };