report_controller.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2019/7/5.
  4. */
  5. const tenderMenu = require('../../config/menu').tenderMenu;
  6. const measureType = require('../const/tender').measureType;
  7. const JpcEx = require('../reports/rpt_component/jpc_ex');
  8. const JV = require('../reports/rpt_component/jpc_value_define');
  9. const rptDataExtractor = require('../reports/util/rpt_calculation_data_util');
  10. const fsUtil = require('../public/js/fsUtil');
  11. module.exports = app => {
  12. class ReportController extends app.BaseController {
  13. /**
  14. * 报表显示页面
  15. *
  16. * @param {Object} ctx - egg全局context
  17. * @return {void}
  18. */
  19. async index(ctx) {
  20. try {
  21. const tender = ctx.tender;
  22. const stage = ctx.stage;
  23. // console.log(tender.data);
  24. // console.log(tender.data.project_id);
  25. let stage_id = -1;
  26. let stage_order = -1;
  27. let stage_times = -1;
  28. const treeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1, tender.data.project_id]);
  29. const custCfg = await ctx.service.rptCustomizeCfg.getCustomizeCfgByUserId('Administrator');
  30. const stageList = await ctx.service.stage.getValidStageShort(tender.id);
  31. // console.log(maxStageAmt[0].maxAmt);
  32. if (stage !== null && stage !== undefined) {
  33. stage_id = stage.id;
  34. stage_order = stage.order;
  35. stage_times = stage.times;
  36. }
  37. const renderData = {
  38. tender: tender.data,
  39. tenderInfo: tender.info,
  40. rpt_tpl_data: JSON.stringify(treeNodes),
  41. cust_cfg: JSON.stringify(custCfg),
  42. project_id: tender.data.project_id,
  43. tender_id: tender.id,
  44. stg_id: stage_id,
  45. stg_order: stage_order,
  46. stg_times: stage_times,
  47. stage_list: JSON.stringify(stageList),
  48. tenderMenu,
  49. preUrl: '/tender/' + tender.id,
  50. measureType,
  51. // jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.ledger.explode),
  52. };
  53. // await this.layout('report/index.ejs', renderData, 'ledger/audit_modal.ejs');
  54. await this.layout('report/index.ejs', renderData);
  55. } catch (err) {
  56. this.log(err);
  57. console.log(err);
  58. // ctx.redirect('/tender/' + ctx.tender.id);
  59. }
  60. }
  61. /**
  62. * 获取报表数据
  63. *
  64. * @param {Object} ctx - egg全局context
  65. * @return {void}
  66. */
  67. async getReport(ctx) {
  68. try {
  69. // console.log('in getReport');
  70. const params = JSON.parse(ctx.request.body.params);
  71. // console.log(params);
  72. let rptTpl = await ctx.service.rptTpl.getTplById(params.rpt_tpl_id);
  73. if (!rptTpl || rptTpl.length !== 1) {
  74. throw '获取模板失败';
  75. }
  76. rptTpl = JSON.parse(rptTpl[0].rpt_content);
  77. // console.log('get the template!');
  78. // test
  79. const pageRst = await getAllPagesCommon(ctx, rptTpl, params, JV.PAGING_OPTION_NORMAL, JV.OUTPUT_TYPE_NORMAL);
  80. // console.log(pageRst);
  81. /*
  82. // --------------------------
  83. const printCom = JpcEx.createNew();
  84. const defProperties = await ctx.service.rptPreDefineCfg.getCfgById('Administrator');
  85. // console.log(rptTpl);
  86. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  87. printCom.initialize(rptTpl);
  88. // console.log('initialize the template!');
  89. const pageRst = printCom.outputAsPreviewPage(rptTpl, JSON.parse(defProperties[0].defined_content));
  90. // console.log(pageRst);
  91. */
  92. ctx.body = { data: pageRst };
  93. // ctx.body = { data: { msg: 'test the network' } };
  94. ctx.status = 201;
  95. } catch (ex) {
  96. this.setMessage(ex.toString(), this.messageType.ERROR);
  97. }
  98. }
  99. }
  100. return ReportController;
  101. };
  102. async function getReportData(ctx, params, filters) {
  103. const rst = {};
  104. const runnableRst = [];
  105. const runnableKey = []; // 这个配合runnableRst用,未来考虑并行查询优化
  106. for (const filter of filters) {
  107. switch (filter) {
  108. case 'project' :
  109. runnableRst.push(ctx.service.project.getProjectById(params.project_id));
  110. runnableKey.push('project');
  111. break;
  112. case 'tender_info' :
  113. runnableRst.push(ctx.service.tenderInfo.getTenderInfo(params.tender_id));
  114. runnableKey.push('tender_info');
  115. break;
  116. case 'ledger' :
  117. runnableRst.push(ctx.service.ledger.getDataByTenderId(params.tender_id, 0));
  118. runnableKey.push('ledger');
  119. break;
  120. case 'stage_bills':
  121. runnableRst.push(ctx.service.stageBills.getLastestStageData(params.tender_id, params.stage_id));
  122. runnableKey.push('stage_bills');
  123. break;
  124. case 'stage_bills_final':
  125. runnableRst.push(ctx.service.stageBillsFinal.getFinalDataEx(params.tender_id, params.stage_order));
  126. runnableKey.push('stage_bills_final');
  127. break;
  128. case 'stage':
  129. // runnableRst.push(ctx.service.stageBillsFinal.getFinalDataEx(params.tender_id, params.stage_order));
  130. // runnableKey.push('pay');
  131. break;
  132. case 'stage_pay':
  133. // runnableRst.push(ctx.service.stageBillsFinal.getFinalDataEx(params.tender_id, params.stage_order));
  134. // runnableKey.push('stage_pay');
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. const queryRst = await Promise.all(runnableRst);
  141. for (let idx = 0; idx < runnableKey.length; idx++) {
  142. rst[runnableKey[idx]] = queryRst[idx];
  143. // console.log(runnableKey[idx]);
  144. // if (rst[runnableKey[idx]] instanceof Array) console.log('is Array')
  145. // else console.log('is not Array');
  146. }
  147. return rst;
  148. }
  149. async function getAllPagesCommon(ctx, rptTpl, params, option, outputType) {
  150. // let rptTpl = null;
  151. const rptDataUtil = new rptDataExtractor();
  152. rptDataUtil.initialize(rptTpl);
  153. // console.log(rptTpl);
  154. const filter = rptDataUtil.getDataRequestFilter();
  155. // console.log(filter.tables);
  156. const rawDataObj = await getReportData(ctx, params, filter.tables);
  157. // console.log(rawDataObj);
  158. try {
  159. // console.log('before assemble');
  160. const tplData = rptDataUtil.assembleData(rawDataObj);
  161. // console.log(tplData);
  162. const printCom = JpcEx.createNew();
  163. if (params.pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = params.pageSize;
  164. if (params.orientation && (params.orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = params.orientation;
  165. let defProperties = await ctx.service.rptPreDefineCfg.getCfgById('Administrator');
  166. // console.log('defProperties: ');
  167. // console.log(defProperties[0].defined_content);
  168. defProperties = JSON.parse(defProperties[0].defined_content);
  169. if (params.custCfg) {
  170. setupCustomizeCfg(params.custCfg, rptTpl, defProperties);
  171. } else {
  172. // setupCustomizeCfg(defProperties, rptTpl, defProperties);
  173. }
  174. const dftOption = params.option || JV.PAGING_OPTION_NORMAL;
  175. printCom.initialize(rptTpl);
  176. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, outputType);
  177. const maxPages = printCom.totalPages;
  178. let pageRst = null;
  179. if (maxPages > 0) {
  180. pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, params.custCfg);
  181. } else {
  182. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  183. }
  184. if (pageRst) {
  185. // fsUtil.writeObjToFile(pageRst, 'D:/GitHome/temp/testBuiltPageResult.jsp');
  186. } else {
  187. //
  188. }
  189. // console.log(pageRst);
  190. return pageRst;
  191. } catch (ex) {
  192. // console.log("报表数据异常: userId " + user_id + ", project id: " + prj_id);
  193. console.log(ex);
  194. }
  195. }
  196. function setupCustomizeCfg(customizeCfg, rptTpl, defProperties) {
  197. const tmpObj = {};
  198. // 1. 字体
  199. const newFonts = [];
  200. for (const font of defProperties.fonts) {
  201. const copyFont = {};
  202. copyFont.ID = font.ID;
  203. for (const fontProp of JV.FONT_PROPS) {
  204. copyFont[fontProp] = font[fontProp];
  205. }
  206. newFonts.push(copyFont);
  207. tmpObj[font.ID] = copyFont;
  208. }
  209. const private_setup_font = function(propStr, newFont) {
  210. if (tmpObj[propStr]) {
  211. tmpObj[propStr].Name = newFont.Name;
  212. tmpObj[propStr].FontHeight = String(newFont.FontHeight);
  213. tmpObj[propStr].FontBold = newFont.FontBold;
  214. tmpObj[propStr].FontItalic = newFont.FontItalic;
  215. tmpObj[propStr].FontUnderline = newFont.FontUnderline;
  216. }
  217. };
  218. for (const custFont of customizeCfg.fonts) {
  219. switch (custFont.CfgDispName) {
  220. case '表标题':
  221. private_setup_font('ReportTitle_Main', custFont);
  222. break;
  223. case '列标题':
  224. private_setup_font('HeaderColumn', custFont);
  225. private_setup_font('FooterColumn', custFont);
  226. break;
  227. case '正文内容':
  228. private_setup_font('Content', custFont);
  229. break;
  230. case '合计':
  231. private_setup_font('GrandTotal', custFont);
  232. private_setup_font('SectionTotal', custFont);
  233. break;
  234. case '表眉/表脚':
  235. private_setup_font('Header', custFont);
  236. private_setup_font('Footer', custFont);
  237. break;
  238. default:
  239. break;
  240. }
  241. }
  242. // 1.1 窄体
  243. if (tmpObj.Content_Narrow) {
  244. if (customizeCfg.isNarrow) {
  245. tmpObj.Content_Narrow.Name = 'Arial Narrow';
  246. } else {
  247. if (tmpObj.Content) {
  248. tmpObj.Content_Narrow.Name = tmpObj.Content.Name;
  249. } else {
  250. tmpObj.Content_Narrow.Name = '宋体';
  251. }
  252. }
  253. }
  254. defProperties.fonts = newFonts;
  255. // 2. 页边距
  256. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT] = customizeCfg.margins[JV.PROP_LEFT] / 10;
  257. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT] = customizeCfg.margins[JV.PROP_RIGHT] / 10;
  258. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP] = customizeCfg.margins[JV.PROP_TOP] / 10;
  259. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM] = customizeCfg.margins[JV.PROP_BOTTOM] / 10;
  260. // 3. 边框竖线
  261. if (!(customizeCfg.showVerticalLine)) {
  262. const private_copy_border = function(src) {
  263. const rst = {};
  264. rst.Position = src.Position;
  265. rst.LineWeight = src.LineWeight;
  266. rst.DashStyle = src.DashStyle;
  267. rst.Color = src.Color;
  268. return rst;
  269. };
  270. const newStyles = [];
  271. for (let i = 0; i < defProperties.styles.length; i++) {
  272. const style = defProperties.styles[i];
  273. newStyles.push(style);
  274. if (style.ID === 'BORDER_ALL_AROUND') {
  275. const newStyle = {};
  276. newStyle.ID = style.ID;
  277. newStyle.CfgDispName = style.CfgDispName;
  278. newStyle.border_style = [];
  279. for (const border of style.border_style) {
  280. const newBorder = private_copy_border(border);
  281. newStyle.border_style.push(newBorder);
  282. if (border.Position === 'Left' || border.Position === 'Right') {
  283. newBorder.LineWeight = 0;
  284. }
  285. }
  286. newStyles[newStyles.length - 1] = newStyle;
  287. }
  288. }
  289. defProperties.styles = newStyles;
  290. }
  291. // 4. 补0
  292. const private_Setup_Format = function(tabFields) {
  293. if (tabFields) {
  294. for (const tabField of tabFields) {
  295. if (tabField[JV.PROP_FORMAT]) {
  296. tabField[JV.PROP_FORMAT] = tabField[JV.PROP_FORMAT].replace(new RegExp('#', 'gm'), '0');
  297. }
  298. }
  299. }
  300. };
  301. if (customizeCfg.fillZero) {
  302. if (rptTpl[JV.NODE_FLOW_INFO]) {
  303. 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]);
  304. 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]);
  305. 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]);
  306. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_GROUP] && rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES][JV.PROP_SUM_FIELDS]);
  307. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO]) {
  308. for (const discrete of rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO]) {
  309. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  310. }
  311. }
  312. if (rptTpl[JV.NODE_FLOW_INFO_EX]) {
  313. 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]);
  314. 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]);
  315. 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]);
  316. 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]);
  317. 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]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES][JV.PROP_SUM_FIELDS]);
  318. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_DISCRETE_INFO]) {
  319. for (const discrete of rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_DISCRETE_INFO]) {
  320. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  321. }
  322. }
  323. }
  324. } else if (rptTpl[JV.NODE_BILL_INFO]) {
  325. 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]);
  326. if (rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO]) {
  327. for (const discrete of rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO]) {
  328. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  329. }
  330. }
  331. } else if (rptTpl[JV.NODE_CROSS_INFO]) {
  332. 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]);
  333. 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]);
  334. 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]);
  335. 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]);
  336. 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]);
  337. 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]);
  338. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO]) {
  339. for (const discrete of rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO]) {
  340. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  341. }
  342. }
  343. }
  344. }
  345. }