rpt_calculation_data_util.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2019/6/18.
  4. * 计量报表数据提取class,是协助报表模板里指标字段自主提取数据的工具类
  5. */
  6. const JV = require('../rpt_component/jpc_value_define');
  7. const $JE = require('../rpt_component/jpc_rte');
  8. const fs = require('fs');
  9. const fsUtil = require('../../public/js/fsUtil');
  10. const stringUtil = require('../../public/js/stringUtil');
  11. const scMathUtil = require('../../public/js/scMathUtil');
  12. const _ = require('lodash');
  13. const treeUtil = require('../public/treeUtil');
  14. const RELATION_TABLES_KEYS = {
  15. project: { main_key: 'id' },
  16. tender: { main_key: 'id', foreign_key: { project: 'project_id' } },
  17. tender_info: { main_key: 'id', foreign_key: { project: 'pid', tender: 'tid' } },
  18. ledger: { main_key: 'id', isTree: true, tree_pid: 'ledger_pid', tree_id: 'ledger_id', foreign_key: { tender: 'tender_id' } },
  19. };
  20. class Rpt_Common {
  21. initialize(rpt_tpl, currentDataObj) {
  22. this.template = rpt_tpl;
  23. this.currentDataObj = currentDataObj;
  24. }
  25. Multiply(val1, val2, fixFormat) {
  26. const rst = [];
  27. let maxLen = val1.length;
  28. let minLen = val2.length;
  29. if (minLen > maxLen) {
  30. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  31. }
  32. for (let i = 0; i < maxLen; i++) {
  33. let value = ((i < val1.length) ? val1[i] : val1[minLen - 1]) * ((i < val2.length) ? val2[i] : val2[minLen - 1]);
  34. if (value === null || value === undefined) {
  35. value = '0';
  36. }
  37. if (fixFormat) value = value.toFixed(fixFormat);
  38. rst.push(value);
  39. }
  40. return rst;
  41. }
  42. Divide(val1, val2, fixFormat) {
  43. const rst = [];
  44. let maxLen = val1.length;
  45. let minLen = val2.length;
  46. if (minLen > maxLen) {
  47. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  48. }
  49. for (let i = 0; i < maxLen; i++) {
  50. let value = ((i < val1.length) ? val1[i] : val1[minLen - 1]) / ((i < val2.length) ? val2[i] : val2[minLen - 1]);
  51. if (fixFormat) value = value.toFixed(fixFormat);
  52. rst.push(value);
  53. }
  54. return rst;
  55. }
  56. Plus(val1, val2, fixFormat) {
  57. const rst = [];
  58. let maxLen = val1.length;
  59. let minLen = val2.length;
  60. if (minLen > maxLen) {
  61. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  62. }
  63. for (let i = 0; i < maxLen; i++) {
  64. let value = ((i < val1.length) ? val1[i] : val1[minLen - 1]) + ((i < val2.length) ? val2[i] : val2[minLen - 1]);
  65. if (fixFormat) value = value.toFixed(fixFormat);
  66. rst.push(value);
  67. }
  68. return rst;
  69. }
  70. MultiPlus(arrVal, fixFormat) {
  71. const rst = [];
  72. for (let i = 0; i < arrVal.length; i++) {
  73. const valItem = arrVal[i];
  74. if (i === 0) {
  75. for (const dtl of valItem) {
  76. let value = parseFloat(dtl);
  77. if (fixFormat) value = value.toFixed(fixFormat);
  78. rst.push(value);
  79. }
  80. } else {
  81. for (let j = 0; j < valItem.length; j++) {
  82. if (j < rst.length) {
  83. let value = rst[j] + valItem[j];
  84. if (fixFormat) value = value.toFixed(fixFormat);
  85. rst[j] = value;
  86. } else {
  87. let value = parseFloat(valItem[j]);
  88. if (fixFormat) value = value.toFixed(fixFormat);
  89. rst.push(value);
  90. }
  91. }
  92. }
  93. }
  94. return rst;
  95. }
  96. Minus(val1, val2, fixFormat) {
  97. const rst = [];
  98. let maxLen = val1.length;
  99. let minLen = val2.length;
  100. if (minLen > maxLen) {
  101. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  102. }
  103. for (let i = 0; i < maxLen; i++) {
  104. let value = ((i < val1.length) ? val1[i] : val1[minLen - 1]) - ((i < val2.length) ? val2[i] : val2[minLen - 1]);
  105. if (fixFormat) value = value.toFixed(fixFormat);
  106. rst.push(value);
  107. }
  108. return rst;
  109. }
  110. FormatString(arrVal, formatStr) {
  111. const rst = [];
  112. for (const val of arrVal) {
  113. rst.push(stringUtil.replaceAll(formatStr, '%S', val));
  114. }
  115. return rst;
  116. }
  117. }
  118. class Rpt_Data_Extractor {
  119. constructor() {
  120. this.COMMON = new Rpt_Common();
  121. }
  122. initialize(tpl) {
  123. this.rptTpl = tpl;
  124. }
  125. // 因为计量是采用传统的关系数据库(MySQL), 有一些表关系是需要先约定(如上下级关系,外键关系等)
  126. // 根据报表模板映射指标(非离散指标)的定义,罗列出所有需要用到的data对象key,作为数据请求的过滤依据
  127. getDataRequestFilter() {
  128. const rst = {};
  129. const tables = []; // 记录需要查询哪些表
  130. // const filters = []; // 记录过滤条件(预处理中的过滤),目前先不处理
  131. const tpl = this.rptTpl;
  132. const pri_func_chk_filter = function(field) {
  133. // 计量的机制有所不同,数据分开保存在不同的表里,
  134. if (field.TableName && tables.indexOf(field.TableName) < 0) {
  135. tables.push(field.TableName);
  136. }
  137. };
  138. const pri_setup_filter = function(FIELD_LIST_KEY) {
  139. // console.log(tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]);
  140. if (tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]) {
  141. for (const field of tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]) {
  142. pri_func_chk_filter(field);
  143. }
  144. }
  145. };
  146. pri_setup_filter(JV.NODE_DISCRETE_FIELDS);
  147. pri_setup_filter(JV.NODE_MASTER_FIELDS);
  148. pri_setup_filter(JV.NODE_DETAIL_FIELDS);
  149. pri_setup_filter(JV.NODE_MASTER_FIELDS_EX);
  150. pri_setup_filter(JV.NODE_DETAIL_FIELDS_EX);
  151. if (tpl[JV.NODE_MAP_DATA_HANDLE_INFO] && tpl[JV.NODE_MAP_DATA_HANDLE_INFO].length > 0) {
  152. // 计量的预处理会有所变化,没有那么多方式,这里只记录过滤
  153. for (const preHandle of tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  154. if (preHandle[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_FILTER) {
  155. if (tables.indexOf(preHandle[JV.PROP_DATA_KEY]) < 0) {
  156. tables.push(preHandle[JV.PROP_DATA_KEY]);
  157. }
  158. // 目前还没想好有哪些过滤,骑驴找马吧
  159. }
  160. }
  161. }
  162. rst.tables = tables;
  163. return rst;
  164. }
  165. // 装配数据(把收集到的数据,依据报表模板的指示,预处理(如:排序、过滤、合计)及装配到相关指标)
  166. assembleData(rawDataObj, baseDir) {
  167. const $PROJECT = { REPORT: {} };
  168. const tpl = this.rptTpl;
  169. this.COMMON.initialize(tpl, rawDataObj);
  170. $PROJECT.COMMON = this.COMMON;
  171. // console.log(rawDataObj);
  172. setupFunc($PROJECT.REPORT, rawDataObj, baseDir);
  173. if (tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  174. for (const preHandle of tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  175. const srcData = getModuleDataByKey(rawDataObj.prjData, preHandle[JV.PROP_DATA_KEY]);
  176. switch (preHandle[JV.PROP_HANDLE_TYPE]) {
  177. case JV.PROP_HANDLE_TYPE_SORT:
  178. sortData(srcData, preHandle, rawDataObj.prjData);
  179. break;
  180. case JV.PROP_HANDLE_TYPE_FILTER:
  181. filterData(srcData, preHandle, rawDataObj.prjData);
  182. break;
  183. case JV.PROP_HANDLE_TYPE_ADJUST:
  184. adjustData(srcData, preHandle);
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. }
  191. const rptDataObj = {};
  192. rptDataObj[JV.DATA_DISCRETE_DATA] = [];
  193. rptDataObj[JV.DATA_MASTER_DATA] = [];
  194. rptDataObj[JV.DATA_DETAIL_DATA] = [];
  195. rptDataObj[JV.DATA_MASTER_DATA_EX] = [];
  196. rptDataObj[JV.DATA_DETAIL_DATA_EX] = [];
  197. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS], rptDataObj[JV.DATA_DISCRETE_DATA], $PROJECT);
  198. // console.log(JV.DATA_DISCRETE_DATA);
  199. // console.log(rptDataObj[JV.DATA_DISCRETE_DATA]);
  200. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS], rptDataObj[JV.DATA_MASTER_DATA], $PROJECT);
  201. // console.log(JV.DATA_MASTER_DATA);
  202. // console.log(rptDataObj[JV.DATA_MASTER_DATA]);
  203. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS], rptDataObj[JV.DATA_DETAIL_DATA], $PROJECT);
  204. // console.log(JV.DATA_DETAIL_DATA);
  205. // console.log(rptDataObj[JV.DATA_DETAIL_DATA]);
  206. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX], rptDataObj[JV.DATA_MASTER_DATA_EX], $PROJECT);
  207. // console.log(JV.DATA_MASTER_DATA_EX);
  208. // console.log(rptDataObj[JV.DATA_MASTER_DATA_EX]);
  209. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX], rptDataObj[JV.DATA_DETAIL_DATA_EX], $PROJECT);
  210. // console.log(JV.DATA_DETAIL_DATA_EX);
  211. // console.log(rptDataObj[JV.DATA_DETAIL_DATA_EX]);
  212. // fsUtil.writeObjToFile(rptDataObj, 'D:/GitHome/temp/insertedOriginalData.jsp');
  213. // fsUtil.writeObjToFile(rawDataObj, 'D:/GitHome/temp/insertedRawDataData.jsp');
  214. // fsUtil.writeObjToFile($PROJECT, 'D:/GitHome/temp/$PROJECTData.jsp');
  215. // fsUtil.writeObjToFile(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS], 'D:/GitHome/temp/masterFieldsAfterAssemble.jsp');
  216. // fsUtil.writeObjToFile(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS], 'D:/GitHome/temp/detailFieldsAfterAssemble.jsp');
  217. // fsUtil.writeObjToFile(tpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS], 'D:/GitHome/temp/discreteFieldsAfterAssemble.jsp');
  218. return rptDataObj;
  219. }
  220. }
  221. function getModuleDataByKey(prjData, key) {
  222. let rst = null;
  223. if (prjData) {
  224. for (const item of prjData) {
  225. if (item.moduleName === key) {
  226. rst = item;
  227. break;
  228. }
  229. }
  230. }
  231. return rst;
  232. }
  233. function filterData(sourceData, handleCfg, prjData) {
  234. let rstArr = [];
  235. const tempRstArr = [];
  236. for (const item of getActDataArr(sourceData)) {
  237. if (item._doc) {
  238. tempRstArr.push(item._doc);
  239. } else {
  240. tempRstArr.push(item);
  241. }
  242. }
  243. const private_chkVal = function(src, compVal, compStr) {
  244. let rst = true;
  245. switch (compStr) {
  246. case '==' :
  247. rst = (src == compVal);
  248. break;
  249. case '===' :
  250. rst = (src === compVal);
  251. break;
  252. case '>' :
  253. rst = (src > compVal);
  254. break;
  255. case '>=' :
  256. rst = (src >= compVal);
  257. break;
  258. case '<' :
  259. rst = (src < compVal);
  260. break;
  261. case '<=' :
  262. rst = (src <= compVal);
  263. break;
  264. case '!=' :
  265. rst = (src != compVal);
  266. break;
  267. case '!==' :
  268. rst = (src !== compVal);
  269. break;
  270. case 'in' :
  271. if (compVal instanceof Array) {
  272. rst = compVal.indexOf(src) >= 0;
  273. } else {
  274. // string,需要转类型
  275. const newInCv = JSON.parse(compVal);
  276. if (newInCv instanceof Array) {
  277. rst = newInCv.indexOf(src) >= 0;
  278. } else {
  279. rst = false;
  280. }
  281. }
  282. break;
  283. case 'not in':
  284. if (compVal instanceof Array) {
  285. rst = compVal.indexOf(src) < 0;
  286. } else {
  287. // string,需要转类型
  288. const newNotInCv = JSON.parse(compVal);
  289. if (newNotInCv instanceof Array) {
  290. rst = (newNotInCv.indexOf(src) < 0);
  291. } else {
  292. rst = true;
  293. }
  294. }
  295. break;
  296. default:
  297. rst = true;
  298. }
  299. return rst;
  300. };
  301. const private_chkArrVal = function(arr, key, compVal, compStr) {
  302. let rst = false;
  303. if (arr.length > 0) {
  304. for (const arrItem of arr) {
  305. if (arrItem[key] !== undefined) {
  306. // 可以为null值去判断
  307. rst = private_chkVal(arrItem[key], compVal, compStr);
  308. }
  309. if (rst) {
  310. break;
  311. }
  312. }
  313. } else {
  314. // 在某些判断条件下(含有'非'判断),如arr没有数组项,默认结果反而是true
  315. switch (compStr) {
  316. case '!=' :
  317. case '!==' :
  318. case 'not in':
  319. rst = true;
  320. break;
  321. default:
  322. break;
  323. }
  324. }
  325. return rst;
  326. };
  327. const private_filter_compare = function(item, filterCfg) {
  328. const compareObj = {};
  329. let compRst = true;
  330. let curComparePrjData = null;
  331. let startIdx = 0;
  332. const private_ref_join = function(refKey, targetDataKey, targetPropertyKey) {
  333. let rst = null;
  334. let objDataArr = null;
  335. curComparePrjData = getModuleDataByKey(prjData, targetDataKey);
  336. try {
  337. if (curComparePrjData !== null) {
  338. objDataArr = getActDataArr(curComparePrjData);
  339. for (const dtl of objDataArr) {
  340. if (item[refKey] === dtl[targetPropertyKey]) {
  341. rst = dtl;
  342. break;
  343. }
  344. }
  345. }
  346. } finally {
  347. curComparePrjData = null;
  348. }
  349. return rst;
  350. };
  351. for (const cfg of filterCfg[JV.PROP_FILTER_KEYS]) {
  352. if (cfg[JV.PROP_FILTER_COMPARE_VAL]) {
  353. // 比较key值
  354. const keys = cfg.key.split('.');
  355. if (keys.length > 1) {
  356. let lastObj = item;
  357. for (let i = 0; i < keys.length - 1; i++) {
  358. if (keys[i].indexOf('ref_join(') === 0) {
  359. const params = keys[i].slice(9, keys[i].length - 1).split(',');
  360. if (params.length === 3) {
  361. lastObj = private_ref_join(params[0], params[1], params[2]);
  362. }
  363. if (!(lastObj)) {
  364. compRst = false;
  365. break;
  366. }
  367. } else {
  368. lastObj = item[keys[i]];
  369. if (!(lastObj)) {
  370. compRst = false;
  371. break;
  372. }
  373. }
  374. }
  375. if (lastObj) {
  376. if (lastObj instanceof Array) {
  377. compRst = private_chkArrVal(lastObj, keys[keys.length - 1], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
  378. } else {
  379. compRst = private_chkVal(lastObj[keys[keys.length - 1]], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
  380. }
  381. }
  382. } else {
  383. compRst = private_chkVal(item[cfg.key], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
  384. }
  385. } else if (cfg[JV.PROP_FILTER_COMPARE_OBJ] && cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]) {
  386. // 通过其他对象来过滤
  387. if (!curComparePrjData) {
  388. curComparePrjData = getModuleDataByKey(prjData, cfg[JV.PROP_FILTER_COMPARE_OBJ]);
  389. }
  390. if (cfg[JV.PROP_FILTER_CONDITION] === 'in' || cfg[JV.PROP_FILTER_CONDITION] === 'not in') {
  391. let compareArr = null;
  392. if (!compareObj.hasOwnProperty(cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString())) {
  393. compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()] = [];
  394. compareArr = compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()];
  395. for (const data of getActDataArr(curComparePrjData)) {
  396. if (compareArr.indexOf(data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]]) < 0) {
  397. compareArr.push(data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]]);
  398. }
  399. }
  400. } else {
  401. compareArr = compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()];
  402. }
  403. compRst = private_chkVal(item[cfg.key], compareArr, cfg[JV.PROP_FILTER_CONDITION]);
  404. } else {
  405. for (const data of getActDataArr(curComparePrjData)) {
  406. compRst = private_chkVal(item[cfg.key], data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]], cfg[JV.PROP_FILTER_CONDITION]);
  407. if (compRst) break;
  408. }
  409. }
  410. }
  411. startIdx++;
  412. if (!compRst) {
  413. break; // 有不符合条件的数据则退出(这里的判断条件是and关系)
  414. }
  415. }
  416. return compRst;
  417. };
  418. const private_sub_filter_compare = function(dtlItem, subFilters) {
  419. let cmpRst = false;
  420. for (const dtlCfg of subFilters) {
  421. cmpRst = private_filter_compare(dtlItem, dtlCfg);
  422. if (cmpRst) {
  423. if (dtlCfg[JV.PROP_OTHER_SUB_FILTER] && dtlCfg[JV.PROP_OTHER_SUB_FILTER].length > 0) {
  424. cmpRst = private_sub_filter_compare(dtlItem, dtlCfg[JV.PROP_OTHER_SUB_FILTER]);
  425. if (cmpRst) break;
  426. } else {
  427. break;
  428. }
  429. }
  430. }
  431. return cmpRst;
  432. };
  433. for (const item of tempRstArr) {
  434. if (private_filter_compare(item, handleCfg)) {
  435. rstArr.push(item);
  436. }
  437. }
  438. if (handleCfg[JV.PROP_OTHER_SUB_FILTER] && handleCfg[JV.PROP_OTHER_SUB_FILTER].length > 0) {
  439. const newRstArr = [];
  440. for (const dtlItem of rstArr) {
  441. const cmpRst = private_sub_filter_compare(dtlItem, handleCfg[JV.PROP_OTHER_SUB_FILTER]);
  442. if (cmpRst) {
  443. newRstArr.push(dtlItem);
  444. }
  445. }
  446. rstArr = newRstArr;
  447. }
  448. replaceActDataArr(sourceData, rstArr);
  449. // fsUtil.writeObjToFile(sourceData.data, 'D:/GitHome/ConstructionCost/tmp/filteredRst.jsp');
  450. }
  451. function adjustData(sourceData, adjustCfg) {
  452. const rstArr = [];
  453. for (const item of getActDataArr(sourceData)) {
  454. if (item._doc) {
  455. rstArr.push(item._doc);
  456. } else {
  457. rstArr.push(item);
  458. }
  459. }
  460. for (const item of adjustCfg[JV.PROP_ADJUST_COLLECTION]) {
  461. for (const rec of rstArr) {
  462. if (item[JV.PROP_ADJUST_ACTION] === 'prefix') {
  463. rec[item.key] = item[JV.PROP_ADJUST_ACTION_VAL] + rec[item.key];
  464. } else if (item[JV.PROP_ADJUST_ACTION] === 'suffix') {
  465. rec[item.key] = rec[item.key] + item[JV.PROP_ADJUST_ACTION_VAL];
  466. }
  467. }
  468. }
  469. replaceActDataArr(sourceData, rstArr);
  470. }
  471. function sortData(sourceData, sortCfg, prjData) {
  472. let rst = getActDataArr(sourceData);
  473. const tempRstArr = [];
  474. const sortType = sortCfg[JV.PROP_SORT_TYPE];
  475. const srcData = getActDataArr(sourceData);
  476. for (const item of srcData) {
  477. if (item._doc) {
  478. tempRstArr.push(item._doc);
  479. } else {
  480. tempRstArr.push(item);
  481. }
  482. }
  483. function private_normal_sort(destArr, sortKeys) {
  484. destArr.sort(function(a, b) {
  485. let compRst = 0;
  486. for (const comp of sortKeys) {
  487. const reverse = (comp.order === 'ascend') ? 1 : (-1);
  488. //
  489. if (a[comp.key] > b[comp.key]) {
  490. compRst = reverse;
  491. break;
  492. } else if (a[comp.key] < b[comp.key]) {
  493. compRst = -reverse;
  494. break;
  495. }
  496. }
  497. return compRst;
  498. });
  499. }
  500. function private_parent_sort(parentArr, parentKeys, childArr, childKeys) {
  501. const tmpRst = {};
  502. const rst = [];
  503. for (const pItem of parentArr) {
  504. let pKey = 'key';
  505. for (const key of parentKeys) {
  506. pKey += '_' + pItem[key];
  507. }
  508. tmpRst[pKey] = [];
  509. }
  510. for (const cItem of childArr) {
  511. let cKey = 'key';
  512. for (const key of childKeys) {
  513. cKey += '_' + cItem[key];
  514. }
  515. if (tmpRst[cKey]) {
  516. tmpRst[cKey].push(cItem);
  517. } else {
  518. // unknown child value! should be filtered!
  519. }
  520. }
  521. // childArr.splice(0);
  522. for (const pItem of parentArr) {
  523. let pKey = 'key';
  524. for (const key of parentKeys) {
  525. pKey += '_' + pItem[key];
  526. }
  527. rst.push(tmpRst[pKey]);
  528. // for (let rItem of tmpRst[pKey]) {
  529. // childArr.push(rItem);
  530. // }
  531. }
  532. return rst;
  533. }
  534. switch (sortType) {
  535. case 'tree':
  536. const addLevel = true;
  537. rst = treeUtil.buildTreeNodeDirectly(tempRstArr, addLevel);
  538. let newTopArr = [];
  539. if ((sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES] && sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].length > 0) ||
  540. (sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES] && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].length > 0)) {
  541. const local_check_bills = function(tItem) {
  542. let chkDtl = false;
  543. if (tItem.flags && tItem.flags.length > 0) {
  544. for (const flagItem of tItem.flags) {
  545. if (sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].indexOf(flagItem.flag) >= 0) {
  546. newTopArr.push(tItem);
  547. chkDtl = true;
  548. break;
  549. }
  550. }
  551. }
  552. if (!chkDtl && tItem.items && tItem.items.length > 0) {
  553. for (const dtlItem of tItem.items) {
  554. local_check_bills(dtlItem);
  555. }
  556. }
  557. };
  558. for (const topItem of rst) {
  559. let chkTop = false;
  560. if (topItem.flags && topItem.flags.length > 0) {
  561. for (const flagItem of topItem.flags) {
  562. if (sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].indexOf(flagItem.flag) >= 0) {
  563. newTopArr.push(topItem);
  564. chkTop = true;
  565. break;
  566. }
  567. }
  568. }
  569. if (!chkTop && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES] && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].length > 0) {
  570. local_check_bills(topItem);
  571. }
  572. }
  573. } else {
  574. newTopArr = rst;
  575. }
  576. const destArr = [];
  577. // fsUtil.writeObjToFile(newTopArr, 'D:/GitHome/ConstructionCost/tmp/sortedAndFlattedRstBefore.jsp');
  578. treeUtil.getFlatArray(newTopArr, destArr, true);
  579. // console.log(destArr);
  580. replaceActDataArr(sourceData, destArr);
  581. // fsUtil.writeObjToFile(sourceData.data, 'D:/GitHome/ConstructionCost/tmp/sortedAndFlattedRst.jsp');
  582. break;
  583. case 'normal':
  584. private_normal_sort(tempRstArr, sortCfg[JV.PROP_SORT_KEYS]);
  585. replaceActDataArr(sourceData, tempRstArr);
  586. // fsUtil.writeObjToFile(sourceData.data, 'D:/GitHome/ConstructionCost/tmp/normalSortedRst.jsp');
  587. break;
  588. case 'accord_to_parent':
  589. const pcKey = sortCfg[JV.PROP_PARENT_CHILD_SORT_KEY];
  590. const parentSrcData = getModuleDataByKey(prjData, pcKey[JV.PROP_PARENT_DATA_KEY]);
  591. if (parentSrcData) {
  592. const tempParentArr = [];
  593. for (const item of getActDataArr(parentSrcData)) {
  594. if (item._doc) {
  595. tempParentArr.push(item._doc);
  596. } else {
  597. tempParentArr.push(item);
  598. }
  599. }
  600. const sortedRstArr = private_parent_sort(tempParentArr, pcKey[JV.PROP_PARENT_SORT_KEYS], tempRstArr, pcKey[JV.PROP_CHILD_SORT_KEYS]);
  601. if (sortCfg[JV.PROP_OTHER_SUB_SORT] && sortCfg[JV.PROP_OTHER_SUB_SORT].length > 0) {
  602. for (const sort of sortCfg[JV.PROP_OTHER_SUB_SORT]) {
  603. if (sort[JV.PROP_SORT_TYPE] === 'normal') {
  604. for (const subArr of sortedRstArr) {
  605. private_normal_sort(subArr, sort[JV.PROP_SORT_KEYS]);
  606. }
  607. } else if (sort[JV.PROP_SORT_TYPE] === 'self_define') {
  608. for (const subArr of sortedRstArr) {
  609. // console.log(subArr);
  610. const selfDefFunc = null;
  611. eval('selfDefFunc = ' + sort[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  612. subArr.sort(selfDefFunc);
  613. // console.log(subArr);
  614. }
  615. }
  616. }
  617. }
  618. tempRstArr.splice(0);
  619. for (const item of sortedRstArr) {
  620. for (const subItem of item) {
  621. tempRstArr.push(subItem);
  622. }
  623. }
  624. }
  625. replaceActDataArr(sourceData, tempRstArr);
  626. break;
  627. case 'self_define':
  628. if (sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]) {
  629. let selfDefFuncA = null;
  630. eval('selfDefFuncA = ' + sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  631. if (selfDefFuncA !== null) {
  632. tempRstArr.sort(selfDefFuncA);
  633. } else {
  634. console.log('sorting function is null!!!');
  635. }
  636. }
  637. replaceActDataArr(sourceData, tempRstArr);
  638. break;
  639. default:
  640. //
  641. }
  642. return rst;
  643. }
  644. function setupFunc(obj, ownRawObj, baseDir) {
  645. obj.myOwnRawDataObj = ownRawObj;
  646. obj.baseDir = baseDir;
  647. obj.getProperty = ext_getProperty;
  648. obj.getSplitProperty = ext_getSplitProperty;
  649. obj.getPicProperty = ext_getPicProperty;
  650. // obj.getPropertyByForeignId = ext_getPropertyByForeignId;
  651. obj.getArrayProperty = ext_getArrayValues;
  652. obj.getBlank = ext_getBlank;
  653. }
  654. function assembleFields(fieldList, rstDataArr, $PROJECT) {
  655. if (fieldList) {
  656. for (const field of fieldList) {
  657. shielded_exec_env($PROJECT, field, rstDataArr);
  658. if ('Precision' in field) {
  659. if (field.Precision.type === 'fixed') {
  660. const vrst = eval(field.Precision.fixedMapExpression);
  661. if (vrst && vrst.length === 1) {
  662. field.fixedPrecisionNum = vrst[0];
  663. vrst.splice(0,1);
  664. }
  665. } else if (field.Precision.type === 'flexible') {
  666. const vrst = eval(field.Precision.flexibleMapExpression);
  667. if (vrst && vrst.length === 1) {
  668. field.flexiblePrecisionRefObj = vrst[0];
  669. vrst.splice(0, 1);
  670. }
  671. }
  672. }
  673. }
  674. }
  675. }
  676. function shielded_exec_env($PROJECT, $ME, rptDataItemObj) {
  677. if ($ME[JV.PROP_FIELD_EXP_MAP]) {
  678. rptDataItemObj.push(eval($ME[JV.PROP_FIELD_EXP_MAP]));
  679. }
  680. }
  681. function getActPropertyVal(firstPropKey, secPropKey, orgObj) {
  682. let rst = null;
  683. if (orgObj[firstPropKey]) {
  684. rst = orgObj[firstPropKey];
  685. } else if (orgObj[secPropKey]) {
  686. rst = orgObj[secPropKey];
  687. }
  688. return rst;
  689. }
  690. function getDeepProperty(propKey, orgObj, destArr) {
  691. const keys = propKey.split('.');
  692. const dftPropKey = 'key';
  693. const dftPropVal = 'value';
  694. const secDftPropVal = 'items';
  695. let parent = orgObj;
  696. let lastVal = null;
  697. for (const key of keys) {
  698. if (parent instanceof Array) {
  699. for (const item of parent) {
  700. if (item[dftPropKey] === key) {
  701. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  702. break;
  703. }
  704. }
  705. } else {
  706. lastVal = null;
  707. if (parent[key] !== undefined) {
  708. lastVal = parent[key];
  709. } else if (parent[secDftPropVal]) {
  710. for (const item of parent[secDftPropVal]) {
  711. if (item[dftPropKey] === key) {
  712. // lastVal = item[dftPropVal];
  713. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  714. break;
  715. }
  716. }
  717. }
  718. }
  719. parent = lastVal;
  720. if (parent === null) break;
  721. }
  722. if (destArr && destArr instanceof Array) {
  723. destArr.push(lastVal);
  724. }
  725. }
  726. function ext_getProperty(dataKey, propKey) {
  727. const rst = [];
  728. const parentObj = this;
  729. const dtObj = parentObj.myOwnRawDataObj[dataKey];
  730. // console.log('dataKey: ' + dataKey);
  731. // console.log(dtObj);
  732. if (propKey && dtObj) {
  733. // console.log('---- dtObj[' + dataKey + '] ----');
  734. // console.log(dtObj[dataKey]);
  735. const da = getActDataArr(dtObj);
  736. // console.log(da);
  737. for (const dItem of da) {
  738. // const doc = (dItem._doc === null || dItem._doc === undefined) ? dItem : dItem._doc;
  739. pri_push_property(propKey, dItem, rst);
  740. }
  741. }
  742. // console.log('---- result ----');
  743. // console.log(rst);
  744. return rst;
  745. }
  746. function ext_getSplitProperty(dataKey, propKey, splitChar, index, dftValue) {
  747. const rst = [];
  748. const parentObj = this;
  749. const dtObj = parentObj.myOwnRawDataObj[dataKey];
  750. if (propKey && dtObj) {
  751. const da = getActDataArr(dtObj);
  752. for (const dItem of da) {
  753. pri_push_property(propKey, dItem, rst);
  754. }
  755. }
  756. // const rst = ext_getProperty(dataKey, propKey);
  757. for (let idx = 0; idx < rst.length; idx++) {
  758. if (typeof rst[idx] === 'string') {
  759. const splitArr = rst[idx].split(splitChar);
  760. if (splitArr.length > index) {
  761. rst[idx] = splitArr[index];
  762. } else {
  763. rst[idx] = dftValue;
  764. }
  765. }
  766. }
  767. return rst;
  768. }
  769. async function ext_getPicProperty(dataKey, propKey, isPath) {
  770. const rst = [];
  771. const parentObj = this;
  772. const dtObj = parentObj.myOwnRawDataObj[dataKey];
  773. if (propKey && dtObj) {
  774. const da = getActDataArr(dtObj);
  775. for (const dItem of da) {
  776. pri_push_property(propKey, dItem, rst);
  777. }
  778. if (isPath) {
  779. for (let picIdx = 0; picIdx < rst.length; picIdx++) {
  780. if (rst[picIdx] !== undefined && rst[picIdx] !== null && rst[picIdx] !== '') {
  781. const filePath = parentObj.baseDir + '/app/' + rst[picIdx];
  782. try {
  783. fs.accessSync(filePath, fs.constants.R_OK);
  784. const bData = fs.readFileSync(filePath);
  785. const base64Str = bData.toString('base64');
  786. const datauri = 'data:image/png;base64,' + base64Str;
  787. rst[picIdx] = datauri;
  788. // const res = await isFileExisted(filePath);
  789. // if (res) {
  790. // const bData = fs.readFileSync(filePath);
  791. // const base64Str = bData.toString('base64');
  792. // const datauri = 'data:image/png;base64,' + base64Str;
  793. // rst[picIdx] = datauri;
  794. // } else {
  795. // rst[picIdx] = ''; // 不存在图片,则设置为空,后续的方法就不会当作图片来处理了
  796. // }
  797. } catch (err) {
  798. rst[picIdx] = ''; // 不存在图片,则设置为空,后续的方法就不会当作图片来处理了
  799. console.error(err);
  800. }
  801. }
  802. }
  803. } else {
  804. // 不是路径,那么必须是image data,不用处理,正常走即可
  805. }
  806. }
  807. return rst;
  808. }
  809. function ext_getArrayValues(dataKey, itemKey) {
  810. const rst = [];
  811. const parentObj = this;
  812. const dtObj = parentObj.myOwnRawDataObj[dataKey];
  813. // 计量需要重写
  814. const keysArr = itemKey.split('.');
  815. const da = getActDataArr(dtObj);
  816. // console.log(keysArr);
  817. for (const dataItem of da) {
  818. let itemArr = [];
  819. if (keysArr.length <= 2) {
  820. if (dataItem[keysArr[0]] instanceof Array) {
  821. if (keysArr.length === 2) {
  822. for (const item of dataItem[keysArr[0]]) {
  823. itemArr.push(item[keysArr[1]]);
  824. }
  825. } else {
  826. itemArr = itemArr.concat(dataItem[keysArr[0]]);
  827. }
  828. } else {
  829. if (keysArr.length === 2) {
  830. const subProperty = dataItem[keysArr[0]][keysArr[1]];
  831. if (subProperty instanceof Array) {
  832. itemArr = itemArr.concat(subProperty);
  833. } else {
  834. itemArr.push(subProperty);
  835. }
  836. } else {
  837. itemArr.push(dataItem[keysArr[0]]);
  838. }
  839. }
  840. }
  841. rst.push(itemArr);
  842. // console.log(itemArr);
  843. }
  844. return rst;
  845. }
  846. function ext_getBlank(dataKey, dftVal) {
  847. const rst = [];
  848. const parentObj = this;
  849. const dtObj = parentObj.myOwnRawDataObj[dataKey]; // dataKey是有必要的,必须知道是哪个Table
  850. // 计量需要重写
  851. if (dtObj) {
  852. const dtData = getActDataArr(dtObj);
  853. for (let i = 0; i < dtData.length; i++) {
  854. if (dftVal !== null && dftVal !== undefined) {
  855. rst.push(dftVal)
  856. } else rst.push('');
  857. }
  858. }
  859. return rst;
  860. }
  861. function ext_getPropertyByForeignId(foreignIdVal, adHocIdKey, propKey, dftValIfNotFound) {
  862. const rst = [];
  863. // 计量需要重写
  864. return rst;
  865. }
  866. function getActDataArr(dtObj) {
  867. let rst = [];
  868. if (dtObj) {
  869. if (dtObj instanceof Array) {
  870. rst = rst.concat(dtObj);
  871. } else {
  872. rst.push(dtObj);
  873. }
  874. }
  875. return rst;
  876. }
  877. function replaceActDataArr(dtObj, newArr) {
  878. if (dtObj.moduleName === 'projectGLJ') {
  879. delete dtObj.data.gljList;
  880. dtObj.data.gljList = newArr;
  881. } else {
  882. delete dtObj.data;
  883. dtObj.data = newArr;
  884. }
  885. }
  886. function pri_push_property(propKey, doc, rst) {
  887. if (propKey instanceof Array) {
  888. // 备注:这里的key数组表示取value的优先级
  889. for (let pi = 0; pi < propKey.length; pi++) {
  890. if (doc.hasOwnProperty('property')) {
  891. if (doc['property'].hasOwnProperty(propKey[pi])) {
  892. rst.push(doc['property'][propKey[pi]]);
  893. break;
  894. }
  895. } else if (doc.hasOwnProperty(propKey[pi])) {
  896. rst.push(doc[propKey[pi]]);
  897. break;
  898. } else {
  899. const lenBefore = rst.length;
  900. getDeepProperty(propKey[pi], doc, rst);
  901. if (rst.length === (lenBefore + 1)) {
  902. if (rst[lenBefore] !== null && rst[lenBefore] !== undefined && rst[lenBefore] !== '') {
  903. break;
  904. } else {
  905. rst.splice(-1, 1); // 删除末尾一条数据,给后面留空间
  906. }
  907. }
  908. }
  909. if (pi === propKey.length - 1) rst.push('');
  910. }
  911. } else {
  912. if (doc.hasOwnProperty('property') && doc['property'].hasOwnProperty(propKey)) {
  913. rst.push(doc['property'][propKey]);
  914. } else if (doc.hasOwnProperty(propKey)) {
  915. rst.push(doc[propKey]);
  916. } else {
  917. getDeepProperty(propKey, doc, rst);
  918. }
  919. }
  920. }
  921. // export default Rpt_Data_Extractor;
  922. module.exports = Rpt_Data_Extractor;