123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- /**
- * Created by Tony on 2017/3/13.
- */
- var JV = require('../rpt_component/Jpc_ValueDefine');
- var Template = require('../models/rpt_template');
- var TemplateData = require('../models/rpt_temp_data');
- var JpcEx = require('../rpt_component/JpcEx');
- //var cache = require('../../../public/cache/cacheUtil');
- var rptUtil = require("../util/rpt_util");
- var rpt_xl_util = require('../util/rpt_excel_util');
- var fs = require('fs');
- //统一回调函数
- var callback = function(req, res, err, data){
- if(err){
- //res.send({success: false, error: err});
- res.json({success: false, error: err});
- }
- else{
- //res.send({success: true, data: data});
- res.json({success:true, data: data});
- }
- };
- module.exports = {
- getReportAllPages: function(req, res){
- var grp_id = req.body.grp_id;
- var tpl_id = req.body.tpl_id;
- var pageSize = req.body.pageSize;
- var rptTpl = null;
- Template.getPromise(grp_id, tpl_id).then(function(rst) {
- rptTpl = rst;
- if (rptTpl) {
- return TemplateData.getPromise(tpl_id);
- } else {
- callback(req, res, 'No report template was found!', null);
- }
- }).then(function(tplData){
- if (tplData) {
- var printCom = JpcEx.createInit();
- rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
- var defProperties = rptUtil.getReportDefaultCache();
- printCom.initialize(rptTpl);
- printCom.analyzeData(rptTpl, tplData, defProperties);
- var maxPages = printCom.totalPages;
- var pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
- if (pageRst) {
- callback(req, res, null, pageRst);
- } else {
- callback(req, res, "Have errors while on going...", null);
- }
- } else {
- callback(req, res, 'No report data were found!', null);
- }
- }
- );
- },
- getExcel: function(req, res) {
- var grp_id = req.params.id, tpl_id = req.params.pm, pageSize = req.params.size, rptName = req.params.rptName;
- var rptTpl = null;
- Template.getPromise(grp_id, tpl_id).then(function(rst) {
- rptTpl = rst;
- if (rptTpl) {
- return TemplateData.getPromise(tpl_id);
- } else {
- callback(req, res, 'No report template was found!', null);
- }
- }).then(function(tplData){
- if (tplData) {
- var printCom = JpcEx.createNew();
- rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
- var defProperties = rptUtil.getReportDefaultCache();
- printCom.initialize(rptTpl);
- printCom.analyzeData(rptTpl, tplData, defProperties);
- var maxPages = printCom.totalPages;
- var pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
- if (pageRst) {
- rpt_xl_util.exportExcel(pageRst, rptName, null, function(newName){
- res.setHeader('Content-Type', 'application/vnd.openxmlformats');
- res.setHeader("Content-Disposition", "attachment; filename=" + rptName + ".xlsx");
- var filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
- filestream.on('data', function(chunk) {
- res.write(chunk);
- });
- filestream.on('end', function() {
- res.end();
- });
- });
- } else {
- callback(req, res, "Have errors while on going...", null);
- }
- } else {
- callback(req, res, 'No report data were found!', null);
- }
- }
- );
- },
- getDummyExcel: function(req, res) {
- var nodeExcel = require('excel-export'); //关联excel-export模块
- console.log("req.params.id:" + req.params.id);
- console.log("req.params.pm:" + req.params.pm);
- var conf = {};
- conf.cols = [
- {caption:'采购编号', type:'string'},
- {caption:'合同名称', type:'string'},
- {caption:'甲方', type:'string'},
- {caption:'甲方部门', type:'string'},
- {caption:'乙方', type:'string'},
- {caption:'乙方部门', type:'string'},
- {caption:'签订日期', type:'date'},
- {caption:'中标日期', type:'date'},
- {caption:'结束日期', type:'date'},
- {caption:'销售负责人', type:'string'},
- {caption:'商务负责人', type:'string'},
- {caption:'业绩归属部门', type:'string'},
- {caption:'金额', type:'number'},
- {caption:'状态', type:'string'}
- ];
- var data = [{myId: 0, name: "dummyExcel", partyA: "", partyADept: "", partyB:"", partyBDept: "", signDate: "", beginDate:"", endDate:"", amount: 100, state:"dummy!"}]
- var m_data = [];
- 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 ];
- m_data[0] = arry;
- conf.rows = m_data;
- var result = nodeExcel.execute(conf);
- //console.log(result);
- res.setHeader('Content-Type', 'application/vnd.openxmlformats');
- res.setHeader("Content-Disposition", "attachment; filename=" +data[0].name+ ".xlsx");
- res.end(result, 'binary');
- },
- dummyFunc: function(req, res){
- }
- };
|