rpt_controller.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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/ConstructionCost/tmp/rawDataObj.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.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. function getGljSummaryReportPages(user_id, prjIds, rpt_id, pageSize, orientation, customizeCfg, option, outputType, cb) {
  553. let rptTpl = null;
  554. rptTplDataFacade.getGLJSummayDatas(prjIds).then(function(summaryRawDataRst) {
  555. rptTplFacade.getRptTemplate(rpt_id).then(function(rptTpl) {
  556. let rptDataUtil = new rptDataExtractor();
  557. rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
  558. try {
  559. // fsUtil.writeObjToFile(summaryRawDataRst, "D:/GitHome/temp/gljSummaryRawDataRstRawData.jsp");
  560. let tplData = rptDataUtil.assembleData(summaryRawDataRst);
  561. let printCom = JpcEx.createNew();
  562. if (pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  563. //console.log("orientation: " + (orientation === 'null'));
  564. if (orientation && (orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = orientation;
  565. let defProperties = rptUtil.getReportDefaultCache();
  566. if (customizeCfg) setupCustomizeCfg(customizeCfg, rptTpl, defProperties);
  567. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  568. printCom.initialize(rptTpl);
  569. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, outputType);
  570. let maxPages = printCom.totalPages;
  571. let pageRst = null;
  572. if (maxPages > 0) {
  573. pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  574. } else {
  575. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  576. }
  577. if (pageRst) {
  578. cb(null, pageRst);
  579. } else {
  580. cb('Have errors while on going...', null);
  581. }
  582. } catch (ex) {
  583. // console.log("报表数据异常: userId " + user_id + ", project id: " + prj_id);
  584. console.log(ex);
  585. cb('Exception occurs while on going...', null);
  586. }
  587. });
  588. });
  589. }
  590. module.exports = {
  591. getReportAllPages: function (req, res) {
  592. let params = JSON.parse(req.body.params),
  593. rpt_id = params.rpt_tpl_id,
  594. prj_id = params.prj_id,
  595. pageSize = params.pageSize,
  596. orientation = params.orientation,
  597. customizeCfg = params.custCfg
  598. ;
  599. // req.session.sessionUser.ssoId
  600. let user_id = req.session.sessionUser.id;
  601. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, customizeCfg, null, JV.OUTPUT_TYPE_NORMAL, function (err, pageRst) {
  602. callback(req, res, err, pageRst);
  603. });
  604. },
  605. getMultiReportsEx: function (req, res) {
  606. //原则说明:把所有报表模板集中获取,统一filter,只读一次数据!
  607. let params = JSON.parse(req.body.params),
  608. prj_id = params.prj_id,
  609. rpt_ids = params.rpt_ids,
  610. //--以下是汇总类数据----------
  611. rpt_bill_tpl_ids = params.rpt_bill_tpl_ids,
  612. rpt_glj_tpl_ids = params.rpt_glj_tpl_ids,
  613. prjIds = params.prjIds,
  614. //------------
  615. pageSize = params.pageSize,
  616. orientation = params.orientation,
  617. customizeCfg = params.custCfg,
  618. option = params.option;
  619. let user_id = req.session.sessionUser.id;
  620. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  621. if (rpt_ids && rpt_ids.length > 0) {
  622. getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_PDF, function (err, rptPageRstArray) {
  623. if (err) {
  624. callback(req, res, '数据有误', null);
  625. } else {
  626. if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  627. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  628. callback(req, res, err, rptSumPageRstArray.concat(rptPageRstArray));
  629. });
  630. } else {
  631. callback(req, res, err, rptPageRstArray);
  632. }
  633. }
  634. });
  635. } else if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  636. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  637. callback(req, res, err, rptSumPageRstArray);
  638. });
  639. }
  640. },
  641. getBillSummaryReportPages: function (req, res) {
  642. let params = JSON.parse(req.body.params),
  643. rpt_id = params.rpt_tpl_id,
  644. prjIds = params.prjIds,
  645. pageSize = params.pageSize,
  646. orientation = params.orientation,
  647. customizeCfg = params.custCfg
  648. ;
  649. let user_id = req.session.sessionUser.id;
  650. getBillsSummaryReportPages(req, user_id, prjIds, rpt_id, pageSize, orientation, customizeCfg, null, JV.OUTPUT_TYPE_NORMAL, function (err, pageRst) {
  651. callback(req, res, err, pageRst);
  652. });
  653. },
  654. getGljSummaryReportPages: function (req, res) {
  655. let params = JSON.parse(req.body.params),
  656. rpt_id = params.rpt_tpl_id,
  657. prjIds = params.prjIds,
  658. pageSize = params.pageSize,
  659. orientation = params.orientation,
  660. customizeCfg = params.custCfg
  661. ;
  662. let user_id = req.session.sessionUser.id;
  663. getGljSummaryReportPages(user_id, prjIds, rpt_id, pageSize, orientation, customizeCfg, null, JV.OUTPUT_TYPE_NORMAL, function (err, pageRst) {
  664. callback(req, res, err, pageRst);
  665. });
  666. },
  667. getReportAllPagesSvg: function (req, res) {
  668. let params = JSON.parse(req.body.params),
  669. rpt_id = params.rpt_tpl_id,
  670. prj_id = params.prj_id,
  671. pageSize = params.pageSize,
  672. orientation = params.orientation,
  673. customizeCfg = params.custCfg
  674. ;
  675. let user_id = req.session.sessionUser.id;
  676. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, customizeCfg, null, JV.OUTPUT_TYPE_SVG, function (err, pageRst) {
  677. let svgRstStrArr = rpt_svg_util.exportSvgStr(pageRst, 0, 0);
  678. callback(req, res, err, svgRstStrArr);
  679. });
  680. },
  681. createExcelFilesInOneBookEx: function (req, res) {
  682. //采用了优化策略
  683. let params = JSON.parse(req.body.params),
  684. prj_id = params.prj_id,
  685. rpt_ids = params.rpt_ids,
  686. //--以下是汇总类数据----------
  687. rpt_bill_tpl_ids = params.rpt_bill_tpl_ids,
  688. rpt_glj_tpl_ids = params.rpt_glj_tpl_ids,
  689. prjIds = params.prjIds,
  690. //------------
  691. rptName = params.rptName,
  692. pageSize = params.pageSize,
  693. orientation = params.orientation,
  694. customizeCfg = params.custCfg,
  695. option = params.option;
  696. let user_id = req.session.sessionUser.id;
  697. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  698. if (rpt_ids && rpt_ids.length > 0) {
  699. getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_NORMAL, function (err, rptPageRstArray) {
  700. if(err){
  701. console.log('导出Excel错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  702. callback(req, res, '数据有误', null);
  703. } else {
  704. if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  705. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  706. rpt_xl_util.exportExcelInOneBook(rptPageRstArray.concat(rptSumPageRstArray), pageSize, rptName, function(uuidName){
  707. let fileRst = {uuid: uuidName, reportName: rptName};
  708. callback(req, res, err, fileRst);
  709. });
  710. });
  711. } else {
  712. rpt_xl_util.exportExcelInOneBook(rptPageRstArray, pageSize, rptName, function(uuidName){
  713. let fileRst = {uuid: uuidName, reportName: rptName};
  714. callback(req, res, err, fileRst);
  715. });
  716. }
  717. }
  718. });
  719. } else if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  720. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptPageRstArray) {
  721. rpt_xl_util.exportExcelInOneBook(rptPageRstArray, pageSize, rptName, function(uuidName){
  722. let fileRst = {uuid: uuidName, reportName: rptName};
  723. callback(req, res, err, fileRst);
  724. });
  725. });
  726. }
  727. },
  728. createExcelFilesEx: function (req, res) {
  729. let params = JSON.parse(req.body.params),
  730. prj_id = params.prj_id,
  731. rpt_ids = params.rpt_ids,
  732. //--以下是汇总类数据----------
  733. rpt_bill_tpl_ids = params.rpt_bill_tpl_ids,
  734. rpt_glj_tpl_ids = params.rpt_glj_tpl_ids,
  735. prjIds = params.prjIds,
  736. sum_rpt_names = params.sum_rpt_names,
  737. //------------
  738. rpt_names = params.rpt_names,
  739. pageSize = params.pageSize,
  740. orientation = params.orientation,
  741. isOneSheet = params.isOneSheet,
  742. customizeCfg = params.custCfg,
  743. option = params.option;
  744. let user_id = req.session.sessionUser.id;
  745. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  746. let pri_Add_Parallel_Functions = function (parallelFunctions, rptPageRstArray, paraRptNames, err) {
  747. for (let idx = 0; idx < rptPageRstArray.length; idx++) {
  748. parallelFunctions.push((function (pageRst, rpt_name) {
  749. return function (cb) {
  750. rpt_xl_util.exportExcel(pageRst, pageSize, rpt_name, isOneSheet, null, null, function(uuidName){
  751. let fileRst = {uuid: uuidName, reportName: rpt_name};
  752. cb(err, fileRst);
  753. })
  754. };
  755. })(rptPageRstArray[idx], paraRptNames[idx]));
  756. }
  757. };
  758. let pri_Run_Parallel_Functions = function (parallelFunctions) {
  759. async.parallel(parallelFunctions, function (err, fileRstArray) {
  760. if (err) {
  761. console.log('导出Excel错误(导出数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  762. callback(req, res, '数据导出错误', null);
  763. } else {
  764. // console.log(err);
  765. callback(req, res, err, fileRstArray);
  766. }
  767. });
  768. };
  769. if (rpt_ids && rpt_ids.length > 0) {
  770. getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_NORMAL, function (err, rptPageRstArray) {
  771. if (err) {
  772. console.log('导出Excel错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  773. callback(req, res, '数据生成错误', null);
  774. } else {
  775. if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  776. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  777. let parallelFunctions = [];
  778. pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err);
  779. pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err);
  780. pri_Run_Parallel_Functions(parallelFunctions);
  781. });
  782. } else {
  783. let parallelFunctions = [];
  784. pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err);
  785. pri_Run_Parallel_Functions(parallelFunctions);
  786. }
  787. }
  788. });
  789. } else if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  790. // console.log('rpt_bill_tpl_ids:');
  791. // console.log(rpt_bill_tpl_ids);
  792. // console.log('rpt_glj_tpl_ids:');
  793. // console.log(rpt_glj_tpl_ids);
  794. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  795. let parallelFunctions = [];
  796. pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err);
  797. pri_Run_Parallel_Functions(parallelFunctions);
  798. });
  799. }
  800. },
  801. getFileByUUID: function (req, res) {
  802. let uuid = req.params.uuid,
  803. rptName = req.params.rptName,
  804. suffix = "." + req.params.suffix
  805. ;
  806. // let user_id = req.session.sessionUser.id; //未来要校验user id
  807. try {
  808. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  809. let rptNameURI = encodeURI(rptName);
  810. res.setHeader("Content-Disposition", "attachment; filename=\"" + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix );
  811. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + uuid + suffix);
  812. filestream.on('data', function(chunk) {
  813. res.write(chunk);
  814. });
  815. filestream.on('end', function() {
  816. res.end();
  817. });
  818. } catch (e) {
  819. console.log(e);
  820. }
  821. },
  822. getExcel: function(req, res) {
  823. let prj_id = req.params.prj_id,
  824. rpt_id = req.params.rpt_id,
  825. pageSize = req.params.size,
  826. orientation = req.params.orientation,
  827. rptName = req.params.rptName,
  828. isOneSheet = req.params.isOneSheet,
  829. option = req.params.option
  830. ;
  831. let customizeCfg = null;
  832. let user_id = req.session.sessionUser.id;
  833. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  834. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_EXCEL, function(err, pageRst){
  835. try {
  836. rpt_xl_util.exportExcel(pageRst, pageSize, rptName, isOneSheet, null, null, function(uuidName){
  837. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  838. let rptNameURI = encodeURI(rptName);
  839. res.setHeader("Content-Disposition", "attachment; filename=\"" + rptNameURI + ".xlsx\"; filename*=utf-8''" + rptNameURI + '.xlsx' );
  840. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + uuidName + '.xlsx');
  841. filestream.on('data', function(chunk) {
  842. res.write(chunk);
  843. });
  844. filestream.on('end', function() {
  845. res.end();
  846. });
  847. });
  848. } catch (e) {
  849. console.log(e);
  850. }
  851. })
  852. },
  853. getExcelInOneBook: function (req, res) {
  854. let prj_id = req.params.prj_id,
  855. rpt_ids = req.params.rpt_ids.split(','),
  856. pageSize = req.params.size,
  857. rptName = req.params.rptName,
  858. option = req.params.option;
  859. let user_id = req.session.sessionUser.id;
  860. let parallelFunctions = [];
  861. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  862. for (let id of rpt_ids) {
  863. parallelFunctions.push((function (rpt_id) {
  864. return function (cb) {
  865. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, null, null, dftOption, JV.OUTPUT_TYPE_EXCEL, function (err, pageRst) {
  866. if(err){
  867. cb(err);
  868. }
  869. else{
  870. cb(null, pageRst);
  871. }
  872. })
  873. }
  874. })(parseInt(id)));
  875. }
  876. async.parallel(parallelFunctions, function (err, pageRstArray) {
  877. if (err) {
  878. callback(req, res, '数据有误', null);
  879. } else {
  880. rpt_xl_util.exportExcelInOneBook(pageRstArray, pageSize, rptName, function(tmpFilePath){
  881. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  882. let rptNameURI = encodeURI(rptName);
  883. res.setHeader("Content-Disposition", "attachment; filename=\"" + rptNameURI + ".xlsx\"; filename*=utf-8''" + rptNameURI + '.xlsx');
  884. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + tmpFilePath + '.xlsx');
  885. filestream.on('data', function(chunk) {
  886. res.write(chunk);
  887. });
  888. filestream.on('end', function() {
  889. res.end();
  890. });
  891. });
  892. }
  893. })
  894. },
  895. createPdfFilesEx: function (req, res) {
  896. let params = JSON.parse(req.body.params),
  897. prj_id = params.prj_id,
  898. rpt_ids = params.rpt_ids,
  899. rpt_names = params.rpt_names,
  900. //--以下是汇总类数据----------
  901. rpt_bill_tpl_ids = params.rpt_bill_tpl_ids,
  902. rpt_glj_tpl_ids = params.rpt_glj_tpl_ids,
  903. prjIds = params.prjIds,
  904. sum_rpt_names = params.sum_rpt_names,
  905. //------------
  906. pageSize = params.pageSize,
  907. orientation = params.orientation,
  908. customizeCfg = params.custCfg,
  909. option = params.option;
  910. let user_id = req.session.sessionUser.id;
  911. let dftOption = option||JV.PAGING_OPTION_NORMAL;
  912. console.log('start!');
  913. let pri_Add_Parallel_Functions = function (parallelFunctions, rptPageRstArray, paraRptNames, err) {
  914. for (let idx = 0; idx < rptPageRstArray.length; idx++) {
  915. parallelFunctions.push((function (pageRst, rpt_name) {
  916. return function (cb) {
  917. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rpt_name, function(uuidName){
  918. let fileRst = {uuid: uuidName, reportName: rpt_name};
  919. cb(err, fileRst);
  920. })
  921. };
  922. })(rptPageRstArray[idx], paraRptNames[idx]));
  923. }
  924. };
  925. let pri_Run_Parallel_Functions = function (parallelFunctions) {
  926. async.parallel(parallelFunctions, function (err, fileRstArray) {
  927. if (err) {
  928. console.log('导出PDF错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  929. callback(req, res, '数据导出错误', null);
  930. } else {
  931. // console.log(err);
  932. callback(req, res, err, fileRstArray);
  933. }
  934. });
  935. };
  936. if (rpt_ids && rpt_ids.length > 0) {
  937. console.log(0);
  938. getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_PDF, function (err, rptPageRstArray) {
  939. if (err) {
  940. console.log('导出Excel错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  941. callback(req, res, '数据生成错误', null);
  942. } else {
  943. if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  944. console.log(1);
  945. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  946. let parallelFunctions = [];
  947. console.log(2);
  948. pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err);
  949. pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err);
  950. pri_Run_Parallel_Functions(parallelFunctions);
  951. });
  952. } else {
  953. console.log(3);
  954. let parallelFunctions = [];
  955. pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err);
  956. pri_Run_Parallel_Functions(parallelFunctions);
  957. }
  958. }
  959. });
  960. } else if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  961. console.log(10);
  962. getSummaryComboPages(req, user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  963. let parallelFunctions = [];
  964. pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err);
  965. pri_Run_Parallel_Functions(parallelFunctions);
  966. });
  967. }
  968. },
  969. getPDF:function (req, res) {
  970. let prj_id = req.params.prj_id,
  971. rpt_id = req.params.rpt_id,
  972. pageSize = req.params.size,
  973. orientation = req.params.orientation,
  974. rptName = req.params.rptName
  975. ;
  976. let user_id = req.session.sessionUser.id;
  977. getAllPagesCommon(user_id, prj_id, rpt_id, pageSize, orientation, null, JV.PAGING_OPTION_NORMAL, JV.OUTPUT_TYPE_PDF, function(err, pageRst){
  978. rpt_pdf_util.export_pdf_file(pageRst, pageSize, rptName,function (uuidName) {
  979. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  980. // res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".pdf");
  981. let rptNameURI = encodeURI(rptName);
  982. res.setHeader("Content-Disposition", "attachment; filename=\"" + rptNameURI + ".pdf\"; filename*=utf-8''" + rptNameURI + ".pdf" );
  983. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + uuidName + '.pdf');
  984. filestream.on('data', function(chunk) {
  985. res.write(chunk);
  986. });
  987. filestream.on('end', function() {
  988. res.end();
  989. });
  990. })
  991. })
  992. }
  993. };