rpt_controller.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * Created by Tony on 2017/3/13.
  3. */
  4. let mongoose = require('mongoose');
  5. let JV = require('../rpt_component/jpc_value_define');
  6. let Template = mongoose.model('rpt_templates');
  7. let rptTplFacade = require('../facade/rpt_template_facade');
  8. let demoTemplateFacade = require('../facade/rpt_tpl_data_demo_facade');
  9. let JpcEx = require('../rpt_component/jpc_ex');
  10. let rptUtil = require("../util/rpt_util");
  11. let rpt_xl_util = require('../util/rpt_excel_util');
  12. let rpt_pdf_util = require('../util/rpt_pdf_util');
  13. let fs = require('fs');
  14. let strUtil = require('../../../public/stringUtil');
  15. //统一回调函数
  16. let callback = function(req, res, err, data){
  17. if(err){
  18. res.json({success: false, error: err});
  19. }
  20. else{
  21. //res.send({success: true, data: data});
  22. res.json({success:true, data: data});
  23. }
  24. };
  25. function getAllPagesCommonOrg(req, res, rpt_id, pageSize, cb) {
  26. let rptTpl = null;
  27. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  28. rptTpl = rst;
  29. if (rptTpl) {
  30. if (rptTpl.ID_KEY) {
  31. return demoTemplateFacade.getDemoData(rptTpl.ID_KEY);
  32. } else {
  33. callback(req, res, 'No report template data were found!', null);
  34. }
  35. } else {
  36. callback(req, res, 'No report template was found!', null);
  37. }
  38. }).then(function(tplData){
  39. if (tplData) {
  40. let printCom = JpcEx.createNew();
  41. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  42. let defProperties = rptUtil.getReportDefaultCache();
  43. printCom.initialize(rptTpl);
  44. printCom.analyzeData(rptTpl, tplData, defProperties);
  45. let maxPages = printCom.totalPages;
  46. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  47. if (pageRst) {
  48. cb(pageRst);
  49. } else {
  50. callback(req, res, "Have errors while on going...", null);
  51. }
  52. } else {
  53. callback(req, res, 'No report data were found!', null);
  54. }
  55. }
  56. );
  57. };
  58. function getAllPagesCommon(req, res, rpt_id, pageSize, cb) {
  59. let rptTpl = null;
  60. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  61. rptTpl = rst;
  62. if (rptTpl) {
  63. let tplData = {};
  64. if (rptTpl[JV.NODE_FIELD_MAP]) {
  65. //1. 离散数据
  66. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS].leng > 0) {
  67. tplData[JV.DATA_DISCRETE_DATA] = [];
  68. }
  69. //2. 主数据
  70. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS].leng > 0) {
  71. tplData[JV.DATA_MASTER_DATA] = [];
  72. }
  73. //3. 从数据
  74. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS].leng > 0) {
  75. tplData[JV.DATA_DETAIL_DATA] = [];
  76. }
  77. //2. Ex主数据
  78. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX].leng > 0) {
  79. tplData[JV.DATA_MASTER_DATA_EX] = [];
  80. }
  81. //3. Ex从数据
  82. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX].leng > 0) {
  83. tplData[JV.DATA_DETAIL_DATA_EX] = [];
  84. }
  85. //4. 重点: 开始组装$PROJECT对象
  86. let $PROJECT = {};
  87. //let $PROJECT.COMMON = {};
  88. //return demoTemplateData.getPromise(rptTpl.ID_KEY);
  89. //return demoTemplateData.findOne({"Data_Key": rptTpl.ID_KEY}).exec();
  90. } else {
  91. callback(req, res, 'No report template data were found!', null);
  92. }
  93. } else {
  94. callback(req, res, 'No report template was found!', null);
  95. }
  96. })
  97. };
  98. module.exports = {
  99. getReportAllPages: function(req, res){
  100. let rpt_id = req.body.ID;
  101. let pageSize = req.body.pageSize;
  102. getAllPagesCommonOrg(req, res, rpt_id, pageSize, function(pageRst){
  103. //fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  104. callback(req, res, null, pageRst);
  105. })
  106. },
  107. getExcel: function(req, res) {
  108. let rpt_id = req.params.id,
  109. pageSize = req.params.size,
  110. rptName = req.params.rptName,
  111. isOneSheet = req.params.isOneSheet;
  112. ;
  113. getAllPagesCommonOrg(req, res, rpt_id, pageSize, function(pageRst){
  114. rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, function(newName){
  115. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  116. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  117. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  118. filestream.on('data', function(chunk) {
  119. res.write(chunk);
  120. });
  121. filestream.on('end', function() {
  122. res.end();
  123. });
  124. });
  125. })
  126. },
  127. getPDF:function (req, res) {
  128. let rpt_id = req.params.id,
  129. pageSize = req.params.size,
  130. rptName = req.params.rptName;
  131. getAllPagesCommonOrg(req, res, rpt_id, pageSize, function(pageRst){
  132. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (newName) {
  133. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  134. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
  135. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.pdf');
  136. filestream.on('data', function(chunk) {
  137. res.write(chunk);
  138. });
  139. filestream.on('end', function() {
  140. res.end();
  141. });
  142. })
  143. })
  144. }
  145. };