rpt_construct_data_util.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /**
  2. * Created by Tony on 2017/7/14.
  3. * 报表数据提取class,是协助报表模板里指标字段自主提取数据的工具类
  4. */
  5. let JV = require('../rpt_component/jpc_value_define');
  6. let $JE = require('../rpt_component/jpc_rte');
  7. let consts = require('../../../modules/main/models/project_consts');
  8. let fsUtil = require("../../../public/fsUtil");
  9. let treeUtil = require('../../../public/treeUtil');
  10. let projectConst = consts.projectConst;
  11. let projectConstList = consts.projectConstList;
  12. const GLJ_TYPE = {
  13. Labour: 1,
  14. Material: 2,
  15. Machine: 3,
  16. Main_Material: 4,
  17. Equipment: 5
  18. };
  19. class Rpt_Common{
  20. initialize(rpt_tpl, currentDataObj) {
  21. this.template = rpt_tpl;
  22. this.currentDataObj = currentDataObj;
  23. };
  24. Multiply(val1, val2, fixFormat) {
  25. let rst = [], maxLen = val1.length, minLen = val2.length;
  26. if (minLen > maxLen) {
  27. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  28. }
  29. for (let i = 0; i < maxLen; i++) {
  30. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) * ((i < val2.length)?val2[i]:val2[minLen - 1]);
  31. if (fixFormat) value = value.toFixed(fixFormat);
  32. rst.push(value);
  33. }
  34. return rst;
  35. };
  36. Divide(val1, val2, fixFormat) {
  37. let rst = [], maxLen = val1.length, minLen = val2.length;
  38. if (minLen > maxLen) {
  39. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  40. }
  41. for (let i = 0; i < maxLen; i++) {
  42. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) / ((i < val2.length)?val2[i]:val2[minLen - 1]);
  43. if (fixFormat) value = value.toFixed(fixFormat);
  44. rst.push(value);
  45. }
  46. return rst;
  47. };
  48. Plus(val1, val2, fixFormat) {
  49. let rst = [], maxLen = val1.length, minLen = val2.length;
  50. if (minLen > maxLen) {
  51. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  52. }
  53. for (let i = 0; i < maxLen; i++) {
  54. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) + ((i < val2.length)?val2[i]:val2[minLen - 1]);
  55. if (fixFormat) value = value.toFixed(fixFormat);
  56. rst.push(value);
  57. }
  58. return rst;
  59. };
  60. MultiPlus(arrVal, fixFormat) {
  61. let rst = [];
  62. for (let i = 0; i < arrVal.length; i++) {
  63. let valItem = arrVal[i];
  64. if (i === 0) {
  65. for (let dtl of valItem) {
  66. let value = parseFloat(dtl);
  67. if (fixFormat) value = value.toFixed(fixFormat);
  68. rst.push(value);
  69. }
  70. } else {
  71. for (let j = 0; j < valItem.length; j++) {
  72. if (j < rst.length) {
  73. let value = rst[j] + valItem[j];
  74. if (fixFormat) value = value.toFixed(fixFormat);
  75. rst[j] = value;
  76. } else {
  77. let value = parseFloat(valItem[j]);
  78. if (fixFormat) value = value.toFixed(fixFormat);
  79. rst.push(value);
  80. }
  81. }
  82. }
  83. }
  84. return rst;
  85. };
  86. Minus(val1, val2, fixFormat) {
  87. let rst = [], maxLen = val1.length, minLen = val2.length;
  88. if (minLen > maxLen) {
  89. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  90. }
  91. for (let i = 0; i < maxLen; i++) {
  92. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) - ((i < val2.length)?val2[i]:val2[minLen - 1]);
  93. if (fixFormat) value = value.toFixed(fixFormat);
  94. rst.push(value);
  95. }
  96. return rst;
  97. };
  98. }
  99. class Rpt_Data_Extractor {
  100. constructor () {
  101. this.COMMON = new Rpt_Common();
  102. };
  103. initialize(tpl) {
  104. this.rptTpl = tpl;
  105. };
  106. //-- 根据报表模板映射指标(非离散指标)的定义,罗列出所有需要用到的data对象key,作为数据请求的过滤依据
  107. getDataRequestFilter() {
  108. let rst = [];
  109. let tpl = this.rptTpl;
  110. let pri_func_chk_filter = function (field) {
  111. for (let key of projectConstList) {
  112. if (rst.indexOf(key) < 0) {
  113. if (field[JV.PROP_FIELD_EXP_MAP]) {
  114. if (field[JV.PROP_FIELD_EXP_MAP].indexOf('.' + key + '.') >= 0) {
  115. rst.push(key);
  116. if (key === projectConst.RATION_GLJ && (rst.indexOf(projectConst.PROJECTGLJ) < 0)) {
  117. rst.push(projectConst.PROJECTGLJ);
  118. }
  119. if (key === projectConst.PROJECTGLJ && (rst.indexOf(projectConst.RATION_GLJ) < 0)) {
  120. rst.push(projectConst.RATION_GLJ);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. };
  127. let pri_setup_filter = function (FIELD_LIST_KEY) {
  128. if (tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]) {
  129. for (let field of tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]) {
  130. pri_func_chk_filter(field);
  131. }
  132. }
  133. };
  134. pri_setup_filter(JV.NODE_DISCRETE_FIELDS);
  135. pri_setup_filter(JV.NODE_MASTER_FIELDS);
  136. pri_setup_filter(JV.NODE_DETAIL_FIELDS);
  137. pri_setup_filter(JV.NODE_MASTER_FIELDS_EX);
  138. pri_setup_filter(JV.NODE_DETAIL_FIELDS_EX);
  139. if (tpl[JV.NODE_MAP_DATA_HANDLE_INFO] && tpl[JV.NODE_MAP_DATA_HANDLE_INFO].length > 0) {
  140. for (let preHandle of tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  141. if (rst.indexOf(preHandle[JV.PROP_DATA_KEY]) < 0) {
  142. rst.push(preHandle[JV.PROP_DATA_KEY]);
  143. }
  144. if (preHandle[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_FILTER) {
  145. if (preHandle[JV.PROP_FILTER_KEYS]) {
  146. for (let filter of preHandle[JV.PROP_FILTER_KEYS]) {
  147. if (filter[JV.PROP_FILTER_COMPARE_OBJ] && rst.indexOf(filter[JV.PROP_FILTER_COMPARE_OBJ]) < 0) {
  148. rst.push(filter[JV.PROP_FILTER_COMPARE_OBJ]);
  149. }
  150. }
  151. }
  152. } else if (preHandle[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_SUM) {
  153. if (preHandle[JV.PROP_SUM_GROUP_KEYS]) {
  154. for (let grpKey of preHandle[JV.PROP_SUM_GROUP_KEYS]) {
  155. if (grpKey.seeking_parent && rst.indexOf(grpKey.seeking_parent) < 0) {
  156. rst.push(grpKey.seeking_parent);
  157. }
  158. }
  159. }
  160. } else if (preHandle[JV.PROP_PARENT_CHILD_SORT_KEY] && preHandle[JV.PROP_PARENT_CHILD_SORT_KEY].length > 0) {
  161. for (let item of preHandle[JV.PROP_PARENT_CHILD_SORT_KEY]) {
  162. if (rst.indexOf(item[JV.PROP_PARENT_DATA_KEY]) < 0) {
  163. rst.push(item[JV.PROP_PARENT_DATA_KEY]);
  164. }
  165. }
  166. }
  167. }
  168. }
  169. if (rst.length === 0) {
  170. rst.push(projectConst.RATION_ASS);
  171. }
  172. return rst;
  173. };
  174. //--- 装配数据(把收集到的数据,依据报表模板的指示,预处理(如:排序、过滤、合计)及装配到相关指标) ---//
  175. assembleData(rawDataObj) {
  176. let $PROJECT = {"COMMON": null, "MAIN": {}, "DETAIL": {}};
  177. let tpl = this.rptTpl;
  178. this.COMMON.initialize(tpl, rawDataObj);
  179. $PROJECT.COMMON = this.COMMON;
  180. $PROJECT.MAIN["myOwnRawDataObj"] = rawDataObj.prj._doc;
  181. $PROJECT.MAIN.getProperty = ext_mainGetPropety;
  182. $PROJECT.MAIN.getFee = ext_mainGetFee;
  183. if (tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  184. for (let preHandle of tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  185. let srcData = getModuleDataByKey(rawDataObj.prjData, preHandle[JV.PROP_DATA_KEY]);
  186. switch(preHandle[JV.PROP_HANDLE_TYPE]) {
  187. case JV.PROP_HANDLE_TYPE_SORT:
  188. sortData(srcData, preHandle, rawDataObj.prjData);
  189. break;
  190. case JV.PROP_HANDLE_TYPE_FILTER:
  191. filterData(srcData, preHandle, rawDataObj.prjData);
  192. break;
  193. case JV.PROP_HANDLE_TYPE_SUM:
  194. summaryData(srcData, preHandle, rawDataObj.prjData);
  195. break;
  196. case JV.PROP_HANDLE_TYPE_ADD_DUMMY:
  197. addDummyData(srcData, preHandle);
  198. break;
  199. case JV.PROP_HANDLE_TYPE_ADJUST:
  200. adjustData(srcData, preHandle);
  201. break;
  202. default:
  203. break;
  204. }
  205. }
  206. }
  207. for (let item of rawDataObj.prjData) {
  208. setupFunc($PROJECT.DETAIL, item.moduleName, item);
  209. }
  210. let rptDataObj = {};
  211. rptDataObj[JV.DATA_DISCRETE_DATA] = [];
  212. rptDataObj[JV.DATA_MASTER_DATA] = [];
  213. rptDataObj[JV.DATA_DETAIL_DATA] = [];
  214. rptDataObj[JV.DATA_MASTER_DATA_EX] = [];
  215. rptDataObj[JV.DATA_DETAIL_DATA_EX] = [];
  216. rptDataObj.DecimalObj = {};
  217. rptDataObj.DecimalObj.prjDecimal = $PROJECT.MAIN["myOwnRawDataObj"].decimal; //为函数 P_REF() 准备数据
  218. rptDataObj.DecimalObj.unitDecimal = getUnitDecimal($PROJECT.MAIN["myOwnRawDataObj"].billsQuantityDecimal); //为函数 U_REF() 准备数据
  219. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS], rptDataObj[JV.DATA_DISCRETE_DATA], $PROJECT);
  220. // console.log(JV.DATA_DISCRETE_DATA);
  221. // console.log(rptDataObj[JV.DATA_DISCRETE_DATA]);
  222. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS], rptDataObj[JV.DATA_MASTER_DATA], $PROJECT);
  223. // console.log(JV.DATA_MASTER_DATA);
  224. // console.log(rptDataObj[JV.DATA_MASTER_DATA]);
  225. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS], rptDataObj[JV.DATA_DETAIL_DATA], $PROJECT);
  226. // console.log(JV.DATA_DETAIL_DATA);
  227. // console.log(rptDataObj[JV.DATA_DETAIL_DATA]);
  228. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX], rptDataObj[JV.DATA_MASTER_DATA_EX], $PROJECT);
  229. // console.log(JV.DATA_MASTER_DATA_EX);
  230. // console.log(rptDataObj[JV.DATA_MASTER_DATA_EX]);
  231. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX], rptDataObj[JV.DATA_DETAIL_DATA_EX], $PROJECT);
  232. // console.log(JV.DATA_DETAIL_DATA_EX);
  233. // console.log(rptDataObj[JV.DATA_DETAIL_DATA_EX]);
  234. return rptDataObj;
  235. };
  236. }
  237. function getUnitDecimal(unitDecimalArr) {
  238. let rst = {"default_decimal": 2};
  239. if (unitDecimalArr) {
  240. for (let item of unitDecimalArr) {
  241. if (item.unit === "其他未列单位") {
  242. rst["default_decimal"] = item.decimal;
  243. } else {
  244. rst['unit_decimal_' + item.unit] = item.decimal;
  245. }
  246. }
  247. }
  248. return rst;
  249. }
  250. function getModuleDataByKey(prjData, key) {
  251. let rst = null;
  252. for (let item of prjData) {
  253. if (item.moduleName === key) {
  254. rst = item;
  255. break;
  256. }
  257. }
  258. return rst;
  259. }
  260. function summaryData(sourceData, handleCfg, prjData){
  261. let rstArr = [], tempRstArr = [];
  262. let curParentPrjData = {};
  263. for (let item of sourceData.data) {
  264. if (item._doc) {
  265. tempRstArr.push(item._doc);
  266. } else {
  267. tempRstArr.push(item);
  268. }
  269. }
  270. let private_get_grp_key = function (item) {
  271. let keys = [];
  272. for (let cfg of handleCfg[JV.PROP_SUM_GROUP_KEYS]) {
  273. if (typeof cfg === "string") {
  274. keys.push(item[cfg]);
  275. } else {
  276. if (!curParentPrjData[cfg["seeking_parent"]]) curParentPrjData[cfg["seeking_parent"]] = getModuleDataByKey(prjData, cfg["seeking_parent"]);
  277. for (let pDataItem of curParentPrjData[cfg["seeking_parent"]].data) {
  278. let data = (pDataItem._doc)?pDataItem._doc:pDataItem;
  279. if (item[cfg["seeking_key"]] === data[cfg["parent_key"]]) {
  280. keys.push(data[cfg["parent_grp_key"]]);
  281. break;
  282. }
  283. }
  284. }
  285. }
  286. return ( "grp_key_" + keys.join('_'));
  287. };
  288. let sumObj = {};
  289. for (let dtl of tempRstArr) {
  290. let grpKey = private_get_grp_key(dtl);
  291. if (sumObj[grpKey] === null || sumObj[grpKey] === undefined) {
  292. sumObj[grpKey] = dtl;
  293. rstArr.push(dtl);
  294. } else {
  295. for (let sumKey of handleCfg[JV.PROP_SUM_SUM_KEYS]) {
  296. if (dtl[sumKey]) {
  297. // sumObj[grpKey][sumKey] += dtl[sumKey];
  298. sumObj[grpKey][sumKey] = parseFloat(sumObj[grpKey][sumKey]) + parseFloat(dtl[sumKey]);
  299. }
  300. }
  301. }
  302. }
  303. delete sourceData.data;
  304. sourceData.data = rstArr;
  305. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/sumRst.js");
  306. }
  307. function filterData(sourceData, handleCfg, prjData) {
  308. let rstArr = [], tempRstArr = [];
  309. for (let item of sourceData.data) {
  310. if (item._doc) {
  311. tempRstArr.push(item._doc);
  312. } else {
  313. tempRstArr.push(item);
  314. }
  315. }
  316. let private_chkVal = function (src, compVal, compStr) {
  317. let rst = true;
  318. switch (compStr) {
  319. case "==" :
  320. rst = (src == compVal);
  321. break;
  322. case "===" :
  323. rst = (src === compVal);
  324. break;
  325. case ">" :
  326. rst = (src > compVal);
  327. break;
  328. case ">=" :
  329. rst = (src >= compVal);
  330. break;
  331. case "<" :
  332. rst = (src < compVal);
  333. break;
  334. case "<=" :
  335. rst = (src <= compVal);
  336. break;
  337. case "!=" :
  338. rst = (src != compVal);
  339. break;
  340. case "!==" :
  341. rst = (src !== compVal);
  342. break;
  343. case "in" :
  344. if (compVal instanceof Array) {
  345. rst = compVal.indexOf(src) >= 0;
  346. } else {
  347. //string,需要转类型
  348. let newCv = JSON.parse(compVal);
  349. if (newCv instanceof Array) {
  350. rst = newCv.indexOf(src) >= 0;
  351. } else {
  352. rst = false;
  353. }
  354. }
  355. break;
  356. case "not in":
  357. if (compVal instanceof Array) {
  358. rst = compVal.indexOf(src) < 0;
  359. } else {
  360. //string,需要转类型
  361. let newCv = JSON.parse(compVal);
  362. if (newCv instanceof Array) {
  363. rst = newCv.indexOf(src) < 0;
  364. } else {
  365. rst = true;
  366. }
  367. }
  368. break;
  369. default:
  370. rst = true;
  371. }
  372. return rst;
  373. };
  374. let compareObj = {};
  375. for (let item of tempRstArr) {
  376. let compRst = true;
  377. let curComparePrjData = null;
  378. let startIdx = 0;
  379. for (let cfg of handleCfg[JV.PROP_FILTER_KEYS]) {
  380. if (cfg[JV.PROP_FILTER_COMPARE_VAL]) {
  381. //比较key值
  382. compRst = private_chkVal(item[cfg.key], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
  383. } else if (cfg[JV.PROP_FILTER_COMPARE_OBJ] && cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]){
  384. //通过其他对象来过滤
  385. if (!curComparePrjData) {
  386. curComparePrjData = getModuleDataByKey(prjData, cfg[JV.PROP_FILTER_COMPARE_OBJ]);
  387. }
  388. if (cfg[JV.PROP_FILTER_CONDITION] === "in" || cfg[JV.PROP_FILTER_CONDITION] === "not in") {
  389. let compareArr = null;
  390. if (!compareObj.hasOwnProperty(cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString())) {
  391. compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()] = [];
  392. compareArr = compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()];
  393. for (let data of curComparePrjData.data) {
  394. if (compareArr.indexOf(data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]]) < 0) {
  395. compareArr.push(data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]]);
  396. }
  397. }
  398. } else {
  399. compareArr = compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()];
  400. }
  401. compRst = private_chkVal(item[cfg.key], compareArr, cfg[JV.PROP_FILTER_CONDITION]);
  402. } else {
  403. for (let data of curComparePrjData.data) {
  404. compRst = private_chkVal(item[cfg.key], data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]], cfg[JV.PROP_FILTER_CONDITION]);
  405. if (compRst) break;
  406. }
  407. }
  408. }
  409. startIdx++;
  410. }
  411. if (compRst) {
  412. rstArr.push(item);
  413. }
  414. }
  415. delete sourceData.data;
  416. sourceData.data = rstArr;
  417. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/filteredRst.js");
  418. }
  419. function adjustData(sourceData, adjustCfg) {
  420. let rstArr = [];
  421. for (let item of sourceData.data) {
  422. if (item._doc) {
  423. rstArr.push(item._doc);
  424. } else {
  425. rstArr.push(item);
  426. }
  427. }
  428. for (let item of adjustCfg[JV.PROP_ADJUST_COLLECTION]) {
  429. for (let rec of rstArr) {
  430. if (item[JV.PROP_ADJUST_ACTION] === "prefix") {
  431. rec[item.key] = item[JV.PROP_ADJUST_ACTION_VAL] + rec[item.key];
  432. } else if (item[JV.PROP_ADJUST_ACTION] === "suffix") {
  433. rec[item.key] = rec[item.key] + item[JV.PROP_ADJUST_ACTION_VAL];
  434. }
  435. }
  436. }
  437. delete sourceData.data;
  438. sourceData.data = rstArr;
  439. }
  440. function getDupGrpKeyVals(sourceData, segKeys) {
  441. let rst = [];
  442. function pushKeyVal(item) {
  443. let tr = {};
  444. for (let i = 0; i < segKeys.length; i++) {
  445. tr[segKeys[i]] = item[segKeys[i]];
  446. }
  447. rst.push(tr);
  448. }
  449. for (let idx = 0; idx < sourceData.length; idx++) {
  450. let itemRec = sourceData[idx];
  451. if (idx === 0) {
  452. pushKeyVal(itemRec);
  453. continue;
  454. }
  455. let hasDiff = false;
  456. for (let i = 0; i < segKeys.length; i++) {
  457. if (itemRec[segKeys[i]] !== sourceData[idx - 1][segKeys[i]]) {
  458. hasDiff = true;
  459. break;
  460. }
  461. }
  462. if (hasDiff) {
  463. pushKeyVal(itemRec);
  464. }
  465. }
  466. return rst;
  467. }
  468. function addDummyData(sourceData, addCfg) {
  469. let rstArr = [], tempRstArr = [];
  470. for (let item of sourceData.data) {
  471. if (item._doc) {
  472. tempRstArr.push(item._doc);
  473. } else {
  474. tempRstArr.push(item);
  475. }
  476. }
  477. for (let item of addCfg[JV.PROP_DUMMY_COLLECTION]) {
  478. let newRecStr = JSON.stringify(item[JV.PROP_DUMMY_VAL]), cacheGrpKeyRecs = null;
  479. if (item[JV.PROP_FREQUENCY] === "OncePerGrp") {
  480. if (!cacheGrpKeyRecs) {
  481. cacheGrpKeyRecs = {};
  482. }
  483. let cacheKey = "";
  484. for (let key of item[JV.PROP_GRP_KEYS]) {
  485. cacheKey += "_" + key;
  486. }
  487. if (!cacheGrpKeyRecs[cacheKey]) {
  488. cacheGrpKeyRecs[cacheKey] = getDupGrpKeyVals(tempRstArr, item[JV.PROP_GRP_KEYS]);
  489. }
  490. for (let kv of cacheGrpKeyRecs[cacheKey]) {
  491. let rec = JSON.parse(newRecStr);
  492. for (let key of item[JV.PROP_GRP_KEYS]) {
  493. rec[key] = kv[key];
  494. }
  495. rstArr.push(rec);
  496. }
  497. } else if (item[JV.PROP_FREQUENCY] === "Once") {
  498. rstArr.push(JSON.parse(newRecStr));
  499. }
  500. }
  501. rstArr = rstArr.concat(tempRstArr);
  502. delete sourceData.data;
  503. sourceData.data = rstArr;
  504. }
  505. function getGLJBizType(orgType, orgCode, orgName) {
  506. let rst = orgType;
  507. if (orgName.indexOf("其他材料费") >= 0) {
  508. rst = 299;
  509. } else if (orgType === GLJ_TYPE.Labour) {
  510. rst = 11;
  511. if (orgCode === "000000") rst = 10;
  512. } else if (orgType === GLJ_TYPE.Main_Material || orgType === GLJ_TYPE.Equipment) {
  513. //未计价材料(主材 + 设备)
  514. rst = 30 + orgType;
  515. } else if (orgType === GLJ_TYPE.Material || (orgType >= 200 && orgType < 300)) {
  516. //材料
  517. if (orgCode === "000000") {
  518. rst = 20; //2.材料
  519. } else if (orgCode === "000000_1") {
  520. rst = 30; //(1) 未计价材料
  521. } else if (orgCode === "000000_2") {
  522. rst = 40; //(2) 辅助材料
  523. } else if (orgCode === "000000_3") {
  524. rst = 50; //(3) 其他材料费
  525. } else {
  526. rst = 45; //到这里就只有辅助材料没有预处理了
  527. }
  528. } else if (orgType === GLJ_TYPE.Machine || (orgType >= 300 && orgType < 400)) {
  529. //机械
  530. if (orgCode === "000000") {
  531. rst = 300; //3.机械
  532. } else if (orgType === GLJ_TYPE.Machine) {
  533. rst = 300.5;
  534. } else {
  535. rst = orgType;
  536. }
  537. }
  538. return rst;
  539. }
  540. function sortData(sourceData, sortCfg, prjData) {
  541. let rst = sourceData.data, tempRstArr = [];
  542. let sortType = sortCfg[JV.PROP_SORT_TYPE];
  543. for (let item of sourceData.data) {
  544. if (item._doc) {
  545. tempRstArr.push(item._doc);
  546. } else {
  547. tempRstArr.push(item);
  548. }
  549. }
  550. function private_normal_sort(destArr, sortKeys) {
  551. destArr.sort(function(a, b){
  552. let compRst = 0;
  553. for (let comp of sortKeys) {
  554. let reverse = (comp.order === 'ascend')?1:(-1);
  555. //
  556. if (a[comp.key] > b[comp.key]) {
  557. compRst = reverse;
  558. break;
  559. } else if (a[comp.key] < b[comp.key]) {
  560. compRst = -reverse;
  561. break;
  562. }
  563. }
  564. return compRst;
  565. });
  566. }
  567. function private_parent_sort(parentArr, parentKeys, childArr, childKeys) {
  568. let tmpRst = {}, rst = [];
  569. for (let pItem of parentArr) {
  570. let pKey = "key";
  571. for (let key of parentKeys) {
  572. pKey += "_" + pItem[key];
  573. }
  574. tmpRst[pKey] = [];
  575. }
  576. for (let cItem of childArr) {
  577. let cKey = "key";
  578. for (let key of childKeys) {
  579. cKey += "_" + cItem[key];
  580. }
  581. if (tmpRst[cKey]) {
  582. tmpRst[cKey].push(cItem);
  583. } else {
  584. //unknown child value! should be filtered!
  585. }
  586. }
  587. // childArr.splice(0);
  588. for (let pItem of parentArr) {
  589. let pKey = "key";
  590. for (let key of parentKeys) {
  591. pKey += "_" + pItem[key];
  592. }
  593. rst.push(tmpRst[pKey]);
  594. // for (let rItem of tmpRst[pKey]) {
  595. // childArr.push(rItem);
  596. // }
  597. }
  598. return rst;
  599. }
  600. switch (sortType) {
  601. case "tree":
  602. rst = treeUtil.buildTreeNodeDirectly(tempRstArr);
  603. let newTopArr = [];
  604. if (sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES] && sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].length > 0) {
  605. for (let topItem of rst) {
  606. if (topItem.flags && topItem.flags.length > 0) {
  607. for (let flagItem of topItem.flags) {
  608. if (sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].indexOf(flagItem.flag) >= 0) {
  609. newTopArr.push(topItem);
  610. break;
  611. }
  612. }
  613. }
  614. }
  615. } else {
  616. newTopArr = rst;
  617. }
  618. let destArr = [];
  619. treeUtil.getFlatArray(newTopArr, destArr);
  620. delete sourceData.data;
  621. sourceData.data = destArr;
  622. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/sortedAndFlattedRst.js");
  623. break;
  624. case "normal":
  625. private_normal_sort(tempRstArr, sortCfg[JV.PROP_SORT_KEYS]);
  626. delete sourceData.data;
  627. sourceData.data = tempRstArr;
  628. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/normalSortedRst.js");
  629. break;
  630. case "accord_to_parent":
  631. let pcKey = sortCfg[JV.PROP_PARENT_CHILD_SORT_KEY];
  632. let parentSrcData = getModuleDataByKey(prjData, pcKey[JV.PROP_PARENT_DATA_KEY]);
  633. if (parentSrcData) {
  634. let tempParentArr = [];
  635. for (let item of parentSrcData.data) {
  636. if (item._doc) {
  637. tempParentArr.push(item._doc);
  638. } else {
  639. tempParentArr.push(item);
  640. }
  641. }
  642. let sortedRstArr = private_parent_sort(tempParentArr, pcKey[JV.PROP_PARENT_SORT_KEYS], tempRstArr, pcKey[JV.PROP_CHILD_SORT_KEYS]);
  643. if (sortCfg[JV.PROP_OTHER_SUB_SORT] && sortCfg[JV.PROP_OTHER_SUB_SORT].length > 0) {
  644. for (let sort of sortCfg[JV.PROP_OTHER_SUB_SORT]) {
  645. if (sort[JV.PROP_SORT_TYPE] === 'normal') {
  646. for (let subArr of sortedRstArr) {
  647. private_normal_sort(subArr, sort[JV.PROP_SORT_KEYS]);
  648. }
  649. } else if (sort[JV.PROP_SORT_TYPE] === 'self_define') {
  650. for (let subArr of sortedRstArr) {
  651. // console.log(subArr);
  652. let selfDefFunc = null;
  653. eval('selfDefFunc = ' + sort[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  654. subArr.sort(selfDefFunc);
  655. // console.log(subArr);
  656. }
  657. }
  658. }
  659. }
  660. tempRstArr.splice(0);
  661. for (let item of sortedRstArr) {
  662. for (let subItem of item) {
  663. tempRstArr.push(subItem);
  664. }
  665. }
  666. }
  667. delete sourceData.data;
  668. sourceData.data = tempRstArr;
  669. break;
  670. case "self_define":
  671. if (sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]) {
  672. let selfDefFunc = null;
  673. eval('selfDefFunc = ' + sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  674. tempRstArr.sort(selfDefFunc);
  675. }
  676. delete sourceData.data;
  677. sourceData.data = tempRstArr;
  678. break;
  679. default:
  680. //
  681. }
  682. return rst;
  683. }
  684. function setupFunc(obj, prop, ownRawObj) {
  685. obj[prop] = {};
  686. obj[prop]["myOwnRawDataObj"] = ownRawObj;
  687. obj[prop].getProperty = ext_getPropety;
  688. obj[prop].getFee = ext_getFee;
  689. obj[prop].getPropertyByForeignId = ext_getPropertyByForeignId;
  690. obj[prop].getArrayItemByKey = ext_getArrayItemByKey;
  691. obj[prop].getPropertyByFlag = ext_getPropertyByFlag;
  692. if (prop === projectConst.CALC_PROGRAM) obj[prop].getCalcProperty = ext_getCalcProperty;
  693. if (prop === projectConst.FEERATE) obj[prop].getFeeRate = ext_getFeeRate;
  694. }
  695. function assembleFields(fieldList, rstDataArr, $PROJECT) {
  696. if (fieldList) {
  697. for (let field of fieldList) {
  698. shielded_exec_env($PROJECT, field, rstDataArr);
  699. }
  700. }
  701. }
  702. function shielded_exec_env($PROJECT, $ME, rptDataItemObj) {
  703. if ($ME[JV.PROP_FIELD_EXP_MAP]) {
  704. rptDataItemObj.push(eval($ME[JV.PROP_FIELD_EXP_MAP]));
  705. }
  706. }
  707. function getActPropertyVal(firstPropKey, secPropKey, orgObj) {
  708. let rst = null;
  709. if (orgObj[firstPropKey]) {
  710. rst = orgObj[firstPropKey];
  711. } else if (orgObj[secPropKey]){
  712. rst = orgObj[secPropKey];
  713. }
  714. return rst;
  715. }
  716. function getDeepProperty(propKey, orgObj, destArr) {
  717. let keys = propKey.split(".");
  718. let dftPropKey = "key", dftPropVal = "value", secDftPropVal = "items";
  719. let parent = orgObj, lastVal = null;
  720. for (let key of keys) {
  721. if (parent instanceof Array) {
  722. for (let item of parent) {
  723. if (item[dftPropKey] === key) {
  724. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  725. break;
  726. }
  727. }
  728. } else {
  729. lastVal = null;
  730. if (parent[key] !== undefined) {
  731. lastVal = parent[key];
  732. } else if (parent[secDftPropVal]){
  733. for (let item of parent[secDftPropVal]) {
  734. if (item[dftPropKey] === key) {
  735. // lastVal = item[dftPropVal];
  736. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  737. break;
  738. }
  739. }
  740. }
  741. }
  742. parent = lastVal;
  743. if (parent === null) break;
  744. }
  745. if (destArr && destArr instanceof Array) {
  746. destArr.push(lastVal);
  747. }
  748. }
  749. function ext_mainGetPropety(propKey) {
  750. let rst = [], parentObj = this;
  751. let dtObj = parentObj["myOwnRawDataObj"];
  752. if (propKey && dtObj) {
  753. if (dtObj.hasOwnProperty("property")) {
  754. if (!dtObj["property"][propKey] && dtObj[propKey]) {
  755. rst.push(dtObj[propKey]);
  756. } else {
  757. getDeepProperty(propKey, dtObj["property"], rst);
  758. }
  759. } else {
  760. // rst.push(dtObj[propKey]);
  761. getDeepProperty(propKey, dtObj, rst);
  762. }
  763. }
  764. return rst;
  765. }
  766. function ext_getPropety(propKey) {
  767. let rst = [], parentObj = this;
  768. let dtObj = parentObj["myOwnRawDataObj"];
  769. if (propKey && dtObj) {
  770. for (let dItem of dtObj.data) {
  771. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  772. if (doc.hasOwnProperty("property")) {
  773. // if (!doc["property"][propKey] && doc[propKey]) {
  774. // rst.push(doc[propKey]);
  775. // } else {
  776. // getDeepProperty(propKey, doc["property"], rst);
  777. // }
  778. rst.push(doc["property"][propKey]);
  779. } else if (doc.hasOwnProperty(propKey)) {
  780. rst.push(doc[propKey]);
  781. } else {
  782. rst.push('');
  783. }
  784. }
  785. }
  786. return rst;
  787. }
  788. function ext_mainGetFee(feeKey, dtlFeeKey) {
  789. let rst = [];
  790. let parentObj = this;
  791. let dtObj = parentObj["myOwnRawDataObj"];
  792. if ((dtObj) && (feeKey)) {
  793. if (dtObj.hasOwnProperty("fees")) {
  794. for (let fee of dtObj["fees"]) {
  795. if (fee["fieldName"] === feeKey) {
  796. if (dtlFeeKey) {
  797. rst.push(fee[dtlFeeKey]);
  798. } else {
  799. rst.push(fee["unitFee"]);
  800. }
  801. break;
  802. }
  803. }
  804. } else if (dtObj.hasOwnProperty(feeKey)) {
  805. rst.push(dtObj[feeKey]);
  806. } else {
  807. //
  808. }
  809. }
  810. return rst;
  811. }
  812. function ext_getFee(feeKey, dtlFeeKey) {
  813. let rst = [], parentObj = this;
  814. let dtObj = parentObj["myOwnRawDataObj"];
  815. if (feeKey && dtObj) {
  816. for (let dItem of dtObj.data) {
  817. rst.push(pri_getFee(dItem, feeKey, dtlFeeKey));
  818. }
  819. }
  820. for (let i = 0; i < rst.length; i++) {
  821. rst[i] = parseFloat(rst[i]);
  822. }
  823. return rst;
  824. }
  825. function pri_getFee(dItem, feeKey, dtlFeeKey) {
  826. let rst = 0;
  827. let hasValue = false;
  828. if (dItem["fees"]) {
  829. for (let fee of dItem["fees"]) {
  830. if (fee["fieldName"] === feeKey) {
  831. if (dtlFeeKey) {
  832. rst = fee[dtlFeeKey];
  833. } else {
  834. rst = fee["unitFee"];
  835. }
  836. hasValue = true;
  837. break;
  838. }
  839. }
  840. } else if (dItem[feeKey]) {
  841. hasValue = true;
  842. rst = dItem[feeKey];
  843. } else {
  844. hasValue = true;
  845. rst = 0;
  846. }
  847. if (!hasValue) {
  848. rst = 0;
  849. }
  850. return rst;
  851. }
  852. function ext_getCalcProperty(templateIDs, calcItemKey, calcItemKeyVal, calcItemRstKey){
  853. let rst = [], parentObj = this; //this should be "calc_program" object
  854. let dtObj = parentObj["myOwnRawDataObj"];
  855. let optimizeObj = {};
  856. let private_getProperty = function (cId) {
  857. let calcTplObj = optimizeObj["calc_program_" + cId];
  858. if (!calcTplObj) {
  859. let templates = (dtObj.data._doc)?dtObj.data._doc.templates:dtObj.data.templates;
  860. for (let tpl of templates) {
  861. if (cId === tpl.ID) {
  862. optimizeObj["calc_program_" + cId] = tpl;
  863. calcTplObj = tpl;
  864. break;
  865. }
  866. }
  867. }
  868. if (calcTplObj) {
  869. for (let calcItem of calcTplObj.calcItems) {
  870. if (calcItem[calcItemKey] === calcItemKeyVal) {
  871. rst.push(calcItem[calcItemRstKey]);
  872. break;
  873. }
  874. }
  875. }
  876. };
  877. if (templateIDs instanceof Array) {
  878. for (let tplId of templateIDs) {
  879. private_getProperty(tplId);
  880. }
  881. } else {
  882. private_getProperty(templateIDs);
  883. }
  884. optimizeObj = null;
  885. return rst;
  886. }
  887. function ext_getFeeRate(fee_Ids){
  888. let rst = [], parentObj = this; //this should be "feeRate" object
  889. let dtObj = parentObj["myOwnRawDataObj"];
  890. let optimizeObj = {};
  891. let private_getFeeRate = function (fId) {
  892. let feeRateItemObj = optimizeObj["fee_rates_" + fId];
  893. if (!feeRateItemObj) {
  894. let rates = (dtObj.data._doc)?dtObj.data._doc.rates:dtObj.data.rates;
  895. for (let feeItem of rates) {
  896. if (fId === feeItem.ID) {
  897. optimizeObj["fee_rates_" + fId] = feeItem;
  898. feeRateItemObj = feeItem;
  899. break;
  900. }
  901. }
  902. }
  903. if (feeRateItemObj) {
  904. rst.push(feeRateItemObj.rate);
  905. } else {
  906. rst.push(0);
  907. }
  908. };
  909. if (fee_Ids instanceof Array) {
  910. for (let fId of fee_Ids) {
  911. private_getFeeRate(fId);
  912. }
  913. } else {
  914. private_getFeeRate(fee_Ids);
  915. }
  916. optimizeObj = null;
  917. return rst;
  918. }
  919. function ext_getArrayItemByKey(arrayKey, itemKey, itemKeyValue, itemRstKey){
  920. let rst = [], parentObj = this;
  921. let dtObj = parentObj["myOwnRawDataObj"];
  922. let private_getItemValue = function (arr, dtlItKV) {
  923. for (let item of arr) {
  924. if (item[itemKey] === dtlItKV) {
  925. if (itemRstKey) {
  926. rst.push(item[itemRstKey]);
  927. } else {
  928. rst.push(item);
  929. }
  930. break;
  931. }
  932. }
  933. };
  934. let arr = dtObj[arrayKey];
  935. if (arr && arr instanceof Array) {
  936. if (itemKeyValue instanceof Array) {
  937. for (let dtlItemKeyVal of itemKeyValue) {
  938. private_getItemValue(arr, dtlItemKeyVal);
  939. }
  940. } else {
  941. private_getItemValue(arr, itemKeyValue);
  942. }
  943. }
  944. }
  945. function ext_getPropertyByFlag(flagVal, rstKey, dftValIfEmpty) {
  946. let rst = [], parentObj = this;
  947. let dtObj = parentObj["myOwnRawDataObj"];
  948. if (flagVal && rstKey && dtObj) {
  949. let isArr = (flagVal instanceof Array);
  950. for (let dItem of dtObj.data) {
  951. let doc = (dItem._doc)?dItem._doc:dItem;
  952. if (doc.hasOwnProperty("flags")) {
  953. let bFlag = false;
  954. for (let flagItem of doc.flags) {
  955. if (isArr) {
  956. bFlag = (flagVal.indexOf(flagItem.flag) >= 0);
  957. } else {
  958. if (flagItem.flag === flagVal) {
  959. bFlag = true;
  960. }
  961. }
  962. if (bFlag) break;
  963. }
  964. if (bFlag) {
  965. let keys = rstKey.split(".");
  966. if (keys[0] === "fees") {
  967. rst.push(pri_getFee(doc, "common", keys[1]));
  968. } else {
  969. //其他,比如名称什么
  970. }
  971. break;
  972. }
  973. }
  974. }
  975. }
  976. if (rst.length === 0 && dftValIfEmpty !== null) {
  977. rst.push(dftValIfEmpty);
  978. }
  979. return rst;
  980. }
  981. function ext_getPropertyByForeignId(foreignIdVal, adHocIdKey, propKey, dftValIfNotFound) {
  982. let rst = [], parentObj = this;
  983. let IdKey = (adHocIdKey)?adHocIdKey:"ID";
  984. let dtObj = parentObj["myOwnRawDataObj"];
  985. if (foreignIdVal !== null && foreignIdVal !== undefined) {
  986. let isFound = false;
  987. if (foreignIdVal instanceof Array) {
  988. for (let idVal of foreignIdVal) {
  989. isFound = false;
  990. let dataArr = dtObj.data;
  991. if (dtObj.moduleName === "projectGLJ") dataArr = dtObj.data.gljList;
  992. for (let i = 0; i < dataArr.length; i++) {
  993. let item = (dataArr[i]._doc)?dataArr[i]._doc:dataArr[i];
  994. if (item[IdKey] === idVal) {
  995. let splitPKey = propKey.split(".");
  996. if (splitPKey.length > 1) {
  997. let rstP = null;
  998. for (let i = 0; i < splitPKey.length; i++) {
  999. if (i === 0) {
  1000. rstP = item[splitPKey[i]];
  1001. } else {
  1002. if (splitPKey[i].indexOf("[") === 0 && splitPKey[i].indexOf("]") === (splitPKey[i].length - 1)) {
  1003. //考虑数组情况^_^!!!
  1004. } else {
  1005. //
  1006. }
  1007. rstP = rstP[splitPKey[i]];
  1008. }
  1009. }
  1010. rst.push(rstP);
  1011. } else {
  1012. rst.push(item[propKey]);
  1013. }
  1014. isFound = true;
  1015. break;
  1016. }
  1017. }
  1018. if (!isFound) {
  1019. rst.push(dftValIfNotFound);
  1020. }
  1021. }
  1022. } else {
  1023. for (let item of dtObj.data) {
  1024. if (item[IdKey] === foreignIdVal) {
  1025. rst.push(item[propKey]);
  1026. isFound = true;
  1027. break;
  1028. }
  1029. }
  1030. if (!isFound) {
  1031. rst.push(dftValIfNotFound);
  1032. }
  1033. }
  1034. }
  1035. return rst;
  1036. }
  1037. export default Rpt_Data_Extractor;