rpt_controller.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Created by Tony on 2017/3/13.
  3. */
  4. var JV = require('../rpt_component/Jpc_ValueDefine');
  5. var Template = require('../models/rpt_template');
  6. var TemplateData = require('../models/rpt_temp_data');
  7. var JpcEx = require('../rpt_component/JpcEx');
  8. //var cache = require('../../../public/cache/cacheUtil');
  9. var rptUtil = require("../util/rpt_util");
  10. var rpt_xl_util = require('../util/rpt_excel_util');
  11. var fs = require('fs');
  12. //统一回调函数
  13. var callback = function(req, res, err, data){
  14. if(err){
  15. //res.send({success: false, error: 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. var grp_id = req.body.grp_id;
  26. var tpl_id = req.body.tpl_id;
  27. var pageSize = req.body.pageSize;
  28. var rptTpl = null;
  29. Template.getPromise(grp_id, tpl_id).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. var printCom = JpcEx.createInit();
  39. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  40. var defProperties = rptUtil.getReportDefaultCache();
  41. printCom.initialize(rptTpl);
  42. printCom.analyzeData(rptTpl, tplData, defProperties);
  43. var maxPages = printCom.totalPages;
  44. var 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. var grp_id = req.params.id, tpl_id = req.params.pm, pageSize = req.params.size, rptName = req.params.rptName;
  58. var rptTpl = null;
  59. Template.getPromise(grp_id, tpl_id).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. var printCom = JpcEx.createNew();
  69. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  70. var defProperties = rptUtil.getReportDefaultCache();
  71. printCom.initialize(rptTpl);
  72. printCom.analyzeData(rptTpl, tplData, defProperties);
  73. var maxPages = printCom.totalPages;
  74. var 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=" + rptName + ".xlsx");
  79. var 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. getDummyExcel: function(req, res) {
  97. var nodeExcel = require('excel-export'); //关联excel-export模块
  98. console.log("req.params.id:" + req.params.id);
  99. console.log("req.params.pm:" + req.params.pm);
  100. var conf = {};
  101. conf.cols = [
  102. {caption:'采购编号', type:'string'},
  103. {caption:'合同名称', type:'string'},
  104. {caption:'甲方', type:'string'},
  105. {caption:'甲方部门', type:'string'},
  106. {caption:'乙方', type:'string'},
  107. {caption:'乙方部门', type:'string'},
  108. {caption:'签订日期', type:'date'},
  109. {caption:'中标日期', type:'date'},
  110. {caption:'结束日期', type:'date'},
  111. {caption:'销售负责人', type:'string'},
  112. {caption:'商务负责人', type:'string'},
  113. {caption:'业绩归属部门', type:'string'},
  114. {caption:'金额', type:'number'},
  115. {caption:'状态', type:'string'}
  116. ];
  117. var data = [{myId: 0, name: "dummyExcel", partyA: "", partyADept: "", partyB:"", partyBDept: "", signDate: "", beginDate:"", endDate:"", amount: 100, state:"dummy!"}]
  118. var m_data = [];
  119. var arry = [data[0].myId, data[0].name, data[0].partyA, data[0].partyADept, data[0].partyB, data[0].partyBDept, data[0].signDate, data[0].beginDate, data[0].endDate, "销售负责人", "商务负责人", "业绩归属部门", data[0].amount, data[0].state ];
  120. m_data[0] = arry;
  121. conf.rows = m_data;
  122. var result = nodeExcel.execute(conf);
  123. //console.log(result);
  124. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  125. res.setHeader("Content-Disposition", "attachment; filename=" +data[0].name+ ".xlsx");
  126. res.end(result, 'binary');
  127. },
  128. dummyFunc: function(req, res){
  129. }
  130. };