rpt_controller_operation.js 9.2 KB

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