rpt_controller.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Created by Tony on 2017/3/13.
  3. */
  4. let JV = require('../rpt_component/jpc_value_define');
  5. let Template = require('../models/rpt_template');
  6. let TemplateData = require('../models/rpt_tpl_data_demo');
  7. let JpcEx = require('../rpt_component/jpc_ex');
  8. //let cache = require('../../../public/cache/cacheUtil');
  9. let rptUtil = require("../util/rpt_util");
  10. let rpt_xl_util = require('../util/rpt_excel_util');
  11. let fs = require('fs');
  12. let strUtil = require('../../../public/stringUtil');
  13. //统一回调函数
  14. let callback = function(req, res, err, data){
  15. if(err){
  16. res.json({success: false, error: err});
  17. }
  18. else{
  19. //res.send({success: true, data: data});
  20. res.json({success:true, data: data});
  21. }
  22. };
  23. module.exports = {
  24. getReportAllPages: function(req, res){
  25. let grp_id = req.body.grp_id;
  26. let tpl_id = req.body.tpl_id;
  27. let pageSize = req.body.pageSize;
  28. let rptTpl = null;
  29. Template.findOne({GROUP_KEY: grp_id, ID_KEY: tpl_id}, '-_id').exec().then(function(rst) {
  30. rptTpl = rst;
  31. if (rptTpl) {
  32. return TemplateData.getPromise(tpl_id);
  33. } else {
  34. callback(req, res, 'No report template was found!', null);
  35. }
  36. }).then(function(tplData){
  37. if (tplData) {
  38. let printCom = JpcEx.createNew();
  39. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  40. let defProperties = rptUtil.getReportDefaultCache();
  41. printCom.initialize(rptTpl);
  42. printCom.analyzeData(rptTpl, tplData, defProperties);
  43. let maxPages = printCom.totalPages;
  44. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  45. if (pageRst) {
  46. callback(req, res, null, pageRst);
  47. } else {
  48. callback(req, res, "Have errors while on going...", null);
  49. }
  50. } else {
  51. callback(req, res, 'No report data were found!', null);
  52. }
  53. }
  54. );
  55. },
  56. getExcel: function(req, res) {
  57. let grp_id = req.params.id, tpl_id = req.params.pm, pageSize = req.params.size, rptName = req.params.rptName;
  58. let rptTpl = null;
  59. Template.findOne({GROUP_KEY: grp_id, ID_KEY: tpl_id}, '-_id').exec().then(function(rst) {
  60. rptTpl = rst;
  61. if (rptTpl) {
  62. return TemplateData.getPromise(tpl_id);
  63. } else {
  64. callback(req, res, 'No report template was found!', null);
  65. }
  66. }).then(function(tplData){
  67. if (tplData) {
  68. let printCom = JpcEx.createNew();
  69. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  70. let defProperties = rptUtil.getReportDefaultCache();
  71. printCom.initialize(rptTpl);
  72. printCom.analyzeData(rptTpl, tplData, defProperties);
  73. let maxPages = printCom.totalPages;
  74. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  75. if (pageRst) {
  76. rpt_xl_util.exportExcel(pageRst, rptName, null, function(newName){
  77. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  78. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  79. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  80. filestream.on('data', function(chunk) {
  81. res.write(chunk);
  82. });
  83. filestream.on('end', function() {
  84. res.end();
  85. });
  86. });
  87. } else {
  88. callback(req, res, "Have errors while on going...", null);
  89. }
  90. } else {
  91. callback(req, res, 'No report data were found!', null);
  92. }
  93. }
  94. );
  95. }
  96. };