rpt_controller_operation.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * Created by Tony on 2017/3/13.
  3. */
  4. import mongoose from "mongoose";
  5. import async from "async";
  6. import JV from "../rpt_component/jpc_value_define";
  7. let Template = mongoose.model('rpt_templates');
  8. // let rptTplDataFacade = require("../facade/rpt_tpl_data_facade");
  9. // let fsUtil = require("../../../public/fsUtil");
  10. import rptTplFacade from "../facade/rpt_template_facade";
  11. import demoTemplateFacade from "../facade/rpt_tpl_data_demo_facade";
  12. import JpcEx from "../rpt_component/jpc_ex";
  13. import rptUtil from "../util/rpt_util";
  14. import rpt_xl_util from "../util/rpt_excel_util";
  15. import rpt_pdf_util from "../util/rpt_pdf_util";
  16. import fs from "fs";
  17. import strUtil from "../../../public/stringUtil";
  18. // import rptDataExtractor from "../util/rpt_construct_data_util";
  19. //统一回调函数
  20. let callback = function(req, res, err, data){
  21. if(err){
  22. res.json({success: false, error: err});
  23. }
  24. else{
  25. //res.send({success: true, data: data});
  26. res.json({success:true, data: data});
  27. }
  28. };
  29. function getAllPagesCommonOrg(rpt_id, pageSize, option, cb) {
  30. let rptTpl = null;
  31. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  32. rptTpl = rst;
  33. if (rptTpl) {
  34. if (rptTpl.ID_KEY) {
  35. return demoTemplateFacade.getDemoData(rptTpl.ID_KEY);
  36. } else {
  37. //callback(req, res, 'No report template data were found!', null);
  38. cb('No report template data were found!', null);
  39. }
  40. } else {
  41. //callback(req, res, 'No report template was found!', null);
  42. cb('No report template was found!', null);
  43. }
  44. }).then(function(tplData){
  45. if (tplData) {
  46. let printCom = JpcEx.createNew();
  47. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  48. let defProperties = rptUtil.getReportDefaultCache();
  49. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  50. printCom.initialize(rptTpl);
  51. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption);
  52. let maxPages = printCom.totalPages;
  53. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  54. if (pageRst) {
  55. cb(null, pageRst);
  56. } else {
  57. //callback(req, res, "Have errors while on going...", null);
  58. cb('Have errors while on going...', null);
  59. }
  60. } else {
  61. //callback(req, res, 'No report data were found!', null);
  62. cb('No report data were found!', null);
  63. }
  64. }
  65. );
  66. }
  67. function getPreViewPage(rpt_id, pageSize, cb) {
  68. let rptTpl = null;
  69. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  70. rptTpl = rst;
  71. if (rptTpl) {
  72. let printCom = JpcEx.createNew();
  73. let defProperties = rptUtil.getReportDefaultCache();
  74. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  75. printCom.initialize(rptTpl);
  76. let pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  77. if (pageRst) {
  78. //fsUtil.writeObjToFile(pageRst, "D:/GitHome/ConstructionCost/tmp/previewPageResult.jsp");
  79. cb(null, pageRst);
  80. } else {
  81. cb('Have errors while on going...', null);
  82. }
  83. } else {
  84. cb('No report template was found!', null);
  85. }
  86. });
  87. }
  88. /*
  89. function getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, option, cb) {
  90. let rptTpl = null;
  91. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  92. rptTpl = rst;
  93. if (rptTpl) {
  94. let rptDataUtil = new rptDataExtractor();
  95. rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
  96. let filter = rptDataUtil.getDataRequestFilter();
  97. rptTplDataFacade.prepareProjectData(user_id, prj_id, filter, function (err, msg, rawDataObj) {
  98. if (!err) {
  99. let tplData = rptDataUtil.assembleData(rawDataObj);
  100. let printCom = JpcEx.createNew();
  101. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  102. let defProperties = rptUtil.getReportDefaultCache();
  103. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  104. printCom.initialize(rptTpl);
  105. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption);
  106. let maxPages = printCom.totalPages;
  107. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  108. if (pageRst) {
  109. //fsUtil.wirteObjToFile(pageRst, "D:/GitHome/ConstructionCost/tmp/testBuiltPageResult.js");
  110. cb(null, pageRst);
  111. } else {
  112. cb('Have errors while on going...', null);
  113. }
  114. } else {
  115. cb('No report data were found!', null);
  116. }
  117. });
  118. } else {
  119. cb('No report template was found!', null);
  120. }
  121. })
  122. }
  123. //*/
  124. module.exports = {
  125. getPreviewPage: function(req, res) {
  126. let params = JSON.parse(req.body.params),
  127. rpt_id = params.rpt_tpl_id,
  128. pageSize = params.pageSize;
  129. getPreViewPage(rpt_id, pageSize, function (err, pageRst) {
  130. callback(req, res, err, pageRst);
  131. });
  132. },
  133. // getReportAllPages: function (req, res) {
  134. // let rpt_id = req.body.rpt_tpl_id, prj_id = req.body.prj_id,
  135. // user_id = req.body.user_id, pageSize = req.body.pageSize;
  136. // getAllPagesCommon(rpt_id, prj_id, user_id, pageSize, function (err, pageRst) {
  137. // callback(req, res, err, pageRst);
  138. // });
  139. // },
  140. getReportAllPages: function (req, res) {
  141. callback(req, res, 'default none!', null);
  142. },
  143. getTestReportAllPages: function(req, res){
  144. let rpt_id = req.body.ID;
  145. let pageSize = req.body.pageSize;
  146. getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
  147. //fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  148. callback(req, res, err, pageRst);
  149. })
  150. },
  151. getTestExcel: function(req, res) {
  152. let rpt_id = req.params.id,
  153. pageSize = req.params.size,
  154. rptName = req.params.rptName,
  155. isOneSheet = req.params.isOneSheet,
  156. option = req.params.option;
  157. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  158. getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function(err, pageRst){
  159. fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  160. try {
  161. rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, null, null, function(newName){
  162. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  163. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  164. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  165. filestream.on('data', function(chunk) {
  166. res.write(chunk);
  167. });
  168. filestream.on('end', function() {
  169. res.end();
  170. });
  171. });
  172. } catch (e) {
  173. console.log(e);
  174. }
  175. })
  176. },
  177. getTestExcelInOneBook: function(req, res) {
  178. let rpt_ids = req.params.ids.split(','),
  179. pageSize = req.params.size,
  180. rptName = req.params.rptName,
  181. option = req.params.option;
  182. let parallelFucs = [];
  183. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  184. for (let id of rpt_ids) {
  185. parallelFucs.push((function (rpt_id) {
  186. return function (cb) {
  187. getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function (err, pageRst) {
  188. if(err){
  189. cb(err);
  190. }
  191. else{
  192. cb(null, pageRst);
  193. }
  194. })
  195. }
  196. })(parseInt(id)));
  197. }
  198. async.parallel(parallelFucs, function (err, pageRstArray) {
  199. if (err) {
  200. callback(req, res, '数据有误', null);
  201. } else {
  202. rpt_xl_util.exportExcelInOneBook(pageRstArray, pageSize, rptName, function(tmpFilePath){
  203. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  204. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  205. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + tmpFilePath + '.xlsx');
  206. filestream.on('data', function(chunk) {
  207. res.write(chunk);
  208. });
  209. filestream.on('end', function() {
  210. res.end();
  211. });
  212. });
  213. //callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
  214. }
  215. })
  216. },
  217. getTestPDF:function (req, res) {
  218. let rpt_id = req.params.id,
  219. pageSize = req.params.size,
  220. rptName = req.params.rptName;
  221. getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
  222. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (newName) {
  223. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  224. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
  225. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.pdf');
  226. filestream.on('data', function(chunk) {
  227. res.write(chunk);
  228. });
  229. filestream.on('end', function() {
  230. res.end();
  231. });
  232. })
  233. })
  234. }
  235. };