report_controller.js 17 KB

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