rpt_construct_data_util.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. if (!compRst) {
  548. break; //由不符合条件的数据则退出(这里的判断条件是and关系)
  549. }
  550. }
  551. return compRst;
  552. };
  553. for (let item of tempRstArr) {
  554. if (private_filter_compare(item, handleCfg)) {
  555. rstArr.push(item);
  556. }
  557. }
  558. if (handleCfg[JV.PROP_OTHER_SUB_FILTER] && handleCfg[JV.PROP_OTHER_SUB_FILTER].length > 0) {
  559. let newRstArr = [];
  560. for (let dtlItem of rstArr) {
  561. let cmpRst = false;
  562. for (let dtlCfg of handleCfg[JV.PROP_OTHER_SUB_FILTER]) {
  563. cmpRst = private_filter_compare(dtlItem, dtlCfg);
  564. if (cmpRst) {
  565. newRstArr.push(dtlItem);
  566. break;
  567. }
  568. }
  569. }
  570. rstArr = newRstArr;
  571. }
  572. replaceActDataArr(sourceData, rstArr);
  573. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/filteredRst.js");
  574. }
  575. function adjustData(sourceData, adjustCfg) {
  576. let rstArr = [];
  577. for (let item of getActDataArr(sourceData)) {
  578. if (item._doc) {
  579. rstArr.push(item._doc);
  580. } else {
  581. rstArr.push(item);
  582. }
  583. }
  584. for (let item of adjustCfg[JV.PROP_ADJUST_COLLECTION]) {
  585. for (let rec of rstArr) {
  586. if (item[JV.PROP_ADJUST_ACTION] === "prefix") {
  587. rec[item.key] = item[JV.PROP_ADJUST_ACTION_VAL] + rec[item.key];
  588. } else if (item[JV.PROP_ADJUST_ACTION] === "suffix") {
  589. rec[item.key] = rec[item.key] + item[JV.PROP_ADJUST_ACTION_VAL];
  590. }
  591. }
  592. }
  593. replaceActDataArr(sourceData, rstArr);
  594. }
  595. function moveRationData(rationData, rawDataObj) {
  596. if (rawDataObj) {
  597. // let rationData = getModuleDataByKey(rawDataObj.prjData, projectConst.RATION);
  598. let rationGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.RATION_GLJ);
  599. let prjGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.PROJECTGLJ);
  600. let dummyCode = " ", dummyPRJID = 10101010;
  601. for (let rationItem of getActDataArr(rationData)) {
  602. if (rationItem.type === 2 || rationItem.type === 3) {
  603. //2: 量材(人工/材料/机械/主材/设备) 3:材料(材料/主材/设备)
  604. let copyItem = {};
  605. copyItem.ID = rationItem.ID;
  606. copyItem.projectID = rationItem.projectID;
  607. copyItem.GLJID = rationItem.GLJID;
  608. copyItem.rationID = rationItem.ID;
  609. copyItem.rationItemQuantity = 1;
  610. copyItem.quantity = 1;
  611. // copyItem.specialType_quantity = rationItem.quantity;
  612. copyItem.name = rationItem.name;
  613. if (rationItem.code) {
  614. copyItem.code = rationItem.ID + "_" + rationItem.code;
  615. } else {
  616. copyItem.code = dummyCode;
  617. dummyCode = dummyCode + " ";
  618. }
  619. copyItem.original_code = rationItem.original_code;
  620. copyItem.unit = rationItem.unit;
  621. copyItem.specs = rationItem.specs;
  622. copyItem.shortName = rationItem.shortName;
  623. copyItem.billsItemID = rationItem.billsItemID;
  624. copyItem.type = rationItem.subType;
  625. if (rationItem.projectGLJID) {
  626. copyItem.projectGLJID = rationItem.projectGLJID;
  627. } else {
  628. //add dummy project GLJ
  629. let dummyPrjItem = {};
  630. copyItem.projectGLJID = dummyPRJID;
  631. dummyPrjItem.id = dummyPRJID;
  632. dummyPRJID++;
  633. dummyPrjItem.unit_price = {};
  634. dummyPrjItem.unit_price.base_price = 0;
  635. dummyPrjItem.unit_price.market_price = (rationItem["marketUnitFee"])?rationItem["marketUnitFee"]:0;
  636. dummyPrjItem.quantity = 0;
  637. dummyPrjItem.is_evaluate = 0;
  638. prjGljData.data.gljList.push(dummyPrjItem);
  639. }
  640. rationGljData.data.push(copyItem);
  641. }
  642. }
  643. // fsUtil.writeObjToFile(rationGljData.data, "D:/GitHome/ConstructionCost/tmp/afterMoveGLJ.jsp");
  644. }
  645. }
  646. function getDupGrpKeyVals(sourceData, segKeys) {
  647. let rst = [];
  648. function pushKeyVal(item) {
  649. let tr = {};
  650. for (let i = 0; i < segKeys.length; i++) {
  651. tr[segKeys[i]] = item[segKeys[i]];
  652. }
  653. rst.push(tr);
  654. }
  655. for (let idx = 0; idx < sourceData.length; idx++) {
  656. let itemRec = sourceData[idx];
  657. if (idx === 0) {
  658. pushKeyVal(itemRec);
  659. continue;
  660. }
  661. let hasDiff = false;
  662. for (let i = 0; i < segKeys.length; i++) {
  663. if (itemRec[segKeys[i]] !== sourceData[idx - 1][segKeys[i]]) {
  664. hasDiff = true;
  665. break;
  666. }
  667. }
  668. if (hasDiff) {
  669. pushKeyVal(itemRec);
  670. }
  671. }
  672. return rst;
  673. }
  674. function addDummyData(sourceData, addCfg) {
  675. let rstArr = [], tempRstArr = [];
  676. for (let item of getActDataArr(sourceData)) {
  677. if (item._doc) {
  678. tempRstArr.push(item._doc);
  679. } else {
  680. tempRstArr.push(item);
  681. }
  682. }
  683. for (let item of addCfg[JV.PROP_DUMMY_COLLECTION]) {
  684. let newRecStr = JSON.stringify(item[JV.PROP_DUMMY_VAL]), cacheGrpKeyRecs = null;
  685. if (item[JV.PROP_FREQUENCY] === "OncePerGrp") {
  686. if (!cacheGrpKeyRecs) {
  687. cacheGrpKeyRecs = {};
  688. }
  689. let cacheKey = "";
  690. for (let key of item[JV.PROP_GRP_KEYS]) {
  691. cacheKey += "_" + key;
  692. }
  693. if (!cacheGrpKeyRecs[cacheKey]) {
  694. cacheGrpKeyRecs[cacheKey] = getDupGrpKeyVals(tempRstArr, item[JV.PROP_GRP_KEYS]);
  695. }
  696. for (let kv of cacheGrpKeyRecs[cacheKey]) {
  697. let rec = JSON.parse(newRecStr);
  698. for (let key of item[JV.PROP_GRP_KEYS]) {
  699. rec[key] = kv[key];
  700. }
  701. rstArr.push(rec);
  702. }
  703. } else if (item[JV.PROP_FREQUENCY] === "Once") {
  704. rstArr.push(JSON.parse(newRecStr));
  705. }
  706. }
  707. rstArr = rstArr.concat(tempRstArr);
  708. replaceActDataArr(sourceData, rstArr);
  709. }
  710. function getGLJBizType(orgType, orgCode, orgName) {
  711. let rst = orgType;
  712. if (orgName.indexOf("其他材料费") >= 0) {
  713. rst = 299;
  714. } else if (orgType === GLJ_TYPE.Labour) {
  715. rst = 11;
  716. if (orgCode === "000000") rst = 10;
  717. } else if (orgType === GLJ_TYPE.Main_Material || orgType === GLJ_TYPE.Equipment) {
  718. //未计价材料(主材 + 设备)
  719. rst = 30 + orgType;
  720. } else if (orgType === GLJ_TYPE.Material || (orgType >= 200 && orgType < 300)) {
  721. //材料
  722. if (orgCode === "000000") {
  723. rst = 20; //2.材料
  724. } else if (orgCode === "000000_1") {
  725. rst = 30; //(1) 未计价材料
  726. } else if (orgCode === "000000_2") {
  727. rst = 40; //(2) 辅助材料
  728. } else if (orgCode === "000000_3") {
  729. rst = 50; //(3) 其他材料费
  730. } else {
  731. rst = 45; //到这里就只有辅助材料没有预处理了
  732. }
  733. } else if (orgType === GLJ_TYPE.Machine || (orgType >= 300 && orgType < 400)) {
  734. //机械
  735. if (orgCode === "000000") {
  736. rst = 300; //3.机械
  737. } else if (orgType === GLJ_TYPE.Machine) {
  738. rst = 300.5;
  739. } else {
  740. rst = orgType;
  741. }
  742. }
  743. return rst;
  744. }
  745. function sortData(sourceData, sortCfg, prjData) {
  746. let rst = getActDataArr(sourceData), tempRstArr = [];
  747. let sortType = sortCfg[JV.PROP_SORT_TYPE];
  748. let srcData = getActDataArr(sourceData);
  749. for (let item of srcData) {
  750. if (item._doc) {
  751. tempRstArr.push(item._doc);
  752. } else {
  753. tempRstArr.push(item);
  754. }
  755. }
  756. function private_normal_sort(destArr, sortKeys) {
  757. destArr.sort(function(a, b){
  758. let compRst = 0;
  759. for (let comp of sortKeys) {
  760. let reverse = (comp.order === 'ascend')?1:(-1);
  761. //
  762. if (a[comp.key] > b[comp.key]) {
  763. compRst = reverse;
  764. break;
  765. } else if (a[comp.key] < b[comp.key]) {
  766. compRst = -reverse;
  767. break;
  768. }
  769. }
  770. return compRst;
  771. });
  772. }
  773. function private_parent_sort(parentArr, parentKeys, childArr, childKeys) {
  774. let tmpRst = {}, rst = [];
  775. for (let pItem of parentArr) {
  776. let pKey = "key";
  777. for (let key of parentKeys) {
  778. pKey += "_" + pItem[key];
  779. }
  780. tmpRst[pKey] = [];
  781. }
  782. for (let cItem of childArr) {
  783. let cKey = "key";
  784. for (let key of childKeys) {
  785. cKey += "_" + cItem[key];
  786. }
  787. if (tmpRst[cKey]) {
  788. tmpRst[cKey].push(cItem);
  789. } else {
  790. //unknown child value! should be filtered!
  791. }
  792. }
  793. // childArr.splice(0);
  794. for (let pItem of parentArr) {
  795. let pKey = "key";
  796. for (let key of parentKeys) {
  797. pKey += "_" + pItem[key];
  798. }
  799. rst.push(tmpRst[pKey]);
  800. // for (let rItem of tmpRst[pKey]) {
  801. // childArr.push(rItem);
  802. // }
  803. }
  804. return rst;
  805. }
  806. switch (sortType) {
  807. case "tree":
  808. let addLevel = true;
  809. rst = treeUtil.buildTreeNodeDirectly(tempRstArr, addLevel);
  810. let newTopArr = [];
  811. if ((sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES] && sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].length > 0) ||
  812. (sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES] && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].length > 0)) {
  813. let local_check_bills = function(tItem) {
  814. let chkDtl = false;
  815. if (tItem.flags && tItem.flags.length > 0) {
  816. for (let flagItem of tItem.flags) {
  817. if (sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].indexOf(flagItem.flag) >= 0) {
  818. newTopArr.push(tItem);
  819. chkDtl = true;
  820. break;
  821. }
  822. }
  823. }
  824. if (!chkDtl && tItem.items && tItem.items.length > 0) {
  825. for (let dtlItem of tItem.items) {
  826. local_check_bills(dtlItem);
  827. }
  828. }
  829. };
  830. for (let topItem of rst) {
  831. let chkTop = false;
  832. if (topItem.flags && topItem.flags.length > 0) {
  833. for (let flagItem of topItem.flags) {
  834. if (sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].indexOf(flagItem.flag) >= 0) {
  835. newTopArr.push(topItem);
  836. chkTop = true;
  837. break;
  838. }
  839. }
  840. }
  841. if (!chkTop && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES] && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].length > 0) {
  842. local_check_bills(topItem);
  843. }
  844. }
  845. } else {
  846. newTopArr = rst;
  847. }
  848. let destArr = [];
  849. treeUtil.getFlatArray(newTopArr, destArr);
  850. replaceActDataArr(sourceData, destArr);
  851. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/sortedAndFlattedRst.jsp");
  852. break;
  853. case "normal":
  854. private_normal_sort(tempRstArr, sortCfg[JV.PROP_SORT_KEYS]);
  855. replaceActDataArr(sourceData, tempRstArr);
  856. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/normalSortedRst.jsp");
  857. break;
  858. case "accord_to_parent":
  859. let pcKey = sortCfg[JV.PROP_PARENT_CHILD_SORT_KEY];
  860. let parentSrcData = getModuleDataByKey(prjData, pcKey[JV.PROP_PARENT_DATA_KEY]);
  861. if (parentSrcData) {
  862. let tempParentArr = [];
  863. for (let item of getActDataArr(parentSrcData)) {
  864. if (item._doc) {
  865. tempParentArr.push(item._doc);
  866. } else {
  867. tempParentArr.push(item);
  868. }
  869. }
  870. let sortedRstArr = private_parent_sort(tempParentArr, pcKey[JV.PROP_PARENT_SORT_KEYS], tempRstArr, pcKey[JV.PROP_CHILD_SORT_KEYS]);
  871. if (sortCfg[JV.PROP_OTHER_SUB_SORT] && sortCfg[JV.PROP_OTHER_SUB_SORT].length > 0) {
  872. for (let sort of sortCfg[JV.PROP_OTHER_SUB_SORT]) {
  873. if (sort[JV.PROP_SORT_TYPE] === 'normal') {
  874. for (let subArr of sortedRstArr) {
  875. private_normal_sort(subArr, sort[JV.PROP_SORT_KEYS]);
  876. }
  877. } else if (sort[JV.PROP_SORT_TYPE] === 'self_define') {
  878. for (let subArr of sortedRstArr) {
  879. // console.log(subArr);
  880. let selfDefFunc = null;
  881. eval('selfDefFunc = ' + sort[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  882. subArr.sort(selfDefFunc);
  883. // console.log(subArr);
  884. }
  885. }
  886. }
  887. }
  888. tempRstArr.splice(0);
  889. for (let item of sortedRstArr) {
  890. for (let subItem of item) {
  891. tempRstArr.push(subItem);
  892. }
  893. }
  894. }
  895. replaceActDataArr(sourceData, tempRstArr);
  896. break;
  897. case "self_define":
  898. if (sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]) {
  899. let selfDefFunc = null;
  900. eval('selfDefFunc = ' + sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  901. tempRstArr.sort(selfDefFunc);
  902. }
  903. replaceActDataArr(sourceData, tempRstArr);
  904. break;
  905. default:
  906. //
  907. }
  908. return rst;
  909. }
  910. function setupFunc(obj, prop, ownRawObj) {
  911. obj[prop] = {};
  912. obj[prop]["myOwnRawDataObj"] = ownRawObj;
  913. obj[prop]["myOwnOrgRawDataObj"] = ownRawObj.data;
  914. obj[prop].getProperty = ext_getPropety;
  915. obj[prop].getPropertyByRefId = ext_getPropertyByRefId;
  916. obj[prop].getFee = ext_getFee;
  917. obj[prop].getPropertyByForeignId = ext_getPropertyByForeignId;
  918. obj[prop].getArrayValues = ext_getArrayValues;
  919. obj[prop].getArrayItemByKey = ext_getArrayItemByKey;
  920. obj[prop].getPropertyByFlag = ext_getPropertyByFlag;
  921. obj[prop].getBlank = ext_getBlank;
  922. if (prop === projectConst.CALC_PROGRAM) obj[prop].getCalcProperty = ext_getCalcProperty;
  923. if (prop === projectConst.FEERATE) obj[prop].getFeeRate = ext_getFeeRate;
  924. }
  925. function assembleFields(fieldList, rstDataArr, $PROJECT) {
  926. if (fieldList) {
  927. for (let field of fieldList) {
  928. shielded_exec_env($PROJECT, field, rstDataArr);
  929. }
  930. }
  931. }
  932. function shielded_exec_env($PROJECT, $ME, rptDataItemObj) {
  933. if ($ME[JV.PROP_FIELD_EXP_MAP]) {
  934. rptDataItemObj.push(eval($ME[JV.PROP_FIELD_EXP_MAP]));
  935. }
  936. }
  937. function getActPropertyVal(firstPropKey, secPropKey, orgObj) {
  938. let rst = null;
  939. if (orgObj[firstPropKey]) {
  940. rst = orgObj[firstPropKey];
  941. } else if (orgObj[secPropKey]){
  942. rst = orgObj[secPropKey];
  943. }
  944. return rst;
  945. }
  946. function getDeepProperty(propKey, orgObj, destArr) {
  947. let keys = propKey.split(".");
  948. let dftPropKey = "key", dftPropVal = "value", secDftPropVal = "items";
  949. let parent = orgObj, lastVal = null;
  950. for (let key of keys) {
  951. if (parent instanceof Array) {
  952. for (let item of parent) {
  953. if (item[dftPropKey] === key) {
  954. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  955. break;
  956. }
  957. }
  958. } else {
  959. lastVal = null;
  960. if (parent[key] !== undefined) {
  961. lastVal = parent[key];
  962. } else if (parent[secDftPropVal]){
  963. for (let item of parent[secDftPropVal]) {
  964. if (item[dftPropKey] === key) {
  965. // lastVal = item[dftPropVal];
  966. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  967. break;
  968. }
  969. }
  970. }
  971. }
  972. parent = lastVal;
  973. if (parent === null) break;
  974. }
  975. if (destArr && destArr instanceof Array) {
  976. destArr.push(lastVal);
  977. }
  978. }
  979. function ext_mainGetPropety(propKey) {
  980. let rst = [], parentObj = this;
  981. let dtObj = parentObj["myOwnRawDataObj"];
  982. if (propKey && dtObj) {
  983. if (dtObj.hasOwnProperty("property")) {
  984. if (!dtObj["property"][propKey] && dtObj[propKey]) {
  985. rst.push(dtObj[propKey]);
  986. } else {
  987. getDeepProperty(propKey, dtObj["property"], rst);
  988. }
  989. } else {
  990. // rst.push(dtObj[propKey]);
  991. getDeepProperty(propKey, dtObj, rst);
  992. }
  993. }
  994. return rst;
  995. }
  996. function ext_getPropety(propKey) {
  997. let rst = [], parentObj = this;
  998. let dtObj = parentObj["myOwnRawDataObj"];
  999. if (propKey && dtObj) {
  1000. for (let dItem of getActDataArr(dtObj)) {
  1001. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  1002. if (propKey instanceof Array) {
  1003. for (let pi = 0; pi < propKey.length; pi++) {
  1004. if (doc.hasOwnProperty("property")) {
  1005. if (doc["property"].hasOwnProperty(propKey[pi])) {
  1006. rst.push(doc["property"][propKey[pi]]);
  1007. break;
  1008. }
  1009. } else if (doc.hasOwnProperty(propKey[pi])) {
  1010. rst.push(doc[propKey[pi]]);
  1011. break;
  1012. }
  1013. if (pi === propKey.length - 1) rst.push('');
  1014. }
  1015. } else {
  1016. if (doc.hasOwnProperty("property")) {
  1017. rst.push(doc["property"][propKey]);
  1018. } else if (doc.hasOwnProperty(propKey)) {
  1019. rst.push(doc[propKey]);
  1020. } else {
  1021. rst.push('');
  1022. }
  1023. }
  1024. }
  1025. }
  1026. return rst;
  1027. }
  1028. function ext_getPropertyByRefId(baseKey, refIDKey, propertyKey){
  1029. let rst = [], parentObj = this;
  1030. let dtObj = parentObj["myOwnRawDataObj"];
  1031. let orgDtObj = parentObj["myOwnOrgRawDataObj"];
  1032. if (baseKey && refIDKey && propertyKey && dtObj) {
  1033. for (let dItem of getActDataArr(dtObj)) {
  1034. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  1035. let tmpRst = doc[baseKey];
  1036. if (typeof tmpRst === "string" && tmpRst[0] === "@") {
  1037. let refKey = tmpRst.slice(1);
  1038. let hasGetRef = false;
  1039. for (let orgDItem of orgDtObj) {
  1040. let oDoc = (orgDItem._doc === null || orgDItem._doc === undefined)?orgDItem:orgDItem._doc;
  1041. if (oDoc[refIDKey] === refKey) {
  1042. rst.push(oDoc[propertyKey]);
  1043. hasGetRef = true;
  1044. break;
  1045. }
  1046. }
  1047. if (!hasGetRef) {
  1048. rst.push(tmpRst);
  1049. }
  1050. } else {
  1051. rst.push(tmpRst);
  1052. }
  1053. }
  1054. }
  1055. return rst;
  1056. }
  1057. function ext_mainGetFee(feeKey, dtlFeeKey) {
  1058. let rst = [];
  1059. let parentObj = this;
  1060. let dtObj = parentObj["myOwnRawDataObj"];
  1061. if ((dtObj) && (feeKey)) {
  1062. if (dtObj.hasOwnProperty("fees")) {
  1063. for (let fee of dtObj["fees"]) {
  1064. if (fee["fieldName"] === feeKey) {
  1065. if (dtlFeeKey) {
  1066. rst.push(fee[dtlFeeKey]);
  1067. } else {
  1068. rst.push(fee["unitFee"]);
  1069. }
  1070. break;
  1071. }
  1072. }
  1073. } else if (dtObj.hasOwnProperty(feeKey)) {
  1074. rst.push(dtObj[feeKey]);
  1075. } else {
  1076. //
  1077. }
  1078. }
  1079. return rst;
  1080. }
  1081. function ext_getFee(feeKey, dtlFeeKey) {
  1082. let rst = [], parentObj = this;
  1083. let dtObj = parentObj["myOwnRawDataObj"];
  1084. if (feeKey && dtObj) {
  1085. for (let dItem of getActDataArr(dtObj)) {
  1086. rst.push(pri_getFee(dItem, feeKey, dtlFeeKey));
  1087. }
  1088. }
  1089. for (let i = 0; i < rst.length; i++) {
  1090. rst[i] = parseFloat(rst[i]);
  1091. }
  1092. return rst;
  1093. }
  1094. function pri_getFee(dItem, feeKey, dtlFeeKey) {
  1095. let rst = 0;
  1096. let hasValue = false;
  1097. if (dItem["fees"]) {
  1098. for (let fee of dItem["fees"]) {
  1099. if (fee["fieldName"] === feeKey) {
  1100. if (dtlFeeKey) {
  1101. rst = fee[dtlFeeKey];
  1102. } else {
  1103. rst = fee["unitFee"];
  1104. }
  1105. hasValue = true;
  1106. break;
  1107. }
  1108. }
  1109. } else if (dItem[feeKey]) {
  1110. hasValue = true;
  1111. rst = dItem[feeKey];
  1112. } else {
  1113. hasValue = true;
  1114. rst = 0;
  1115. }
  1116. if (!hasValue) {
  1117. rst = 0;
  1118. }
  1119. return rst;
  1120. }
  1121. function ext_getCalcProperty(templateIDs, calcItemKey, calcItemKeyVal, calcItemRstKey){
  1122. let rst = [], parentObj = this; //this should be "calc_program" object
  1123. let dtObj = parentObj["myOwnRawDataObj"];
  1124. let optimizeObj = {};
  1125. let private_getProperty = function (cId) {
  1126. let calcTplObj = optimizeObj["calc_program_" + cId];
  1127. if (!calcTplObj) {
  1128. let templates = (dtObj.data._doc)?dtObj.data._doc.templates:dtObj.data.templates;
  1129. for (let tpl of templates) {
  1130. if (cId === tpl.ID) {
  1131. optimizeObj["calc_program_" + cId] = tpl;
  1132. calcTplObj = tpl;
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. if (calcTplObj) {
  1138. for (let calcItem of calcTplObj.calcItems) {
  1139. if (calcItem[calcItemKey] === calcItemKeyVal) {
  1140. rst.push(calcItem[calcItemRstKey]);
  1141. break;
  1142. }
  1143. }
  1144. }
  1145. };
  1146. if (templateIDs instanceof Array) {
  1147. for (let tplId of templateIDs) {
  1148. private_getProperty(tplId);
  1149. }
  1150. } else {
  1151. private_getProperty(templateIDs);
  1152. }
  1153. optimizeObj = null;
  1154. return rst;
  1155. }
  1156. function ext_getFeeRate(fee_Ids){
  1157. let rst = [], parentObj = this; //this should be "feeRate" object
  1158. let dtObj = parentObj["myOwnRawDataObj"];
  1159. let optimizeObj = {};
  1160. let private_getFeeRate = function (fId) {
  1161. let feeRateItemObj = optimizeObj["fee_rates_" + fId];
  1162. if (!feeRateItemObj) {
  1163. let rates = (dtObj.data._doc)?dtObj.data._doc.rates:dtObj.data.rates;
  1164. for (let feeItem of rates) {
  1165. if (fId === feeItem.ID) {
  1166. optimizeObj["fee_rates_" + fId] = feeItem;
  1167. feeRateItemObj = feeItem;
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. if (feeRateItemObj) {
  1173. rst.push(feeRateItemObj.rate);
  1174. } else {
  1175. rst.push(0);
  1176. }
  1177. };
  1178. if (fee_Ids instanceof Array) {
  1179. for (let fId of fee_Ids) {
  1180. private_getFeeRate(fId);
  1181. }
  1182. } else {
  1183. private_getFeeRate(fee_Ids);
  1184. }
  1185. optimizeObj = null;
  1186. return rst;
  1187. }
  1188. function ext_getArrayValues(itemKey) {
  1189. let rst = [], parentObj = this;
  1190. let dtObj = parentObj["myOwnRawDataObj"];
  1191. let keysArr = itemKey.split(".");
  1192. for (let dataItem of getActDataArr(dtObj)) {
  1193. let itemArr = [];
  1194. if (keysArr.length <= 2) {
  1195. if (dataItem[keysArr[0]] instanceof Array) {
  1196. if (keysArr.length === 2) {
  1197. for (let item of dataItem[keysArr[0]]) {
  1198. itemArr.push(item[keysArr[1]]);
  1199. }
  1200. } else {
  1201. itemArr = itemArr.concat(dataItem[keysArr[0]]);
  1202. }
  1203. } else {
  1204. if (keysArr.length === 2) {
  1205. let subProperty = dataItem[keysArr[0]][keysArr[1]];
  1206. if (subProperty instanceof Array) {
  1207. itemArr = itemArr.concat(subProperty);
  1208. } else {
  1209. itemArr.push(subProperty);
  1210. }
  1211. } else {
  1212. itemArr.push(dataItem[keysArr[0]]);
  1213. }
  1214. }
  1215. }
  1216. rst.push(itemArr);
  1217. }
  1218. return rst;
  1219. }
  1220. function ext_getArrayItemByKey(arrayKey, itemKey, itemKeyValue, itemRstKey){
  1221. let rst = [], parentObj = this;
  1222. let dtObj = parentObj["myOwnRawDataObj"];
  1223. let private_getItemValue = function (arr, dtlItKV) {
  1224. for (let item of arr) {
  1225. if (item[itemKey] === dtlItKV) {
  1226. if (itemRstKey) {
  1227. rst.push(item[itemRstKey]);
  1228. } else {
  1229. rst.push(item);
  1230. }
  1231. break;
  1232. }
  1233. }
  1234. };
  1235. let arr = dtObj[arrayKey];
  1236. if (arr && arr instanceof Array) {
  1237. if (itemKeyValue instanceof Array) {
  1238. for (let dtlItemKeyVal of itemKeyValue) {
  1239. private_getItemValue(arr, dtlItemKeyVal);
  1240. }
  1241. } else {
  1242. private_getItemValue(arr, itemKeyValue);
  1243. }
  1244. }
  1245. }
  1246. function ext_getBlank(dftVal) {
  1247. let rst = [], parentObj = this;
  1248. let dtObj = parentObj["myOwnRawDataObj"];
  1249. if (dtObj) {
  1250. let dtData = getActDataArr(dtObj);
  1251. for (let i = 0; i < dtData.length; i++) {
  1252. if (dftVal !== null && dftVal !== undefined) {
  1253. rst.push(dftVal)
  1254. } else rst.push('');
  1255. }
  1256. }
  1257. return rst;
  1258. }
  1259. function ext_getPropertyByFlag(flagVal, rstKey, dftValIfEmpty) {
  1260. let rst = [], parentObj = this;
  1261. let dtObj = parentObj["myOwnRawDataObj"];
  1262. if (flagVal && rstKey && dtObj) {
  1263. let isArr = (flagVal instanceof Array);
  1264. for (let dItem of getActDataArr(dtObj)) {
  1265. let doc = (dItem._doc)?dItem._doc:dItem;
  1266. if (doc.hasOwnProperty("flags")) {
  1267. let bFlag = false;
  1268. for (let flagItem of doc.flags) {
  1269. if (isArr) {
  1270. bFlag = (flagVal.indexOf(flagItem.flag) >= 0);
  1271. } else {
  1272. if (flagItem.flag === flagVal) {
  1273. bFlag = true;
  1274. }
  1275. }
  1276. if (bFlag) break;
  1277. }
  1278. if (bFlag) {
  1279. let keys = rstKey.split(".");
  1280. if (keys[0] === "fees") {
  1281. rst.push(pri_getFee(doc, "common", keys[1]));
  1282. } else {
  1283. //其他,比如名称什么
  1284. }
  1285. break;
  1286. }
  1287. }
  1288. }
  1289. }
  1290. if (rst.length === 0 && dftValIfEmpty !== null) {
  1291. rst.push(dftValIfEmpty);
  1292. }
  1293. return rst;
  1294. }
  1295. function ext_getPropertyByForeignId(foreignIdVal, adHocIdKey, propKey, dftValIfNotFound) {
  1296. let rst = [], parentObj = this;
  1297. let IdKey = (adHocIdKey)?adHocIdKey:"ID";
  1298. let dtObj = parentObj["myOwnRawDataObj"];
  1299. let splitPKey = propKey.split(".");
  1300. if (foreignIdVal !== null && foreignIdVal !== undefined) {
  1301. let isFound = false;
  1302. if (foreignIdVal instanceof Array) {
  1303. for (let idVal of foreignIdVal) {
  1304. isFound = false;
  1305. let dataArr = getActDataArr(dtObj);
  1306. for (let i = 0; i < dataArr.length; i++) {
  1307. let item = (dataArr[i]._doc)?dataArr[i]._doc:dataArr[i];
  1308. if (item[IdKey] === idVal) {
  1309. if (splitPKey.length > 1) {
  1310. let rstP = null;
  1311. for (let i = 0; i < splitPKey.length; i++) {
  1312. if (i === 0) {
  1313. rstP = item[splitPKey[i]];
  1314. } else {
  1315. if (splitPKey[i].indexOf("[") === 0 && splitPKey[i].indexOf("]") === (splitPKey[i].length - 1)) {
  1316. //考虑数组情况^_^!!!
  1317. } else {
  1318. //
  1319. }
  1320. rstP = rstP[splitPKey[i]];
  1321. }
  1322. }
  1323. rst.push(rstP);
  1324. } else {
  1325. rst.push(item[propKey]);
  1326. }
  1327. isFound = true;
  1328. break;
  1329. }
  1330. }
  1331. if (!isFound) {
  1332. rst.push(dftValIfNotFound);
  1333. }
  1334. }
  1335. } else {
  1336. for (let item of getActDataArr(dtObj)) {
  1337. if (item[IdKey] === foreignIdVal) {
  1338. rst.push(item[propKey]);
  1339. isFound = true;
  1340. break;
  1341. }
  1342. }
  1343. if (!isFound) {
  1344. rst.push(dftValIfNotFound);
  1345. }
  1346. }
  1347. }
  1348. return rst;
  1349. }
  1350. function getActDataArr(dtObj) {
  1351. let rst = null;
  1352. if (dtObj) {
  1353. rst = dtObj.data;
  1354. if (dtObj.moduleName === "projectGLJ") {
  1355. rst = dtObj.data.gljList;
  1356. }
  1357. }
  1358. return rst;
  1359. }
  1360. function replaceActDataArr(dtObj, newArr) {
  1361. if (dtObj.moduleName === "projectGLJ") {
  1362. delete dtObj.data.gljList;
  1363. dtObj.data.gljList = newArr;
  1364. } else {
  1365. delete dtObj.data;
  1366. dtObj.data = newArr;
  1367. }
  1368. }
  1369. export default Rpt_Data_Extractor;