rpt_controller.js 8.8 KB

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