rpt_controller.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, option, cb) {
  68. let rptTpl = null;
  69. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  70. rptTpl = rst;
  71. if (rptTpl) {
  72. let rptDataUtil = new rptDataExtractor();
  73. rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
  74. let filter = rptDataUtil.getDataRequestFilter();
  75. rptTplDataFacade.prepareProjectData(user_id, prj_id, filter, function (err, msg, rawDataObj) {
  76. if (!err) {
  77. let tplData = rptDataUtil.assembleData(rawDataObj);
  78. let printCom = JpcEx.createNew();
  79. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  80. let defProperties = rptUtil.getReportDefaultCache();
  81. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  82. printCom.initialize(rptTpl);
  83. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption);
  84. let maxPages = printCom.totalPages;
  85. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  86. if (pageRst) {
  87. //fsUtil.wirteObjToFile(pageRst, "D:/GitHome/ConstructionCost/tmp/testBuiltPageResult.js");
  88. cb(null, pageRst);
  89. } else {
  90. cb('Have errors while on going...', null);
  91. }
  92. } else {
  93. cb('No report data were found!', null);
  94. }
  95. });
  96. } else {
  97. cb('No report template was found!', null);
  98. }
  99. })
  100. }
  101. module.exports = {
  102. getReportAllPages: function (req, res) {
  103. let rpt_id = req.body.rpt_tpl_id, prj_id = req.body.prj_id,
  104. user_id = req.body.user_id, pageSize = req.body.pageSize;
  105. getAllPagesCommon(rpt_id, prj_id, user_id, pageSize, function (err, pageRst) {
  106. callback(req, res, err, pageRst);
  107. });
  108. },
  109. getTestReportAllPages: function(req, res){
  110. let rpt_id = req.body.ID;
  111. let pageSize = req.body.pageSize;
  112. getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
  113. //fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  114. callback(req, res, err, pageRst);
  115. })
  116. },
  117. getTestExcel: function(req, res) {
  118. let rpt_id = req.params.id,
  119. pageSize = req.params.size,
  120. rptName = req.params.rptName,
  121. isOneSheet = req.params.isOneSheet,
  122. option = req.params.option;
  123. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  124. getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function(err, pageRst){
  125. fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  126. try {
  127. rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, null, function(newName){
  128. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  129. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  130. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  131. filestream.on('data', function(chunk) {
  132. res.write(chunk);
  133. });
  134. filestream.on('end', function() {
  135. res.end();
  136. });
  137. });
  138. } catch (e) {
  139. console.log(e);
  140. }
  141. })
  142. },
  143. getTestExcelInOneBook: function(req, res) {
  144. let rpt_ids = req.params.ids.split(','),
  145. pageSize = req.params.size,
  146. rptName = req.params.rptName,
  147. option = req.params.option;
  148. let parallelFucs = [];
  149. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  150. for (let id of rpt_ids) {
  151. parallelFucs.push((function (rpt_id) {
  152. return function (cb) {
  153. getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function (err, pageRst) {
  154. if(err){
  155. cb(err);
  156. }
  157. else{
  158. cb(null, pageRst);
  159. }
  160. })
  161. }
  162. })(parseInt(id)));
  163. }
  164. async.parallel(parallelFucs, function (err, pageRstArray) {
  165. if (err) {
  166. callback(req, res, '数据有误', null);
  167. } else {
  168. rpt_xl_util.exportExcelInOneBook(pageRstArray, pageSize, rptName, function(tmpFilePath){
  169. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  170. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  171. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + tmpFilePath + '.xlsx');
  172. filestream.on('data', function(chunk) {
  173. res.write(chunk);
  174. });
  175. filestream.on('end', function() {
  176. res.end();
  177. });
  178. });
  179. //callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
  180. }
  181. })
  182. },
  183. getTestPDF:function (req, res) {
  184. let rpt_id = req.params.id,
  185. pageSize = req.params.size,
  186. rptName = req.params.rptName;
  187. getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
  188. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (newName) {
  189. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  190. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
  191. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.pdf');
  192. filestream.on('data', function(chunk) {
  193. res.write(chunk);
  194. });
  195. filestream.on('end', function() {
  196. res.end();
  197. });
  198. })
  199. })
  200. }
  201. };