rpt_construct_data_util.js 56 KB

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