rpt_construct_data_util.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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 stringUtil = require("../../../public/stringUtil");
  10. let treeUtil = require('../../../public/treeUtil');
  11. let projectConst = consts.projectConst;
  12. let projectConstList = consts.projectConstList;
  13. const gljUtil = require('../../../public/gljUtil');
  14. const GLJ_TYPE = {
  15. Labour: 1,
  16. Material: 2,
  17. Machine: 3,
  18. Main_Material: 4,
  19. Equipment: 5
  20. };
  21. class Rpt_Common{
  22. initialize(rpt_tpl, currentDataObj) {
  23. this.template = rpt_tpl;
  24. this.currentDataObj = currentDataObj;
  25. };
  26. Multiply(val1, val2, fixFormat) {
  27. let rst = [], maxLen = val1.length, minLen = val2.length;
  28. if (minLen > maxLen) {
  29. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  30. }
  31. for (let i = 0; i < maxLen; i++) {
  32. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) * ((i < val2.length)?val2[i]:val2[minLen - 1]);
  33. if (value === null || value === undefined) {
  34. value = '0';
  35. }
  36. if (fixFormat) value = value.toFixed(fixFormat);
  37. rst.push(value);
  38. }
  39. return rst;
  40. };
  41. Divide(val1, val2, fixFormat) {
  42. let rst = [], maxLen = val1.length, minLen = val2.length;
  43. if (minLen > maxLen) {
  44. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  45. }
  46. for (let i = 0; i < maxLen; i++) {
  47. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) / ((i < val2.length)?val2[i]:val2[minLen - 1]);
  48. if (fixFormat) value = value.toFixed(fixFormat);
  49. rst.push(value);
  50. }
  51. return rst;
  52. };
  53. Plus(val1, val2, fixFormat) {
  54. let rst = [], maxLen = val1.length, minLen = val2.length;
  55. if (minLen > maxLen) {
  56. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  57. }
  58. for (let i = 0; i < maxLen; i++) {
  59. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) + ((i < val2.length)?val2[i]:val2[minLen - 1]);
  60. if (fixFormat) value = value.toFixed(fixFormat);
  61. rst.push(value);
  62. }
  63. return rst;
  64. };
  65. MultiPlus(arrVal, fixFormat) {
  66. let rst = [];
  67. for (let i = 0; i < arrVal.length; i++) {
  68. let valItem = arrVal[i];
  69. if (i === 0) {
  70. for (let dtl of valItem) {
  71. let value = parseFloat(dtl);
  72. if (fixFormat) value = value.toFixed(fixFormat);
  73. rst.push(value);
  74. }
  75. } else {
  76. for (let j = 0; j < valItem.length; j++) {
  77. if (j < rst.length) {
  78. let value = rst[j] + valItem[j];
  79. if (fixFormat) value = value.toFixed(fixFormat);
  80. rst[j] = value;
  81. } else {
  82. let value = parseFloat(valItem[j]);
  83. if (fixFormat) value = value.toFixed(fixFormat);
  84. rst.push(value);
  85. }
  86. }
  87. }
  88. }
  89. return rst;
  90. };
  91. Minus(val1, val2, fixFormat) {
  92. let rst = [], maxLen = val1.length, minLen = val2.length;
  93. if (minLen > maxLen) {
  94. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  95. }
  96. for (let i = 0; i < maxLen; i++) {
  97. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) - ((i < val2.length)?val2[i]:val2[minLen - 1]);
  98. if (fixFormat) value = value.toFixed(fixFormat);
  99. rst.push(value);
  100. }
  101. return rst;
  102. };
  103. FormatString(arrVal, formatStr){
  104. let rst = [];
  105. for (let val of arrVal) {
  106. rst.push(stringUtil.replaceAll(formatStr, '%S', val));
  107. }
  108. return rst;
  109. };
  110. }
  111. class Rpt_Data_Extractor {
  112. constructor () {
  113. this.COMMON = new Rpt_Common();
  114. };
  115. initialize(tpl) {
  116. this.rptTpl = tpl;
  117. };
  118. //-- 根据报表模板映射指标(非离散指标)的定义,罗列出所有需要用到的data对象key,作为数据请求的过滤依据
  119. getDataRequestFilter() {
  120. let rst = [];
  121. let tpl = this.rptTpl;
  122. let pri_func_chk_filter = function (field) {
  123. for (let key of projectConstList) {
  124. if (rst.indexOf(key) < 0) {
  125. if (field[JV.PROP_FIELD_EXP_MAP]) {
  126. if (field[JV.PROP_FIELD_EXP_MAP].indexOf('.' + key + '.') >= 0) {
  127. rst.push(key);
  128. if (key === projectConst.RATION_GLJ && (rst.indexOf(projectConst.PROJECTGLJ) < 0)) {
  129. rst.push(projectConst.PROJECTGLJ);
  130. }
  131. if (key === projectConst.PROJECTGLJ) {
  132. if (rst.indexOf(projectConst.RATION_GLJ) < 0) rst.push(projectConst.RATION_GLJ);
  133. if (field[JV.PROP_FIELD_EXP_MAP].indexOf("'quantity'") > 0 ||
  134. field[JV.PROP_FIELD_EXP_MAP].indexOf("'subdivisionQuantity'") > 0 ||
  135. field[JV.PROP_FIELD_EXP_MAP].indexOf("'techQuantity'") > 0 ) {
  136. if (rst.indexOf(projectConst.RATION) < 0) rst.push(projectConst.RATION);
  137. if (rst.indexOf(projectConst.BILLS) < 0) rst.push(projectConst.BILLS);
  138. }
  139. }
  140. }
  141. }
  142. } else if (key === projectConst.PROJECTGLJ) {
  143. if (rst.indexOf(projectConst.RATION_GLJ) < 0) rst.push(projectConst.RATION_GLJ);
  144. if (field[JV.PROP_FIELD_EXP_MAP].indexOf("'quantity'") > 0 ||
  145. field[JV.PROP_FIELD_EXP_MAP].indexOf("'subdivisionQuantity'") > 0 ||
  146. field[JV.PROP_FIELD_EXP_MAP].indexOf("'techQuantity'") > 0 ) {
  147. if (rst.indexOf(projectConst.RATION) < 0) rst.push(projectConst.RATION);
  148. if (rst.indexOf(projectConst.BILLS) < 0) rst.push(projectConst.BILLS);
  149. }
  150. }
  151. }
  152. };
  153. let pri_setup_filter = function (FIELD_LIST_KEY) {
  154. if (tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]) {
  155. for (let field of tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]) {
  156. pri_func_chk_filter(field);
  157. }
  158. }
  159. };
  160. pri_setup_filter(JV.NODE_DISCRETE_FIELDS);
  161. pri_setup_filter(JV.NODE_MASTER_FIELDS);
  162. pri_setup_filter(JV.NODE_DETAIL_FIELDS);
  163. pri_setup_filter(JV.NODE_MASTER_FIELDS_EX);
  164. pri_setup_filter(JV.NODE_DETAIL_FIELDS_EX);
  165. if (tpl[JV.NODE_MAP_DATA_HANDLE_INFO] && tpl[JV.NODE_MAP_DATA_HANDLE_INFO].length > 0) {
  166. for (let preHandle of tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  167. if (rst.indexOf(preHandle[JV.PROP_DATA_KEY]) < 0) {
  168. rst.push(preHandle[JV.PROP_DATA_KEY]);
  169. }
  170. if (preHandle[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_FILTER) {
  171. if (preHandle[JV.PROP_FILTER_KEYS]) {
  172. for (let filter of preHandle[JV.PROP_FILTER_KEYS]) {
  173. if (filter[JV.PROP_FILTER_COMPARE_OBJ] && rst.indexOf(filter[JV.PROP_FILTER_COMPARE_OBJ]) < 0) {
  174. rst.push(filter[JV.PROP_FILTER_COMPARE_OBJ]);
  175. }
  176. }
  177. }
  178. } else if (preHandle[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_SUM) {
  179. if (preHandle[JV.PROP_SUM_GROUP_KEYS]) {
  180. for (let grpKey of preHandle[JV.PROP_SUM_GROUP_KEYS]) {
  181. if (grpKey.seeking_parent && rst.indexOf(grpKey.seeking_parent) < 0) {
  182. rst.push(grpKey.seeking_parent);
  183. }
  184. }
  185. }
  186. } else if (preHandle[JV.PROP_PARENT_CHILD_SORT_KEY] && preHandle[JV.PROP_PARENT_CHILD_SORT_KEY].length > 0) {
  187. for (let item of preHandle[JV.PROP_PARENT_CHILD_SORT_KEY]) {
  188. if (rst.indexOf(item[JV.PROP_PARENT_DATA_KEY]) < 0) {
  189. rst.push(item[JV.PROP_PARENT_DATA_KEY]);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. if (rst.length === 0) {
  196. rst.push(projectConst.RATION_ASS);
  197. }
  198. return rst;
  199. };
  200. //--- 装配数据(把收集到的数据,依据报表模板的指示,预处理(如:排序、过滤、合计)及装配到相关指标) ---//
  201. assembleData(rawDataObj) {
  202. let $PROJECT = {"COMMON": null, "MAIN": {}, "DETAIL": {}};
  203. let tpl = this.rptTpl;
  204. this.COMMON.initialize(tpl, rawDataObj);
  205. $PROJECT.COMMON = this.COMMON;
  206. $PROJECT.MAIN["myOwnRawDataObj"] = rawDataObj.prj._doc;
  207. $PROJECT.MAIN.getProperty = ext_mainGetPropety;
  208. $PROJECT.MAIN.getFee = ext_mainGetFee;
  209. for (let item of rawDataObj.prjData) {
  210. setupFunc($PROJECT.DETAIL, item.moduleName, item);
  211. }
  212. let projectGLJDatas = getModuleDataByKey(rawDataObj.prjData, "projectGLJ");
  213. let rationGLJDatas = getModuleDataByKey(rawDataObj.prjData, "ration_glj");
  214. let rationDatas = getModuleDataByKey(rawDataObj.prjData, "ration");
  215. let billsDatas = getModuleDataByKey(rawDataObj.prjData, "bills");
  216. if (projectGLJDatas && rationGLJDatas && rationDatas && billsDatas) {
  217. gljUtil.calcProjectGLJQuantity(projectGLJDatas.data, rationGLJDatas.data, rationDatas.data, billsDatas.data, 4);
  218. }
  219. if (tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  220. for (let preHandle of tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  221. let srcData = getModuleDataByKey(rawDataObj.prjData, preHandle[JV.PROP_DATA_KEY]);
  222. switch(preHandle[JV.PROP_HANDLE_TYPE]) {
  223. case JV.PROP_HANDLE_TYPE_SORT:
  224. sortData(srcData, preHandle, rawDataObj.prjData);
  225. break;
  226. case JV.PROP_HANDLE_TYPE_FILTER:
  227. filterData(srcData, preHandle, rawDataObj.prjData);
  228. break;
  229. case JV.PROP_HANDLE_TYPE_SUM:
  230. // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/insertedRawDataData10.jsp");
  231. summaryData(srcData, preHandle, rawDataObj.prjData);
  232. // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/insertedRawDataData11.jsp");
  233. break;
  234. case JV.PROP_HANDLE_TYPE_ADD_DUMMY:
  235. addDummyData(srcData, preHandle);
  236. break;
  237. case JV.PROP_HANDLE_TYPE_ADJUST:
  238. adjustData(srcData, preHandle);
  239. break;
  240. case JV.PROP_HANDLE_TYPE_BILLS_DATA_MOVE:
  241. moveRationData(srcData, rawDataObj);
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. }
  248. let rptDataObj = {};
  249. rptDataObj[JV.DATA_DISCRETE_DATA] = [];
  250. rptDataObj[JV.DATA_MASTER_DATA] = [];
  251. rptDataObj[JV.DATA_DETAIL_DATA] = [];
  252. rptDataObj[JV.DATA_MASTER_DATA_EX] = [];
  253. rptDataObj[JV.DATA_DETAIL_DATA_EX] = [];
  254. rptDataObj.DecimalObj = {};
  255. rptDataObj.DecimalObj.prjDecimal = $PROJECT.MAIN["myOwnRawDataObj"].decimal; //为函数 P_REF() 准备数据
  256. rptDataObj.DecimalObj.unitDecimal = getUnitDecimal($PROJECT.MAIN["myOwnRawDataObj"].billsQuantityDecimal); //为函数 U_REF() 准备数据
  257. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS], rptDataObj[JV.DATA_DISCRETE_DATA], $PROJECT);
  258. // console.log(JV.DATA_DISCRETE_DATA);
  259. // console.log(rptDataObj[JV.DATA_DISCRETE_DATA]);
  260. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS], rptDataObj[JV.DATA_MASTER_DATA], $PROJECT);
  261. // console.log(JV.DATA_MASTER_DATA);
  262. // console.log(rptDataObj[JV.DATA_MASTER_DATA]);
  263. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS], rptDataObj[JV.DATA_DETAIL_DATA], $PROJECT);
  264. // console.log(JV.DATA_DETAIL_DATA);
  265. // console.log(rptDataObj[JV.DATA_DETAIL_DATA]);
  266. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX], rptDataObj[JV.DATA_MASTER_DATA_EX], $PROJECT);
  267. // console.log(JV.DATA_MASTER_DATA_EX);
  268. // console.log(rptDataObj[JV.DATA_MASTER_DATA_EX]);
  269. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX], rptDataObj[JV.DATA_DETAIL_DATA_EX], $PROJECT);
  270. // console.log(JV.DATA_DETAIL_DATA_EX);
  271. // console.log(rptDataObj[JV.DATA_DETAIL_DATA_EX]);
  272. // fsUtil.writeObjToFile(rptDataObj, "D:/GitHome/ConstructionCost/tmp/insertedOriginalData.jsp");
  273. // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/insertedRawDataData.jsp");
  274. // fsUtil.writeObjToFile($PROJECT, "D:/GitHome/ConstructionCost/tmp/$PROJECTData.jsp");
  275. return rptDataObj;
  276. };
  277. }
  278. //小数位数依据单位来调整(如吨、公斤的精度要求就不同)
  279. function getUnitDecimal(unitDecimalArr) {
  280. let rst = {"default_decimal": 2};
  281. if (unitDecimalArr) {
  282. for (let item of unitDecimalArr) {
  283. if (item.unit === "其他未列单位") {
  284. rst["default_decimal"] = item.decimal;
  285. } else {
  286. rst['unit_decimal_' + item.unit] = item.decimal;
  287. }
  288. }
  289. }
  290. return rst;
  291. }
  292. function getModuleDataByKey(prjData, key) {
  293. let rst = null;
  294. for (let item of prjData) {
  295. if (item.moduleName === key) {
  296. rst = item;
  297. break;
  298. }
  299. }
  300. return rst;
  301. }
  302. function summaryData(sourceData, handleCfg, prjData){
  303. let rstArr = [], tempRstArr = [];
  304. let curParentPrjData = {};
  305. for (let item of getActDataArr(sourceData)) {
  306. if (item._doc) {
  307. tempRstArr.push(item._doc);
  308. } else {
  309. tempRstArr.push(item);
  310. }
  311. }
  312. let private_cal_before_sum = function () {
  313. for (let calcAheadObj of handleCfg[JV.PROP_SUM_CALC_AHEAD]) {
  314. if (!curParentPrjData[calcAheadObj["seeking_parent"]]) curParentPrjData[calcAheadObj["seeking_parent"]] = getModuleDataByKey(prjData, calcAheadObj["seeking_parent"]);
  315. for (let idxP = 0; idxP < curParentPrjData[calcAheadObj["seeking_parent"]].data.length; idxP++) {
  316. let pDataItem = curParentPrjData[calcAheadObj["seeking_parent"]].data[idxP];
  317. let data = (pDataItem._doc)?pDataItem._doc:pDataItem;
  318. for (let idx = 0; idx < tempRstArr.length; idx++) {
  319. let dtlItem = tempRstArr[idx];
  320. if (dtlItem[calcAheadObj["seeking_key"]] === data[calcAheadObj["parent_key"]]) {
  321. for (let sumKey of handleCfg[JV.PROP_SUM_SUM_KEYS]) {
  322. switch (calcAheadObj[JV.PROP_SUM_CACL_TYPE]) {
  323. case "+":
  324. dtlItem[sumKey] = parseFloat(dtlItem[sumKey]) + parseFloat(data[calcAheadObj["calc_property"]]);
  325. break;
  326. case "-":
  327. dtlItem[sumKey] = parseFloat(dtlItem[sumKey]) - parseFloat(data[calcAheadObj["calc_property"]]);
  328. break;
  329. case "*":
  330. dtlItem[sumKey] = dtlItem[sumKey] * parseFloat(data[calcAheadObj["calc_property"]]).toFixed(4);
  331. break;
  332. case "/":
  333. dtlItem[sumKey] = dtlItem[sumKey] / parseFloat(data[calcAheadObj["calc_property"]]).toFixed(4);
  334. break;
  335. default:
  336. break;
  337. }
  338. }
  339. // break;
  340. }
  341. }
  342. }
  343. }
  344. };
  345. let private_get_grp_key = function (item) {
  346. let keys = [];
  347. for (let cfg of handleCfg[JV.PROP_SUM_GROUP_KEYS]) {
  348. if (typeof cfg === "string") {
  349. keys.push(item[cfg]);
  350. } else {
  351. if (!curParentPrjData[cfg["seeking_parent"]]) curParentPrjData[cfg["seeking_parent"]] = getModuleDataByKey(prjData, cfg["seeking_parent"]);
  352. for (let pDataItem of curParentPrjData[cfg["seeking_parent"]].data) {
  353. let data = (pDataItem._doc)?pDataItem._doc:pDataItem;
  354. if (item[cfg["seeking_key"]] === data[cfg["parent_key"]]) {
  355. keys.push(data[cfg["parent_grp_key"]]);
  356. break;
  357. }
  358. }
  359. }
  360. }
  361. return ( "grp_key_" + keys.join('_'));
  362. };
  363. let sumObj = {};
  364. if (handleCfg[JV.PROP_SUM_CALC_AHEAD] && handleCfg[JV.PROP_SUM_CALC_AHEAD].length > 0) {
  365. private_cal_before_sum();
  366. }
  367. for (let dtl of tempRstArr) {
  368. let grpKey = private_get_grp_key(dtl);
  369. if (sumObj[grpKey] === null || sumObj[grpKey] === undefined) {
  370. sumObj[grpKey] = dtl;
  371. rstArr.push(dtl);
  372. } else {
  373. for (let sumKey of handleCfg[JV.PROP_SUM_SUM_KEYS]) {
  374. if (dtl[sumKey]) {
  375. // sumObj[grpKey][sumKey] += dtl[sumKey];
  376. sumObj[grpKey][sumKey] = parseFloat(sumObj[grpKey][sumKey]) + parseFloat(dtl[sumKey]);
  377. }
  378. }
  379. }
  380. }
  381. replaceActDataArr(sourceData, rstArr);
  382. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/sumRst.jsp");
  383. }
  384. function filterData(sourceData, handleCfg, prjData) {
  385. let rstArr = [], tempRstArr = [];
  386. for (let item of getActDataArr(sourceData)) {
  387. if (item._doc) {
  388. tempRstArr.push(item._doc);
  389. } else {
  390. tempRstArr.push(item);
  391. }
  392. }
  393. let private_chkVal = function (src, compVal, compStr) {
  394. let rst = true;
  395. switch (compStr) {
  396. case "==" :
  397. rst = (src == compVal);
  398. break;
  399. case "===" :
  400. rst = (src === compVal);
  401. break;
  402. case ">" :
  403. rst = (src > compVal);
  404. break;
  405. case ">=" :
  406. rst = (src >= compVal);
  407. break;
  408. case "<" :
  409. rst = (src < compVal);
  410. break;
  411. case "<=" :
  412. rst = (src <= compVal);
  413. break;
  414. case "!=" :
  415. rst = (src != compVal);
  416. break;
  417. case "!==" :
  418. rst = (src !== compVal);
  419. break;
  420. case "in" :
  421. if (compVal instanceof Array) {
  422. rst = compVal.indexOf(src) >= 0;
  423. } else {
  424. //string,需要转类型
  425. let newCv = JSON.parse(compVal);
  426. if (newCv instanceof Array) {
  427. rst = newCv.indexOf(src) >= 0;
  428. } else {
  429. rst = false;
  430. }
  431. }
  432. break;
  433. case "not in":
  434. if (compVal instanceof Array) {
  435. rst = compVal.indexOf(src) < 0;
  436. } else {
  437. //string,需要转类型
  438. let newCv = JSON.parse(compVal);
  439. if (newCv instanceof Array) {
  440. rst = newCv.indexOf(src) < 0;
  441. } else {
  442. rst = true;
  443. }
  444. }
  445. break;
  446. default:
  447. rst = true;
  448. }
  449. return rst;
  450. };
  451. let private_chkArrVal = function(arr, key, compVal, compStr){
  452. let rst = false;
  453. for (let arrItem of arr) {
  454. if (arrItem[key]) {
  455. rst = private_chkVal(arrItem[key], compVal, compStr);
  456. }
  457. if (rst) {
  458. break;
  459. }
  460. }
  461. return rst;
  462. };
  463. let private_filter_compare = function (item, filterCfg) {
  464. let compareObj = {};
  465. let compRst = true;
  466. let curComparePrjData = null;
  467. let startIdx = 0;
  468. let private_ref_join = function(refKey, targetDataKey, targetPropertyKey) {
  469. let rst = null, objDataArr = null;
  470. curComparePrjData = getModuleDataByKey(prjData, targetDataKey);
  471. try {
  472. if (curComparePrjData !== null) {
  473. objDataArr = getActDataArr(curComparePrjData);
  474. for (let dtl of objDataArr) {
  475. if (item[refKey] === dtl[targetPropertyKey]) {
  476. rst = dtl;
  477. break;
  478. }
  479. }
  480. }
  481. } finally {
  482. curComparePrjData = null;
  483. }
  484. return rst;
  485. };
  486. for (let cfg of filterCfg[JV.PROP_FILTER_KEYS]) {
  487. if (cfg[JV.PROP_FILTER_COMPARE_VAL]) {
  488. //比较key值
  489. let keys = cfg.key.split(".");
  490. if (keys.length > 1) {
  491. let lastObj = item;
  492. for (let i = 0; i < keys.length - 1; i++) {
  493. if (keys[i].indexOf("ref_join(") === 0) {
  494. let params = keys[i].slice(9, keys[i].length - 1).split(",");
  495. if (params.length === 3) {
  496. lastObj = private_ref_join(params[0], params[1], params[2]);
  497. }
  498. if (!(lastObj)) {
  499. compRst = false;
  500. break;
  501. }
  502. } else {
  503. lastObj = item[keys[i]];
  504. if (!(lastObj)) {
  505. compRst = false;
  506. break;
  507. }
  508. }
  509. }
  510. if (lastObj) {
  511. if (lastObj instanceof Array) {
  512. compRst = private_chkArrVal(lastObj, keys[keys.length - 1], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
  513. } else {
  514. compRst = private_chkVal(lastObj[keys[keys.length - 1]], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
  515. }
  516. }
  517. } else {
  518. compRst = private_chkVal(item[cfg.key], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
  519. }
  520. } else if (cfg[JV.PROP_FILTER_COMPARE_OBJ] && cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]){
  521. //通过其他对象来过滤
  522. if (!curComparePrjData) {
  523. curComparePrjData = getModuleDataByKey(prjData, cfg[JV.PROP_FILTER_COMPARE_OBJ]);
  524. }
  525. if (cfg[JV.PROP_FILTER_CONDITION] === "in" || cfg[JV.PROP_FILTER_CONDITION] === "not in") {
  526. let compareArr = null;
  527. if (!compareObj.hasOwnProperty(cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString())) {
  528. compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()] = [];
  529. compareArr = compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()];
  530. for (let data of getActDataArr(curComparePrjData)) {
  531. if (compareArr.indexOf(data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]]) < 0) {
  532. compareArr.push(data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]]);
  533. }
  534. }
  535. } else {
  536. compareArr = compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()];
  537. }
  538. compRst = private_chkVal(item[cfg.key], compareArr, cfg[JV.PROP_FILTER_CONDITION]);
  539. } else {
  540. for (let data of getActDataArr(curComparePrjData)) {
  541. compRst = private_chkVal(item[cfg.key], data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]], cfg[JV.PROP_FILTER_CONDITION]);
  542. if (compRst) break;
  543. }
  544. }
  545. }
  546. startIdx++;
  547. }
  548. return compRst;
  549. };
  550. for (let item of tempRstArr) {
  551. if (private_filter_compare(item, handleCfg)) {
  552. rstArr.push(item);
  553. }
  554. }
  555. if (handleCfg[JV.PROP_OTHER_SUB_FILTER] && handleCfg[JV.PROP_OTHER_SUB_FILTER].length > 0) {
  556. let newRstArr = [];
  557. for (let dtlItem of rstArr) {
  558. let cmpRst = false;
  559. for (let dtlCfg of handleCfg[JV.PROP_OTHER_SUB_FILTER]) {
  560. cmpRst = private_filter_compare(dtlItem, dtlCfg);
  561. if (cmpRst) {
  562. newRstArr.push(dtlItem);
  563. break;
  564. }
  565. }
  566. }
  567. rstArr = newRstArr;
  568. }
  569. replaceActDataArr(sourceData, rstArr);
  570. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/filteredRst.js");
  571. }
  572. function adjustData(sourceData, adjustCfg) {
  573. let rstArr = [];
  574. for (let item of getActDataArr(sourceData)) {
  575. if (item._doc) {
  576. rstArr.push(item._doc);
  577. } else {
  578. rstArr.push(item);
  579. }
  580. }
  581. for (let item of adjustCfg[JV.PROP_ADJUST_COLLECTION]) {
  582. for (let rec of rstArr) {
  583. if (item[JV.PROP_ADJUST_ACTION] === "prefix") {
  584. rec[item.key] = item[JV.PROP_ADJUST_ACTION_VAL] + rec[item.key];
  585. } else if (item[JV.PROP_ADJUST_ACTION] === "suffix") {
  586. rec[item.key] = rec[item.key] + item[JV.PROP_ADJUST_ACTION_VAL];
  587. }
  588. }
  589. }
  590. replaceActDataArr(sourceData, rstArr);
  591. }
  592. function moveRationData(rationData, rawDataObj) {
  593. if (rawDataObj) {
  594. // let rationData = getModuleDataByKey(rawDataObj.prjData, projectConst.RATION);
  595. let rationGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.RATION_GLJ);
  596. let prjGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.PROJECTGLJ);
  597. let dummyCode = " ", dummyPRJID = 10101010;
  598. for (let rationItem of getActDataArr(rationData)) {
  599. if (rationItem.type === 2 || rationItem.type === 3) {
  600. //2: 量材(人工/材料/机械/主材/设备) 3:材料(材料/主材/设备)
  601. let copyItem = {};
  602. copyItem.ID = rationItem.ID;
  603. copyItem.projectID = rationItem.projectID;
  604. copyItem.GLJID = rationItem.GLJID;
  605. copyItem.rationID = rationItem.ID;
  606. copyItem.rationItemQuantity = 1;
  607. copyItem.quantity = 1;
  608. // copyItem.specialType_quantity = rationItem.quantity;
  609. copyItem.name = rationItem.name;
  610. if (rationItem.code) {
  611. copyItem.code = rationItem.ID + "_" + rationItem.code;
  612. } else {
  613. copyItem.code = dummyCode;
  614. dummyCode = dummyCode + " ";
  615. }
  616. copyItem.original_code = rationItem.original_code;
  617. copyItem.unit = rationItem.unit;
  618. copyItem.specs = rationItem.specs;
  619. copyItem.shortName = rationItem.shortName;
  620. copyItem.billsItemID = rationItem.billsItemID;
  621. copyItem.type = rationItem.subType;
  622. if (rationItem.projectGLJID) {
  623. copyItem.projectGLJID = rationItem.projectGLJID;
  624. } else {
  625. //add dummy project GLJ
  626. let dummyPrjItem = {};
  627. copyItem.projectGLJID = dummyPRJID;
  628. dummyPrjItem.id = dummyPRJID;
  629. dummyPRJID++;
  630. dummyPrjItem.unit_price = {};
  631. dummyPrjItem.unit_price.base_price = 0;
  632. dummyPrjItem.unit_price.market_price = (rationItem["marketUnitFee"])?rationItem["marketUnitFee"]:0;
  633. dummyPrjItem.quantity = 0;
  634. dummyPrjItem.is_evaluate = 0;
  635. prjGljData.data.gljList.push(dummyPrjItem);
  636. }
  637. rationGljData.data.push(copyItem);
  638. }
  639. }
  640. // fsUtil.writeObjToFile(rationGljData.data, "D:/GitHome/ConstructionCost/tmp/afterMoveGLJ.jsp");
  641. }
  642. }
  643. function getDupGrpKeyVals(sourceData, segKeys) {
  644. let rst = [];
  645. function pushKeyVal(item) {
  646. let tr = {};
  647. for (let i = 0; i < segKeys.length; i++) {
  648. tr[segKeys[i]] = item[segKeys[i]];
  649. }
  650. rst.push(tr);
  651. }
  652. for (let idx = 0; idx < sourceData.length; idx++) {
  653. let itemRec = sourceData[idx];
  654. if (idx === 0) {
  655. pushKeyVal(itemRec);
  656. continue;
  657. }
  658. let hasDiff = false;
  659. for (let i = 0; i < segKeys.length; i++) {
  660. if (itemRec[segKeys[i]] !== sourceData[idx - 1][segKeys[i]]) {
  661. hasDiff = true;
  662. break;
  663. }
  664. }
  665. if (hasDiff) {
  666. pushKeyVal(itemRec);
  667. }
  668. }
  669. return rst;
  670. }
  671. function addDummyData(sourceData, addCfg) {
  672. let rstArr = [], tempRstArr = [];
  673. for (let item of getActDataArr(sourceData)) {
  674. if (item._doc) {
  675. tempRstArr.push(item._doc);
  676. } else {
  677. tempRstArr.push(item);
  678. }
  679. }
  680. for (let item of addCfg[JV.PROP_DUMMY_COLLECTION]) {
  681. let newRecStr = JSON.stringify(item[JV.PROP_DUMMY_VAL]), cacheGrpKeyRecs = null;
  682. if (item[JV.PROP_FREQUENCY] === "OncePerGrp") {
  683. if (!cacheGrpKeyRecs) {
  684. cacheGrpKeyRecs = {};
  685. }
  686. let cacheKey = "";
  687. for (let key of item[JV.PROP_GRP_KEYS]) {
  688. cacheKey += "_" + key;
  689. }
  690. if (!cacheGrpKeyRecs[cacheKey]) {
  691. cacheGrpKeyRecs[cacheKey] = getDupGrpKeyVals(tempRstArr, item[JV.PROP_GRP_KEYS]);
  692. }
  693. for (let kv of cacheGrpKeyRecs[cacheKey]) {
  694. let rec = JSON.parse(newRecStr);
  695. for (let key of item[JV.PROP_GRP_KEYS]) {
  696. rec[key] = kv[key];
  697. }
  698. rstArr.push(rec);
  699. }
  700. } else if (item[JV.PROP_FREQUENCY] === "Once") {
  701. rstArr.push(JSON.parse(newRecStr));
  702. }
  703. }
  704. rstArr = rstArr.concat(tempRstArr);
  705. replaceActDataArr(sourceData, rstArr);
  706. }
  707. function getGLJBizType(orgType, orgCode, orgName) {
  708. let rst = orgType;
  709. if (orgName.indexOf("其他材料费") >= 0) {
  710. rst = 299;
  711. } else if (orgType === GLJ_TYPE.Labour) {
  712. rst = 11;
  713. if (orgCode === "000000") rst = 10;
  714. } else if (orgType === GLJ_TYPE.Main_Material || orgType === GLJ_TYPE.Equipment) {
  715. //未计价材料(主材 + 设备)
  716. rst = 30 + orgType;
  717. } else if (orgType === GLJ_TYPE.Material || (orgType >= 200 && orgType < 300)) {
  718. //材料
  719. if (orgCode === "000000") {
  720. rst = 20; //2.材料
  721. } else if (orgCode === "000000_1") {
  722. rst = 30; //(1) 未计价材料
  723. } else if (orgCode === "000000_2") {
  724. rst = 40; //(2) 辅助材料
  725. } else if (orgCode === "000000_3") {
  726. rst = 50; //(3) 其他材料费
  727. } else {
  728. rst = 45; //到这里就只有辅助材料没有预处理了
  729. }
  730. } else if (orgType === GLJ_TYPE.Machine || (orgType >= 300 && orgType < 400)) {
  731. //机械
  732. if (orgCode === "000000") {
  733. rst = 300; //3.机械
  734. } else if (orgType === GLJ_TYPE.Machine) {
  735. rst = 300.5;
  736. } else {
  737. rst = orgType;
  738. }
  739. }
  740. return rst;
  741. }
  742. function sortData(sourceData, sortCfg, prjData) {
  743. let rst = getActDataArr(sourceData), tempRstArr = [];
  744. let sortType = sortCfg[JV.PROP_SORT_TYPE];
  745. let srcData = getActDataArr(sourceData);
  746. for (let item of srcData) {
  747. if (item._doc) {
  748. tempRstArr.push(item._doc);
  749. } else {
  750. tempRstArr.push(item);
  751. }
  752. }
  753. function private_normal_sort(destArr, sortKeys) {
  754. destArr.sort(function(a, b){
  755. let compRst = 0;
  756. for (let comp of sortKeys) {
  757. let reverse = (comp.order === 'ascend')?1:(-1);
  758. //
  759. if (a[comp.key] > b[comp.key]) {
  760. compRst = reverse;
  761. break;
  762. } else if (a[comp.key] < b[comp.key]) {
  763. compRst = -reverse;
  764. break;
  765. }
  766. }
  767. return compRst;
  768. });
  769. }
  770. function private_parent_sort(parentArr, parentKeys, childArr, childKeys) {
  771. let tmpRst = {}, rst = [];
  772. for (let pItem of parentArr) {
  773. let pKey = "key";
  774. for (let key of parentKeys) {
  775. pKey += "_" + pItem[key];
  776. }
  777. tmpRst[pKey] = [];
  778. }
  779. for (let cItem of childArr) {
  780. let cKey = "key";
  781. for (let key of childKeys) {
  782. cKey += "_" + cItem[key];
  783. }
  784. if (tmpRst[cKey]) {
  785. tmpRst[cKey].push(cItem);
  786. } else {
  787. //unknown child value! should be filtered!
  788. }
  789. }
  790. // childArr.splice(0);
  791. for (let pItem of parentArr) {
  792. let pKey = "key";
  793. for (let key of parentKeys) {
  794. pKey += "_" + pItem[key];
  795. }
  796. rst.push(tmpRst[pKey]);
  797. // for (let rItem of tmpRst[pKey]) {
  798. // childArr.push(rItem);
  799. // }
  800. }
  801. return rst;
  802. }
  803. switch (sortType) {
  804. case "tree":
  805. let addLevel = true;
  806. rst = treeUtil.buildTreeNodeDirectly(tempRstArr, addLevel);
  807. let newTopArr = [];
  808. if ((sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES] && sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].length > 0) ||
  809. (sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES] && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].length > 0)) {
  810. let local_check_bills = function(tItem) {
  811. let chkDtl = false;
  812. if (tItem.flags && tItem.flags.length > 0) {
  813. for (let flagItem of tItem.flags) {
  814. if (sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].indexOf(flagItem.flag) >= 0) {
  815. newTopArr.push(tItem);
  816. chkDtl = true;
  817. break;
  818. }
  819. }
  820. }
  821. if (!chkDtl && tItem.items && tItem.items.length > 0) {
  822. for (let dtlItem of tItem.items) {
  823. local_check_bills(dtlItem);
  824. }
  825. }
  826. };
  827. for (let topItem of rst) {
  828. let chkTop = false;
  829. if (topItem.flags && topItem.flags.length > 0) {
  830. for (let flagItem of topItem.flags) {
  831. if (sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].indexOf(flagItem.flag) >= 0) {
  832. newTopArr.push(topItem);
  833. chkTop = true;
  834. break;
  835. }
  836. }
  837. }
  838. if (!chkTop && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES] && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].length > 0) {
  839. local_check_bills(topItem);
  840. }
  841. }
  842. } else {
  843. newTopArr = rst;
  844. }
  845. let destArr = [];
  846. treeUtil.getFlatArray(newTopArr, destArr);
  847. replaceActDataArr(sourceData, destArr);
  848. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/sortedAndFlattedRst.jsp");
  849. break;
  850. case "normal":
  851. private_normal_sort(tempRstArr, sortCfg[JV.PROP_SORT_KEYS]);
  852. replaceActDataArr(sourceData, tempRstArr);
  853. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/normalSortedRst.jsp");
  854. break;
  855. case "accord_to_parent":
  856. let pcKey = sortCfg[JV.PROP_PARENT_CHILD_SORT_KEY];
  857. let parentSrcData = getModuleDataByKey(prjData, pcKey[JV.PROP_PARENT_DATA_KEY]);
  858. if (parentSrcData) {
  859. let tempParentArr = [];
  860. for (let item of getActDataArr(parentSrcData)) {
  861. if (item._doc) {
  862. tempParentArr.push(item._doc);
  863. } else {
  864. tempParentArr.push(item);
  865. }
  866. }
  867. let sortedRstArr = private_parent_sort(tempParentArr, pcKey[JV.PROP_PARENT_SORT_KEYS], tempRstArr, pcKey[JV.PROP_CHILD_SORT_KEYS]);
  868. if (sortCfg[JV.PROP_OTHER_SUB_SORT] && sortCfg[JV.PROP_OTHER_SUB_SORT].length > 0) {
  869. for (let sort of sortCfg[JV.PROP_OTHER_SUB_SORT]) {
  870. if (sort[JV.PROP_SORT_TYPE] === 'normal') {
  871. for (let subArr of sortedRstArr) {
  872. private_normal_sort(subArr, sort[JV.PROP_SORT_KEYS]);
  873. }
  874. } else if (sort[JV.PROP_SORT_TYPE] === 'self_define') {
  875. for (let subArr of sortedRstArr) {
  876. // console.log(subArr);
  877. let selfDefFunc = null;
  878. eval('selfDefFunc = ' + sort[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  879. subArr.sort(selfDefFunc);
  880. // console.log(subArr);
  881. }
  882. }
  883. }
  884. }
  885. tempRstArr.splice(0);
  886. for (let item of sortedRstArr) {
  887. for (let subItem of item) {
  888. tempRstArr.push(subItem);
  889. }
  890. }
  891. }
  892. replaceActDataArr(sourceData, tempRstArr);
  893. break;
  894. case "self_define":
  895. if (sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]) {
  896. let selfDefFunc = null;
  897. eval('selfDefFunc = ' + sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  898. tempRstArr.sort(selfDefFunc);
  899. }
  900. replaceActDataArr(sourceData, tempRstArr);
  901. break;
  902. default:
  903. //
  904. }
  905. return rst;
  906. }
  907. function setupFunc(obj, prop, ownRawObj) {
  908. obj[prop] = {};
  909. obj[prop]["myOwnRawDataObj"] = ownRawObj;
  910. obj[prop]["myOwnOrgRawDataObj"] = ownRawObj.data;
  911. obj[prop].getProperty = ext_getPropety;
  912. obj[prop].getPropertyByRefId = ext_getPropertyByRefId;
  913. obj[prop].getFee = ext_getFee;
  914. obj[prop].getPropertyByForeignId = ext_getPropertyByForeignId;
  915. obj[prop].getArrayValues = ext_getArrayValues;
  916. obj[prop].getArrayItemByKey = ext_getArrayItemByKey;
  917. obj[prop].getPropertyByFlag = ext_getPropertyByFlag;
  918. obj[prop].getBlank = ext_getBlank;
  919. if (prop === projectConst.CALC_PROGRAM) obj[prop].getCalcProperty = ext_getCalcProperty;
  920. if (prop === projectConst.FEERATE) obj[prop].getFeeRate = ext_getFeeRate;
  921. }
  922. function assembleFields(fieldList, rstDataArr, $PROJECT) {
  923. if (fieldList) {
  924. for (let field of fieldList) {
  925. shielded_exec_env($PROJECT, field, rstDataArr);
  926. }
  927. }
  928. }
  929. function shielded_exec_env($PROJECT, $ME, rptDataItemObj) {
  930. if ($ME[JV.PROP_FIELD_EXP_MAP]) {
  931. rptDataItemObj.push(eval($ME[JV.PROP_FIELD_EXP_MAP]));
  932. }
  933. }
  934. function getActPropertyVal(firstPropKey, secPropKey, orgObj) {
  935. let rst = null;
  936. if (orgObj[firstPropKey]) {
  937. rst = orgObj[firstPropKey];
  938. } else if (orgObj[secPropKey]){
  939. rst = orgObj[secPropKey];
  940. }
  941. return rst;
  942. }
  943. function getDeepProperty(propKey, orgObj, destArr) {
  944. let keys = propKey.split(".");
  945. let dftPropKey = "key", dftPropVal = "value", secDftPropVal = "items";
  946. let parent = orgObj, lastVal = null;
  947. for (let key of keys) {
  948. if (parent instanceof Array) {
  949. for (let item of parent) {
  950. if (item[dftPropKey] === key) {
  951. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  952. break;
  953. }
  954. }
  955. } else {
  956. lastVal = null;
  957. if (parent[key] !== undefined) {
  958. lastVal = parent[key];
  959. } else if (parent[secDftPropVal]){
  960. for (let item of parent[secDftPropVal]) {
  961. if (item[dftPropKey] === key) {
  962. // lastVal = item[dftPropVal];
  963. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  964. break;
  965. }
  966. }
  967. }
  968. }
  969. parent = lastVal;
  970. if (parent === null) break;
  971. }
  972. if (destArr && destArr instanceof Array) {
  973. destArr.push(lastVal);
  974. }
  975. }
  976. function ext_mainGetPropety(propKey) {
  977. let rst = [], parentObj = this;
  978. let dtObj = parentObj["myOwnRawDataObj"];
  979. if (propKey && dtObj) {
  980. if (dtObj.hasOwnProperty("property")) {
  981. if (!dtObj["property"][propKey] && dtObj[propKey]) {
  982. rst.push(dtObj[propKey]);
  983. } else {
  984. getDeepProperty(propKey, dtObj["property"], rst);
  985. }
  986. } else {
  987. // rst.push(dtObj[propKey]);
  988. getDeepProperty(propKey, dtObj, rst);
  989. }
  990. }
  991. return rst;
  992. }
  993. function ext_getPropety(propKey) {
  994. let rst = [], parentObj = this;
  995. let dtObj = parentObj["myOwnRawDataObj"];
  996. if (propKey && dtObj) {
  997. for (let dItem of getActDataArr(dtObj)) {
  998. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  999. if (propKey instanceof Array) {
  1000. for (let pi = 0; pi < propKey.length; pi++) {
  1001. if (doc.hasOwnProperty("property")) {
  1002. if (doc["property"].hasOwnProperty(propKey[pi])) {
  1003. rst.push(doc["property"][propKey[pi]]);
  1004. break;
  1005. }
  1006. } else if (doc.hasOwnProperty(propKey[pi])) {
  1007. rst.push(doc[propKey[pi]]);
  1008. break;
  1009. }
  1010. if (pi === propKey.length - 1) rst.push('');
  1011. }
  1012. } else {
  1013. if (doc.hasOwnProperty("property")) {
  1014. rst.push(doc["property"][propKey]);
  1015. } else if (doc.hasOwnProperty(propKey)) {
  1016. rst.push(doc[propKey]);
  1017. } else {
  1018. rst.push('');
  1019. }
  1020. }
  1021. }
  1022. }
  1023. return rst;
  1024. }
  1025. function ext_getPropertyByRefId(baseKey, refIDKey, propertyKey){
  1026. let rst = [], parentObj = this;
  1027. let dtObj = parentObj["myOwnRawDataObj"];
  1028. let orgDtObj = parentObj["myOwnOrgRawDataObj"];
  1029. if (baseKey && refIDKey && propertyKey && dtObj) {
  1030. for (let dItem of getActDataArr(dtObj)) {
  1031. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  1032. let tmpRst = doc[baseKey];
  1033. if (typeof tmpRst === "string" && tmpRst[0] === "@") {
  1034. let refKey = tmpRst.slice(1);
  1035. let hasGetRef = false;
  1036. for (let orgDItem of orgDtObj) {
  1037. let oDoc = (orgDItem._doc === null || orgDItem._doc === undefined)?orgDItem:orgDItem._doc;
  1038. if (oDoc[refIDKey] === refKey) {
  1039. rst.push(oDoc[propertyKey]);
  1040. hasGetRef = true;
  1041. break;
  1042. }
  1043. }
  1044. if (!hasGetRef) {
  1045. rst.push(tmpRst);
  1046. }
  1047. } else {
  1048. rst.push(tmpRst);
  1049. }
  1050. }
  1051. }
  1052. return rst;
  1053. }
  1054. function ext_mainGetFee(feeKey, dtlFeeKey) {
  1055. let rst = [];
  1056. let parentObj = this;
  1057. let dtObj = parentObj["myOwnRawDataObj"];
  1058. if ((dtObj) && (feeKey)) {
  1059. if (dtObj.hasOwnProperty("fees")) {
  1060. for (let fee of dtObj["fees"]) {
  1061. if (fee["fieldName"] === feeKey) {
  1062. if (dtlFeeKey) {
  1063. rst.push(fee[dtlFeeKey]);
  1064. } else {
  1065. rst.push(fee["unitFee"]);
  1066. }
  1067. break;
  1068. }
  1069. }
  1070. } else if (dtObj.hasOwnProperty(feeKey)) {
  1071. rst.push(dtObj[feeKey]);
  1072. } else {
  1073. //
  1074. }
  1075. }
  1076. return rst;
  1077. }
  1078. function ext_getFee(feeKey, dtlFeeKey) {
  1079. let rst = [], parentObj = this;
  1080. let dtObj = parentObj["myOwnRawDataObj"];
  1081. if (feeKey && dtObj) {
  1082. for (let dItem of getActDataArr(dtObj)) {
  1083. rst.push(pri_getFee(dItem, feeKey, dtlFeeKey));
  1084. }
  1085. }
  1086. for (let i = 0; i < rst.length; i++) {
  1087. rst[i] = parseFloat(rst[i]);
  1088. }
  1089. return rst;
  1090. }
  1091. function pri_getFee(dItem, feeKey, dtlFeeKey) {
  1092. let rst = 0;
  1093. let hasValue = false;
  1094. if (dItem["fees"]) {
  1095. for (let fee of dItem["fees"]) {
  1096. if (fee["fieldName"] === feeKey) {
  1097. if (dtlFeeKey) {
  1098. rst = fee[dtlFeeKey];
  1099. } else {
  1100. rst = fee["unitFee"];
  1101. }
  1102. hasValue = true;
  1103. break;
  1104. }
  1105. }
  1106. } else if (dItem[feeKey]) {
  1107. hasValue = true;
  1108. rst = dItem[feeKey];
  1109. } else {
  1110. hasValue = true;
  1111. rst = 0;
  1112. }
  1113. if (!hasValue) {
  1114. rst = 0;
  1115. }
  1116. return rst;
  1117. }
  1118. function ext_getCalcProperty(templateIDs, calcItemKey, calcItemKeyVal, calcItemRstKey){
  1119. let rst = [], parentObj = this; //this should be "calc_program" object
  1120. let dtObj = parentObj["myOwnRawDataObj"];
  1121. let optimizeObj = {};
  1122. let private_getProperty = function (cId) {
  1123. let calcTplObj = optimizeObj["calc_program_" + cId];
  1124. if (!calcTplObj) {
  1125. let templates = (dtObj.data._doc)?dtObj.data._doc.templates:dtObj.data.templates;
  1126. for (let tpl of templates) {
  1127. if (cId === tpl.ID) {
  1128. optimizeObj["calc_program_" + cId] = tpl;
  1129. calcTplObj = tpl;
  1130. break;
  1131. }
  1132. }
  1133. }
  1134. if (calcTplObj) {
  1135. for (let calcItem of calcTplObj.calcItems) {
  1136. if (calcItem[calcItemKey] === calcItemKeyVal) {
  1137. rst.push(calcItem[calcItemRstKey]);
  1138. break;
  1139. }
  1140. }
  1141. }
  1142. };
  1143. if (templateIDs instanceof Array) {
  1144. for (let tplId of templateIDs) {
  1145. private_getProperty(tplId);
  1146. }
  1147. } else {
  1148. private_getProperty(templateIDs);
  1149. }
  1150. optimizeObj = null;
  1151. return rst;
  1152. }
  1153. function ext_getFeeRate(fee_Ids){
  1154. let rst = [], parentObj = this; //this should be "feeRate" object
  1155. let dtObj = parentObj["myOwnRawDataObj"];
  1156. let optimizeObj = {};
  1157. let private_getFeeRate = function (fId) {
  1158. let feeRateItemObj = optimizeObj["fee_rates_" + fId];
  1159. if (!feeRateItemObj) {
  1160. let rates = (dtObj.data._doc)?dtObj.data._doc.rates:dtObj.data.rates;
  1161. for (let feeItem of rates) {
  1162. if (fId === feeItem.ID) {
  1163. optimizeObj["fee_rates_" + fId] = feeItem;
  1164. feeRateItemObj = feeItem;
  1165. break;
  1166. }
  1167. }
  1168. }
  1169. if (feeRateItemObj) {
  1170. rst.push(feeRateItemObj.rate);
  1171. } else {
  1172. rst.push(0);
  1173. }
  1174. };
  1175. if (fee_Ids instanceof Array) {
  1176. for (let fId of fee_Ids) {
  1177. private_getFeeRate(fId);
  1178. }
  1179. } else {
  1180. private_getFeeRate(fee_Ids);
  1181. }
  1182. optimizeObj = null;
  1183. return rst;
  1184. }
  1185. function ext_getArrayValues(itemKey) {
  1186. let rst = [], parentObj = this;
  1187. let dtObj = parentObj["myOwnRawDataObj"];
  1188. let keysArr = itemKey.split(".");
  1189. for (let dataItem of getActDataArr(dtObj)) {
  1190. let itemArr = [];
  1191. if (keysArr.length <= 2) {
  1192. if (dataItem[keysArr[0]] instanceof Array) {
  1193. if (keysArr.length === 2) {
  1194. for (let item of dataItem[keysArr[0]]) {
  1195. itemArr.push(item[keysArr[1]]);
  1196. }
  1197. } else {
  1198. itemArr = itemArr.concat(dataItem[keysArr[0]]);
  1199. }
  1200. } else {
  1201. if (keysArr.length === 2) {
  1202. let subProperty = dataItem[keysArr[0]][keysArr[1]];
  1203. if (subProperty instanceof Array) {
  1204. itemArr = itemArr.concat(subProperty);
  1205. } else {
  1206. itemArr.push(subProperty);
  1207. }
  1208. } else {
  1209. itemArr.push(dataItem[keysArr[0]]);
  1210. }
  1211. }
  1212. }
  1213. rst.push(itemArr);
  1214. }
  1215. return rst;
  1216. }
  1217. function ext_getArrayItemByKey(arrayKey, itemKey, itemKeyValue, itemRstKey){
  1218. let rst = [], parentObj = this;
  1219. let dtObj = parentObj["myOwnRawDataObj"];
  1220. let private_getItemValue = function (arr, dtlItKV) {
  1221. for (let item of arr) {
  1222. if (item[itemKey] === dtlItKV) {
  1223. if (itemRstKey) {
  1224. rst.push(item[itemRstKey]);
  1225. } else {
  1226. rst.push(item);
  1227. }
  1228. break;
  1229. }
  1230. }
  1231. };
  1232. let arr = dtObj[arrayKey];
  1233. if (arr && arr instanceof Array) {
  1234. if (itemKeyValue instanceof Array) {
  1235. for (let dtlItemKeyVal of itemKeyValue) {
  1236. private_getItemValue(arr, dtlItemKeyVal);
  1237. }
  1238. } else {
  1239. private_getItemValue(arr, itemKeyValue);
  1240. }
  1241. }
  1242. }
  1243. function ext_getBlank(dftVal) {
  1244. let rst = [], parentObj = this;
  1245. let dtObj = parentObj["myOwnRawDataObj"];
  1246. if (dtObj) {
  1247. let dtData = getActDataArr(dtObj);
  1248. for (let i = 0; i < dtData.length; i++) {
  1249. if (dftVal !== null && dftVal !== undefined) {
  1250. rst.push(dftVal)
  1251. } else rst.push('');
  1252. }
  1253. }
  1254. return rst;
  1255. }
  1256. function ext_getPropertyByFlag(flagVal, rstKey, dftValIfEmpty) {
  1257. let rst = [], parentObj = this;
  1258. let dtObj = parentObj["myOwnRawDataObj"];
  1259. if (flagVal && rstKey && dtObj) {
  1260. let isArr = (flagVal instanceof Array);
  1261. for (let dItem of getActDataArr(dtObj)) {
  1262. let doc = (dItem._doc)?dItem._doc:dItem;
  1263. if (doc.hasOwnProperty("flags")) {
  1264. let bFlag = false;
  1265. for (let flagItem of doc.flags) {
  1266. if (isArr) {
  1267. bFlag = (flagVal.indexOf(flagItem.flag) >= 0);
  1268. } else {
  1269. if (flagItem.flag === flagVal) {
  1270. bFlag = true;
  1271. }
  1272. }
  1273. if (bFlag) break;
  1274. }
  1275. if (bFlag) {
  1276. let keys = rstKey.split(".");
  1277. if (keys[0] === "fees") {
  1278. rst.push(pri_getFee(doc, "common", keys[1]));
  1279. } else {
  1280. //其他,比如名称什么
  1281. }
  1282. break;
  1283. }
  1284. }
  1285. }
  1286. }
  1287. if (rst.length === 0 && dftValIfEmpty !== null) {
  1288. rst.push(dftValIfEmpty);
  1289. }
  1290. return rst;
  1291. }
  1292. function ext_getPropertyByForeignId(foreignIdVal, adHocIdKey, propKey, dftValIfNotFound) {
  1293. let rst = [], parentObj = this;
  1294. let IdKey = (adHocIdKey)?adHocIdKey:"ID";
  1295. let dtObj = parentObj["myOwnRawDataObj"];
  1296. let splitPKey = propKey.split(".");
  1297. if (foreignIdVal !== null && foreignIdVal !== undefined) {
  1298. let isFound = false;
  1299. if (foreignIdVal instanceof Array) {
  1300. for (let idVal of foreignIdVal) {
  1301. isFound = false;
  1302. let dataArr = getActDataArr(dtObj);
  1303. for (let i = 0; i < dataArr.length; i++) {
  1304. let item = (dataArr[i]._doc)?dataArr[i]._doc:dataArr[i];
  1305. if (item[IdKey] === idVal) {
  1306. if (splitPKey.length > 1) {
  1307. let rstP = null;
  1308. for (let i = 0; i < splitPKey.length; i++) {
  1309. if (i === 0) {
  1310. rstP = item[splitPKey[i]];
  1311. } else {
  1312. if (splitPKey[i].indexOf("[") === 0 && splitPKey[i].indexOf("]") === (splitPKey[i].length - 1)) {
  1313. //考虑数组情况^_^!!!
  1314. } else {
  1315. //
  1316. }
  1317. rstP = rstP[splitPKey[i]];
  1318. }
  1319. }
  1320. rst.push(rstP);
  1321. } else {
  1322. rst.push(item[propKey]);
  1323. }
  1324. isFound = true;
  1325. break;
  1326. }
  1327. }
  1328. if (!isFound) {
  1329. rst.push(dftValIfNotFound);
  1330. }
  1331. }
  1332. } else {
  1333. for (let item of getActDataArr(dtObj)) {
  1334. if (item[IdKey] === foreignIdVal) {
  1335. rst.push(item[propKey]);
  1336. isFound = true;
  1337. break;
  1338. }
  1339. }
  1340. if (!isFound) {
  1341. rst.push(dftValIfNotFound);
  1342. }
  1343. }
  1344. }
  1345. return rst;
  1346. }
  1347. function getActDataArr(dtObj) {
  1348. let rst = null;
  1349. if (dtObj) {
  1350. rst = dtObj.data;
  1351. if (dtObj.moduleName === "projectGLJ") {
  1352. rst = dtObj.data.gljList;
  1353. }
  1354. }
  1355. return rst;
  1356. }
  1357. function replaceActDataArr(dtObj, newArr) {
  1358. if (dtObj.moduleName === "projectGLJ") {
  1359. delete dtObj.data.gljList;
  1360. dtObj.data.gljList = newArr;
  1361. } else {
  1362. delete dtObj.data;
  1363. dtObj.data = newArr;
  1364. }
  1365. }
  1366. export default Rpt_Data_Extractor;