report_controller.js 17 KB

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