rpt_controller.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Created by Tony on 2017/3/13.
  3. */
  4. let mongoose = require('mongoose');
  5. let async = require('async');
  6. let JV = require('../rpt_component/jpc_value_define');
  7. let Template = mongoose.model('rpt_templates');
  8. let rptTplFacade = require('../facade/rpt_template_facade');
  9. let demoTemplateFacade = require('../facade/rpt_tpl_data_demo_facade');
  10. let JpcEx = require('../rpt_component/jpc_ex');
  11. let rptUtil = require("../util/rpt_util");
  12. let rpt_xl_util = require('../util/rpt_excel_util');
  13. let rpt_pdf_util = require('../util/rpt_pdf_util');
  14. let fs = require('fs');
  15. let strUtil = require('../../../public/stringUtil');
  16. //统一回调函数
  17. let callback = function(req, res, err, data){
  18. if(err){
  19. res.json({success: false, error: err});
  20. }
  21. else{
  22. //res.send({success: true, data: data});
  23. res.json({success:true, data: data});
  24. }
  25. };
  26. function getAllPagesCommonOrg(rpt_id, pageSize, option, cb) {
  27. let rptTpl = null;
  28. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  29. rptTpl = rst;
  30. if (rptTpl) {
  31. if (rptTpl.ID_KEY) {
  32. return demoTemplateFacade.getDemoData(rptTpl.ID_KEY);
  33. } else {
  34. //callback(req, res, 'No report template data were found!', null);
  35. cb('No report template data were found!', null);
  36. }
  37. } else {
  38. //callback(req, res, 'No report template was found!', null);
  39. cb('No report template was found!', null);
  40. }
  41. }).then(function(tplData){
  42. if (tplData) {
  43. let printCom = JpcEx.createNew();
  44. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  45. let defProperties = rptUtil.getReportDefaultCache();
  46. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  47. printCom.initialize(rptTpl);
  48. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption);
  49. let maxPages = printCom.totalPages;
  50. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  51. if (pageRst) {
  52. cb(null, pageRst);
  53. } else {
  54. //callback(req, res, "Have errors while on going...", null);
  55. cb('Have errors while on going...', null);
  56. }
  57. } else {
  58. //callback(req, res, 'No report data were found!', null);
  59. cb('No report data were found!', null);
  60. }
  61. }
  62. );
  63. };
  64. function getAllPagesCommon(rpt_id, pageSize, cb) {
  65. let rptTpl = null;
  66. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  67. rptTpl = rst;
  68. if (rptTpl) {
  69. let tplData = {};
  70. if (rptTpl[JV.NODE_FIELD_MAP]) {
  71. //1. 离散数据
  72. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS].leng > 0) {
  73. tplData[JV.DATA_DISCRETE_DATA] = [];
  74. }
  75. //2. 主数据
  76. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS].leng > 0) {
  77. tplData[JV.DATA_MASTER_DATA] = [];
  78. }
  79. //3. 从数据
  80. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS].leng > 0) {
  81. tplData[JV.DATA_DETAIL_DATA] = [];
  82. }
  83. //2. Ex主数据
  84. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX].leng > 0) {
  85. tplData[JV.DATA_MASTER_DATA_EX] = [];
  86. }
  87. //3. Ex从数据
  88. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX].leng > 0) {
  89. tplData[JV.DATA_DETAIL_DATA_EX] = [];
  90. }
  91. //4. 重点: 开始组装$PROJECT对象
  92. let $PROJECT = {};
  93. //let $PROJECT.COMMON = {};
  94. //return demoTemplateData.getPromise(rptTpl.ID_KEY);
  95. //return demoTemplateData.findOne({"Data_Key": rptTpl.ID_KEY}).exec();
  96. } else {
  97. cb('No report template data were found!', null);
  98. }
  99. } else {
  100. cb('No report template was found!', null);
  101. }
  102. })
  103. };
  104. module.exports = {
  105. getReportAllPages: function(req, res){
  106. let rpt_id = req.body.ID;
  107. let pageSize = req.body.pageSize;
  108. getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
  109. //fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  110. callback(req, res, err, pageRst);
  111. })
  112. },
  113. getExcel: function(req, res) {
  114. let rpt_id = req.params.id,
  115. pageSize = req.params.size,
  116. rptName = req.params.rptName,
  117. isOneSheet = req.params.isOneSheet,
  118. option = req.params.option;
  119. ;
  120. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  121. getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function(err, pageRst){
  122. rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, null, function(newName){
  123. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  124. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  125. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  126. filestream.on('data', function(chunk) {
  127. res.write(chunk);
  128. });
  129. filestream.on('end', function() {
  130. res.end();
  131. });
  132. });
  133. })
  134. },
  135. getExcelInOneBook: function(req, res) {
  136. let rpt_ids = req.params.ids.split(','),
  137. pageSize = req.params.size,
  138. rptName = req.params.rptName,
  139. option = req.params.option;
  140. ;
  141. let parallelFucs = [];
  142. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  143. for (let id of rpt_ids) {
  144. parallelFucs.push((function (rpt_id) {
  145. return function (cb) {
  146. getAllPagesCommonOrg(rpt_id, pageSize, dftOption, function (err, pageRst) {
  147. if(err){
  148. cb(err);
  149. }
  150. else{
  151. cb(null, pageRst);
  152. }
  153. })
  154. }
  155. })(parseInt(id)));
  156. }
  157. async.parallel(parallelFucs, function (err, pageRstArray) {
  158. if (err) {
  159. callback(req, res, '数据有误', null);
  160. } else {
  161. rpt_xl_util.exportExcelInOneBook(pageRstArray, pageSize, rptName, function(tmpFilePath){
  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/' + tmpFilePath + '.xlsx');
  165. filestream.on('data', function(chunk) {
  166. res.write(chunk);
  167. });
  168. filestream.on('end', function() {
  169. res.end();
  170. });
  171. });
  172. //callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
  173. }
  174. })
  175. },
  176. getPDF:function (req, res) {
  177. let rpt_id = req.params.id,
  178. pageSize = req.params.size,
  179. rptName = req.params.rptName;
  180. getAllPagesCommonOrg(rpt_id, pageSize, JV.PAGING_OPTION_NORMAL, function(err, pageRst){
  181. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (newName) {
  182. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  183. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
  184. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.pdf');
  185. filestream.on('data', function(chunk) {
  186. res.write(chunk);
  187. });
  188. filestream.on('end', function() {
  189. res.end();
  190. });
  191. })
  192. })
  193. }
  194. };