rpt_controller.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 params = JSON.parse(req.body.params),
  104. rpt_id = params.rpt_tpl_id,
  105. prj_id = params.prj_id,
  106. user_id = params.user_id,
  107. pageSize = params.pageSize;
  108. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, null, function (err, pageRst) {
  109. callback(req, res, err, pageRst);
  110. });
  111. },
  112. getTestReportAllPages: function(req, res){
  113. let rpt_id = req.body.ID;
  114. let pageSize = req.body.pageSize;
  115. getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
  116. //fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  117. callback(req, res, err, pageRst);
  118. })
  119. },
  120. getTestExcel: function(req, res) {
  121. let rpt_id = req.params.id,
  122. pageSize = req.params.size,
  123. rptName = req.params.rptName,
  124. isOneSheet = req.params.isOneSheet,
  125. option = req.params.option;
  126. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  127. getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function(err, pageRst){
  128. fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  129. try {
  130. rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, null, function(newName){
  131. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  132. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  133. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  134. filestream.on('data', function(chunk) {
  135. res.write(chunk);
  136. });
  137. filestream.on('end', function() {
  138. res.end();
  139. });
  140. });
  141. } catch (e) {
  142. console.log(e);
  143. }
  144. })
  145. },
  146. getTestExcelInOneBook: function(req, res) {
  147. let rpt_ids = req.params.ids.split(','),
  148. pageSize = req.params.size,
  149. rptName = req.params.rptName,
  150. option = req.params.option;
  151. let parallelFucs = [];
  152. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  153. for (let id of rpt_ids) {
  154. parallelFucs.push((function (rpt_id) {
  155. return function (cb) {
  156. getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function (err, pageRst) {
  157. if(err){
  158. cb(err);
  159. }
  160. else{
  161. cb(null, pageRst);
  162. }
  163. })
  164. }
  165. })(parseInt(id)));
  166. }
  167. async.parallel(parallelFucs, function (err, pageRstArray) {
  168. if (err) {
  169. callback(req, res, '数据有误', null);
  170. } else {
  171. rpt_xl_util.exportExcelInOneBook(pageRstArray, pageSize, rptName, function(tmpFilePath){
  172. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  173. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  174. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + tmpFilePath + '.xlsx');
  175. filestream.on('data', function(chunk) {
  176. res.write(chunk);
  177. });
  178. filestream.on('end', function() {
  179. res.end();
  180. });
  181. });
  182. //callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
  183. }
  184. })
  185. },
  186. getTestPDF:function (req, res) {
  187. let rpt_id = req.params.id,
  188. pageSize = req.params.size,
  189. rptName = req.params.rptName;
  190. getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
  191. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (newName) {
  192. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  193. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
  194. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.pdf');
  195. filestream.on('data', function(chunk) {
  196. res.write(chunk);
  197. });
  198. filestream.on('end', function() {
  199. res.end();
  200. });
  201. })
  202. })
  203. }
  204. };