rpt_controller.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. //统一回调函数
  11. var callback = function(req, res, err, data){
  12. if(err){
  13. //res.send({success: false, error: err});
  14. res.json({success: false, error: err});
  15. }
  16. else{
  17. //res.send({success: true, data: data});
  18. res.json({success:true, data: data});
  19. }
  20. };
  21. module.exports = {
  22. getReportAllPages: function(req, res){
  23. var grp_id = req.body.grp_id;
  24. var tpl_id = req.body.tpl_id;
  25. var pageSize = req.body.pageSize;
  26. var rptTpl = null;
  27. Template.getPromise(grp_id, tpl_id).then(function(rst) {
  28. rptTpl = rst;
  29. if (rptTpl) {
  30. return TemplateData.getPromise(tpl_id);
  31. } else {
  32. callback(req, res, 'No report template was found!', null);
  33. }
  34. }).then(function(tplData){
  35. if (tplData) {
  36. var printCom = JpcEx.createInit();
  37. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  38. var defProperties = rptUtil.getReportDefaultCache();
  39. printCom.initialize(rptTpl);
  40. printCom.analyzeData(rptTpl, tplData, defProperties);
  41. var maxPages = printCom.totalPages;
  42. var pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  43. if (pageRst) {
  44. callback(req, res, null, pageRst);
  45. } else {
  46. callback(req, res, "Have errors while on going...", null);
  47. }
  48. } else {
  49. callback(req, res, 'No report data were found!', null);
  50. }
  51. }
  52. );
  53. },
  54. getDummyExcel: function(req, res) {
  55. var nodeExcel = require('excel-export'); //关联excel-export模块
  56. console.log("req.params.id:" + req.params.id);
  57. console.log("req.params.pm:" + req.params.pm);
  58. var conf = {};
  59. conf.cols = [
  60. {caption:'采购编号', type:'string'},
  61. {caption:'合同名称', type:'string'},
  62. {caption:'甲方', type:'string'},
  63. {caption:'甲方部门', type:'string'},
  64. {caption:'乙方', type:'string'},
  65. {caption:'乙方部门', type:'string'},
  66. {caption:'签订日期', type:'date'},
  67. {caption:'中标日期', type:'date'},
  68. {caption:'结束日期', type:'date'},
  69. {caption:'销售负责人', type:'string'},
  70. {caption:'商务负责人', type:'string'},
  71. {caption:'业绩归属部门', type:'string'},
  72. {caption:'金额', type:'number'},
  73. {caption:'状态', type:'string'}
  74. ];
  75. var data = [{myId: 0, name: "dummyExcel", partyA: "", partyADept: "", partyB:"", partyBDept: "", signDate: "", beginDate:"", endDate:"", amount: 100, state:"dummy!"}]
  76. var m_data = [];
  77. 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 ];
  78. m_data[0] = arry;
  79. conf.rows = m_data;
  80. var result = nodeExcel.execute(conf);
  81. //console.log(result);
  82. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  83. res.setHeader("Content-Disposition", "attachment; filename=" +data[0].name+ ".xlsx");
  84. res.end(result, 'binary');
  85. },
  86. dummyFunc: function(req, res){
  87. }
  88. };