rpt_controller.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /**
  2. * Created by Tony on 2017/3/13.
  3. */
  4. const mongoose = require("mongoose");
  5. const async = require("async");
  6. const JV = require("../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. let pm_facade = require('../../../modules/pm/facade/pm_facade');
  11. const rptTplFacade = require("../facade/rpt_template_facade");
  12. const demoTemplateFacade = require("../facade/rpt_tpl_data_demo_facade");
  13. const JpcEx = require("../rpt_component/jpc_ex");
  14. const rptUtil = require("../util/rpt_util");
  15. const rpt_xl_util = require("../util/rpt_excel_util");
  16. const rpt_pdf_util = require("../util/rpt_pdf_util");
  17. const rpt_svg_util = require("../util/rpt_svg_util");
  18. const fs = require("fs");
  19. const strUtil = require("../../../public/stringUtil");
  20. const rptDataExtractor = require("../util/rpt_yanghu_data_util");
  21. //统一回调函数
  22. let callback = function(req, res, err, data){
  23. if(err){
  24. res.json({success: false, error: err});
  25. }
  26. else{
  27. //res.send({success: true, data: data});
  28. res.json({success:true, data: data});
  29. }
  30. };
  31. function getAllPagesCommonOrg(rpt_id, pageSize, option, cb) {
  32. let rptTpl = null;
  33. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  34. rptTpl = rst;
  35. if (rptTpl) {
  36. if (rptTpl.ID_KEY) {
  37. return demoTemplateFacade.getDemoData(rptTpl.ID_KEY);
  38. } else {
  39. //callback(req, res, 'No report template data were found!', null);
  40. cb('No report template data were found!', null);
  41. }
  42. } else {
  43. //callback(req, res, 'No report template was found!', null);
  44. cb('No report template was found!', null);
  45. }
  46. }).then(function(tplData){
  47. if (tplData) {
  48. let printCom = JpcEx.createNew();
  49. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  50. let defProperties = rptUtil.getReportDefaultCache();
  51. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  52. printCom.initialize(rptTpl);
  53. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, JV.OUTPUT_TYPE_NORMAL);
  54. let maxPages = printCom.totalPages;
  55. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  56. if (pageRst) {
  57. cb(null, pageRst);
  58. } else {
  59. //callback(req, res, "Have errors while on going...", null);
  60. cb('Have errors while on going...', null);
  61. }
  62. } else {
  63. //callback(req, res, 'No report data were found!', null);
  64. cb('No report data were found!', null);
  65. }
  66. }
  67. );
  68. }
  69. function setupCustomizeCfg(customizeCfg, rptTpl, defProperties) {
  70. let tmpObj = {};
  71. //1. 字体
  72. let newFonts = [];
  73. for (let font of defProperties.fonts) {
  74. let copyFont = {};
  75. copyFont.ID = font.ID;
  76. for (let fontProp of JV.FONT_PROPS) {
  77. copyFont[fontProp] = font[fontProp];
  78. }
  79. newFonts.push(copyFont);
  80. tmpObj[font.ID] = copyFont;
  81. }
  82. let private_setup_font = function (propStr, newFont) {
  83. if (tmpObj[propStr]) {
  84. tmpObj[propStr].Name = newFont.Name;
  85. tmpObj[propStr].FontHeight = String(newFont.FontHeight);
  86. tmpObj[propStr].FontBold = newFont.FontBold;
  87. tmpObj[propStr].FontItalic = newFont.FontItalic;
  88. tmpObj[propStr].FontUnderline = newFont.FontUnderline;
  89. }
  90. };
  91. for (let custFont of customizeCfg.fonts) {
  92. switch (custFont.CfgDispName) {
  93. case "表标题":
  94. private_setup_font("ReportTitle_Main", custFont);
  95. break;
  96. case "列标题":
  97. private_setup_font("HeaderColumn", custFont);
  98. private_setup_font("FooterColumn", custFont);
  99. break;
  100. case "正文内容":
  101. private_setup_font("Content", custFont);
  102. break;
  103. case "合计":
  104. private_setup_font("GrandTotal", custFont);
  105. private_setup_font("SectionTotal", custFont);
  106. break;
  107. case "表眉/表脚":
  108. private_setup_font("Header", custFont);
  109. private_setup_font("Footer", custFont);
  110. break;
  111. }
  112. }
  113. // 1.1 窄体
  114. if (tmpObj["Content_Narrow"]) {
  115. if (customizeCfg.isNarrow) {
  116. tmpObj["Content_Narrow"].Name = "Arial Narrow";
  117. } else {
  118. if (tmpObj["Content"]) {
  119. tmpObj["Content_Narrow"].Name = tmpObj["Content"].Name;
  120. } else {
  121. tmpObj["Content_Narrow"].Name = "宋体";
  122. }
  123. }
  124. }
  125. defProperties.fonts = newFonts;
  126. //2. 页边距
  127. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT] = customizeCfg.margins[JV.PROP_LEFT] / 10;
  128. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT] = customizeCfg.margins[JV.PROP_RIGHT] / 10;
  129. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP] = customizeCfg.margins[JV.PROP_TOP] / 10;
  130. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM] = customizeCfg.margins[JV.PROP_BOTTOM] / 10;
  131. //3. 边框竖线
  132. if (!(customizeCfg.showVerticalLine)) {
  133. let private_copy_border = function (src) {
  134. let rst = {};
  135. rst.Position = src.Position;
  136. rst.LineWeight = src.LineWeight;
  137. rst.DashStyle = src.DashStyle;
  138. rst.Color = src.Color;
  139. return rst;
  140. };
  141. let newStyles = [];
  142. for (let i =0 ; i < defProperties.styles.length; i++) {
  143. let style = defProperties.styles[i];
  144. newStyles.push(style);
  145. if (style.ID === "BORDER_ALL_AROUND") {
  146. let newStyle = {};
  147. newStyle.ID = style.ID;
  148. newStyle.CfgDispName = style.CfgDispName;
  149. newStyle.border_style = [];
  150. for (let border of style.border_style) {
  151. let newBorder = private_copy_border(border);
  152. newStyle.border_style.push(newBorder);
  153. if (border.Position === "Left" || border.Position === "Right") {
  154. newBorder.LineWeight = 0;
  155. }
  156. }
  157. newStyles[newStyles.length - 1] = newStyle;
  158. }
  159. }
  160. defProperties.styles = newStyles;
  161. }
  162. //4. 补0
  163. let private_Setup_Format = function(tabFields) {
  164. if (tabFields) {
  165. for (let tabField of tabFields) {
  166. if (tabField[JV.PROP_FORMAT]) {
  167. tabField[JV.PROP_FORMAT] = tabField[JV.PROP_FORMAT].replace(new RegExp("#","gm"),"0");
  168. }
  169. }
  170. }
  171. };
  172. if (customizeCfg.fillZero) {
  173. if (rptTpl[JV.NODE_FLOW_INFO]) {
  174. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_FLOW_FIELDS]);
  175. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_PAGE_SUM]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_PAGE_SUM][JV.PROP_SUM_FIELDS]);
  176. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_SEG_SUM]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_SEG_SUM][JV.PROP_SUM_FIELDS]);
  177. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_GROUP] && rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES])
  178. private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES][JV.PROP_SUM_FIELDS]);
  179. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO]) {
  180. for (let discrete of rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO]) {
  181. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  182. }
  183. }
  184. if (rptTpl[JV.NODE_FLOW_INFO_EX]) {
  185. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_CONTENT]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_CONTENT][JV.PROP_FLOW_FIELDS]);
  186. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_PAGE_SUM]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_PAGE_SUM][JV.PROP_SUM_FIELDS]);
  187. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_SEG_SUM]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_SEG_SUM][JV.PROP_SUM_FIELDS]);
  188. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_GROUP]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_GROUP][JV.PROP_SUM_FIELDS]);
  189. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_GROUP] && rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES])
  190. private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES][JV.PROP_SUM_FIELDS]);
  191. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_DISCRETE_INFO]) {
  192. for (let discrete of rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_DISCRETE_INFO]) {
  193. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  194. }
  195. }
  196. }
  197. } else if (rptTpl[JV.NODE_BILL_INFO]) {
  198. if (rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT]) private_Setup_Format(rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT][JV.PROP_BILL_FIELDS]);
  199. if (rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO]) {
  200. for (let discrete of rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO]) {
  201. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  202. }
  203. }
  204. } else if (rptTpl[JV.NODE_CROSS_INFO]) {
  205. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_CONTENT]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_CONTENT][JV.TAB_CROSS_FIELDS]);
  206. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW][JV.TAB_CROSS_FIELDS]);
  207. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_COL]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_COL][JV.TAB_CROSS_FIELDS]);
  208. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_COL_SUM]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_COL_SUM][JV.TAB_CROSS_FIELDS]);
  209. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_EXT]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_EXT][JV.TAB_CROSS_FIELDS]);
  210. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_SUM_EXT]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_SUM_EXT][JV.TAB_CROSS_FIELDS]);
  211. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO]) {
  212. for (let discrete of rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO]) {
  213. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  214. }
  215. }
  216. }
  217. }
  218. }
  219. function getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, customizeCfg, option, outputType, cb) {
  220. let rptTpl = null;
  221. rptTplFacade.getRptTemplate(rpt_id).then(function(rst) {
  222. rptTpl = rst;
  223. if (rptTpl) {
  224. let rptDataUtil = new rptDataExtractor();
  225. rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
  226. let summaryRst = [];
  227. let filter = rptDataUtil.getDataRequestFilter(summaryRst);
  228. let promiseArr = [null, null];
  229. if (summaryRst.length > 0) {
  230. if (summaryRst.indexOf(`Construct`) >= 0 || summaryRst.indexOf(`ConstructDetail`) >= 0) {
  231. promiseArr[0] = pm_facade.getSummaryInfoByTender(prj_id, pm_facade.projectType.project);
  232. }
  233. if (summaryRst.indexOf(`Segment`) >= 0 || summaryRst.indexOf(`SegmentDetail`) >= 0) {
  234. promiseArr[1] = pm_facade.getSummaryInfoByTender(prj_id, pm_facade.projectType.engineering);
  235. }
  236. }
  237. rptTplDataFacade.prepareProjectData(user_id, prj_id, filter, function (err, msg, rawDataObj) {
  238. if (!err) {
  239. let buildPageData = function() {
  240. try {
  241. // fsUtil.writeObjToFile(rawDataObj, `D:/GitHome/YangHuCost/tmp/rawDataObj_${(new Date).getTime()}.jsp`);
  242. let tplData = rptDataUtil.assembleData(rawDataObj);
  243. let printCom = JpcEx.createNew();
  244. if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  245. if (orientation && (orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = orientation;
  246. let defProperties = rptUtil.getReportDefaultCache();
  247. if (customizeCfg) setupCustomizeCfg(customizeCfg, rptTpl, defProperties);
  248. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  249. printCom.initialize(rptTpl);
  250. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, outputType);
  251. let maxPages = printCom.totalPages;
  252. let pageRst = null;
  253. if (maxPages > 0) {
  254. pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  255. } else {
  256. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  257. }
  258. if (pageRst) {
  259. // fsUtil.writeObjToFile(pageRst, `D:/GitHome/ConstructionCost/tmp/testBuiltPageResult_${(new Date).getTime()}.jsp`);
  260. cb(null, pageRst);
  261. } else {
  262. cb('Have errors while on going...', null);
  263. }
  264. } catch (ex) {
  265. console.log("报表数据异常: userId " + user_id + ", project id: " + prj_id);
  266. console.log(ex.message);
  267. cb('Exception occurs while on going...', null);
  268. }
  269. };
  270. //*/
  271. //取汇总数据流程
  272. if (promiseArr[0] !== null) {
  273. promiseArr[0].then(function (rst) {
  274. rawDataObj.Construct = rst.parent;
  275. rawDataObj.ConstructDetail = rst.subList;
  276. if (promiseArr[1] !== null) {
  277. promiseArr[1].then(function (rst) {
  278. rawDataObj.Segment = rst.parent;
  279. rawDataObj.SegmentDetail = rst.subList;
  280. buildPageData(rawDataObj, rptDataUtil, rptTpl);
  281. });
  282. } else {
  283. buildPageData(rawDataObj, rptDataUtil, rptTpl);
  284. }
  285. });
  286. } else if (promiseArr[1] !== null) {
  287. promiseArr[1].then(function (rst) {
  288. rawDataObj.Segment = rst.parent;
  289. rawDataObj.SegmentDetail = rst.subList;
  290. buildPageData(rawDataObj, rptDataUtil, rptTpl);
  291. });
  292. } else {
  293. buildPageData(rawDataObj, rptDataUtil, rptTpl);
  294. }
  295. /*/
  296. let tplData = rptDataUtil.assembleData(rawDataObj);
  297. let printCom = JpcEx.createNew();
  298. if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  299. //console.log("orientation: " + (orientation === 'null'));
  300. if (orientation && (orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = orientation;
  301. let defProperties = rptUtil.getReportDefaultCache();
  302. if (customizeCfg) setupCustomizeCfg(customizeCfg, rptTpl, defProperties);
  303. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  304. printCom.initialize(rptTpl);
  305. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, outputType);
  306. let maxPages = printCom.totalPages;
  307. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  308. if (pageRst) {
  309. // fsUtil.writeObjToFile(pageRst, "D:/GitHome/ConstructionCost/tmp/testBuiltPageResult.jsp");
  310. cb(null, pageRst);
  311. } else {
  312. cb('Have errors while on going...', null);
  313. }
  314. //*/
  315. } else {
  316. cb('No report data were found!', null);
  317. }
  318. });
  319. } else {
  320. cb('No report template was found!', null);
  321. }
  322. });
  323. }
  324. function getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, option, outputType, cb) {
  325. for (let idx = 0; idx < rpt_ids.length; idx++) {
  326. rpt_ids[idx] = parseInt(rpt_ids[idx]); //转换一下,以防万一
  327. }
  328. rptTplFacade.getRptTemplates(rpt_ids).then(function(rptTpls) {
  329. let rptDataUtil = new rptDataExtractor();
  330. let filters = [];
  331. if (rptTpls.length > 1) {
  332. rptTpls.sort(function (item1, item2){
  333. let i1 = (item1._doc)?item1._doc:item1, i2 = (item2._doc)?item2._doc:item2;
  334. let ID1 = rpt_ids.indexOf(i1[JV.PROP_ID]), ID2 = rpt_ids.indexOf(i2[JV.PROP_ID]);
  335. return ID1 - ID2;
  336. });
  337. }
  338. for (let rptTpl of rptTpls) {
  339. rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
  340. let filter = rptDataUtil.getDataRequestFilter();
  341. for (let dtlFilter of filter) {
  342. if (filters.indexOf(dtlFilter) < 0) {
  343. filters.push(dtlFilter);
  344. }
  345. }
  346. }
  347. //正常应该根据报表模板定义的数据类型来请求数据
  348. rptTplDataFacade.prepareProjectData(user_id, prj_id, filters, function (err, msg, rawDataObj) {
  349. if (!err) {
  350. try {
  351. let rptPageRstArray = [];
  352. //1. 这里只用一份数据,根据实际应用情况,只需要保留copy三样数据: bills, ration, ration_glj, projectGLJ
  353. let savedBillsData = [], savedRationData = [], savedGljData = [], savedPrjGljData = [], savedFeeRateData = [];
  354. for (let dtlData of rawDataObj.prjData) {
  355. if (dtlData.moduleName === 'bills') {
  356. // Object.assign(savedBillsData, dtlData.data);
  357. savedBillsData = JSON.stringify(dtlData.data);
  358. } else if (dtlData.moduleName === 'ration') {
  359. // Object.assign(savedRationData, dtlData.data);
  360. savedRationData = JSON.stringify(dtlData.data);
  361. } else if (dtlData.moduleName === 'ration_glj') {
  362. // Object.assign(savedGljData, dtlData.data);
  363. savedGljData = JSON.stringify(dtlData.data);
  364. } else if (dtlData.moduleName === 'feeRate') {
  365. Object.assign(savedFeeRateData, dtlData.data._doc.rates);
  366. } else if (dtlData.moduleName === 'projectGLJ') {
  367. // Object.assign(savedPrjGljData, dtlData.data.gljList);
  368. savedPrjGljData = JSON.stringify(dtlData.data.gljList);
  369. }
  370. // 备注:经测试,feeRate目前只能用assign方式来保存,用JSON方式会报错
  371. }
  372. //2. 一个一个模板创建数据
  373. for (let tplIdx = 0; tplIdx < rptTpls.length; tplIdx++) {
  374. // console.log('tplIdx: ' + tplIdx);
  375. let rptTpl = (rptTpls[tplIdx]._doc)?rptTpls[tplIdx]._doc:rptTpls[tplIdx];
  376. rptDataUtil.initialize(rptTpl);
  377. let tplData = rptDataUtil.assembleData(rawDataObj);
  378. let printCom = JpcEx.createNew();
  379. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  380. if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  381. if (orientation && (orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = orientation;
  382. let defProperties = rptUtil.getReportDefaultCache();
  383. if (customizeCfg) setupCustomizeCfg(customizeCfg, rptTpl, defProperties);
  384. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  385. printCom.initialize(rptTpl);
  386. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, outputType);
  387. let maxPages = printCom.totalPages;
  388. let pageRst = null;
  389. if (maxPages > 0) {
  390. pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  391. } else {
  392. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  393. }
  394. rptPageRstArray.push(pageRst);
  395. //注意:这里需要把清单、定额、工料机数据assign回去!!!
  396. for (let dtlData of rawDataObj.prjData) {
  397. if (dtlData.moduleName === 'bills' && savedBillsData.length > 0) {
  398. // Object.assign(dtlData.data, savedBillsData);
  399. dtlData.data = JSON.parse(savedBillsData);
  400. } else if (dtlData.moduleName === 'ration' && savedRationData.length > 0) {
  401. // Object.assign(dtlData.data, savedRationData);
  402. dtlData.data = JSON.parse(savedRationData);
  403. } else if (dtlData.moduleName === 'ration_glj' && savedGljData.length > 0) {
  404. // Object.assign(dtlData.data, savedGljData);
  405. dtlData.data = JSON.parse(savedGljData);
  406. } else if (dtlData.moduleName === 'feeRate' && savedFeeRateData.length > 0) {
  407. Object.assign(dtlData.data._doc.rates, savedFeeRateData);
  408. // 备注:经测试,feeRate目前只能用assign方式来回滚,用JSON方式会报错
  409. } else if (dtlData.moduleName === 'projectGLJ' && savedPrjGljData.length > 0) {
  410. // Object.assign(dtlData.data.gljList, savedPrjGljData);
  411. dtlData.data.gljList = JSON.parse(savedPrjGljData);
  412. }
  413. }
  414. }
  415. cb(null, rptPageRstArray);
  416. } catch (ex) {
  417. console.log("报表数据异常(getMultiRptsCommon): userId " + user_id + ", project id: " + prj_id);
  418. console.log(ex.toString());
  419. cb('Exception occurs while on going...', null);
  420. } finally {
  421. }
  422. } else {
  423. cb('No report data were found!', null);
  424. }
  425. })
  426. });
  427. }
  428. function getBillsSummaryReportPages(req, user_id, prjIds, rpt_id, pageSize, orientation, customizeCfg, option, outputType, cb) {
  429. let rptTpl = null;
  430. rptTplDataFacade.getBudgetSummayDatas(prjIds, req.session.sessionUser.id, req.session.sessionCompilation._id, req.session.sessionCompilation.overWriteUrl).then(function(summaryRawDataRst) {
  431. rptTplFacade.getRptTemplate(rpt_id).then(function(rptTpl) {
  432. let rptDataUtil = new rptDataExtractor();
  433. rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
  434. try {
  435. let tplData = rptDataUtil.assembleData(summaryRawDataRst);
  436. // fsUtil.writeObjToFile(summaryRawDataRst, "D:/GitHome/temp/billsSummaryRawDataRstRawData.jsp");
  437. let printCom = JpcEx.createNew();
  438. if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  439. //console.log("orientation: " + (orientation === 'null'));
  440. if (orientation && (orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = orientation;
  441. let defProperties = rptUtil.getReportDefaultCache();
  442. if (customizeCfg) setupCustomizeCfg(customizeCfg, rptTpl, defProperties);
  443. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  444. printCom.initialize(rptTpl);
  445. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, outputType);
  446. let maxPages = printCom.totalPages;
  447. let pageRst = null;
  448. if (maxPages > 0) {
  449. pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  450. } else {
  451. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  452. }
  453. if (pageRst) {
  454. cb(null, pageRst);
  455. } else {
  456. cb('Have errors while on going...', null);
  457. }
  458. } catch (ex) {
  459. console.log("报表数据异常: userId " + user_id + ", project id: " + JSON.stringify(prjIds));
  460. console.log(ex.message);
  461. cb('Exception occurs while on going...', null);
  462. }
  463. });
  464. });
  465. }
  466. function getSummaryComboPages(req, user_id, prjIds, billsSummaryRpt_ids, gljSummaryRpt_ids, pageSize, orientation, customizeCfg, option, outputType, cb) {
  467. let pageRstArr = [];
  468. let pri_setup_rpt_tpl_data = function (rptTpl, summaryRawDataRst) {
  469. let rptDataUtil = new rptDataExtractor();
  470. rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
  471. try {
  472. let tplData = rptDataUtil.assembleData(summaryRawDataRst);
  473. let printCom = JpcEx.createNew();
  474. if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  475. if (orientation && (orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = orientation;
  476. let defProperties = rptUtil.getReportDefaultCache();
  477. if (customizeCfg) setupCustomizeCfg(customizeCfg, rptTpl, defProperties);
  478. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  479. printCom.initialize(rptTpl);
  480. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, outputType);
  481. let maxPages = printCom.totalPages;
  482. let pageRst = null;
  483. if (maxPages > 0) {
  484. pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  485. } else {
  486. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  487. }
  488. pageRstArr.push(pageRst);
  489. } catch (ex) {
  490. console.log("Summary报表数据异常: userId " + user_id + ", project ids: " + prjIds);
  491. console.log(ex.message);
  492. }
  493. };
  494. let billRpts = [], gljRpts = [];
  495. rptTplFacade.getRptTemplates(billsSummaryRpt_ids.concat(gljSummaryRpt_ids)).then(function(rptTpls) {
  496. if (rptTpls && rptTpls.length > 0) {
  497. for (let rptTpl of rptTpls) {
  498. if (billsSummaryRpt_ids.indexOf(rptTpl.ID) >= 0) {
  499. billRpts.push(rptTpl);
  500. } else if (gljSummaryRpt_ids.indexOf(rptTpl.ID) >= 0) {
  501. gljRpts.push(rptTpl);
  502. }
  503. }
  504. }
  505. if (billRpts.length > 0) {
  506. rptTplDataFacade.getBudgetSummayDatas(prjIds, req.session.sessionUser.id, req.session.sessionCompilation._id, req.session.sessionCompilation.overWriteUrl).then(function(summaryRawDataRst) {
  507. // let savedBillsData = [];
  508. try {
  509. // Object.assign(savedBillsData, summaryRawDataRst.SummaryAuditDetail);
  510. for (let idx = 0; idx < billRpts.length; idx++) {
  511. pri_setup_rpt_tpl_data(billRpts[idx], summaryRawDataRst);
  512. // if (idx < billRpts.length - 1) {
  513. // Object.assign(summaryRawDataRst.SummaryAuditDetail, savedBillsData);
  514. // }
  515. }
  516. if (gljRpts && gljRpts.length > 0) {
  517. rptTplDataFacade.getGLJSummayDatas(prjIds).then(function(summaryRawDataRst) {
  518. // let savedBillsData = [];
  519. // Object.assign(savedBillsData, summaryRawDataRst.SummaryAuditDetail);
  520. for (let idx = 0; idx < gljRpts.length; idx++) {
  521. pri_setup_rpt_tpl_data(gljRpts[idx], summaryRawDataRst);
  522. // if (idx < gljRpts.length - 1) {
  523. // Object.assign(summaryRawDataRst.SummaryAuditDetail, savedBillsData);
  524. // }
  525. }
  526. cb(null, pageRstArr);
  527. });
  528. } else {
  529. cb(null, pageRstArr);
  530. }
  531. } catch (ex) {
  532. console.log(ex);
  533. cb(null, pageRstArr);
  534. }
  535. });
  536. } else if (gljRpts.length > 0) {
  537. rptTplDataFacade.getGLJSummayDatas(prjIds).then(function(summaryRawDataRst) {
  538. // let savedBillsData = [];
  539. // Object.assign(savedBillsData, summaryRawDataRst.SummaryAuditDetail);
  540. // 养护工料机汇总不用考虑back up问题。模板需要自觉一点,不要修改数据!!!
  541. for (let idx = 0; idx < gljRpts.length; idx++) {
  542. pri_setup_rpt_tpl_data(gljRpts[idx], summaryRawDataRst);
  543. if (idx < gljRpts.length - 1) {
  544. // Object.assign(summaryRawDataRst.SummaryAuditDetail, savedBillsData);
  545. }
  546. }
  547. cb(null, pageRstArr);
  548. });
  549. }
  550. });
  551. }
  552. async function getGljSummaryReportPages(user_id, prjIds, rpt_id, pageSize, orientation, customizeCfg, option, outputType, cb) {
  553. let summaryRawDataRst = await rptTplDataFacade.getGLJSummayDatas(prjIds);
  554. let rptTpl = await rptTplFacade.getRptTemplate(rpt_id);
  555. let rptDataUtil = new rptDataExtractor();
  556. rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
  557. try {
  558. // fsUtil.writeObjToFile(summaryRawDataRst, "D:/GitHome/temp/gljSummaryRawDataRstRawData.jsp");
  559. let tplData = rptDataUtil.assembleData(summaryRawDataRst);
  560. let printCom = JpcEx.createNew();
  561. if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  562. //console.log("orientation: " + (orientation === 'null'));
  563. if (orientation && (orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = orientation;
  564. let defProperties = rptUtil.getReportDefaultCache();
  565. if (customizeCfg) setupCustomizeCfg(customizeCfg, rptTpl, defProperties);
  566. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  567. printCom.initialize(rptTpl);
  568. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, outputType);
  569. let maxPages = printCom.totalPages;
  570. let pageRst = null;
  571. if (maxPages > 0) {
  572. pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  573. } else {
  574. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  575. }
  576. if (pageRst) {
  577. cb(null, pageRst);
  578. } else {
  579. cb('Have errors while on going...', null);
  580. }
  581. } catch (ex) {
  582. // console.log("报表数据异常: userId " + user_id + ", project id: " + prj_id);
  583. console.log(ex);
  584. cb('Exception occurs while on going...', null);
  585. }
  586. }
  587. function getGljSummaryReportPages_oldstyle(user_id, prjIds, rpt_id, pageSize, orientation, customizeCfg, option, outputType, cb) {
  588. let rptTpl = null;
  589. rptTplDataFacade.getGLJSummayDatas(prjIds).then(function(summaryRawDataRst) {
  590. rptTplFacade.getRptTemplate(rpt_id).then(function(rptTpl) {
  591. let rptDataUtil = new rptDataExtractor();
  592. rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
  593. try {
  594. // fsUtil.writeObjToFile(summaryRawDataRst, "D:/GitHome/temp/gljSummaryRawDataRstRawData.jsp");
  595. let tplData = rptDataUtil.assembleData(summaryRawDataRst);
  596. let printCom = JpcEx.createNew();
  597. if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  598. //console.log("orientation: " + (orientation === 'null'));
  599. if (orientation && (orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = orientation;
  600. let defProperties = rptUtil.getReportDefaultCache();
  601. if (customizeCfg) setupCustomizeCfg(customizeCfg, rptTpl, defProperties);
  602. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  603. printCom.initialize(rptTpl);
  604. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, outputType);
  605. let maxPages = printCom.totalPages;
  606. let pageRst = null;
  607. if (maxPages > 0) {
  608. pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  609. } else {
  610. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  611. }
  612. if (pageRst) {
  613. cb(null, pageRst);
  614. } else {
  615. cb('Have errors while on going...', null);
  616. }
  617. } catch (ex) {
  618. // console.log("报表数据异常: userId " + user_id + ", project id: " + prj_id);
  619. console.log(ex);
  620. cb('Exception occurs while on going...', null);
  621. }
  622. });
  623. });
  624. }
  625. module.exports = {
  626. getReportAllPages: function (req, res) {
  627. let params = JSON.parse(req.body.params),
  628. rpt_id = params.rpt_tpl_id,
  629. prj_id = params.prj_id,
  630. pageSize = params.pageSize,
  631. orientation = params.orientation,
  632. customizeCfg = params.custCfg
  633. ;
  634. // req.session.sessionUser.ssoId
  635. let user_id = req.session.sessionUser.id;
  636. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, customizeCfg, null, JV.OUTPUT_TYPE_NORMAL, function (err, pageRst) {
  637. callback(req, res, err, pageRst);
  638. });
  639. },
  640. getMultiReportsEx: function (req, res) {
  641. //原则说明:把所有报表模板集中获取,统一filter,只读一次数据!
  642. let params = JSON.parse(req.body.params),
  643. prj_id = params.prj_id,
  644. rpt_ids = params.rpt_ids,
  645. //--以下是汇总类数据----------
  646. rpt_bill_tpl_ids = params.rpt_bill_tpl_ids,
  647. rpt_glj_tpl_ids = params.rpt_glj_tpl_ids,
  648. prjIds = params.prjIds,
  649. //------------
  650. pageSize = params.pageSize,
  651. orientation = params.orientation,
  652. customizeCfg = params.custCfg,
  653. option = params.option;
  654. let user_id = req.session.sessionUser.id;
  655. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  656. if (rpt_ids && rpt_ids.length > 0) {
  657. getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_PDF, function (err, rptPageRstArray) {
  658. if (err) {
  659. callback(req, res, '数据有误', null);
  660. } else {
  661. if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  662. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  663. callback(req, res, err, rptSumPageRstArray.concat(rptPageRstArray));
  664. });
  665. } else {
  666. callback(req, res, err, rptPageRstArray);
  667. }
  668. }
  669. });
  670. } else if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  671. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  672. callback(req, res, err, rptSumPageRstArray);
  673. });
  674. }
  675. },
  676. getBillSummaryReportPages: function (req, res) {
  677. let params = JSON.parse(req.body.params),
  678. rpt_id = params.rpt_tpl_id,
  679. prjIds = params.prjIds,
  680. pageSize = params.pageSize,
  681. orientation = params.orientation,
  682. customizeCfg = params.custCfg
  683. ;
  684. let user_id = req.session.sessionUser.id;
  685. getBillsSummaryReportPages(req, user_id, prjIds, rpt_id, pageSize, orientation, customizeCfg, null, JV.OUTPUT_TYPE_NORMAL, function (err, pageRst) {
  686. callback(req, res, err, pageRst);
  687. });
  688. },
  689. getGljSummaryReportPages: function (req, res) {
  690. let params = JSON.parse(req.body.params),
  691. rpt_id = params.rpt_tpl_id,
  692. prjIds = params.prjIds,
  693. pageSize = params.pageSize,
  694. orientation = params.orientation,
  695. customizeCfg = params.custCfg
  696. ;
  697. let user_id = req.session.sessionUser.id;
  698. getGljSummaryReportPages(user_id, prjIds, rpt_id, pageSize, orientation, customizeCfg, null, JV.OUTPUT_TYPE_NORMAL, function (err, pageRst) {
  699. callback(req, res, err, pageRst);
  700. });
  701. },
  702. getReportAllPagesSvg: function (req, res) {
  703. let params = JSON.parse(req.body.params),
  704. rpt_id = params.rpt_tpl_id,
  705. prj_id = params.prj_id,
  706. pageSize = params.pageSize,
  707. orientation = params.orientation,
  708. customizeCfg = params.custCfg
  709. ;
  710. let user_id = req.session.sessionUser.id;
  711. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, customizeCfg, null, JV.OUTPUT_TYPE_SVG, function (err, pageRst) {
  712. let svgRstStrArr = rpt_svg_util.exportSvgStr(pageRst, 0, 0);
  713. callback(req, res, err, svgRstStrArr);
  714. });
  715. },
  716. createExcelFilesInOneBookEx: function (req, res) {
  717. //采用了优化策略
  718. let params = JSON.parse(req.body.params),
  719. prj_id = params.prj_id,
  720. rpt_ids = params.rpt_ids,
  721. //--以下是汇总类数据----------
  722. rpt_bill_tpl_ids = params.rpt_bill_tpl_ids,
  723. rpt_glj_tpl_ids = params.rpt_glj_tpl_ids,
  724. prjIds = params.prjIds,
  725. //------------
  726. rptName = params.rptName,
  727. pageSize = params.pageSize,
  728. orientation = params.orientation,
  729. customizeCfg = params.custCfg,
  730. option = params.option;
  731. let user_id = req.session.sessionUser.id;
  732. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  733. if (rpt_ids && rpt_ids.length > 0) {
  734. getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_NORMAL, function (err, rptPageRstArray) {
  735. if(err){
  736. console.log('导出Excel错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  737. callback(req, res, '数据有误', null);
  738. } else {
  739. if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  740. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  741. rpt_xl_util.exportExcelInOneBook(rptPageRstArray.concat(rptSumPageRstArray), pageSize, rptName, function(uuidName){
  742. let fileRst = {uuid: uuidName, reportName: rptName};
  743. callback(req, res, err, fileRst);
  744. });
  745. });
  746. } else {
  747. rpt_xl_util.exportExcelInOneBook(rptPageRstArray, pageSize, rptName, function(uuidName){
  748. let fileRst = {uuid: uuidName, reportName: rptName};
  749. callback(req, res, err, fileRst);
  750. });
  751. }
  752. }
  753. });
  754. } else if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  755. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptPageRstArray) {
  756. rpt_xl_util.exportExcelInOneBook(rptPageRstArray, pageSize, rptName, function(uuidName){
  757. let fileRst = {uuid: uuidName, reportName: rptName};
  758. callback(req, res, err, fileRst);
  759. });
  760. });
  761. }
  762. },
  763. createExcelFilesEx: function (req, res) {
  764. let params = JSON.parse(req.body.params),
  765. prj_id = params.prj_id,
  766. rpt_ids = params.rpt_ids,
  767. //--以下是汇总类数据----------
  768. rpt_bill_tpl_ids = params.rpt_bill_tpl_ids,
  769. rpt_glj_tpl_ids = params.rpt_glj_tpl_ids,
  770. prjIds = params.prjIds,
  771. sum_rpt_names = params.sum_rpt_names,
  772. //------------
  773. rpt_names = params.rpt_names,
  774. pageSize = params.pageSize,
  775. orientation = params.orientation,
  776. isOneSheet = params.isOneSheet,
  777. customizeCfg = params.custCfg,
  778. option = params.option;
  779. let user_id = req.session.sessionUser.id;
  780. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  781. let pri_Add_Parallel_Functions = function (parallelFunctions, rptPageRstArray, paraRptNames, err) {
  782. for (let idx = 0; idx < rptPageRstArray.length; idx++) {
  783. parallelFunctions.push((function (pageRst, rpt_name) {
  784. return function (cb) {
  785. rpt_xl_util.exportExcel(pageRst, pageSize, rpt_name, isOneSheet, null, null, function(uuidName){
  786. let fileRst = {uuid: uuidName, reportName: rpt_name};
  787. cb(err, fileRst);
  788. })
  789. };
  790. })(rptPageRstArray[idx], paraRptNames[idx]));
  791. }
  792. };
  793. let pri_Run_Parallel_Functions = function (parallelFunctions) {
  794. async.parallel(parallelFunctions, function (err, fileRstArray) {
  795. if (err) {
  796. console.log('导出Excel错误(导出数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  797. callback(req, res, '数据导出错误', null);
  798. } else {
  799. // console.log(err);
  800. callback(req, res, err, fileRstArray);
  801. }
  802. });
  803. };
  804. if (rpt_ids && rpt_ids.length > 0) {
  805. getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_NORMAL, function (err, rptPageRstArray) {
  806. if (err) {
  807. console.log('导出Excel错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  808. callback(req, res, '数据生成错误', null);
  809. } else {
  810. if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  811. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  812. let parallelFunctions = [];
  813. pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err);
  814. pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err);
  815. pri_Run_Parallel_Functions(parallelFunctions);
  816. });
  817. } else {
  818. let parallelFunctions = [];
  819. pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err);
  820. pri_Run_Parallel_Functions(parallelFunctions);
  821. }
  822. }
  823. });
  824. } else if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  825. // console.log('rpt_bill_tpl_ids:');
  826. // console.log(rpt_bill_tpl_ids);
  827. // console.log('rpt_glj_tpl_ids:');
  828. // console.log(rpt_glj_tpl_ids);
  829. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  830. let parallelFunctions = [];
  831. pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err);
  832. pri_Run_Parallel_Functions(parallelFunctions);
  833. });
  834. }
  835. },
  836. getFileByUUID: function (req, res) {
  837. let uuid = req.params.uuid,
  838. rptName = req.params.rptName,
  839. suffix = "." + req.params.suffix
  840. ;
  841. // let user_id = req.session.sessionUser.id; //未来要校验user id
  842. try {
  843. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  844. let rptNameURI = encodeURI(rptName);
  845. res.setHeader("Content-Disposition", "attachment; filename=\"" + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix );
  846. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + uuid + suffix);
  847. filestream.on('data', function(chunk) {
  848. res.write(chunk);
  849. });
  850. filestream.on('end', function() {
  851. res.end();
  852. });
  853. } catch (e) {
  854. console.log(e);
  855. }
  856. },
  857. getExcel: function(req, res) {
  858. let prj_id = req.params.prj_id,
  859. rpt_id = req.params.rpt_id,
  860. pageSize = req.params.size,
  861. orientation = req.params.orientation,
  862. rptName = req.params.rptName,
  863. isOneSheet = req.params.isOneSheet,
  864. option = req.params.option
  865. ;
  866. let customizeCfg = null;
  867. let user_id = req.session.sessionUser.id;
  868. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  869. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_EXCEL, function(err, pageRst){
  870. try {
  871. rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, null, null, function(uuidName){
  872. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  873. let rptNameURI = encodeURI(rptName);
  874. res.setHeader("Content-Disposition", "attachment; filename=\"" + rptNameURI + ".xlsx\"; filename*=utf-8''" + rptNameURI + '.xlsx' );
  875. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + uuidName + '.xlsx');
  876. filestream.on('data', function(chunk) {
  877. res.write(chunk);
  878. });
  879. filestream.on('end', function() {
  880. res.end();
  881. });
  882. });
  883. } catch (e) {
  884. console.log(e);
  885. }
  886. })
  887. },
  888. getExcelInOneBook: function (req, res) {
  889. let prj_id = req.params.prj_id,
  890. rpt_ids = req.params.rpt_ids.split(','),
  891. pageSize = req.params.size,
  892. rptName = req.params.rptName,
  893. option = req.params.option;
  894. let user_id = req.session.sessionUser.id;
  895. let parallelFunctions = [];
  896. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  897. for (let id of rpt_ids) {
  898. parallelFunctions.push((function (rpt_id) {
  899. return function (cb) {
  900. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, null, null, dftOption, JV.OUTPUT_TYPE_EXCEL, function (err, pageRst) {
  901. if(err){
  902. cb(err);
  903. }
  904. else{
  905. cb(null, pageRst);
  906. }
  907. })
  908. }
  909. })(parseInt(id)));
  910. }
  911. async.parallel(parallelFunctions, function (err, pageRstArray) {
  912. if (err) {
  913. callback(req, res, '数据有误', null);
  914. } else {
  915. rpt_xl_util.exportExcelInOneBook(pageRstArray, pageSize, rptName, function(tmpFilePath){
  916. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  917. let rptNameURI = encodeURI(rptName);
  918. res.setHeader("Content-Disposition", "attachment; filename=\"" + rptNameURI + ".xlsx\"; filename*=utf-8''" + rptNameURI + '.xlsx');
  919. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + tmpFilePath + '.xlsx');
  920. filestream.on('data', function(chunk) {
  921. res.write(chunk);
  922. });
  923. filestream.on('end', function() {
  924. res.end();
  925. });
  926. });
  927. }
  928. })
  929. },
  930. createPdfFilesEx: function (req, res) {
  931. let params = JSON.parse(req.body.params),
  932. prj_id = params.prj_id,
  933. rpt_ids = params.rpt_ids,
  934. rpt_names = params.rpt_names,
  935. //--以下是汇总类数据----------
  936. rpt_bill_tpl_ids = params.rpt_bill_tpl_ids,
  937. rpt_glj_tpl_ids = params.rpt_glj_tpl_ids,
  938. prjIds = params.prjIds,
  939. sum_rpt_names = params.sum_rpt_names,
  940. //------------
  941. pageSize = params.pageSize,
  942. orientation = params.orientation,
  943. customizeCfg = params.custCfg,
  944. option = params.option;
  945. let user_id = req.session.sessionUser.id;
  946. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  947. console.log('start!');
  948. let pri_Add_Parallel_Functions = function (parallelFunctions, rptPageRstArray, paraRptNames, err) {
  949. for (let idx = 0; idx < rptPageRstArray.length; idx++) {
  950. parallelFunctions.push((function (pageRst, rpt_name) {
  951. return function (cb) {
  952. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rpt_name, function(uuidName){
  953. let fileRst = {uuid: uuidName, reportName: rpt_name};
  954. cb(err, fileRst);
  955. })
  956. };
  957. })(rptPageRstArray[idx], paraRptNames[idx]));
  958. }
  959. };
  960. let pri_Run_Parallel_Functions = function (parallelFunctions) {
  961. async.parallel(parallelFunctions, function (err, fileRstArray) {
  962. if (err) {
  963. console.log('导出PDF错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  964. callback(req, res, '数据导出错误', null);
  965. } else {
  966. // console.log(err);
  967. callback(req, res, err, fileRstArray);
  968. }
  969. });
  970. };
  971. if (rpt_ids && rpt_ids.length > 0) {
  972. console.log(0);
  973. getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_PDF, function (err, rptPageRstArray) {
  974. if (err) {
  975. console.log('导出Excel错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  976. callback(req, res, '数据生成错误', null);
  977. } else {
  978. if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  979. console.log(1);
  980. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  981. let parallelFunctions = [];
  982. console.log(2);
  983. pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err);
  984. pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err);
  985. pri_Run_Parallel_Functions(parallelFunctions);
  986. });
  987. } else {
  988. console.log(3);
  989. let parallelFunctions = [];
  990. pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err);
  991. pri_Run_Parallel_Functions(parallelFunctions);
  992. }
  993. }
  994. });
  995. } else if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  996. console.log(10);
  997. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  998. let parallelFunctions = [];
  999. pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err);
  1000. pri_Run_Parallel_Functions(parallelFunctions);
  1001. });
  1002. }
  1003. },
  1004. getPDF:function (req, res) {
  1005. let prj_id = req.params.prj_id,
  1006. rpt_id = req.params.rpt_id,
  1007. pageSize = req.params.size,
  1008. orientation = req.params.orientation,
  1009. rptName = req.params.rptName
  1010. ;
  1011. let user_id = req.session.sessionUser.id;
  1012. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, null, JV.PAGING_OPTION_NORMAL, JV.OUTPUT_TYPE_PDF, function(err, pageRst){
  1013. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (uuidName) {
  1014. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  1015. // res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
  1016. let rptNameURI = encodeURI(rptName);
  1017. res.setHeader("Content-Disposition", "attachment; filename=\"" + rptNameURI + ".pdf\"; filename*=utf-8''" + rptNameURI + ".pdf" );
  1018. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + uuidName + '.pdf');
  1019. filestream.on('data', function(chunk) {
  1020. res.write(chunk);
  1021. });
  1022. filestream.on('end', function() {
  1023. res.end();
  1024. });
  1025. })
  1026. })
  1027. }
  1028. };