rpt_controller.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Created by Tony on 2017/3/13.
  3. */
  4. var JV = require('../rpt_component/jpc_value_define');
  5. var Template = require('../models/rpt_template');
  6. var TemplateData = require('../models/rpt_tpl_data');
  7. var JpcEx = require('../rpt_component/jpc_ex');
  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. var strUtil = require('../../../public/stringUtil');
  13. //统一回调函数
  14. var callback = function(req, res, err, data){
  15. if(err){
  16. //res.send({success: false, error: 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. module.exports = {
  25. getReportAllPages: function(req, res){
  26. var grp_id = req.body.grp_id;
  27. var tpl_id = req.body.tpl_id;
  28. var pageSize = req.body.pageSize;
  29. var rptTpl = null;
  30. Template.getPromise(grp_id, tpl_id).then(function(rst) {
  31. rptTpl = rst;
  32. if (rptTpl) {
  33. return TemplateData.getPromise(tpl_id);
  34. } else {
  35. callback(req, res, 'No report template was found!', null);
  36. }
  37. }).then(function(tplData){
  38. if (tplData) {
  39. var printCom = JpcEx.createNew();
  40. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  41. var defProperties = rptUtil.getReportDefaultCache();
  42. printCom.initialize(rptTpl);
  43. printCom.analyzeData(rptTpl, tplData, defProperties);
  44. var maxPages = printCom.totalPages;
  45. var pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  46. if (pageRst) {
  47. callback(req, res, null, 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. getExcel: function(req, res) {
  58. var grp_id = req.params.id, tpl_id = req.params.pm, pageSize = req.params.size, rptName = req.params.rptName;
  59. var rptTpl = null;
  60. Template.getPromise(grp_id, tpl_id).then(function(rst) {
  61. rptTpl = rst;
  62. if (rptTpl) {
  63. return TemplateData.getPromise(tpl_id);
  64. } else {
  65. callback(req, res, 'No report template was found!', null);
  66. }
  67. }).then(function(tplData){
  68. if (tplData) {
  69. var printCom = JpcEx.createNew();
  70. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  71. var defProperties = rptUtil.getReportDefaultCache();
  72. printCom.initialize(rptTpl);
  73. printCom.analyzeData(rptTpl, tplData, defProperties);
  74. var maxPages = printCom.totalPages;
  75. var pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  76. if (pageRst) {
  77. rpt_xl_util.exportExcel(pageRst, rptName, null, function(newName){
  78. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  79. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  80. var filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  81. filestream.on('data', function(chunk) {
  82. res.write(chunk);
  83. });
  84. filestream.on('end', function() {
  85. res.end();
  86. });
  87. });
  88. } else {
  89. callback(req, res, "Have errors while on going...", null);
  90. }
  91. } else {
  92. callback(req, res, 'No report data were found!', null);
  93. }
  94. }
  95. );
  96. },
  97. getDummyExcel: function(req, res) {
  98. var nodeExcel = require('excel-export'); //关联excel-export模块
  99. console.log("req.params.id:" + req.params.id);
  100. console.log("req.params.pm:" + req.params.pm);
  101. var conf = {};
  102. conf.cols = [
  103. {caption:'采购编号', type:'string'},
  104. {caption:'合同名称', type:'string'},
  105. {caption:'甲方', type:'string'},
  106. {caption:'甲方部门', type:'string'},
  107. {caption:'乙方', type:'string'},
  108. {caption:'乙方部门', type:'string'},
  109. {caption:'签订日期', type:'date'},
  110. {caption:'中标日期', type:'date'},
  111. {caption:'结束日期', type:'date'},
  112. {caption:'销售负责人', type:'string'},
  113. {caption:'商务负责人', type:'string'},
  114. {caption:'业绩归属部门', type:'string'},
  115. {caption:'金额', type:'number'},
  116. {caption:'状态', type:'string'}
  117. ];
  118. var data = [{myId: 0, name: "dummyExcel", partyA: "", partyADept: "", partyB:"", partyBDept: "", signDate: "", beginDate:"", endDate:"", amount: 100, state:"dummy!"}]
  119. var m_data = [];
  120. 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 ];
  121. m_data[0] = arry;
  122. conf.rows = m_data;
  123. var result = nodeExcel.execute(conf);
  124. //console.log(result);
  125. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  126. res.setHeader("Content-Disposition", "attachment; filename=" +data[0].name+ ".xlsx");
  127. res.end(result, 'binary');
  128. },
  129. dummyFunc: function(req, res){
  130. }
  131. };