| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 | /** * Created by Tony on 2017/3/13. */let mongoose = require('mongoose');let JV = require('../rpt_component/jpc_value_define');let Template = mongoose.model('rpt_templates');let rptTplFacade = require('../facade/rpt_template_facade');let demoTemplateFacade = require('../facade/rpt_tpl_data_demo_facade');let JpcEx = require('../rpt_component/jpc_ex');let rptUtil = require("../util/rpt_util");let rpt_xl_util = require('../util/rpt_excel_util');let fs = require('fs');let strUtil = require('../../../public/stringUtil');//统一回调函数let callback = function(req, res, err, data){    if(err){        res.json({success: false, error: err});    }    else{        //res.send({success: true, data: data});        res.json({success:true, data: data});    }};function getAllPagesCommonOrg(req, res, rpt_id, pageSize, cb) {    let rptTpl = null;    rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {        rptTpl = rst;        if (rptTpl) {            if (rptTpl.ID_KEY) {                return demoTemplateFacade.getDemoData(rptTpl.ID_KEY);            } else {                callback(req, res, 'No report template data were found!', null);            }        } else {            callback(req, res, 'No report template was found!', null);        }    }).then(function(tplData){            if (tplData) {                let printCom = JpcEx.createNew();                rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;                let defProperties = rptUtil.getReportDefaultCache();                printCom.initialize(rptTpl);                printCom.analyzeData(rptTpl, tplData, defProperties);                let maxPages = printCom.totalPages;                let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);                if (pageRst) {                    cb(pageRst);                } else {                    callback(req, res, "Have errors while on going...", null);                }            } else {                callback(req, res, 'No report data were found!', null);            }        }    );};function getAllPagesCommon(req, res, rpt_id, pageSize, cb) {    let rptTpl = null;    rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {        rptTpl = rst;        if (rptTpl) {            let tplData = {};            if (rptTpl[JV.NODE_FIELD_MAP]) {                //1. 离散数据                if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS].leng > 0) {                    tplData[JV.DATA_DISCRETE_DATA] = [];                }                //2. 主数据                if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS].leng > 0) {                    tplData[JV.DATA_MASTER_DATA] = [];                }                //3. 从数据                if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS].leng > 0) {                    tplData[JV.DATA_DETAIL_DATA] = [];                }                //2. Ex主数据                if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX].leng > 0) {                    tplData[JV.DATA_MASTER_DATA_EX] = [];                }                //3. Ex从数据                if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX].leng > 0) {                    tplData[JV.DATA_DETAIL_DATA_EX] = [];                }                //4. 重点: 开始组装$PROJECT对象                let $PROJECT = {};                //let $PROJECT.COMMON = {};                //return demoTemplateData.getPromise(rptTpl.ID_KEY);                //return demoTemplateData.findOne({"Data_Key": rptTpl.ID_KEY}).exec();            } else {                callback(req, res, 'No report template data were found!', null);            }        } else {            callback(req, res, 'No report template was found!', null);        }    })};module.exports = {    getReportAllPages: function(req, res){        let rpt_id = req.body.ID;        let pageSize = req.body.pageSize;        getAllPagesCommonOrg(req, res, rpt_id, pageSize, function(pageRst){            //fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));            callback(req, res, null, pageRst);        })    },    getExcel: function(req, res) {        let rpt_id = req.params.id,            pageSize = req.params.size,            rptName = req.params.rptName,            isOneSheet = req.params.isOneSheet;        ;        getAllPagesCommonOrg(req, res, rpt_id, pageSize, function(pageRst){            rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, function(newName){                res.setHeader('Content-Type', 'application/vnd.openxmlformats');                res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");                let 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();                });            });        })    }};
 |