rpt_yanghu_data_util.js 81 KB

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