rpt_construct_data_util.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 destArr = [];
  604. treeUtil.getFlatArray(rst, destArr);
  605. delete sourceData.data;
  606. sourceData.data = destArr;
  607. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/sortedAndFlattedRst.js");
  608. break;
  609. case "normal":
  610. private_normal_sort(tempRstArr, sortCfg[JV.PROP_SORT_KEYS]);
  611. delete sourceData.data;
  612. sourceData.data = tempRstArr;
  613. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/normalSortedRst.js");
  614. break;
  615. case "accord_to_parent":
  616. let pcKey = sortCfg[JV.PROP_PARENT_CHILD_SORT_KEY];
  617. let parentSrcData = getModuleDataByKey(prjData, pcKey[JV.PROP_PARENT_DATA_KEY]);
  618. if (parentSrcData) {
  619. let tempParentArr = [];
  620. for (let item of parentSrcData.data) {
  621. if (item._doc) {
  622. tempParentArr.push(item._doc);
  623. } else {
  624. tempParentArr.push(item);
  625. }
  626. }
  627. let sortedRstArr = private_parent_sort(tempParentArr, pcKey[JV.PROP_PARENT_SORT_KEYS], tempRstArr, pcKey[JV.PROP_CHILD_SORT_KEYS]);
  628. if (sortCfg[JV.PROP_OTHER_SUB_SORT] && sortCfg[JV.PROP_OTHER_SUB_SORT].length > 0) {
  629. for (let sort of sortCfg[JV.PROP_OTHER_SUB_SORT]) {
  630. if (sort[JV.PROP_SORT_TYPE] === 'normal') {
  631. for (let subArr of sortedRstArr) {
  632. private_normal_sort(subArr, sort[JV.PROP_SORT_KEYS]);
  633. }
  634. } else if (sort[JV.PROP_SORT_TYPE] === 'self_define') {
  635. for (let subArr of sortedRstArr) {
  636. // console.log(subArr);
  637. let selfDefFunc = null;
  638. eval('selfDefFunc = ' + sort[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  639. subArr.sort(selfDefFunc);
  640. // console.log(subArr);
  641. }
  642. }
  643. }
  644. }
  645. tempRstArr.splice(0);
  646. for (let item of sortedRstArr) {
  647. for (let subItem of item) {
  648. tempRstArr.push(subItem);
  649. }
  650. }
  651. }
  652. delete sourceData.data;
  653. sourceData.data = tempRstArr;
  654. break;
  655. case "self_define":
  656. if (sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]) {
  657. let selfDefFunc = null;
  658. eval('selfDefFunc = ' + sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  659. tempRstArr.sort(selfDefFunc);
  660. }
  661. delete sourceData.data;
  662. sourceData.data = tempRstArr;
  663. break;
  664. default:
  665. //
  666. }
  667. return rst;
  668. }
  669. function setupFunc(obj, prop, ownRawObj) {
  670. obj[prop] = {};
  671. obj[prop]["myOwnRawDataObj"] = ownRawObj;
  672. obj[prop].getProperty = ext_getPropety;
  673. obj[prop].getFee = ext_getFee;
  674. obj[prop].getPropertyByForeignId = ext_getPropertyByForeignId;
  675. obj[prop].getArrayItemByKey = ext_getArrayItemByKey;
  676. obj[prop].getPropertyByFlag = ext_getPropertyByFlag;
  677. if (prop === projectConst.CALC_PROGRAM) obj[prop].getCalcProperty = ext_getCalcProperty;
  678. if (prop === projectConst.FEERATE) obj[prop].getFeeRate = ext_getFeeRate;
  679. }
  680. function assembleFields(fieldList, rstDataArr, $PROJECT) {
  681. if (fieldList) {
  682. for (let field of fieldList) {
  683. shielded_exec_env($PROJECT, field, rstDataArr);
  684. }
  685. }
  686. }
  687. function shielded_exec_env($PROJECT, $ME, rptDataItemObj) {
  688. if ($ME[JV.PROP_FIELD_EXP_MAP]) {
  689. rptDataItemObj.push(eval($ME[JV.PROP_FIELD_EXP_MAP]));
  690. }
  691. }
  692. function getActPropertyVal(firstPropKey, secPropKey, orgObj) {
  693. let rst = null;
  694. if (orgObj[firstPropKey]) {
  695. rst = orgObj[firstPropKey];
  696. } else if (orgObj[secPropKey]){
  697. rst = orgObj[secPropKey];
  698. }
  699. return rst;
  700. }
  701. function getDeepProperty(propKey, orgObj, destArr) {
  702. let keys = propKey.split(".");
  703. let dftPropKey = "key", dftPropVal = "value", secDftPropVal = "items";
  704. let parent = orgObj, lastVal = null;
  705. for (let key of keys) {
  706. if (parent instanceof Array) {
  707. for (let item of parent) {
  708. if (item[dftPropKey] === key) {
  709. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  710. break;
  711. }
  712. }
  713. } else {
  714. lastVal = null;
  715. if (parent[key] !== undefined) {
  716. lastVal = parent[key];
  717. } else if (parent[secDftPropVal]){
  718. for (let item of parent[secDftPropVal]) {
  719. if (item[dftPropKey] === key) {
  720. // lastVal = item[dftPropVal];
  721. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  722. break;
  723. }
  724. }
  725. }
  726. }
  727. parent = lastVal;
  728. if (parent === null) break;
  729. }
  730. if (destArr && destArr instanceof Array) {
  731. destArr.push(lastVal);
  732. }
  733. }
  734. function ext_mainGetPropety(propKey) {
  735. let rst = [], parentObj = this;
  736. let dtObj = parentObj["myOwnRawDataObj"];
  737. if (propKey && dtObj) {
  738. if (dtObj.hasOwnProperty("property")) {
  739. if (!dtObj["property"][propKey] && dtObj[propKey]) {
  740. rst.push(dtObj[propKey]);
  741. } else {
  742. getDeepProperty(propKey, dtObj["property"], rst);
  743. }
  744. } else {
  745. // rst.push(dtObj[propKey]);
  746. getDeepProperty(propKey, dtObj, rst);
  747. }
  748. }
  749. return rst;
  750. }
  751. function ext_getPropety(propKey) {
  752. let rst = [], parentObj = this;
  753. let dtObj = parentObj["myOwnRawDataObj"];
  754. if (propKey && dtObj) {
  755. for (let dItem of dtObj.data) {
  756. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  757. if (doc.hasOwnProperty("property")) {
  758. // if (!doc["property"][propKey] && doc[propKey]) {
  759. // rst.push(doc[propKey]);
  760. // } else {
  761. // getDeepProperty(propKey, doc["property"], rst);
  762. // }
  763. rst.push(doc["property"][propKey]);
  764. } else if (doc.hasOwnProperty(propKey)) {
  765. rst.push(doc[propKey]);
  766. } else {
  767. rst.push('');
  768. }
  769. }
  770. }
  771. return rst;
  772. }
  773. function ext_mainGetFee(feeKey, dtlFeeKey) {
  774. let rst = [];
  775. let parentObj = this;
  776. let dtObj = parentObj["myOwnRawDataObj"];
  777. if ((dtObj) && (feeKey)) {
  778. if (dtObj.hasOwnProperty("fees")) {
  779. for (let fee of dtObj["fees"]) {
  780. if (fee["fieldName"] === feeKey) {
  781. if (dtlFeeKey) {
  782. rst.push(fee[dtlFeeKey]);
  783. } else {
  784. rst.push(fee["unitFee"]);
  785. }
  786. break;
  787. }
  788. }
  789. } else if (dtObj.hasOwnProperty(feeKey)) {
  790. rst.push(dtObj[feeKey]);
  791. } else {
  792. //
  793. }
  794. }
  795. return rst;
  796. }
  797. function ext_getFee(feeKey, dtlFeeKey) {
  798. let rst = [], parentObj = this;
  799. let dtObj = parentObj["myOwnRawDataObj"];
  800. if (feeKey && dtObj) {
  801. for (let dItem of dtObj.data) {
  802. rst.push(pri_getFee(dItem, feeKey, dtlFeeKey));
  803. }
  804. }
  805. for (let i = 0; i < rst.length; i++) {
  806. rst[i] = parseFloat(rst[i]);
  807. }
  808. return rst;
  809. }
  810. function pri_getFee(dItem, feeKey, dtlFeeKey) {
  811. let rst = 0;
  812. let hasValue = false;
  813. if (dItem["fees"]) {
  814. for (let fee of dItem["fees"]) {
  815. if (fee["fieldName"] === feeKey) {
  816. if (dtlFeeKey) {
  817. rst = fee[dtlFeeKey];
  818. } else {
  819. rst = fee["unitFee"];
  820. }
  821. hasValue = true;
  822. break;
  823. }
  824. }
  825. } else if (dItem[feeKey]) {
  826. hasValue = true;
  827. rst = dItem[feeKey];
  828. } else {
  829. hasValue = true;
  830. rst = 0;
  831. }
  832. if (!hasValue) {
  833. rst = 0;
  834. }
  835. return rst;
  836. }
  837. function ext_getCalcProperty(templateIDs, calcItemKey, calcItemKeyVal, calcItemRstKey){
  838. let rst = [], parentObj = this; //this should be "calc_program" object
  839. let dtObj = parentObj["myOwnRawDataObj"];
  840. let optimizeObj = {};
  841. let private_getProperty = function (cId) {
  842. let calcTplObj = optimizeObj["calc_program_" + cId];
  843. if (!calcTplObj) {
  844. let templates = (dtObj.data._doc)?dtObj.data._doc.templates:dtObj.data.templates;
  845. for (let tpl of templates) {
  846. if (cId === tpl.ID) {
  847. optimizeObj["calc_program_" + cId] = tpl;
  848. calcTplObj = tpl;
  849. break;
  850. }
  851. }
  852. }
  853. if (calcTplObj) {
  854. for (let calcItem of calcTplObj.calcItems) {
  855. if (calcItem[calcItemKey] === calcItemKeyVal) {
  856. rst.push(calcItem[calcItemRstKey]);
  857. break;
  858. }
  859. }
  860. }
  861. };
  862. if (templateIDs instanceof Array) {
  863. for (let tplId of templateIDs) {
  864. private_getProperty(tplId);
  865. }
  866. } else {
  867. private_getProperty(templateIDs);
  868. }
  869. optimizeObj = null;
  870. return rst;
  871. }
  872. function ext_getFeeRate(fee_Ids){
  873. let rst = [], parentObj = this; //this should be "feeRate" object
  874. let dtObj = parentObj["myOwnRawDataObj"];
  875. let optimizeObj = {};
  876. let private_getFeeRate = function (fId) {
  877. let feeRateItemObj = optimizeObj["fee_rates_" + fId];
  878. if (!feeRateItemObj) {
  879. let rates = (dtObj.data._doc)?dtObj.data._doc.rates:dtObj.data.rates;
  880. for (let feeItem of rates) {
  881. if (fId === feeItem.ID) {
  882. optimizeObj["fee_rates_" + fId] = feeItem;
  883. feeRateItemObj = feeItem;
  884. break;
  885. }
  886. }
  887. }
  888. if (feeRateItemObj) {
  889. rst.push(feeRateItemObj.rate);
  890. } else {
  891. rst.push(0);
  892. }
  893. };
  894. if (fee_Ids instanceof Array) {
  895. for (let fId of fee_Ids) {
  896. private_getFeeRate(fId);
  897. }
  898. } else {
  899. private_getFeeRate(fee_Ids);
  900. }
  901. optimizeObj = null;
  902. return rst;
  903. }
  904. function ext_getArrayItemByKey(arrayKey, itemKey, itemKeyValue, itemRstKey){
  905. let rst = [], parentObj = this;
  906. let dtObj = parentObj["myOwnRawDataObj"];
  907. let private_getItemValue = function (arr, dtlItKV) {
  908. for (let item of arr) {
  909. if (item[itemKey] === dtlItKV) {
  910. if (itemRstKey) {
  911. rst.push(item[itemRstKey]);
  912. } else {
  913. rst.push(item);
  914. }
  915. break;
  916. }
  917. }
  918. };
  919. let arr = dtObj[arrayKey];
  920. if (arr && arr instanceof Array) {
  921. if (itemKeyValue instanceof Array) {
  922. for (let dtlItemKeyVal of itemKeyValue) {
  923. private_getItemValue(arr, dtlItemKeyVal);
  924. }
  925. } else {
  926. private_getItemValue(arr, itemKeyValue);
  927. }
  928. }
  929. }
  930. function ext_getPropertyByFlag(flagVal, rstKey, dftValIfEmpty) {
  931. let rst = [], parentObj = this;
  932. let dtObj = parentObj["myOwnRawDataObj"];
  933. if (flagVal && rstKey && dtObj) {
  934. let isArr = (flagVal instanceof Array);
  935. for (let dItem of dtObj.data) {
  936. let doc = (dItem._doc)?dItem._doc:dItem;
  937. if (doc.hasOwnProperty("flags")) {
  938. let bFlag = false;
  939. for (let flagItem of doc.flags) {
  940. if (isArr) {
  941. bFlag = (flagVal.indexOf(flagItem.flag) >= 0);
  942. } else {
  943. if (flagItem.flag === flagVal) {
  944. bFlag = true;
  945. }
  946. }
  947. if (bFlag) break;
  948. }
  949. if (bFlag) {
  950. let keys = rstKey.split(".");
  951. if (keys[0] === "fees") {
  952. rst.push(pri_getFee(doc, "common", keys[1]));
  953. } else {
  954. //其他,比如名称什么
  955. }
  956. break;
  957. }
  958. }
  959. }
  960. }
  961. if (rst.length === 0 && dftValIfEmpty !== null) {
  962. rst.push(dftValIfEmpty);
  963. }
  964. return rst;
  965. }
  966. function ext_getPropertyByForeignId(foreignIdVal, adHocIdKey, propKey, dftValIfNotFound) {
  967. let rst = [], parentObj = this;
  968. let IdKey = (adHocIdKey)?adHocIdKey:"ID";
  969. let dtObj = parentObj["myOwnRawDataObj"];
  970. if (foreignIdVal !== null && foreignIdVal !== undefined) {
  971. let isFound = false;
  972. if (foreignIdVal instanceof Array) {
  973. for (let idVal of foreignIdVal) {
  974. isFound = false;
  975. for (let i = 0; i < dtObj.data.length; i++) {
  976. let item = (dtObj.data[i]._doc)?dtObj.data[i]._doc:dtObj.data[i];
  977. if (item[IdKey] === idVal) {
  978. let splitPKey = propKey.split(".");
  979. if (splitPKey.length > 1) {
  980. let rstP = null;
  981. for (let i = 0; i < splitPKey.length; i++) {
  982. if (i === 0) {
  983. rstP = item[splitPKey[i]];
  984. } else {
  985. if (splitPKey[i].indexOf("[") === 0 && splitPKey[i].indexOf("]") === (splitPKey[i].length - 1)) {
  986. //考虑数组情况^_^!!!
  987. } else {
  988. //
  989. }
  990. rstP = rstP[splitPKey[i]];
  991. }
  992. }
  993. rst.push(rstP);
  994. } else {
  995. rst.push(item[propKey]);
  996. }
  997. isFound = true;
  998. break;
  999. }
  1000. }
  1001. if (!isFound) {
  1002. rst.push(dftValIfNotFound);
  1003. }
  1004. }
  1005. } else {
  1006. for (let item of dtObj.data) {
  1007. if (item[IdKey] === foreignIdVal) {
  1008. rst.push(item[propKey]);
  1009. isFound = true;
  1010. break;
  1011. }
  1012. }
  1013. if (!isFound) {
  1014. rst.push(dftValIfNotFound);
  1015. }
  1016. }
  1017. }
  1018. return rst;
  1019. }
  1020. export default Rpt_Data_Extractor;