rpt_controller.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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, 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. printCom.initialize(rptTpl);
  47. printCom.analyzeData(rptTpl, tplData, defProperties);
  48. let maxPages = printCom.totalPages;
  49. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  50. if (pageRst) {
  51. cb(null, pageRst);
  52. } else {
  53. //callback(req, res, "Have errors while on going...", null);
  54. cb('Have errors while on going...', null);
  55. }
  56. } else {
  57. //callback(req, res, 'No report data were found!', null);
  58. cb('No report data were found!', null);
  59. }
  60. }
  61. );
  62. };
  63. function getAllPagesCommon(rpt_id, pageSize, cb) {
  64. let rptTpl = null;
  65. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  66. rptTpl = rst;
  67. if (rptTpl) {
  68. let tplData = {};
  69. if (rptTpl[JV.NODE_FIELD_MAP]) {
  70. //1. 离散数据
  71. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS].leng > 0) {
  72. tplData[JV.DATA_DISCRETE_DATA] = [];
  73. }
  74. //2. 主数据
  75. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS].leng > 0) {
  76. tplData[JV.DATA_MASTER_DATA] = [];
  77. }
  78. //3. 从数据
  79. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS].leng > 0) {
  80. tplData[JV.DATA_DETAIL_DATA] = [];
  81. }
  82. //2. Ex主数据
  83. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX].leng > 0) {
  84. tplData[JV.DATA_MASTER_DATA_EX] = [];
  85. }
  86. //3. Ex从数据
  87. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX] && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX].leng > 0) {
  88. tplData[JV.DATA_DETAIL_DATA_EX] = [];
  89. }
  90. //4. 重点: 开始组装$PROJECT对象
  91. let $PROJECT = {};
  92. //let $PROJECT.COMMON = {};
  93. //return demoTemplateData.getPromise(rptTpl.ID_KEY);
  94. //return demoTemplateData.findOne({"Data_Key": rptTpl.ID_KEY}).exec();
  95. } else {
  96. cb('No report template data were found!', null);
  97. }
  98. } else {
  99. cb('No report template was found!', null);
  100. }
  101. })
  102. };
  103. module.exports = {
  104. getReportAllPages: function(req, res){
  105. let rpt_id = req.body.ID;
  106. let pageSize = req.body.pageSize;
  107. getAllPagesCommonOrg(rpt_id, pageSize, function(err, pageRst){
  108. //fs.writeFileSync('D:/GitHome/ConstructionOperation/tmp/testRpt.js', JSON.stringify(pageRst));
  109. callback(req, res, err, pageRst);
  110. })
  111. },
  112. getExcel: function(req, res) {
  113. let rpt_id = req.params.id,
  114. pageSize = req.params.size,
  115. rptName = req.params.rptName,
  116. isOneSheet = req.params.isOneSheet;
  117. ;
  118. getAllPagesCommonOrg(rpt_id, pageSize, function(err, pageRst){
  119. rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, null, function(newName){
  120. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  121. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  122. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  123. filestream.on('data', function(chunk) {
  124. res.write(chunk);
  125. });
  126. filestream.on('end', function() {
  127. res.end();
  128. });
  129. });
  130. })
  131. },
  132. getExcelInOneBook: function(req, res) {
  133. let rpt_ids = req.params.ids.split(','),
  134. pageSize = req.params.size,
  135. rptName = req.params.rptName;
  136. ;
  137. let parallelFucs = [];
  138. for (let id of rpt_ids) {
  139. parallelFucs.push((function (rpt_id) {
  140. return function (cb) {
  141. getAllPagesCommonOrg(rpt_id, pageSize, function (err, pageRst) {
  142. if(err){
  143. cb(err);
  144. }
  145. else{
  146. cb(null, pageRst);
  147. }
  148. })
  149. }
  150. })(parseInt(id)));
  151. }
  152. async.parallel(parallelFucs, function (err, pageRstArray) {
  153. if (err) {
  154. callback(req, res, '数据有误', null);
  155. } else {
  156. rpt_xl_util.exportExcelInOneBook(pageRstArray, pageSize, rptName, function(tmpFilePath){
  157. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  158. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  159. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + tmpFilePath + '.xlsx');
  160. filestream.on('data', function(chunk) {
  161. res.write(chunk);
  162. });
  163. filestream.on('end', function() {
  164. res.end();
  165. });
  166. });
  167. //callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
  168. }
  169. })
  170. },
  171. getPDF:function (req, res) {
  172. let rpt_id = req.params.id,
  173. pageSize = req.params.size,
  174. rptName = req.params.rptName;
  175. getAllPagesCommonOrg(rpt_id, pageSize, function(err, pageRst){
  176. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (newName) {
  177. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  178. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
  179. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.pdf');
  180. filestream.on('data', function(chunk) {
  181. res.write(chunk);
  182. });
  183. filestream.on('end', function() {
  184. res.end();
  185. });
  186. })
  187. })
  188. }
  189. };