rpt_yanghu_data_util.js 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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 scMathUtil = require("../../../public/scMathUtil");
  11. let _ = require("lodash");
  12. let treeUtil = require('../../../public/treeUtil');
  13. let projectConst = consts.projectConst;
  14. let projectConstList = consts.projectConstList;
  15. const gljUtil = require('../../../public/gljUtil');
  16. const gljType = require('../../common/const/glj_type_const');
  17. // const pm_facade = require('../../pm/facade/pm_facade');
  18. const GLJID_PRE = `gljId_`, COMPONENT_GLJID_PRE = `componetGljId_`;
  19. const GLJ_TYPE = {
  20. Labour: 1,
  21. Material: 2,
  22. Machine: 3,
  23. Main_Material: 4,
  24. Equipment: 5,
  25. OTHER_MATERIAL: 207,
  26. MACHINE_LABOUR: 303,
  27. INSTRUMENT: 304,
  28. FUEL_POWER_FEE: 305
  29. };
  30. const oprMachines = [306, 307, 308, 309, 310, 311, 312];
  31. class Rpt_Common{
  32. initialize(rpt_tpl, currentDataObj) {
  33. this.template = rpt_tpl;
  34. this.currentDataObj = currentDataObj;
  35. };
  36. Multiply(val1, val2, fixFormat) {
  37. let rst = [], maxLen = val1.length, minLen = val2.length;
  38. if (minLen > maxLen) {
  39. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  40. }
  41. for (let i = 0; i < maxLen; i++) {
  42. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) * ((i < val2.length)?val2[i]:val2[minLen - 1]);
  43. if (value === null || value === undefined) {
  44. value = '0';
  45. }
  46. if (fixFormat) value = value.toFixed(fixFormat);
  47. rst.push(value);
  48. }
  49. return rst;
  50. };
  51. Divide(val1, val2, fixFormat) {
  52. let rst = [], maxLen = val1.length, minLen = val2.length;
  53. if (minLen > maxLen) {
  54. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  55. }
  56. for (let i = 0; i < maxLen; i++) {
  57. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) / ((i < val2.length)?val2[i]:val2[minLen - 1]);
  58. if (fixFormat) value = value.toFixed(fixFormat);
  59. rst.push(value);
  60. }
  61. return rst;
  62. };
  63. Plus(val1, val2, fixFormat) {
  64. let rst = [], maxLen = val1.length, minLen = val2.length;
  65. if (minLen > maxLen) {
  66. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  67. }
  68. for (let i = 0; i < maxLen; i++) {
  69. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) + ((i < val2.length)?val2[i]:val2[minLen - 1]);
  70. if (fixFormat) value = value.toFixed(fixFormat);
  71. rst.push(value);
  72. }
  73. return rst;
  74. };
  75. MultiPlus(arrVal, fixFormat) {
  76. let rst = [];
  77. for (let i = 0; i < arrVal.length; i++) {
  78. let valItem = arrVal[i];
  79. if (i === 0) {
  80. for (let dtl of valItem) {
  81. let value = parseFloat(dtl);
  82. if (fixFormat) value = value.toFixed(fixFormat);
  83. rst.push(value);
  84. }
  85. } else {
  86. for (let j = 0; j < valItem.length; j++) {
  87. if (j < rst.length) {
  88. let value = rst[j] + valItem[j];
  89. if (fixFormat) value = value.toFixed(fixFormat);
  90. rst[j] = value;
  91. } else {
  92. let value = parseFloat(valItem[j]);
  93. if (fixFormat) value = value.toFixed(fixFormat);
  94. rst.push(value);
  95. }
  96. }
  97. }
  98. }
  99. return rst;
  100. };
  101. Minus(val1, val2, fixFormat) {
  102. let rst = [], maxLen = val1.length, minLen = val2.length;
  103. if (minLen > maxLen) {
  104. maxLen = maxLen + minLen; minLen = maxLen - minLen; maxLen = maxLen - minLen;
  105. }
  106. for (let i = 0; i < maxLen; i++) {
  107. let value = ((i < val1.length)?val1[i]:val1[minLen - 1]) - ((i < val2.length)?val2[i]:val2[minLen - 1]);
  108. if (fixFormat) value = value.toFixed(fixFormat);
  109. rst.push(value);
  110. }
  111. return rst;
  112. };
  113. FormatString(arrVal, formatStr){
  114. let rst = [];
  115. for (let val of arrVal) {
  116. rst.push(stringUtil.replaceAll(formatStr, '%S', val));
  117. }
  118. return rst;
  119. };
  120. }
  121. class Rpt_Data_Extractor {
  122. constructor () {
  123. this.COMMON = new Rpt_Common();
  124. };
  125. initialize(tpl) {
  126. this.rptTpl = tpl;
  127. };
  128. //-- 根据报表模板映射指标(非离散指标)的定义,罗列出所有需要用到的data对象key,作为数据请求的过滤依据
  129. getDataRequestFilter(summaryRst) {
  130. let rst = [];
  131. let tpl = this.rptTpl;
  132. let pri_func_chk_filter = function (field) {
  133. for (let key of projectConstList) {
  134. if (rst.indexOf(key) < 0) {
  135. if (field[JV.PROP_FIELD_EXP_MAP]) {
  136. if (field[JV.PROP_FIELD_EXP_MAP].indexOf('.' + key + '.') >= 0) {
  137. rst.push(key);
  138. if (key === projectConst.RATION_GLJ && (rst.indexOf(projectConst.PROJECTGLJ) < 0)) {
  139. rst.push(projectConst.PROJECTGLJ);
  140. }
  141. if (key === projectConst.PROJECTGLJ) {
  142. if (rst.indexOf(projectConst.RATION_GLJ) < 0) rst.push(projectConst.RATION_GLJ);
  143. if (field[JV.PROP_FIELD_EXP_MAP].indexOf("'quantity'") > 0 ||
  144. field[JV.PROP_FIELD_EXP_MAP].indexOf("'subdivisionQuantity'") > 0 ||
  145. field[JV.PROP_FIELD_EXP_MAP].indexOf("'techQuantity'") > 0 ) {
  146. if (rst.indexOf(projectConst.RATION) < 0) rst.push(projectConst.RATION);
  147. if (rst.indexOf(projectConst.BILLS) < 0) rst.push(projectConst.BILLS);
  148. //备注:项目工料机的数量是需要根据定额清单来计算的!这里的逻辑是对的
  149. }
  150. }
  151. }
  152. }
  153. } else if (key === projectConst.PROJECTGLJ) {
  154. if (rst.indexOf(projectConst.RATION_GLJ) < 0) rst.push(projectConst.RATION_GLJ);
  155. if (field[JV.PROP_FIELD_EXP_MAP].indexOf("'quantity'") > 0 ||
  156. field[JV.PROP_FIELD_EXP_MAP].indexOf("'subdivisionQuantity'") > 0 ||
  157. field[JV.PROP_FIELD_EXP_MAP].indexOf("'techQuantity'") > 0 ) {
  158. if (rst.indexOf(projectConst.RATION) < 0) rst.push(projectConst.RATION);
  159. if (rst.indexOf(projectConst.BILLS) < 0) rst.push(projectConst.BILLS);
  160. //备注:项目工料机的数量是需要根据定额清单来计算的!这里的逻辑是对的
  161. }
  162. }
  163. }
  164. if (summaryRst instanceof Array) {
  165. for (let key of consts.summaryConstList) {
  166. if (summaryRst.indexOf(key) < 0) {
  167. if (field[JV.PROP_FIELD_EXP_MAP]) {
  168. if (field[JV.PROP_FIELD_EXP_MAP].indexOf('.' + key + '.') >= 0) {
  169. summaryRst.push(key);
  170. }
  171. }
  172. }
  173. }
  174. }
  175. };
  176. let pri_setup_filter = function (FIELD_LIST_KEY) {
  177. if (tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]) {
  178. for (let field of tpl[JV.NODE_FIELD_MAP][FIELD_LIST_KEY]) {
  179. pri_func_chk_filter(field);
  180. }
  181. }
  182. };
  183. pri_setup_filter(JV.NODE_DISCRETE_FIELDS);
  184. pri_setup_filter(JV.NODE_MASTER_FIELDS);
  185. pri_setup_filter(JV.NODE_DETAIL_FIELDS);
  186. pri_setup_filter(JV.NODE_MASTER_FIELDS_EX);
  187. pri_setup_filter(JV.NODE_DETAIL_FIELDS_EX);
  188. if (tpl[JV.NODE_MAP_DATA_HANDLE_INFO] && tpl[JV.NODE_MAP_DATA_HANDLE_INFO].length > 0) {
  189. for (let preHandle of tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  190. if (rst.indexOf(preHandle[JV.PROP_DATA_KEY]) < 0) {
  191. rst.push(preHandle[JV.PROP_DATA_KEY]);
  192. }
  193. if (preHandle[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_FILTER) {
  194. if (preHandle[JV.PROP_FILTER_KEYS]) {
  195. for (let filter of preHandle[JV.PROP_FILTER_KEYS]) {
  196. if (filter[JV.PROP_FILTER_COMPARE_OBJ] && rst.indexOf(filter[JV.PROP_FILTER_COMPARE_OBJ]) < 0) {
  197. rst.push(filter[JV.PROP_FILTER_COMPARE_OBJ]);
  198. }
  199. }
  200. }
  201. } else if (preHandle[JV.PROP_HANDLE_TYPE] === JV.PROP_HANDLE_TYPE_SUM) {
  202. if (preHandle[JV.PROP_SUM_GROUP_KEYS]) {
  203. for (let grpKey of preHandle[JV.PROP_SUM_GROUP_KEYS]) {
  204. if (grpKey.seeking_parent && rst.indexOf(grpKey.seeking_parent) < 0) {
  205. rst.push(grpKey.seeking_parent);
  206. }
  207. }
  208. }
  209. } else if (preHandle[JV.PROP_PARENT_CHILD_SORT_KEY] && preHandle[JV.PROP_PARENT_CHILD_SORT_KEY].length > 0) {
  210. for (let item of preHandle[JV.PROP_PARENT_CHILD_SORT_KEY]) {
  211. if (rst.indexOf(item[JV.PROP_PARENT_DATA_KEY]) < 0) {
  212. rst.push(item[JV.PROP_PARENT_DATA_KEY]);
  213. }
  214. }
  215. }
  216. }
  217. }
  218. if (rst.indexOf(projectConst.RATION) >= 0 &&
  219. rst.indexOf(projectConst.RATION_GLJ) >= 0 && rst.indexOf(projectConst.RATION_COE) < 0) {
  220. rst.push(projectConst.RATION_COE);
  221. //备注:在此情况下,根据业务特点,有可能需要额外计算project工料机的基价单价及市场单价,需要用到一些额外信息处理。
  222. // 这里也不精细控制,直接多加一个请求类型。以后说不定还有其他类型的请求,到时候再加。
  223. }
  224. if (rst.length === 0) {
  225. rst.push(projectConst.RATION_ASS);
  226. }
  227. return rst;
  228. };
  229. //--- 装配数据(把收集到的数据,依据报表模板的指示,预处理(如:排序、过滤、合计)及装配到相关指标) ---//
  230. assembleData(rawDataObj) {
  231. let $PROJECT = {"COMMON": null, "MAIN": {}, "DETAIL": {}};
  232. let tpl = this.rptTpl;
  233. this.COMMON.initialize(tpl, rawDataObj);
  234. $PROJECT.COMMON = this.COMMON;
  235. if (rawDataObj.hasOwnProperty(`prj`) && rawDataObj.hasOwnProperty(`prjData`)) {
  236. setupMainFunc($PROJECT, `MAIN`, rawDataObj.prj._doc);
  237. // $PROJECT.MAIN["myOwnRawDataObj"] = rawDataObj.prj._doc;
  238. // $PROJECT.MAIN.getProperty = ext_mainGetPropety;
  239. // $PROJECT.MAIN.getFee = ext_mainGetFee;
  240. for (let item of rawDataObj.prjData) {
  241. setupFunc($PROJECT.DETAIL, item.moduleName, item);
  242. }
  243. let projectGLJDatas = getModuleDataByKey(rawDataObj.prjData, "projectGLJ");
  244. if (projectGLJDatas) {
  245. //增加一个辅助对象,方便通过gljid key直接获取数据,省去循环时间
  246. let gljAssitantObj = {"gljIds": {}, "componentGljIds": {}};
  247. projectGLJDatas.data.gljAssistant = gljAssitantObj;
  248. for (let gljItem of projectGLJDatas.data.gljList) {
  249. gljAssitantObj.gljIds[GLJID_PRE + gljItem.id] = gljItem;
  250. if (!(gljItem.ratio_data) || gljItem.ratio_data.length === 0) {
  251. gljAssitantObj.componentGljIds[COMPONENT_GLJID_PRE + gljItem.glj_id] = gljItem;
  252. }
  253. }
  254. //顺便做个排序(跟UI一致的排序)
  255. }
  256. let rationGLJDatas = getModuleDataByKey(rawDataObj.prjData, "ration_glj");
  257. let rationDatas = getModuleDataByKey(rawDataObj.prjData, "ration");
  258. let billsDatas = getModuleDataByKey(rawDataObj.prjData, "bills");
  259. let decimal = rawDataObj.prj.property.decimal.glj.quantity;
  260. // let labourCoeDatas = getModuleDataByKey(rawDataObj.prjData, "labour_coe");
  261. let calcOptions = rawDataObj.prj.property.calcOptions;
  262. if (projectGLJDatas && rationGLJDatas && rationDatas && billsDatas) {
  263. gljUtil.calcProjectGLJQuantity(projectGLJDatas.data, rationGLJDatas.data, rationDatas.data, billsDatas.data, decimal);
  264. }
  265. if (projectGLJDatas && rationGLJDatas) {
  266. //考虑调价情况
  267. for (const glj of projectGLJDatas.data.gljList) {
  268. glj.unit_price.market_price = gljUtil.getMarketPrice(glj, projectGLJDatas.data, calcOptions, [], rawDataObj.prj.property.decimal, false);
  269. //备注:养护没有调价,就不用_tender了
  270. }
  271. }
  272. }
  273. //还有汇总的...
  274. if (rawDataObj.hasOwnProperty(`Construct`) || rawDataObj.hasOwnProperty(`ConstructDetail`) || rawDataObj.hasOwnProperty(`Segment`) || rawDataObj.hasOwnProperty(`SegmentDetail`)
  275. || rawDataObj.hasOwnProperty(`SummaryAudit`) || rawDataObj.hasOwnProperty(`SummaryAuditDetail`)
  276. || rawDataObj.hasOwnProperty(`SummaryGljAudit`) || rawDataObj.hasOwnProperty(`SummaryGljAuditDetail`)) {
  277. $PROJECT.SUMMARY = {};
  278. if (rawDataObj.Construct) {
  279. setupMainFunc($PROJECT.SUMMARY, `Construct`, rawDataObj.Construct);
  280. }
  281. if (rawDataObj.ConstructDetail) {
  282. setupFunc($PROJECT.SUMMARY, `ConstructDetail`, {"data": rawDataObj.ConstructDetail});
  283. }
  284. if (rawDataObj.Segment) {
  285. setupMainFunc($PROJECT.SUMMARY, `Segment`, rawDataObj.Segment);
  286. }
  287. if (rawDataObj.SegmentDetail) {
  288. setupFunc($PROJECT.SUMMARY, `SegmentDetail`, {"data": rawDataObj.SegmentDetail});
  289. }
  290. if (rawDataObj.SummaryAudit) {
  291. setupMainFunc($PROJECT.SUMMARY, `SummaryAudit`, rawDataObj.SummaryAudit);
  292. }
  293. if (rawDataObj.SummaryAuditDetail) {
  294. setupFunc($PROJECT.SUMMARY, `SummaryAuditDetail`, {"data": rawDataObj.SummaryAuditDetail});
  295. }
  296. if (rawDataObj.SummaryGljAudit) {
  297. setupMainFunc($PROJECT.SUMMARY, `SummaryGljAudit`, rawDataObj.SummaryGljAudit);
  298. }
  299. if (rawDataObj.SummaryGljAuditDetail) {
  300. setupFunc($PROJECT.SUMMARY, `SummaryGljAuditDetail`, {"data": rawDataObj.SummaryGljAuditDetail});
  301. }
  302. }
  303. //综合费率
  304. let feeRate = getModuleDataByKey(rawDataObj.prjData, "feeRate");
  305. if (feeRate) {
  306. //把综合费率树结构数据拉扁
  307. // console.log(feeRate);
  308. let newFeeRates = setupFeeRate(feeRate.data._doc);
  309. // console.log(newFeeRates);
  310. feeRate.data._doc.rates = newFeeRates;
  311. }
  312. if (tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  313. for (let preHandle of tpl[JV.NODE_MAP_DATA_HANDLE_INFO]) {
  314. let srcData = getModuleDataByKey(rawDataObj.prjData, preHandle[JV.PROP_DATA_KEY]);
  315. switch(preHandle[JV.PROP_HANDLE_TYPE]) {
  316. case JV.PROP_HANDLE_TYPE_SORT:
  317. sortData(srcData, preHandle, rawDataObj.prjData);
  318. break;
  319. case JV.PROP_HANDLE_TYPE_FILTER:
  320. filterData(srcData, preHandle, rawDataObj.prjData);
  321. break;
  322. case JV.PROP_HANDLE_TYPE_SUM:
  323. // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/insertedRawDataData10.jsp");
  324. summaryData(srcData, preHandle, rawDataObj.prjData, rawDataObj.prj);
  325. // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/insertedRawDataData11.jsp");
  326. break;
  327. case JV.PROP_HANDLE_TYPE_ADD_DUMMY:
  328. addDummyData(srcData, preHandle);
  329. break;
  330. case JV.PROP_HANDLE_TYPE_ADJUST:
  331. adjustData(srcData, preHandle);
  332. break;
  333. case JV.PROP_HANDLE_TYPE_BILLS_DATA_MOVE:
  334. //把显示在清单中的量材转移到工料机去, 顺带把项目工料机的组成物工料机分解出来,分解的工料机作为定额工料机保存(02表用)
  335. moveRationData(srcData, rawDataObj);
  336. break;
  337. case JV.PROP_HANDLE_TYPE_COMPONENT_MOVE:
  338. //把定额下的组成物的工料机分解出来,分解的工料机作为定额工料机保存(02表用)
  339. moveRationComponentData(srcData, rawDataObj);
  340. break;
  341. case JV.PROP_HANDLE_TYPE_COMPONENT_REPLACEMENT:
  342. //重庆2018 09-x表专用,机械数据用 组成物替换掉 ^_^!
  343. componentReplacement(rawDataObj);
  344. break;
  345. default:
  346. break;
  347. }
  348. }
  349. }
  350. //一些计算(不保存数据,需要动态计算的)
  351. if (rawDataObj.hasOwnProperty(`prj`) && rawDataObj.hasOwnProperty(`prjData`)) {
  352. let calcOptions = rawDataObj.prj._doc.property.calcOptions;
  353. let decimalObj = rawDataObj.prj._doc.property.decimal;
  354. let labourCoeDatas = getModuleDataByKey(rawDataObj.prjData, "labour_coe");
  355. let prjGLJDatas = getModuleDataByKey(rawDataObj.prjData, "projectGLJ");
  356. let rationDatas = getModuleDataByKey(rawDataObj.prjData, "ration");
  357. if (calcOptions && decimalObj && labourCoeDatas && labourCoeDatas && prjGLJDatas && rationDatas) {
  358. for (let rationItem of rationDatas.data) {
  359. let glj = _.find(prjGLJDatas.data.gljList, {'id': rationItem.projectGLJID});
  360. if (glj) {
  361. let newGlj = gljUtil.getGLJPrice(glj, prjGLJDatas.data, calcOptions, labourCoeDatas, decimalObj, false, _, scMathUtil);
  362. rationItem.marketPrice = newGlj.marketPrice;
  363. rationItem.basePrice = newGlj.basePrice;
  364. rationItem.adjustPrice = newGlj.adjustPrice;
  365. rationItem.marketUnitFee = newGlj.marketPrice;//更新树节点市场单价列的值
  366. }
  367. }
  368. }
  369. // console.log("重新计算!");
  370. }
  371. //
  372. let rptDataObj = {};
  373. rptDataObj[JV.DATA_DISCRETE_DATA] = [];
  374. rptDataObj[JV.DATA_MASTER_DATA] = [];
  375. rptDataObj[JV.DATA_DETAIL_DATA] = [];
  376. rptDataObj[JV.DATA_MASTER_DATA_EX] = [];
  377. rptDataObj[JV.DATA_DETAIL_DATA_EX] = [];
  378. if (rawDataObj.hasOwnProperty(`prj`) && rawDataObj.hasOwnProperty(`prjData`)) {
  379. rptDataObj.DecimalObj = {};
  380. rptDataObj.DecimalObj.prjDecimal = $PROJECT.MAIN["myOwnRawDataObj"].decimal; //为函数 P_REF() 准备数据
  381. rptDataObj.DecimalObj.unitDecimal = getUnitDecimal($PROJECT.MAIN["myOwnRawDataObj"].billsQuantityDecimal); //为函数 U_REF() 准备数据
  382. }
  383. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS], rptDataObj[JV.DATA_DISCRETE_DATA], $PROJECT);
  384. // console.log(JV.DATA_DISCRETE_DATA);
  385. // console.log(rptDataObj[JV.DATA_DISCRETE_DATA]);
  386. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS], rptDataObj[JV.DATA_MASTER_DATA], $PROJECT);
  387. // console.log(JV.DATA_MASTER_DATA);
  388. // console.log(rptDataObj[JV.DATA_MASTER_DATA]);
  389. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS], rptDataObj[JV.DATA_DETAIL_DATA], $PROJECT);
  390. // console.log(JV.DATA_DETAIL_DATA);
  391. // console.log(rptDataObj[JV.DATA_DETAIL_DATA]);
  392. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX], rptDataObj[JV.DATA_MASTER_DATA_EX], $PROJECT);
  393. // console.log(JV.DATA_MASTER_DATA_EX);
  394. // console.log(rptDataObj[JV.DATA_MASTER_DATA_EX]);
  395. assembleFields(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX], rptDataObj[JV.DATA_DETAIL_DATA_EX], $PROJECT);
  396. // console.log(JV.DATA_DETAIL_DATA_EX);
  397. // console.log(rptDataObj[JV.DATA_DETAIL_DATA_EX]);
  398. // fsUtil.writeObjToFile(rptDataObj, "D:/GitHome/ConstructionCost/tmp/insertedOriginalData.jsp");
  399. // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/insertedRawDataData.jsp");
  400. // fsUtil.writeObjToFile($PROJECT, "D:/GitHome/ConstructionCost/tmp/$PROJECTData.jsp");
  401. // fsUtil.writeObjToFile(tpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS], "D:/GitHome/ConstructionCost/tmp/masterFieldsAfterAssemble.jsp");
  402. // fsUtil.writeObjToFile(tpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS], "D:/GitHome/ConstructionCost/tmp/detailFieldsAfterAssemble.jsp");
  403. return rptDataObj;
  404. };
  405. }
  406. //小数位数依据单位来调整(如吨、公斤的精度要求就不同)
  407. function getUnitDecimal(unitDecimalArr) {
  408. let rst = {"default_decimal": 2};
  409. if (unitDecimalArr) {
  410. for (let item of unitDecimalArr) {
  411. if (item.unit === "其他未列单位") {
  412. rst["default_decimal"] = item.decimal;
  413. } else {
  414. rst['unit_decimal_' + item.unit] = item.decimal;
  415. }
  416. }
  417. }
  418. return rst;
  419. }
  420. function getModuleDataByKey(prjData, key) {
  421. let rst = null;
  422. if (prjData) {
  423. for (let item of prjData) {
  424. if (item.moduleName === key) {
  425. rst = item;
  426. break;
  427. }
  428. }
  429. }
  430. return rst;
  431. }
  432. function summaryData(sourceData, handleCfg, prjData, prjMain){
  433. let rstArr = [], tempRstArr = [];
  434. let curParentPrjData = {};
  435. let precision = 6;
  436. if (handleCfg[JV.PROP_HANDLE_TYPE_PRECISION]) {
  437. if (isNaN(parseInt(handleCfg[JV.PROP_HANDLE_TYPE_PRECISION]))) {
  438. let properties = handleCfg[JV.PROP_HANDLE_TYPE_PRECISION].split('.');
  439. let currentProperty = prjMain[properties[0]];
  440. for (let idx = 1; idx < properties.length; idx++) {
  441. currentProperty = currentProperty[properties[idx]];
  442. }
  443. if (!isNaN(parseInt(currentProperty))) {
  444. precision = parseInt(currentProperty);
  445. }
  446. } else {
  447. precision = parseInt(handleCfg[JV.PROP_HANDLE_TYPE_PRECISION]);
  448. }
  449. }
  450. for (let item of getActDataArr(sourceData)) {
  451. if (item._doc) {
  452. tempRstArr.push(item._doc);
  453. } else {
  454. tempRstArr.push(item);
  455. }
  456. }
  457. let private_cal_before_sum = function () {
  458. for (let calcAheadObj of handleCfg[JV.PROP_SUM_CALC_AHEAD]) {
  459. if (!curParentPrjData[calcAheadObj["seeking_parent"]]) curParentPrjData[calcAheadObj["seeking_parent"]] = getModuleDataByKey(prjData, calcAheadObj["seeking_parent"]);
  460. for (let idxP = 0; idxP < curParentPrjData[calcAheadObj["seeking_parent"]].data.length; idxP++) {
  461. let pDataItem = curParentPrjData[calcAheadObj["seeking_parent"]].data[idxP];
  462. let data = (pDataItem._doc)?pDataItem._doc:pDataItem;
  463. for (let idx = 0; idx < tempRstArr.length; idx++) {
  464. let dtlItem = tempRstArr[idx];
  465. if (dtlItem[calcAheadObj["seeking_key"]] === data[calcAheadObj["parent_key"]]) {
  466. for (let sumKey of handleCfg[JV.PROP_SUM_SUM_KEYS]) {
  467. switch (calcAheadObj[JV.PROP_SUM_CACL_TYPE]) {
  468. case "+":
  469. dtlItem[sumKey] = parseFloat(dtlItem[sumKey]) + parseFloat(data[calcAheadObj["calc_property"]]);
  470. break;
  471. case "-":
  472. dtlItem[sumKey] = parseFloat(dtlItem[sumKey]) - parseFloat(data[calcAheadObj["calc_property"]]);
  473. break;
  474. case "*":
  475. dtlItem[sumKey] = dtlItem[sumKey] * parseFloat(data[calcAheadObj["calc_property"]]);
  476. break;
  477. case "/":
  478. dtlItem[sumKey] = dtlItem[sumKey] / parseFloat(data[calcAheadObj["calc_property"]]);
  479. break;
  480. default:
  481. break;
  482. }
  483. }
  484. // break;
  485. }
  486. }
  487. }
  488. }
  489. };
  490. let private_get_grp_key = function (item) {
  491. let keys = [];
  492. for (let cfg of handleCfg[JV.PROP_SUM_GROUP_KEYS]) {
  493. if (typeof cfg === "string") {
  494. keys.push(item[cfg]);
  495. } else {
  496. if (!curParentPrjData[cfg["seeking_parent"]]) curParentPrjData[cfg["seeking_parent"]] = getModuleDataByKey(prjData, cfg["seeking_parent"]);
  497. for (let pDataItem of curParentPrjData[cfg["seeking_parent"]].data) {
  498. let data = (pDataItem._doc)?pDataItem._doc:pDataItem;
  499. if (item[cfg["seeking_key"]] === data[cfg["parent_key"]]) {
  500. keys.push(data[cfg["parent_grp_key"]]);
  501. break;
  502. }
  503. }
  504. }
  505. }
  506. return ( "grp_key_" + keys.join('_'));
  507. };
  508. let sumObj = {};
  509. if (handleCfg[JV.PROP_SUM_CALC_AHEAD] && handleCfg[JV.PROP_SUM_CALC_AHEAD].length > 0) {
  510. private_cal_before_sum();
  511. }
  512. for (let dtl of tempRstArr) {
  513. let grpKey = private_get_grp_key(dtl);
  514. if (sumObj[grpKey] === null || sumObj[grpKey] === undefined) {
  515. sumObj[grpKey] = dtl;
  516. rstArr.push(dtl);
  517. } else {
  518. for (let sumKey of handleCfg[JV.PROP_SUM_SUM_KEYS]) {
  519. if (dtl[sumKey]) {
  520. // sumObj[grpKey][sumKey] += dtl[sumKey];
  521. sumObj[grpKey][sumKey] = parseFloat(parseFloat(sumObj[grpKey][sumKey]).toFixed(precision)) + parseFloat(parseFloat(dtl[sumKey]).toFixed(precision));
  522. }
  523. }
  524. }
  525. }
  526. replaceActDataArr(sourceData, rstArr);
  527. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/sumRst.jsp");
  528. }
  529. function filterData(sourceData, handleCfg, prjData) {
  530. let rstArr = [], tempRstArr = [];
  531. for (let item of getActDataArr(sourceData)) {
  532. if (item._doc) {
  533. tempRstArr.push(item._doc);
  534. } else {
  535. tempRstArr.push(item);
  536. }
  537. }
  538. let private_chkVal = function (src, compVal, compStr) {
  539. let rst = true;
  540. switch (compStr) {
  541. case "==" :
  542. rst = (src == compVal);
  543. break;
  544. case "===" :
  545. rst = (src === compVal);
  546. break;
  547. case ">" :
  548. rst = (src > compVal);
  549. break;
  550. case ">=" :
  551. rst = (src >= compVal);
  552. break;
  553. case "<" :
  554. rst = (src < compVal);
  555. break;
  556. case "<=" :
  557. rst = (src <= compVal);
  558. break;
  559. case "!=" :
  560. rst = (src != compVal);
  561. break;
  562. case "!==" :
  563. rst = (src !== compVal);
  564. break;
  565. case "in" :
  566. if (compVal instanceof Array) {
  567. rst = compVal.indexOf(src) >= 0;
  568. } else {
  569. //string,需要转类型
  570. let newInCv = JSON.parse(compVal);
  571. if (newInCv instanceof Array) {
  572. rst = newInCv.indexOf(src) >= 0;
  573. } else {
  574. rst = false;
  575. }
  576. }
  577. break;
  578. case "not in":
  579. if (compVal instanceof Array) {
  580. rst = compVal.indexOf(src) < 0;
  581. } else {
  582. //string,需要转类型
  583. let newNotInCv = JSON.parse(compVal);
  584. if (newNotInCv instanceof Array) {
  585. rst = (newNotInCv.indexOf(src) < 0);
  586. } else {
  587. rst = true;
  588. }
  589. }
  590. break;
  591. default:
  592. rst = true;
  593. }
  594. return rst;
  595. };
  596. let private_chkArrVal = function(arr, key, compVal, compStr){
  597. let rst = false;
  598. if (arr.length > 0) {
  599. for (let arrItem of arr) {
  600. if (arrItem[key] !== undefined) {
  601. // 可以为null值去判断
  602. rst = private_chkVal(arrItem[key], compVal, compStr);
  603. }
  604. if (rst) {
  605. break;
  606. }
  607. }
  608. } else {
  609. //在某些判断条件下(含有'非'判断),如arr没有数组项,默认结果反而是true
  610. switch (compStr) {
  611. case "!=" :
  612. case "!==" :
  613. case "not in":
  614. rst = true;
  615. break;
  616. default:
  617. break;
  618. }
  619. }
  620. return rst;
  621. };
  622. let private_filter_compare = function (item, filterCfg) {
  623. let compareObj = {};
  624. let compRst = true;
  625. let curComparePrjData = null;
  626. let startIdx = 0;
  627. let private_ref_join = function(refKey, targetDataKey, targetPropertyKey) {
  628. let rst = null, objDataArr = null;
  629. curComparePrjData = getModuleDataByKey(prjData, targetDataKey);
  630. try {
  631. if (curComparePrjData !== null) {
  632. objDataArr = getActDataArr(curComparePrjData);
  633. for (let dtl of objDataArr) {
  634. if (item[refKey] === dtl[targetPropertyKey]) {
  635. rst = dtl;
  636. break;
  637. }
  638. }
  639. }
  640. } finally {
  641. curComparePrjData = null;
  642. }
  643. return rst;
  644. };
  645. for (let cfg of filterCfg[JV.PROP_FILTER_KEYS]) {
  646. if (cfg[JV.PROP_FILTER_COMPARE_VAL]) {
  647. //比较key值
  648. let keys = cfg.key.split(".");
  649. if (keys.length > 1) {
  650. let lastObj = item;
  651. for (let i = 0; i < keys.length - 1; i++) {
  652. if (keys[i].indexOf("ref_join(") === 0) {
  653. let params = keys[i].slice(9, keys[i].length - 1).split(",");
  654. if (params.length === 3) {
  655. lastObj = private_ref_join(params[0], params[1], params[2]);
  656. }
  657. if (!(lastObj)) {
  658. compRst = false;
  659. break;
  660. }
  661. } else {
  662. lastObj = item[keys[i]];
  663. if (!(lastObj)) {
  664. compRst = false;
  665. break;
  666. }
  667. }
  668. }
  669. if (lastObj) {
  670. if (lastObj instanceof Array) {
  671. compRst = private_chkArrVal(lastObj, keys[keys.length - 1], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
  672. } else {
  673. compRst = private_chkVal(lastObj[keys[keys.length - 1]], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
  674. }
  675. }
  676. } else {
  677. compRst = private_chkVal(item[cfg.key], cfg[JV.PROP_FILTER_COMPARE_VAL], cfg[JV.PROP_FILTER_CONDITION]);
  678. }
  679. } else if (cfg[JV.PROP_FILTER_COMPARE_OBJ] && cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]){
  680. //通过其他对象来过滤
  681. if (!curComparePrjData) {
  682. curComparePrjData = getModuleDataByKey(prjData, cfg[JV.PROP_FILTER_COMPARE_OBJ]);
  683. }
  684. if (cfg[JV.PROP_FILTER_CONDITION] === "in" || cfg[JV.PROP_FILTER_CONDITION] === "not in") {
  685. let compareArr = null;
  686. if (!compareObj.hasOwnProperty(cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString())) {
  687. compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()] = [];
  688. compareArr = compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()];
  689. for (let data of getActDataArr(curComparePrjData)) {
  690. if (compareArr.indexOf(data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]]) < 0) {
  691. compareArr.push(data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]]);
  692. }
  693. }
  694. } else {
  695. compareArr = compareObj[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY] + startIdx.toString()];
  696. }
  697. compRst = private_chkVal(item[cfg.key], compareArr, cfg[JV.PROP_FILTER_CONDITION]);
  698. } else {
  699. for (let data of getActDataArr(curComparePrjData)) {
  700. compRst = private_chkVal(item[cfg.key], data[cfg[JV.PROP_FILTER_COMPARE_OBJ_KEY]], cfg[JV.PROP_FILTER_CONDITION]);
  701. if (compRst) break;
  702. }
  703. }
  704. }
  705. startIdx++;
  706. if (!compRst) {
  707. break; //有不符合条件的数据则退出(这里的判断条件是and关系)
  708. }
  709. }
  710. return compRst;
  711. };
  712. let private_sub_filter_compare = function (dtlItem, subFilters) {
  713. let cmpRst = false;
  714. for (let dtlCfg of subFilters) {
  715. cmpRst = private_filter_compare(dtlItem, dtlCfg);
  716. if (cmpRst) {
  717. if (dtlCfg[JV.PROP_OTHER_SUB_FILTER] && dtlCfg[JV.PROP_OTHER_SUB_FILTER].length > 0) {
  718. cmpRst = private_sub_filter_compare(dtlItem, dtlCfg[JV.PROP_OTHER_SUB_FILTER]);
  719. if (cmpRst) break;
  720. } else {
  721. break;
  722. }
  723. }
  724. }
  725. return cmpRst;
  726. };
  727. for (let item of tempRstArr) {
  728. if (private_filter_compare(item, handleCfg)) {
  729. rstArr.push(item);
  730. }
  731. }
  732. if (handleCfg[JV.PROP_OTHER_SUB_FILTER] && handleCfg[JV.PROP_OTHER_SUB_FILTER].length > 0) {
  733. let newRstArr = [];
  734. for (let dtlItem of rstArr) {
  735. let cmpRst = private_sub_filter_compare(dtlItem, handleCfg[JV.PROP_OTHER_SUB_FILTER]);
  736. if (cmpRst) {
  737. newRstArr.push(dtlItem);
  738. }
  739. }
  740. rstArr = newRstArr;
  741. }
  742. replaceActDataArr(sourceData, rstArr);
  743. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/filteredRst.jsp");
  744. }
  745. function adjustData(sourceData, adjustCfg) {
  746. let rstArr = [];
  747. for (let item of getActDataArr(sourceData)) {
  748. if (item._doc) {
  749. rstArr.push(item._doc);
  750. } else {
  751. rstArr.push(item);
  752. }
  753. }
  754. for (let item of adjustCfg[JV.PROP_ADJUST_COLLECTION]) {
  755. for (let rec of rstArr) {
  756. if (item[JV.PROP_ADJUST_ACTION] === "prefix") {
  757. rec[item.key] = item[JV.PROP_ADJUST_ACTION_VAL] + rec[item.key];
  758. } else if (item[JV.PROP_ADJUST_ACTION] === "suffix") {
  759. rec[item.key] = rec[item.key] + item[JV.PROP_ADJUST_ACTION_VAL];
  760. }
  761. }
  762. }
  763. replaceActDataArr(sourceData, rstArr);
  764. }
  765. function componentReplacement(rawDataObj) {
  766. let rationGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.RATION_GLJ),
  767. prjGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.PROJECTGLJ),
  768. gljAssistant = prjGljData.data.gljAssistant;
  769. let connectStrArr = [], dataArr = getActDataArr(rationGljData), rvRGljIds = [], replaceGljItems = [];
  770. for (let idx = 0; idx < dataArr.length; idx++) {
  771. let rationGljItem = dataArr[idx];
  772. if (rationGljItem.type === 301 || rationGljItem.type === 302) {
  773. connectStrArr[0] = rationGljItem.code;
  774. connectStrArr[1] = rationGljItem.name;
  775. connectStrArr[2] = rationGljItem.specs;
  776. connectStrArr[3] = rationGljItem.unit;
  777. connectStrArr[4] = rationGljItem.type;
  778. let mapData = prjGljData.data.mixRatioMap[connectStrArr.join('|-|')];
  779. if (mapData) {
  780. rvRGljIds.push(idx);
  781. let rgItems = [];
  782. replaceGljItems.push(rgItems);
  783. for (let mapItem of mapData) {
  784. let copyItem = {};
  785. rgItems.push(copyItem);
  786. copyItem.ID = `Replace_` + rationGljItem.ID;
  787. copyItem.projectID = rationGljItem.projectID;
  788. copyItem.GLJID = rationGljItem.GLJID;
  789. copyItem.rationID = rationGljItem.rationID;
  790. if (gljAssistant.componentGljIds[COMPONENT_GLJID_PRE + mapItem.glj_id]) {
  791. copyItem.projectGLJID = gljAssistant.componentGljIds[COMPONENT_GLJID_PRE + mapItem.glj_id].id; //关键key
  792. } else {
  793. copyItem.projectGLJID = mapItem.glj_id; //不应该走到这一步,否则就是错误!!!
  794. }
  795. copyItem.rationItemQuantity = rationGljItem.rationItemQuantity;
  796. // copyItem.quantity = rationGljItem.quantity;
  797. copyItem.quantity = rationGljItem.quantity * mapItem.consumption;
  798. copyItem.name = mapItem.name;
  799. copyItem.code = mapItem.code;
  800. copyItem.original_code = mapItem.code;
  801. copyItem.unit = mapItem.unit;
  802. copyItem.specs = mapItem.specs;
  803. copyItem.model = gljAssistant.gljIds['gljId_'+rationGljItem.projectGLJID].model;
  804. copyItem.shortName = mapItem.shortName;
  805. copyItem.billsItemID = rationGljItem.billsItemID;
  806. copyItem.type = mapItem.type; //关键key
  807. }
  808. }
  809. }
  810. }
  811. if (rvRGljIds.length > 0) {
  812. for (let lIdx = rvRGljIds.length - 1; lIdx >= 0; lIdx--) {
  813. dataArr.splice(rvRGljIds[lIdx],1);
  814. }
  815. for (let componentItems of replaceGljItems) {
  816. for (let compItem of componentItems) {
  817. dataArr.push(compItem);
  818. //gljAssistant[COMPONENT_GLJID_PRE + mapItem.glj_id]
  819. }
  820. }
  821. }
  822. }
  823. function moveRationData(rationData, rawDataObj) {
  824. if (rawDataObj) {
  825. // let rationData = getModuleDataByKey(rawDataObj.prjData, projectConst.RATION);
  826. let rationGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.RATION_GLJ);
  827. let prjGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.PROJECTGLJ);
  828. let dummyCode = " ", dummyPRJID = 10101010;
  829. for (let rationItem of getActDataArr(rationData)) {
  830. if (rationItem.type === 2 || rationItem.type === 3) {
  831. //2: 量材(人工/材料/机械/主材/设备) 3:材料(材料/主材/设备)
  832. let copyItem = {};
  833. copyItem.ID = rationItem.ID;
  834. copyItem.projectID = rationItem.projectID;
  835. copyItem.GLJID = rationItem.GLJID;
  836. copyItem.rationID = rationItem.ID;
  837. copyItem.rationItemQuantity = 1;
  838. copyItem.quantity = 1;
  839. // copyItem.specialType_quantity = rationItem.quantity;
  840. copyItem.name = rationItem.name;
  841. if (rationItem.code) {
  842. copyItem.code = rationItem.ID + "_" + rationItem.code;
  843. } else {
  844. copyItem.code = dummyCode;
  845. dummyCode = dummyCode + " ";
  846. }
  847. copyItem.original_code = rationItem.original_code;
  848. copyItem.unit = rationItem.unit;
  849. copyItem.specs = rationItem.specs;
  850. copyItem.shortName = rationItem.shortName;
  851. copyItem.billsItemID = rationItem.billsItemID;
  852. copyItem.type = rationItem.subType;
  853. if (rationItem.projectGLJID) {
  854. copyItem.projectGLJID = rationItem.projectGLJID;
  855. } else {
  856. //add dummy project GLJ
  857. let dummyPrjItem = {};
  858. copyItem.projectGLJID = dummyPRJID;
  859. dummyPrjItem.id = dummyPRJID;
  860. dummyPRJID++;
  861. dummyPrjItem.name = rationItem.name;
  862. dummyPrjItem.unit_price = {};
  863. dummyPrjItem.unit_price.base_price = 0;
  864. dummyPrjItem.unit_price.market_price = (rationItem["marketUnitFee"])?rationItem["marketUnitFee"]:0;
  865. dummyPrjItem.quantity = 1; //这里设置为1只是为了不被误过滤
  866. dummyPrjItem.is_evaluate = 0;
  867. prjGljData.data.gljList.push(dummyPrjItem);
  868. }
  869. rationGljData.data.push(copyItem);
  870. } else {
  871. //无
  872. }
  873. }
  874. // fsUtil.writeObjToFile(rationGljData.data, "D:/GitHome/ConstructionCost/tmp/afterMoveGLJ.jsp");
  875. }
  876. }
  877. //有些项目工料机有组成物,有些表需要显示这些组成物,此函数把这些组成物抽出并归类到定额工料机下
  878. function moveRationComponentData(rationData, rawDataObj) {
  879. let rationGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.RATION_GLJ);
  880. let prjGljData = getModuleDataByKey(rawDataObj.prjData, projectConst.PROJECTGLJ);
  881. let newComponentItem = [];
  882. for (let rationGljItem of getActDataArr(rationGljData)) {
  883. for (let prjGljItem of prjGljData.data.gljList) {
  884. if (prjGljItem.id === rationGljItem.projectGLJID) {
  885. if (prjGljItem.ratio_data && prjGljItem.ratio_data.length > 0 && !gljUtil.isConcreteType(prjGljItem.type)) {
  886. //有组成物
  887. for (let comItem of prjGljItem.ratio_data) {
  888. let copyItem = {};
  889. copyItem.ID = comItem._id.toString();
  890. copyItem.projectID = rationGljItem.projectID;
  891. copyItem.rationID = rationGljItem.rationID;
  892. // copyItem.rationItemQuantity = rationGljItem.quantity;
  893. copyItem.quantity = parseFloat(rationGljItem.quantity) * parseFloat(comItem.consumption);
  894. copyItem.name = comItem.name;
  895. copyItem.code = comItem.code;
  896. copyItem.unit = comItem.unit;
  897. copyItem.specs = comItem.specs;
  898. // copyItem.shortName = comItem.shortName;
  899. copyItem.billsItemID = rationGljItem.billsItemID;
  900. copyItem.type = comItem.type;
  901. //其他属性,要根据工料机ID来查找
  902. for (let gljIdx = 0; gljIdx < prjGljData.data.gljList.length; gljIdx++) {
  903. if (prjGljData.data.gljList[gljIdx].code === comItem.code) {
  904. copyItem.projectGLJID = prjGljData.data.gljList[gljIdx].id;
  905. copyItem.original_code = prjGljData.data.gljList[gljIdx].original_code;
  906. copyItem.is_main_material = prjGljData.data.gljList[gljIdx].is_main_material;
  907. copyItem.is_adjust_price = prjGljData.data.gljList[gljIdx].is_adjust_price;
  908. copyItem.unit_price = {};
  909. copyItem.unit_price.base_price = prjGljData.data.gljList[gljIdx].unit_price.base_price;
  910. copyItem.unit_price.market_price = prjGljData.data.gljList[gljIdx].unit_price.market_price;
  911. break;
  912. }
  913. }
  914. newComponentItem.push(copyItem);
  915. }
  916. }
  917. break;
  918. }
  919. }
  920. }
  921. rationGljData.data = rationGljData.data.concat(newComponentItem);
  922. }
  923. function getDupGrpKeyVals(sourceData, segKeys) {
  924. let rst = [];
  925. function pushKeyVal(item) {
  926. let tr = {};
  927. for (let i = 0; i < segKeys.length; i++) {
  928. tr[segKeys[i]] = item[segKeys[i]];
  929. }
  930. rst.push(tr);
  931. }
  932. for (let idx = 0; idx < sourceData.length; idx++) {
  933. let itemRec = sourceData[idx];
  934. if (idx === 0) {
  935. pushKeyVal(itemRec);
  936. continue;
  937. }
  938. let hasDiff = false;
  939. for (let i = 0; i < segKeys.length; i++) {
  940. if (itemRec[segKeys[i]] !== sourceData[idx - 1][segKeys[i]]) {
  941. hasDiff = true;
  942. break;
  943. }
  944. }
  945. if (hasDiff) {
  946. pushKeyVal(itemRec);
  947. }
  948. }
  949. return rst;
  950. }
  951. function addDummyData(sourceData, addCfg) {
  952. let rstArr = [], tempRstArr = [];
  953. for (let item of getActDataArr(sourceData)) {
  954. if (item._doc) {
  955. tempRstArr.push(item._doc);
  956. } else {
  957. tempRstArr.push(item);
  958. }
  959. }
  960. for (let item of addCfg[JV.PROP_DUMMY_COLLECTION]) {
  961. let newRecStr = JSON.stringify(item[JV.PROP_DUMMY_VAL]), cacheGrpKeyRecs = null;
  962. if (item[JV.PROP_FREQUENCY] === "OncePerGrp") {
  963. if (!cacheGrpKeyRecs) {
  964. cacheGrpKeyRecs = {};
  965. }
  966. let cacheKey = "";
  967. for (let key of item[JV.PROP_GRP_KEYS]) {
  968. cacheKey += "_" + key;
  969. }
  970. if (!cacheGrpKeyRecs[cacheKey]) {
  971. cacheGrpKeyRecs[cacheKey] = getDupGrpKeyVals(tempRstArr, item[JV.PROP_GRP_KEYS]);
  972. }
  973. for (let kv of cacheGrpKeyRecs[cacheKey]) {
  974. let rec = JSON.parse(newRecStr);
  975. for (let key of item[JV.PROP_GRP_KEYS]) {
  976. rec[key] = kv[key];
  977. }
  978. rstArr.push(rec);
  979. }
  980. } else if (item[JV.PROP_FREQUENCY] === "Once") {
  981. rstArr.push(JSON.parse(newRecStr));
  982. }
  983. }
  984. rstArr = rstArr.concat(tempRstArr);
  985. replaceActDataArr(sourceData, rstArr);
  986. }
  987. function getGLJBizType2018(orgType, orgCode, orgName) {
  988. let rst = orgType;
  989. if (orgType === GLJ_TYPE.Labour) {
  990. rst = 11;
  991. if (orgCode === "000000") rst = 10;
  992. } else if (orgType === GLJ_TYPE.Material || (orgType >= 200 && orgType < 300)) {
  993. //材料
  994. if (orgCode === "000000") {
  995. rst = 200; //2.材料
  996. } else if (orgCode === "000000_1") {
  997. rst = 230; //(1) 计价材料
  998. } else if (orgCode === "000000_2") {
  999. rst = 240; //(2) 其他材料费
  1000. } else {
  1001. if (orgType === GLJ_TYPE.OTHER_MATERIAL) {
  1002. //其他材料
  1003. rst = 245;
  1004. } else {
  1005. //计价材料
  1006. rst = 235;
  1007. }
  1008. }
  1009. } else if (orgType === GLJ_TYPE.Machine || (orgType >= 300 && orgType < 400)) {
  1010. //机械
  1011. if (orgCode === "000000") {
  1012. rst = 300; //3.机械
  1013. } else if (orgCode === "000000_1") {
  1014. rst = 330; //(1) 机上人工
  1015. } else if (orgCode === "000000_2") {
  1016. rst = 340; //(2) 燃油动力费
  1017. } else if (orgCode === "000000_3") {
  1018. rst = 350; //(2) 施工机具摊销费
  1019. } else {
  1020. if (orgType === GLJ_TYPE.MACHINE_LABOUR) {
  1021. //机上人工
  1022. rst = 335;
  1023. } else if (orgType === GLJ_TYPE.FUEL_POWER_FEE) {
  1024. //燃油动力费
  1025. rst = 345;
  1026. } else if (oprMachines.indexOf(orgType) >= 0) {
  1027. //施工机具摊销费
  1028. rst = 355 + oprMachines.indexOf(orgType);
  1029. }
  1030. }
  1031. }
  1032. return rst;
  1033. }
  1034. function getGLJBizType(orgType, orgCode, orgName) {
  1035. let rst = orgType;
  1036. if (orgName.indexOf("其他材料费") >= 0) {
  1037. rst = 299;
  1038. } else if (orgType === GLJ_TYPE.Labour) {
  1039. rst = 11;
  1040. if (orgCode === "000000") rst = 10;
  1041. } else if (orgType === GLJ_TYPE.Main_Material || orgType === GLJ_TYPE.Equipment) {
  1042. //未计价材料(主材 + 设备)
  1043. rst = 30 + orgType;
  1044. } else if (orgType === GLJ_TYPE.Material || (orgType >= 200 && orgType < 300)) {
  1045. //材料
  1046. if (orgCode === "000000") {
  1047. rst = 20; //2.材料
  1048. } else if (orgCode === "000000_1") {
  1049. rst = 30; //(1) 未计价材料
  1050. } else if (orgCode === "000000_2") {
  1051. rst = 40; //(2) 辅助材料
  1052. } else if (orgCode === "000000_3") {
  1053. rst = 50; //(3) 其他材料费
  1054. } else {
  1055. rst = 45; //到这里就只有辅助材料没有预处理了
  1056. }
  1057. } else if (orgType === GLJ_TYPE.Machine || (orgType >= 300 && orgType < 400)) {
  1058. //机械
  1059. if (orgCode === "000000") {
  1060. rst = 300; //3.机械
  1061. } else if (orgType === GLJ_TYPE.Machine) {
  1062. rst = 300.5;
  1063. } else {
  1064. rst = orgType;
  1065. }
  1066. }
  1067. return rst;
  1068. }
  1069. function sortData(sourceData, sortCfg, prjData) {
  1070. let rst = getActDataArr(sourceData), tempRstArr = [];
  1071. let sortType = sortCfg[JV.PROP_SORT_TYPE];
  1072. let srcData = getActDataArr(sourceData);
  1073. for (let item of srcData) {
  1074. if (item._doc) {
  1075. tempRstArr.push(item._doc);
  1076. } else {
  1077. tempRstArr.push(item);
  1078. }
  1079. }
  1080. function private_normal_sort(destArr, sortKeys) {
  1081. destArr.sort(function(a, b){
  1082. let compRst = 0;
  1083. for (let comp of sortKeys) {
  1084. let reverse = (comp.order === 'ascend')?1:(-1);
  1085. //
  1086. if (a[comp.key] > b[comp.key]) {
  1087. compRst = reverse;
  1088. break;
  1089. } else if (a[comp.key] < b[comp.key]) {
  1090. compRst = -reverse;
  1091. break;
  1092. }
  1093. }
  1094. return compRst;
  1095. });
  1096. }
  1097. function private_parent_sort(parentArr, parentKeys, childArr, childKeys) {
  1098. let tmpRst = {}, rst = [];
  1099. for (let pItem of parentArr) {
  1100. let pKey = "key";
  1101. for (let key of parentKeys) {
  1102. pKey += "_" + pItem[key];
  1103. }
  1104. tmpRst[pKey] = [];
  1105. }
  1106. for (let cItem of childArr) {
  1107. let cKey = "key";
  1108. for (let key of childKeys) {
  1109. cKey += "_" + cItem[key];
  1110. }
  1111. if (tmpRst[cKey]) {
  1112. tmpRst[cKey].push(cItem);
  1113. } else {
  1114. //unknown child value! should be filtered!
  1115. }
  1116. }
  1117. // childArr.splice(0);
  1118. for (let pItem of parentArr) {
  1119. let pKey = "key";
  1120. for (let key of parentKeys) {
  1121. pKey += "_" + pItem[key];
  1122. }
  1123. rst.push(tmpRst[pKey]);
  1124. // for (let rItem of tmpRst[pKey]) {
  1125. // childArr.push(rItem);
  1126. // }
  1127. }
  1128. return rst;
  1129. }
  1130. switch (sortType) {
  1131. case "tree":
  1132. let addLevel = true;
  1133. rst = treeUtil.buildTreeNodeDirectly(tempRstArr, addLevel);
  1134. let newTopArr = [];
  1135. if ((sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES] && sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].length > 0) ||
  1136. (sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES] && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].length > 0)) {
  1137. let local_check_bills = function(tItem) {
  1138. let chkDtl = false;
  1139. if (tItem.flags && tItem.flags.length > 0) {
  1140. for (let flagItem of tItem.flags) {
  1141. if (sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].indexOf(flagItem.flag) >= 0) {
  1142. newTopArr.push(tItem);
  1143. chkDtl = true;
  1144. break;
  1145. }
  1146. }
  1147. }
  1148. if (!chkDtl && tItem.items && tItem.items.length > 0) {
  1149. for (let dtlItem of tItem.items) {
  1150. local_check_bills(dtlItem);
  1151. }
  1152. }
  1153. };
  1154. for (let topItem of rst) {
  1155. let chkTop = false;
  1156. if (topItem.flags && topItem.flags.length > 0) {
  1157. for (let flagItem of topItem.flags) {
  1158. if (sortCfg[JV.PROP_FILTER_TOP_BILLS_NODES].indexOf(flagItem.flag) >= 0) {
  1159. newTopArr.push(topItem);
  1160. chkTop = true;
  1161. break;
  1162. }
  1163. }
  1164. }
  1165. if (!chkTop && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES] && sortCfg[JV.PROP_FILTER_OTHER_BILLS_NODES].length > 0) {
  1166. local_check_bills(topItem);
  1167. }
  1168. }
  1169. } else {
  1170. newTopArr = rst;
  1171. }
  1172. let destArr = [];
  1173. // fsUtil.writeObjToFile(newTopArr, "D:/GitHome/ConstructionCost/tmp/sortedAndFlattedRstBefore.jsp");
  1174. treeUtil.getFlatArray(newTopArr, destArr, true);
  1175. // console.log(destArr);
  1176. replaceActDataArr(sourceData, destArr);
  1177. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/sortedAndFlattedRst.jsp");
  1178. break;
  1179. case "normal":
  1180. private_normal_sort(tempRstArr, sortCfg[JV.PROP_SORT_KEYS]);
  1181. replaceActDataArr(sourceData, tempRstArr);
  1182. // fsUtil.writeObjToFile(sourceData.data, "D:/GitHome/ConstructionCost/tmp/normalSortedRst.jsp");
  1183. break;
  1184. case "accord_to_parent":
  1185. let pcKey = sortCfg[JV.PROP_PARENT_CHILD_SORT_KEY];
  1186. let parentSrcData = getModuleDataByKey(prjData, pcKey[JV.PROP_PARENT_DATA_KEY]);
  1187. if (parentSrcData) {
  1188. let tempParentArr = [];
  1189. for (let item of getActDataArr(parentSrcData)) {
  1190. if (item._doc) {
  1191. tempParentArr.push(item._doc);
  1192. } else {
  1193. tempParentArr.push(item);
  1194. }
  1195. }
  1196. let sortedRstArr = private_parent_sort(tempParentArr, pcKey[JV.PROP_PARENT_SORT_KEYS], tempRstArr, pcKey[JV.PROP_CHILD_SORT_KEYS]);
  1197. if (sortCfg[JV.PROP_OTHER_SUB_SORT] && sortCfg[JV.PROP_OTHER_SUB_SORT].length > 0) {
  1198. for (let sort of sortCfg[JV.PROP_OTHER_SUB_SORT]) {
  1199. if (sort[JV.PROP_SORT_TYPE] === 'normal') {
  1200. for (let subArr of sortedRstArr) {
  1201. private_normal_sort(subArr, sort[JV.PROP_SORT_KEYS]);
  1202. }
  1203. } else if (sort[JV.PROP_SORT_TYPE] === 'self_define') {
  1204. for (let subArr of sortedRstArr) {
  1205. // console.log(subArr);
  1206. let selfDefFunc = null;
  1207. eval('selfDefFunc = ' + sort[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  1208. subArr.sort(selfDefFunc);
  1209. // console.log(subArr);
  1210. }
  1211. }
  1212. }
  1213. }
  1214. tempRstArr.splice(0);
  1215. for (let item of sortedRstArr) {
  1216. for (let subItem of item) {
  1217. tempRstArr.push(subItem);
  1218. }
  1219. }
  1220. }
  1221. replaceActDataArr(sourceData, tempRstArr);
  1222. break;
  1223. case "self_define":
  1224. if (sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]) {
  1225. let selfDefFuncA = null;
  1226. eval('selfDefFuncA = ' + sortCfg[JV.PROP_SORT_TYPE_SELF_DEFINE_LOGIC]);
  1227. if (selfDefFuncA !== null) {
  1228. tempRstArr.sort(selfDefFuncA);
  1229. } else {
  1230. console.log('sorting function is null!!!');
  1231. }
  1232. }
  1233. replaceActDataArr(sourceData, tempRstArr);
  1234. break;
  1235. default:
  1236. //
  1237. }
  1238. return rst;
  1239. }
  1240. function setupMainFunc(obj, prop, ownRawObj) {
  1241. obj[prop] = {};
  1242. obj[prop]["myOwnRawDataObj"] = ownRawObj;
  1243. obj[prop].getProperty = ext_mainGetPropety;
  1244. obj[prop].getFee = ext_mainGetFee;
  1245. }
  1246. function setupFeeRate(feeRateData){
  1247. let cacheObj = {}, topArr = [], rstArr = [], id_pre = 'ID_';
  1248. const rateProperties = [];
  1249. for (let rate of feeRateData.rates) {
  1250. cacheObj[id_pre + rate._doc.ID] = {"obj": rate._doc, "items": []};
  1251. // cacheObj[id_pre + rate.ID].items = [];
  1252. if (rate._doc.ParentID === null || rate._doc.ParentID === undefined) {
  1253. topArr.push(cacheObj[id_pre + rate._doc.ID]);
  1254. }
  1255. }
  1256. for (let rate of feeRateData.rates) {
  1257. if (rate._doc.ParentID && cacheObj[id_pre + rate._doc.ParentID]) {
  1258. cacheObj[id_pre + rate._doc.ParentID].items.push(cacheObj[id_pre + rate._doc.ID]);
  1259. if (rateProperties.indexOf(rate._doc.name) < 0) {
  1260. rateProperties.push(rate._doc.name);
  1261. }
  1262. }
  1263. }
  1264. let pri_func_set_property_rate = function (orgRateItem, rstRateItem) {
  1265. rstRateItem[orgRateItem.obj.name] = orgRateItem.obj.rate;
  1266. for (let subOrgRateItem of orgRateItem.items) {
  1267. pri_func_set_property_rate(subOrgRateItem, rstRateItem);
  1268. }
  1269. };
  1270. for (let topRate of topArr) {
  1271. let rstItem = {"name": topRate.obj.name, "ID": topRate.obj.ID};
  1272. for (let property of rateProperties) {
  1273. rstItem[property] = 0;
  1274. }
  1275. for (let dtlRateItem of topRate.items) {
  1276. pri_func_set_property_rate(dtlRateItem, rstItem);
  1277. }
  1278. rstArr.push(rstItem);
  1279. }
  1280. return rstArr;
  1281. }
  1282. function setupFunc(obj, prop, ownRawObj) {
  1283. obj[prop] = {};
  1284. obj[prop]["myOwnRawDataObj"] = ownRawObj;
  1285. obj[prop]["myOwnOrgRawDataObj"] = ownRawObj.data;
  1286. obj[prop].getProperty = ext_getProperty;
  1287. obj[prop].getGljFreightProperty = ext_getGljFreightProperty;
  1288. obj[prop].getAdHocGljProperty = ext_getAdHocGljProperty;
  1289. obj[prop].getGljOriginalProperty = ext_getGljOriginalProperty;
  1290. obj[prop].getPropertyByRefId = ext_getPropertyByRefId;
  1291. obj[prop].getFee = ext_getFee;
  1292. obj[prop].getPropertyByForeignId = ext_getPropertyByForeignId;
  1293. obj[prop].getArrayValues = ext_getArrayValues;
  1294. obj[prop].getArrayItemByKey = ext_getArrayItemByKey;
  1295. obj[prop].getPropertyByFlag = ext_getPropertyByFlag;
  1296. obj[prop].getSubRateProperty = ext_getRateProperty;
  1297. obj[prop].getRateProperty = ext_getRateProperty;
  1298. obj[prop].getBlank = ext_getBlank;
  1299. obj[prop].getRatioDataProperty = ext_getRatioDataProperty;
  1300. obj[prop].getRatioDataParentProperty = ext_getRatioDataParentProperty;
  1301. obj[prop].getRatioDataPriceMapProperty = ext_getRatioDataPriceMapProperty;
  1302. if (prop === projectConst.CALC_PROGRAM) obj[prop].getCalcProperty = ext_getCalcProperty;
  1303. if (prop === projectConst.FEERATE) obj[prop].getFeeRate = ext_getFeeRate;
  1304. }
  1305. function assembleFields(fieldList, rstDataArr, $PROJECT) {
  1306. if (fieldList) {
  1307. for (let field of fieldList) {
  1308. shielded_exec_env($PROJECT, field, rstDataArr);
  1309. if ("Precision" in field) {
  1310. if (field["Precision"]["type"] === "fixed") {
  1311. let vrst = eval(field["Precision"]["fixedMapExpression"]);
  1312. if (vrst && vrst.length === 1) {
  1313. field.fixedPrecisionNum = vrst[0];
  1314. vrst.splice(0,1);
  1315. }
  1316. } else if (field["Precision"]["type"] === "flexible") {
  1317. let vrst = eval(field["Precision"]["flexibleMapExpression"]);
  1318. if (vrst && vrst.length === 1) {
  1319. field.flexiblePrecisionRefObj =vrst[0];
  1320. vrst.splice(0,1);
  1321. }
  1322. }
  1323. }
  1324. }
  1325. }
  1326. }
  1327. function shielded_exec_env($PROJECT, $ME, rptDataItemObj) {
  1328. if ($ME[JV.PROP_FIELD_EXP_MAP]) {
  1329. rptDataItemObj.push(eval($ME[JV.PROP_FIELD_EXP_MAP]));
  1330. }
  1331. }
  1332. function getActPropertyVal(firstPropKey, secPropKey, orgObj) {
  1333. let rst = null;
  1334. if (orgObj[firstPropKey]) {
  1335. rst = orgObj[firstPropKey];
  1336. } else if (orgObj[secPropKey]){
  1337. rst = orgObj[secPropKey];
  1338. }
  1339. return rst;
  1340. }
  1341. function getDeepProperty(propKey, orgObj, destArr) {
  1342. let keys = propKey.split(".");
  1343. let dftPropKey = "key", dftPropVal = "value", secDftPropVal = "items";
  1344. let parent = orgObj, lastVal = null;
  1345. for (let key of keys) {
  1346. if (parent instanceof Array) {
  1347. for (let item of parent) {
  1348. if (item[dftPropKey] === key) {
  1349. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  1350. break;
  1351. }
  1352. }
  1353. } else {
  1354. lastVal = null;
  1355. if (parent[key] !== undefined) {
  1356. lastVal = parent[key];
  1357. } else if (parent[secDftPropVal]){
  1358. for (let item of parent[secDftPropVal]) {
  1359. if (item[dftPropKey] === key) {
  1360. // lastVal = item[dftPropVal];
  1361. lastVal = getActPropertyVal(dftPropVal, secDftPropVal, item);
  1362. break;
  1363. }
  1364. }
  1365. }
  1366. }
  1367. parent = lastVal;
  1368. if (parent === null) break;
  1369. }
  1370. if (destArr && destArr instanceof Array) {
  1371. destArr.push(lastVal);
  1372. }
  1373. }
  1374. function ext_mainGetPropety(propKey) {
  1375. let rst = [], parentObj = this;
  1376. let dtObj = parentObj["myOwnRawDataObj"];
  1377. if (propKey && dtObj) {
  1378. if (dtObj.hasOwnProperty("property")) {
  1379. if (!dtObj["property"][propKey] && dtObj[propKey]) {
  1380. rst.push(dtObj[propKey]);
  1381. } else {
  1382. getDeepProperty(propKey, dtObj["property"], rst);
  1383. }
  1384. } else {
  1385. // rst.push(dtObj[propKey]);
  1386. getDeepProperty(propKey, dtObj, rst);
  1387. }
  1388. }
  1389. return rst;
  1390. }
  1391. function ext_getRateProperty(propKey) {
  1392. let rst = [], parentObj = this;
  1393. let dtObj = parentObj["myOwnRawDataObj"];
  1394. if (propKey && dtObj) {
  1395. for (let rate of dtObj.data._doc.rates) {
  1396. rst.push(rate[propKey]);
  1397. }
  1398. }
  1399. return rst;
  1400. }
  1401. function ext_getRatioDataProperty(propKey) {
  1402. //获取工料机的组成物属性
  1403. let rst = [], parentObj = this;
  1404. let dtObj = parentObj["myOwnRawDataObj"];
  1405. if (propKey && dtObj) {
  1406. for (let dItem of getActDataArr(dtObj)) {
  1407. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  1408. for (let ratioItem of doc.ratio_data) {
  1409. rst.push(ratioItem[propKey]);
  1410. }
  1411. }
  1412. }
  1413. return rst;
  1414. }
  1415. function pri_push_property(propKey, doc, rst) {
  1416. if (propKey instanceof Array) {
  1417. //备注:这里的key数组表示取value的优先级
  1418. for (let pi = 0; pi < propKey.length; pi++) {
  1419. if (doc.hasOwnProperty("property")) {
  1420. if (doc["property"].hasOwnProperty(propKey[pi])) {
  1421. rst.push(doc["property"][propKey[pi]]);
  1422. break;
  1423. }
  1424. } else if (doc.hasOwnProperty(propKey[pi])) {
  1425. rst.push(doc[propKey[pi]]);
  1426. break;
  1427. } else {
  1428. let lenBefore = rst.length;
  1429. getDeepProperty(propKey[pi], doc, rst);
  1430. if (rst.length === (lenBefore + 1)) {
  1431. if (rst[lenBefore] !== null && rst[lenBefore] !== undefined && rst[lenBefore] !== "") {
  1432. break;
  1433. } else {
  1434. rst.splice(-1, 1); //删除末尾一条数据,给后面留空间
  1435. }
  1436. }
  1437. }
  1438. if (pi === propKey.length - 1) rst.push('');
  1439. }
  1440. } else {
  1441. if (doc.hasOwnProperty("property") && doc["property"].hasOwnProperty(propKey)) {
  1442. rst.push(doc["property"][propKey]);
  1443. } else if (doc.hasOwnProperty(propKey)) {
  1444. rst.push(doc[propKey]);
  1445. } else {
  1446. getDeepProperty(propKey, doc, rst);
  1447. }
  1448. }
  1449. }
  1450. function ext_getRatioDataParentProperty(propKey) {
  1451. //获取组成物的父项属性
  1452. let rst = [], parentObj = this;
  1453. let dtObj = parentObj["myOwnRawDataObj"];
  1454. if (propKey && dtObj) {
  1455. for (let dItem of getActDataArr(dtObj)) {
  1456. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  1457. for (let ratioItem of doc.ratio_data) {
  1458. pri_push_property(propKey, doc, rst);
  1459. }
  1460. }
  1461. }
  1462. return rst;
  1463. }
  1464. function ext_getRatioDataPriceMapProperty(propKey) {
  1465. //获取组成物的单价映射属性
  1466. let rst = [], parentObj = this;
  1467. let dtObj = parentObj["myOwnRawDataObj"];
  1468. if (propKey && dtObj) {
  1469. let unitPriceMap = dtObj.data.unitPriceMap;
  1470. let upmDoc = (unitPriceMap._doc === null || unitPriceMap._doc === undefined)?unitPriceMap:unitPriceMap._doc;
  1471. for (let dItem of getActDataArr(dtObj)) {
  1472. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  1473. for (let ratioItem of doc.ratio_data) {
  1474. let connectionKeys = [];
  1475. connectionKeys.push(ratioItem.code);
  1476. connectionKeys.push(ratioItem.name);
  1477. connectionKeys.push((ratioItem.specs === "")?"null":ratioItem.specs);
  1478. connectionKeys.push(ratioItem.unit);
  1479. connectionKeys.push(ratioItem.type);
  1480. connectionKeys.join("|-|");
  1481. let strPK = connectionKeys.join("|-|");
  1482. let dtlDoc = (upmDoc[strPK]._doc)?upmDoc[strPK]._doc:upmDoc[strPK];
  1483. rst.push(dtlDoc[propKey]);
  1484. }
  1485. }
  1486. }
  1487. return rst;
  1488. }
  1489. function ext_getAdHocGljProperty(adhocDataKey, propKey) {
  1490. let rst = [], parentObj = this;
  1491. let dtObj = parentObj["myOwnRawDataObj"];
  1492. if (propKey && dtObj) {
  1493. for (let dItem of getActDataArr(dtObj, adhocDataKey)) {
  1494. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  1495. pri_push_property(propKey, doc, rst);
  1496. }
  1497. }
  1498. return rst;
  1499. }
  1500. function ext_getGljFreightProperty(propKey) {
  1501. let rst = [], parentObj = this;
  1502. let dtObj = parentObj["myOwnRawDataObj"];
  1503. if (propKey && dtObj) {
  1504. for (let dItem of getActDataArr(dtObj, "freightList")) {
  1505. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  1506. pri_push_property(propKey, doc, rst);
  1507. }
  1508. }
  1509. return rst;
  1510. }
  1511. function ext_getGljOriginalProperty(propKey) {
  1512. let rst = [], parentObj = this;
  1513. let dtObj = parentObj["myOwnRawDataObj"];
  1514. if (propKey && dtObj) {
  1515. for (let dItem of getActDataArr(dtObj, "originalList")) {
  1516. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  1517. pri_push_property(propKey, doc, rst);
  1518. }
  1519. }
  1520. return rst;
  1521. }
  1522. function ext_getProperty(propKey) {
  1523. let rst = [], parentObj = this;
  1524. let dtObj = parentObj["myOwnRawDataObj"];
  1525. if (propKey && dtObj) {
  1526. for (let dItem of getActDataArr(dtObj)) {
  1527. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  1528. pri_push_property(propKey, doc, rst);
  1529. }
  1530. }
  1531. return rst;
  1532. }
  1533. function ext_getPropertyByRefId(baseKey, refIDKey, propertyKey){
  1534. let rst = [], parentObj = this;
  1535. let dtObj = parentObj["myOwnRawDataObj"];
  1536. let orgDtObj = parentObj["myOwnOrgRawDataObj"];
  1537. if (baseKey && refIDKey && propertyKey && dtObj) {
  1538. for (let dItem of getActDataArr(dtObj)) {
  1539. let doc = (dItem._doc === null || dItem._doc === undefined)?dItem:dItem._doc;
  1540. let tmpRst = doc[baseKey];
  1541. if (typeof tmpRst === "string" && tmpRst[0] === "@") {
  1542. let refKey = tmpRst.slice(1);
  1543. let hasGetRef = false;
  1544. for (let orgDItem of orgDtObj) {
  1545. let oDoc = (orgDItem._doc === null || orgDItem._doc === undefined)?orgDItem:orgDItem._doc;
  1546. if (oDoc[refIDKey] === refKey) {
  1547. rst.push(oDoc[propertyKey]);
  1548. hasGetRef = true;
  1549. break;
  1550. }
  1551. }
  1552. if (!hasGetRef) {
  1553. rst.push(tmpRst);
  1554. }
  1555. } else {
  1556. rst.push(tmpRst);
  1557. }
  1558. }
  1559. }
  1560. return rst;
  1561. }
  1562. function ext_mainGetFee(feeKey, dtlFeeKey) {
  1563. let rst = [];
  1564. let parentObj = this;
  1565. let dtObj = parentObj["myOwnRawDataObj"];
  1566. if ((dtObj) && (feeKey)) {
  1567. if (dtObj.hasOwnProperty("fees")) {
  1568. for (let fee of dtObj["fees"]) {
  1569. if (fee["fieldName"] === feeKey) {
  1570. if (dtlFeeKey) {
  1571. rst.push(fee[dtlFeeKey]);
  1572. } else {
  1573. rst.push(fee["unitFee"]);
  1574. }
  1575. break;
  1576. }
  1577. }
  1578. } else if (dtObj.hasOwnProperty(feeKey)) {
  1579. rst.push(dtObj[feeKey]);
  1580. } else {
  1581. //
  1582. }
  1583. }
  1584. return rst;
  1585. }
  1586. function ext_getFee(feeKey, dtlFeeKey) {
  1587. let rst = [], parentObj = this;
  1588. let dtObj = parentObj["myOwnRawDataObj"];
  1589. if (feeKey && dtObj) {
  1590. for (let dItem of getActDataArr(dtObj)) {
  1591. rst.push(pri_getFee(dItem, feeKey, dtlFeeKey));
  1592. }
  1593. }
  1594. for (let i = 0; i < rst.length; i++) {
  1595. rst[i] = parseFloat(rst[i]);
  1596. }
  1597. return rst;
  1598. }
  1599. function pri_getFee(dItem, feeKey, dtlFeeKey) {
  1600. let rst = 0;
  1601. let hasValue = false;
  1602. if (dItem["fees"]) {
  1603. for (let fee of dItem["fees"]) {
  1604. if (fee["fieldName"] === feeKey) {
  1605. if (dtlFeeKey) {
  1606. rst = fee[dtlFeeKey];
  1607. } else {
  1608. rst = fee["unitFee"];
  1609. }
  1610. hasValue = true;
  1611. break;
  1612. }
  1613. }
  1614. } else if (dItem[feeKey]) {
  1615. hasValue = true;
  1616. rst = parseFloat(dItem[feeKey]);
  1617. } else {
  1618. hasValue = true;
  1619. rst = 0;
  1620. }
  1621. if (!hasValue) {
  1622. rst = 0;
  1623. }
  1624. return rst;
  1625. }
  1626. function ext_getCalcProperty(templateIDs, calcItemKey, calcItemKeyVal, calcItemRstKey){
  1627. let rst = [], parentObj = this; //this should be "calc_program" object
  1628. let dtObj = parentObj["myOwnRawDataObj"];
  1629. let optimizeObj = {};
  1630. let private_getProperty = function (cId) {
  1631. let calcTplObj = optimizeObj["calc_program_" + cId];
  1632. if (!calcTplObj) {
  1633. let templates = (dtObj.data._doc)?dtObj.data._doc.templates:dtObj.data.templates;
  1634. for (let tpl of templates) {
  1635. if (cId === tpl.ID) {
  1636. optimizeObj["calc_program_" + cId] = tpl;
  1637. calcTplObj = tpl;
  1638. break;
  1639. }
  1640. }
  1641. }
  1642. if (calcTplObj) {
  1643. for (let calcItem of calcTplObj.calcItems) {
  1644. if (calcItem[calcItemKey] === calcItemKeyVal) {
  1645. if (calcItem[calcItemRstKey] !== undefined && calcItem[calcItemRstKey] !== null) {
  1646. rst.push(calcItem[calcItemRstKey]);
  1647. } else {
  1648. rst.push(calcItem['feeRate']); //业务特点,如果上述key没有值的话,直接取‘feeRate’的value,省的去改指标定义了
  1649. }
  1650. break;
  1651. }
  1652. }
  1653. }
  1654. };
  1655. if (templateIDs instanceof Array) {
  1656. for (let tplId of templateIDs) {
  1657. private_getProperty(tplId);
  1658. }
  1659. } else {
  1660. private_getProperty(templateIDs);
  1661. }
  1662. optimizeObj = null;
  1663. return rst;
  1664. }
  1665. function ext_getFeeRate(fee_Ids, feeName){
  1666. let rst = [], parentObj = this; //this should be "feeRate" object
  1667. let dtObj = parentObj["myOwnRawDataObj"];
  1668. let optimizeObj = {};
  1669. let private_getFeeRate = function (fId) {
  1670. let feeRateItemObj = optimizeObj["fee_rates_" + fId];
  1671. if (!feeRateItemObj) {
  1672. let rates = (dtObj.data._doc)?dtObj.data._doc.rates:dtObj.data.rates;
  1673. for (let feeItem of rates) {
  1674. if (fId === feeItem.ID) {
  1675. optimizeObj["fee_rates_" + fId] = feeItem;
  1676. feeRateItemObj = feeItem;
  1677. break;
  1678. }
  1679. }
  1680. }
  1681. if (feeRateItemObj) {
  1682. //rst.push(feeRateItemObj.rate);
  1683. rst.push(feeRateItemObj[feeName]);
  1684. } else {
  1685. rst.push(0);
  1686. }
  1687. };
  1688. if (fee_Ids instanceof Array) {
  1689. for (let fId of fee_Ids) {
  1690. private_getFeeRate(fId);
  1691. }
  1692. } else {
  1693. private_getFeeRate(fee_Ids);
  1694. }
  1695. optimizeObj = null;
  1696. return rst;
  1697. }
  1698. function ext_getArrayValues(itemKey) {
  1699. let rst = [], parentObj = this;
  1700. let dtObj = parentObj["myOwnRawDataObj"];
  1701. let keysArr = itemKey.split(".");
  1702. for (let dataItem of getActDataArr(dtObj)) {
  1703. let itemArr = [];
  1704. if (keysArr.length <= 2) {
  1705. if (dataItem[keysArr[0]] instanceof Array) {
  1706. if (keysArr.length === 2) {
  1707. for (let item of dataItem[keysArr[0]]) {
  1708. itemArr.push(item[keysArr[1]]);
  1709. }
  1710. } else {
  1711. itemArr = itemArr.concat(dataItem[keysArr[0]]);
  1712. }
  1713. } else {
  1714. if (keysArr.length === 2) {
  1715. let subProperty = dataItem[keysArr[0]][keysArr[1]];
  1716. if (subProperty instanceof Array) {
  1717. itemArr = itemArr.concat(subProperty);
  1718. } else {
  1719. itemArr.push(subProperty);
  1720. }
  1721. } else {
  1722. itemArr.push(dataItem[keysArr[0]]);
  1723. }
  1724. }
  1725. }
  1726. rst.push(itemArr);
  1727. }
  1728. return rst;
  1729. }
  1730. function ext_getArrayItemByKey(arrayKey, itemKey, itemKeyValue, itemRstKey){
  1731. let rst = [], parentObj = this;
  1732. let dtObj = parentObj["myOwnRawDataObj"];
  1733. let private_getItemValue = function (arr, dtlItKV) {
  1734. for (let item of arr) {
  1735. if (item[itemKey] === dtlItKV) {
  1736. if (itemRstKey) {
  1737. rst.push(item[itemRstKey]);
  1738. } else {
  1739. rst.push(item);
  1740. }
  1741. break;
  1742. }
  1743. }
  1744. };
  1745. let arr = dtObj[arrayKey];
  1746. if (arr && arr instanceof Array) {
  1747. if (itemKeyValue instanceof Array) {
  1748. for (let dtlItemKeyVal of itemKeyValue) {
  1749. private_getItemValue(arr, dtlItemKeyVal);
  1750. }
  1751. } else {
  1752. private_getItemValue(arr, itemKeyValue);
  1753. }
  1754. }
  1755. }
  1756. function ext_getBlank(dftVal) {
  1757. let rst = [], parentObj = this;
  1758. let dtObj = parentObj["myOwnRawDataObj"];
  1759. if (dtObj) {
  1760. let dtData = getActDataArr(dtObj);
  1761. for (let i = 0; i < dtData.length; i++) {
  1762. if (dftVal !== null && dftVal !== undefined) {
  1763. rst.push(dftVal)
  1764. } else rst.push('');
  1765. }
  1766. }
  1767. return rst;
  1768. }
  1769. function ext_getPropertyByFlag(flagVal, rstKey, dftValIfEmpty) {
  1770. let rst = [], parentObj = this;
  1771. let dtObj = parentObj["myOwnRawDataObj"];
  1772. if (flagVal && rstKey && dtObj) {
  1773. let isArr = (flagVal instanceof Array);
  1774. for (let dItem of getActDataArr(dtObj)) {
  1775. let doc = (dItem._doc)?dItem._doc:dItem;
  1776. if (doc.hasOwnProperty("flags")) {
  1777. let bFlag = false;
  1778. for (let flagItem of doc.flags) {
  1779. if (isArr) {
  1780. bFlag = (flagVal.indexOf(flagItem.flag) >= 0);
  1781. } else {
  1782. if (flagItem.flag === flagVal) {
  1783. bFlag = true;
  1784. }
  1785. }
  1786. if (bFlag) break;
  1787. }
  1788. if (bFlag) {
  1789. let keys = rstKey.split(".");
  1790. if (keys[0] === "fees") {
  1791. rst.push(pri_getFee(doc, "common", keys[1]));
  1792. } else {
  1793. //其他,比如名称什么
  1794. }
  1795. break;
  1796. }
  1797. }
  1798. }
  1799. }
  1800. if (rst.length === 0 && dftValIfEmpty !== null) {
  1801. rst.push(dftValIfEmpty);
  1802. }
  1803. return rst;
  1804. }
  1805. function ext_getPropertyByForeignId(foreignIdVal, adHocIdKey, propKey, dftValIfNotFound) {
  1806. let rst = [], parentObj = this;
  1807. let IdKey = (adHocIdKey)?adHocIdKey:"ID";
  1808. let dtObj = parentObj["myOwnRawDataObj"];
  1809. let splitPKey = propKey.split(".");
  1810. if (foreignIdVal !== null && foreignIdVal !== undefined) {
  1811. let isFound = false;
  1812. if (foreignIdVal instanceof Array) {
  1813. for (let idVal of foreignIdVal) {
  1814. isFound = false;
  1815. let dataArr = getActDataArr(dtObj);
  1816. for (let i = 0; i < dataArr.length; i++) {
  1817. let item = (dataArr[i]._doc)?dataArr[i]._doc:dataArr[i];
  1818. if (item[IdKey] === idVal) {
  1819. if (splitPKey.length > 1) {
  1820. let rstP = null;
  1821. for (let i = 0; i < splitPKey.length; i++) {
  1822. if (i === 0) {
  1823. rstP = item[splitPKey[i]];
  1824. } else {
  1825. if (splitPKey[i].indexOf("[") === 0 && splitPKey[i].indexOf("]") === (splitPKey[i].length - 1)) {
  1826. //考虑数组情况^_^!!!
  1827. } else {
  1828. //
  1829. }
  1830. rstP = rstP[splitPKey[i]];
  1831. }
  1832. }
  1833. rst.push(rstP);
  1834. } else {
  1835. rst.push(item[propKey]);
  1836. }
  1837. isFound = true;
  1838. break;
  1839. }
  1840. }
  1841. if (!isFound) {
  1842. rst.push(dftValIfNotFound);
  1843. }
  1844. }
  1845. } else {
  1846. for (let item of getActDataArr(dtObj)) {
  1847. if (item[IdKey] === foreignIdVal) {
  1848. rst.push(item[propKey]);
  1849. isFound = true;
  1850. break;
  1851. }
  1852. }
  1853. if (!isFound) {
  1854. rst.push(dftValIfNotFound);
  1855. }
  1856. }
  1857. }
  1858. return rst;
  1859. }
  1860. function getActDataArr(dtObj, adhocKey) {
  1861. let rst = null;
  1862. if (dtObj) {
  1863. rst = dtObj.data;
  1864. if (dtObj.moduleName === "projectGLJ") {
  1865. if (adhocKey) {
  1866. // rst = dtObj.data[adhocKey];
  1867. let props = adhocKey.split(',');
  1868. let nextData = dtObj.data[props[0]];
  1869. if (nextData._doc) {
  1870. nextData = nextData._doc;
  1871. }
  1872. for (let idx = 1; idx < props.length; idx++) {
  1873. nextData = nextData[props[idx]];
  1874. if (nextData._doc) {
  1875. nextData = nextData._doc;
  1876. }
  1877. }
  1878. if (nextData instanceof Array) {
  1879. rst = nextData;
  1880. } else {
  1881. rst = [];
  1882. rst.push(nextData);
  1883. }
  1884. } else {
  1885. rst = dtObj.data.gljList;
  1886. }
  1887. }
  1888. if (dtObj.moduleName === "calc_program") {
  1889. rst = dtObj.data.templates;
  1890. }
  1891. }
  1892. return rst;
  1893. }
  1894. function replaceActDataArr(dtObj, newArr) {
  1895. if (dtObj.moduleName === "projectGLJ") {
  1896. delete dtObj.data.gljList;
  1897. dtObj.data.gljList = newArr;
  1898. } else {
  1899. delete dtObj.data;
  1900. dtObj.data = newArr;
  1901. }
  1902. }
  1903. module.exports = Rpt_Data_Extractor;