rpt_controller.js 8.7 KB

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