123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- /**
- * Created by Tony on 2017/3/13.
- */
- import mongoose from "mongoose";
- import async from "async";
- import JV from "../rpt_component/jpc_value_define";
- let Template = mongoose.model('rpt_templates');
- let rptTplDataFacade = require("../facade/rpt_tpl_data_facade");
- //let fsUtil = require("../../../public/fsUtil");
- import rptTplFacade from "../facade/rpt_template_facade";
- import demoTemplateFacade from "../facade/rpt_tpl_data_demo_facade";
- import JpcEx from "../rpt_component/jpc_ex";
- import rptUtil from "../util/rpt_util";
- import rpt_xl_util from "../util/rpt_excel_util";
- import rpt_pdf_util from "../util/rpt_pdf_util";
- import fs from "fs";
- import strUtil from "../../../public/stringUtil";
- import rptDataExtractor from "../util/rpt_construct_data_util";
- //统一回调函数
- 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(rpt_id, pageSize, option, 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);
- cb('No report template data were found!', null);
- }
- } else {
- //callback(req, res, 'No report template was found!', null);
- cb('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();
- let dftOption = option||JV.PAGING_OPTION_NORMAL;
- printCom.initialize(rptTpl);
- printCom.analyzeData(rptTpl, tplData, defProperties, dftOption);
- let maxPages = printCom.totalPages;
- let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
- if (pageRst) {
- cb(null, pageRst);
- } else {
- //callback(req, res, "Have errors while on going...", null);
- cb('Have errors while on going...', null);
- }
- } else {
- //callback(req, res, 'No report data were found!', null);
- cb('No report data were found!', null);
- }
- }
- );
- }
- function setupCustomizeCfg(customizeCfg, rptTpl) {
- //
- }
- function getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, customizeCfg, option, cb) {
- let rptTpl = null;
- rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
- rptTpl = rst;
- if (rptTpl) {
- let rptDataUtil = new rptDataExtractor();
- rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
- let filter = rptDataUtil.getDataRequestFilter();
- rptTplDataFacade.prepareProjectData(user_id, prj_id, filter, function (err, msg, rawDataObj) {
- if (!err) {
- let tplData = rptDataUtil.assembleData(rawDataObj);
- let printCom = JpcEx.createNew();
- if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
- if (orientation) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = orientation;
- if (customizeCfg) setupCustomizeCfg(customizeCfg, rptTpl);
- let defProperties = rptUtil.getReportDefaultCache();
- let dftOption = option||JV.PAGING_OPTION_NORMAL;
- printCom.initialize(rptTpl);
- printCom.analyzeData(rptTpl, tplData, defProperties, dftOption);
- let maxPages = printCom.totalPages;
- let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
- if (pageRst) {
- //fsUtil.writeObjToFile(pageRst, "D:/GitHome/ConstructionCost/tmp/testBuiltPageResult.jsp");
- cb(null, pageRst);
- } else {
- cb('Have errors while on going...', null);
- }
- } else {
- cb('No report data were found!', null);
- }
- });
- } else {
- cb('No report template was found!', null);
- }
- })
- }
- module.exports = {
- getReportAllPages: function (req, res) {
- let params = JSON.parse(req.body.params),
- rpt_id = params.rpt_tpl_id,
- prj_id = params.prj_id,
- pageSize = params.pageSize,
- orientation = params.orientation,
- customizeCfg = params.custCfg
- ;
- // req.session.sessionUser.ssoId
- let user_id = req.session.sessionUser.id;
- getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, customizeCfg, null, function (err, pageRst) {
- callback(req, res, err, pageRst);
- });
- },
- getTestReportAllPages: function(req, res){
- let rpt_id = req.body.ID;
- let pageSize = req.body.pageSize;
- getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
- //fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
- callback(req, res, err, pageRst);
- })
- },
- getExcel: function(req, res) {
- let prj_id = req.params.prj_id,
- rpt_id = req.params.rpt_id,
- pageSize = req.params.size,
- orientation = req.params.orientation,
- rptName = req.params.rptName,
- isOneSheet = req.params.isOneSheet,
- option = req.params.option;
- let user_id = req.session.sessionUser.id;
- let dftOption = option||JV.PAGING_OPTION_NORMAL;
- getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, null, dftOption, function(err, pageRst){
- try {
- rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, null, 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();
- });
- });
- } catch (e) {
- console.log(e);
- }
- })
- },
- getTestExcel: function(req, res) {
- let rpt_id = req.params.id,
- pageSize = req.params.size,
- rptName = req.params.rptName,
- isOneSheet = req.params.isOneSheet,
- option = req.params.option;
- let dftOption = option||JV.PAGING_OPTION_NORMAL;
- getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function(err, pageRst){
- fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
- try {
- rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, null, 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();
- });
- });
- } catch (e) {
- console.log(e);
- }
- })
- },
- getTestExcelInOneBook: function(req, res) {
- let rpt_ids = req.params.ids.split(','),
- pageSize = req.params.size,
- rptName = req.params.rptName,
- option = req.params.option;
- let parallelFucs = [];
- let dftOption = option||JV.PAGING_OPTION_NORMAL;
- for (let id of rpt_ids) {
- parallelFucs.push((function (rpt_id) {
- return function (cb) {
- getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function (err, pageRst) {
- if(err){
- cb(err);
- }
- else{
- cb(null, pageRst);
- }
- })
- }
- })(parseInt(id)));
- }
- async.parallel(parallelFucs, function (err, pageRstArray) {
- if (err) {
- callback(req, res, '数据有误', null);
- } else {
- rpt_xl_util.exportExcelInOneBook(pageRstArray, pageSize, rptName, function(tmpFilePath){
- 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/' + tmpFilePath + '.xlsx');
- filestream.on('data', function(chunk) {
- res.write(chunk);
- });
- filestream.on('end', function() {
- res.end();
- });
- });
- //callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
- }
- })
- },
- getPDF:function (req, res) {
- let prj_id = req.params.prj_id,
- rpt_id = req.params.rpt_id,
- pageSize = req.params.size,
- orientation = req.params.orientation,
- rptName = req.params.rptName
- ;
- let user_id = req.session.sessionUser.id;
- getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, null, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
- rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (newName) {
- res.setHeader('Content-Type', 'application/vnd.openxmlformats');
- res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
- let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.pdf');
- filestream.on('data', function(chunk) {
- res.write(chunk);
- });
- filestream.on('end', function() {
- res.end();
- });
- })
- })
- },
- getTestPDF:function (req, res) {
- let rpt_id = req.params.id,
- pageSize = req.params.size,
- rptName = req.params.rptName;
- getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
- rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (newName) {
- res.setHeader('Content-Type', 'application/vnd.openxmlformats');
- res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
- let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.pdf');
- filestream.on('data', function(chunk) {
- res.write(chunk);
- });
- filestream.on('end', function() {
- res.end();
- });
- })
- })
- }
- };
|