rpt_controller.js 5.4 KB

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