rpt_controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 setupCustomizeCfg(customizeCfg, rptTpl) {
  68. //
  69. }
  70. function getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, customizeCfg, option, cb) {
  71. let rptTpl = null;
  72. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  73. rptTpl = rst;
  74. if (rptTpl) {
  75. let rptDataUtil = new rptDataExtractor();
  76. rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
  77. let filter = rptDataUtil.getDataRequestFilter();
  78. rptTplDataFacade.prepareProjectData(user_id, prj_id, filter, function (err, msg, rawDataObj) {
  79. if (!err) {
  80. let tplData = rptDataUtil.assembleData(rawDataObj);
  81. let printCom = JpcEx.createNew();
  82. if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  83. if (orientation) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = orientation;
  84. if (customizeCfg) setupCustomizeCfg(customizeCfg, rptTpl);
  85. let defProperties = rptUtil.getReportDefaultCache();
  86. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  87. printCom.initialize(rptTpl);
  88. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption);
  89. let maxPages = printCom.totalPages;
  90. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  91. if (pageRst) {
  92. //fsUtil.writeObjToFile(pageRst, "D:/GitHome/ConstructionCost/tmp/testBuiltPageResult.jsp");
  93. cb(null, pageRst);
  94. } else {
  95. cb('Have errors while on going...', null);
  96. }
  97. } else {
  98. cb('No report data were found!', null);
  99. }
  100. });
  101. } else {
  102. cb('No report template was found!', null);
  103. }
  104. })
  105. }
  106. module.exports = {
  107. getReportAllPages: function (req, res) {
  108. let params = JSON.parse(req.body.params),
  109. rpt_id = params.rpt_tpl_id,
  110. prj_id = params.prj_id,
  111. pageSize = params.pageSize,
  112. orientation = params.orientation,
  113. customizeCfg = params.custCfg
  114. ;
  115. // req.session.sessionUser.ssoId
  116. let user_id = req.session.sessionUser.id;
  117. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, customizeCfg, null, function (err, pageRst) {
  118. callback(req, res, err, pageRst);
  119. });
  120. },
  121. getTestReportAllPages: function(req, res){
  122. let rpt_id = req.body.ID;
  123. let pageSize = req.body.pageSize;
  124. getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
  125. //fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  126. callback(req, res, err, pageRst);
  127. })
  128. },
  129. getExcel: function(req, res) {
  130. let prj_id = req.params.prj_id,
  131. rpt_id = req.params.rpt_id,
  132. pageSize = req.params.size,
  133. orientation = req.params.orientation,
  134. rptName = req.params.rptName,
  135. isOneSheet = req.params.isOneSheet,
  136. option = req.params.option;
  137. let user_id = req.session.sessionUser.id;
  138. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  139. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, null, dftOption, function(err, pageRst){
  140. try {
  141. rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, null, function(newName){
  142. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  143. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  144. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  145. filestream.on('data', function(chunk) {
  146. res.write(chunk);
  147. });
  148. filestream.on('end', function() {
  149. res.end();
  150. });
  151. });
  152. } catch (e) {
  153. console.log(e);
  154. }
  155. })
  156. },
  157. getTestExcel: function(req, res) {
  158. let rpt_id = req.params.id,
  159. pageSize = req.params.size,
  160. rptName = req.params.rptName,
  161. isOneSheet = req.params.isOneSheet,
  162. option = req.params.option;
  163. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  164. getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function(err, pageRst){
  165. fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  166. try {
  167. rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, null, function(newName){
  168. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  169. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  170. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  171. filestream.on('data', function(chunk) {
  172. res.write(chunk);
  173. });
  174. filestream.on('end', function() {
  175. res.end();
  176. });
  177. });
  178. } catch (e) {
  179. console.log(e);
  180. }
  181. })
  182. },
  183. getTestExcelInOneBook: function(req, res) {
  184. let rpt_ids = req.params.ids.split(','),
  185. pageSize = req.params.size,
  186. rptName = req.params.rptName,
  187. option = req.params.option;
  188. let parallelFucs = [];
  189. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  190. for (let id of rpt_ids) {
  191. parallelFucs.push((function (rpt_id) {
  192. return function (cb) {
  193. getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function (err, pageRst) {
  194. if(err){
  195. cb(err);
  196. }
  197. else{
  198. cb(null, pageRst);
  199. }
  200. })
  201. }
  202. })(parseInt(id)));
  203. }
  204. async.parallel(parallelFucs, function (err, pageRstArray) {
  205. if (err) {
  206. callback(req, res, '数据有误', null);
  207. } else {
  208. rpt_xl_util.exportExcelInOneBook(pageRstArray, pageSize, rptName, function(tmpFilePath){
  209. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  210. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  211. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + tmpFilePath + '.xlsx');
  212. filestream.on('data', function(chunk) {
  213. res.write(chunk);
  214. });
  215. filestream.on('end', function() {
  216. res.end();
  217. });
  218. });
  219. //callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
  220. }
  221. })
  222. },
  223. getPDF:function (req, res) {
  224. let prj_id = req.params.prj_id,
  225. rpt_id = req.params.rpt_id,
  226. pageSize = req.params.size,
  227. orientation = req.params.orientation,
  228. rptName = req.params.rptName
  229. ;
  230. let user_id = req.session.sessionUser.id;
  231. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, null, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
  232. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (newName) {
  233. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  234. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
  235. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.pdf');
  236. filestream.on('data', function(chunk) {
  237. res.write(chunk);
  238. });
  239. filestream.on('end', function() {
  240. res.end();
  241. });
  242. })
  243. })
  244. },
  245. getTestPDF:function (req, res) {
  246. let rpt_id = req.params.id,
  247. pageSize = req.params.size,
  248. rptName = req.params.rptName;
  249. getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
  250. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (newName) {
  251. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  252. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
  253. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.pdf');
  254. filestream.on('data', function(chunk) {
  255. res.write(chunk);
  256. });
  257. filestream.on('end', function() {
  258. res.end();
  259. });
  260. })
  261. })
  262. }
  263. };