rpt_controller.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. //return demoTemplateData.getPromise(rptTpl.ID_KEY);
  77. //return demoTemplateData.findOne({"Data_Key": rptTpl.ID_KEY}).exec();
  78. } else {
  79. callback(req, res, 'No report template data were found!', null);
  80. }
  81. } else {
  82. callback(req, res, 'No report template was found!', null);
  83. }
  84. })
  85. };
  86. module.exports = {
  87. getReportAllPages: function(req, res){
  88. let rpt_id = req.body.ID;
  89. let pageSize = req.body.pageSize;
  90. getAllPagesCommonOrg(req, res, rpt_id, pageSize, function(pageRst){
  91. //fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  92. callback(req, res, null, pageRst);
  93. })
  94. },
  95. getExcel: function(req, res) {
  96. let rpt_id = req.params.id,
  97. pageSize = req.params.size,
  98. rptName = req.params.rptName,
  99. isOneSheet = req.params.isOneSheet;
  100. ;
  101. getAllPagesCommonOrg(req, res, rpt_id, pageSize, function(pageRst){
  102. rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, function(newName){
  103. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  104. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  105. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  106. filestream.on('data', function(chunk) {
  107. res.write(chunk);
  108. });
  109. filestream.on('end', function() {
  110. res.end();
  111. });
  112. });
  113. })
  114. }
  115. };