rpt_data_analysis.js 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const math = require('mathjs');
  10. const standard = require('../const/standard');
  11. const moment = require('moment');
  12. moment.locale('zh-cn');
  13. class PosIndex {
  14. /**
  15. * 构造函数
  16. * @param {id|String, masterId|String, order|String} setting
  17. */
  18. constructor (setting) {
  19. // 无索引
  20. this.datas = [];
  21. // 以key为索引
  22. this.items = {};
  23. // 以分类id为索引的有序
  24. this.ledgerPos = {};
  25. // pos设置
  26. this.setting = setting;
  27. }
  28. /**
  29. * 加载部位明细数据
  30. * @param datas
  31. */
  32. loadDatas(datas) {
  33. this.datas = datas;
  34. this.items = {};
  35. this.ledgerPos = {};
  36. for (const data of this.datas) {
  37. const key = data[this.setting.id];
  38. this.items[key] = data;
  39. const masterKey = data[this.setting.masterId];
  40. if (!this.ledgerPos[masterKey]) {
  41. this.ledgerPos[masterKey] = [];
  42. }
  43. this.ledgerPos[masterKey].push(data);
  44. }
  45. for (const prop in this.ledgerPos) {
  46. this.resortLedgerPos(this.ledgerPos[prop]);
  47. }
  48. }
  49. getLedgerPos(mid) {
  50. return this.ledgerPos[mid];
  51. }
  52. resortLedgerPos(ledgerPos) {
  53. const self = this;
  54. if (ledgerPos instanceof Array) {
  55. ledgerPos.sort(function (a, b) {
  56. return a[self.setting.order] - b[self.setting.order];
  57. })
  58. }
  59. }
  60. }
  61. // 通用方法
  62. const rdaUtils = {
  63. orderCalc: function (ctx, data, fields) {
  64. const orderMatch = new RegExp('o[0-9]+', 'igm');
  65. for (const c of data) {
  66. if (c.order_calc && c.order_calc !== '') {
  67. const matchs = c.order_calc.match(orderMatch);
  68. const calcMatch = [];
  69. for (const m of matchs) {
  70. const order = m.substring(1, m.length);
  71. const orderData = data.find(function (x) {return (x.order + '') === order});
  72. if (orderData) {
  73. calcMatch.push({match: m, value: orderData})
  74. }
  75. }
  76. for (const f of fields) {
  77. let expr = c.order_calc;
  78. for (const m of calcMatch) {
  79. expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);
  80. }
  81. try {
  82. c[f] = ctx.helper.round(math.eval(expr), 6);
  83. } catch(err) {
  84. }
  85. }
  86. }
  87. }
  88. }
  89. };
  90. // 可提供的数据处理方法
  91. const changeSort = {
  92. name: '变更令排序',
  93. hint: '默认的变更令排序,同时对变更令,变更清单进行排序\n' +
  94. '变更令排序勿需勾选任何预定义处理指标,但是在指标映射中,需要添加如下指标:' +
  95. '1. 如果只有变更令数据,则必须添加,"变更令(change)"下的"变更令号"' +
  96. '2. 如果有变更清单需要排序,则必须在1的基础上添加,变更令(change)"下的"变更令uuid"和"变更清单(chang_audit_list)"下的"所属变更令uuid"&"清单编号"',
  97. intro: '报表对于主从表的排序有要求,解决变更令数据中的变更令、变更清单排序问题',
  98. /**
  99. *
  100. * @param ctx - context常量
  101. * @param data - 全部数据源{Array}
  102. * @param fieldsKey - 计算字段
  103. * @param options - 计算设置
  104. */
  105. fun: function(ctx, data, fieldsKey, options) {
  106. const change = options && options.change ? data[options.change] : data.change;
  107. if (!change) return;
  108. // 变更令排序
  109. change.sort(function (a, b) {
  110. return a.code.localeCompare(b.code);
  111. });
  112. const changeBills = options && options.changeBills ? data[options.changeBills] : data.change_audit_list;
  113. if (changeBills) {
  114. changeBills.sort(function (a, b) {
  115. const aCIndex = data.change.findIndex(function (c) {
  116. return c.cid === a.cid;
  117. });
  118. const bCIndex = data.change.findIndex(function (c) {
  119. return c.cid === b.cid;
  120. });
  121. return aCIndex === bCIndex
  122. ? ctx.helper.compareCode(a.code, b.code)
  123. : aCIndex - bCIndex;
  124. });
  125. }
  126. },
  127. };
  128. const gatherGcl = {
  129. name: '汇总工程量清单',
  130. hint: '请使用mem_stage_bills下指标,注意事项:\n' +
  131. '1. 以下字段,不管报表是否实际使用,均应添加至指标映射,且在此处应勾选(不要求顺序):\n' +
  132. ' 清单编号(b_code), 名称(name), 单位(unit), 单价(unit_price), 树结构-是否子项(is_leaf)\n' +
  133. '2. 汇总后,以下字段,均会失效, 请勿使用:\n' +
  134. ' 台账ID(id), 树结构-ID(ledger_id), 树结构父项-ID(ledger_pid),\n' +
  135. ' 树结构-层级(level), 树结构-同层排序(order), 树结构-完整路径(full_path),\n' +
  136. ' 图册号(drawing_code), 备注(memo), 节点类型(node_type), 总额计量(is_tp)\n' +
  137. '3. 如需汇总"未计入清单章节项",请勾选"章节编号(chapter)"字段\n',
  138. intro: '根据三级清单树结构,汇总平面工程量清单,目前仅支持mem_stage_bills表',
  139. _gatherCalcField: function (ctx, gcl, data) {
  140. for (const prop in data) {
  141. if (((prop === 'total_price' || prop.indexOf('tp') > 0) ||
  142. (prop === 'quantity' || prop.indexOf('qty') > 0)) && data[prop])
  143. {
  144. gcl[prop] = ctx.helper.add(gcl[prop], data[prop]);
  145. }
  146. }
  147. },
  148. _gatherTpField: function (ctx, gcl, data) {
  149. for (const prop in data) {
  150. if ((prop === 'total_price' || prop.indexOf('tp') > 0) && data[prop]) {
  151. gcl[prop] = ctx.helper.add(gcl[prop], data[prop]);
  152. }
  153. }
  154. },
  155. _loadGatherInfo: function (gcl, data) {
  156. for (const prop in data) {
  157. if (/^t_[0-9]+_(id|name)$/.test(prop)) {
  158. gcl[prop] = data[prop];
  159. }
  160. }
  161. },
  162. _loadStageSumInfo: function (gcl, data) {
  163. for (const prop in data) {
  164. if (/^s_[0-9]+_order$/.test(prop)) {
  165. gcl[prop] = data[prop];
  166. }
  167. }
  168. },
  169. _gatherFields: function(gcl, data, fields) {
  170. for (const f of fields) {
  171. if (data[f]) {
  172. gcl[f] = ctx.helper.add(gcl[f], data[f]);
  173. }
  174. }
  175. },
  176. fun: function (ctx, data, fieldsKey, options) {
  177. const tableName = options ? options.table : 'mem_stage_bills';
  178. const gatherData = data[tableName];
  179. if (!gatherData || gatherData.length === 0) return;
  180. const fields = ctx.helper._.map(fieldsKey, 'field');
  181. const needFields = ['b_code', 'name', 'unit', 'unit_price', 'is_leaf'];
  182. for (const nf of needFields) {
  183. if (fields.indexOf(nf) === -1) return;
  184. }
  185. const gatherOther = fields.indexOf('chapter') >= 0;
  186. const gclBills = [], other = {name: '未计入清单章节项', chapter: '10000'};
  187. if (tableName.indexOf('mem_gather_') >= 0) this._loadGatherInfo(other, gatherData[0]);
  188. if (tableName.indexOf('mem_stage_sum_') >= 0) this._loadStageSumInfo(other, gatherData[0]);
  189. for (const b of gatherData) {
  190. const child = ctx.helper._.find(gatherData, {ledger_pid: b.ledger_id});
  191. if (child) continue;
  192. if (b.b_code && b.b_code !== '') {
  193. let gcl = gclBills.find(function (g) {
  194. return g.b_code === b.b_code && g.name === b.name && g.unit === b.unit
  195. && ctx.helper.checkZero(ctx.helper.sub(g.unit_price, b.unit_price));
  196. });
  197. if (!gcl) {
  198. gcl = {
  199. b_code: b.b_code, name: b.name, unit: b.unit,
  200. unit_price: b.unit_price,
  201. qc_bgl_code: [], chapter: b.chapter,
  202. deal_bills_qty: b.deal_bills_qty, deal_bills_tp: b.deal_bills_tp,
  203. };
  204. if (tableName.indexOf('mem_gather_') >= 0) this._loadGatherInfo(gcl, b);
  205. if (tableName.indexOf('mem_stage_sum_') >= 0) this._loadStageSumInfo(gcl, b);
  206. gclBills.push(gcl);
  207. }
  208. this._gatherCalcField(ctx, gcl, b);
  209. if (b.qc_bgl_code && b.qc_bgl_code !== '') {
  210. gcl.qc_bgl_code = gcl.qc_bgl_code.concat(b.qc_bgl_code.split(';'));
  211. }
  212. } else if (gatherOther) {
  213. this._gatherTpField(ctx, other, b);
  214. }
  215. }
  216. if (gatherOther) gclBills.push(other);
  217. for (const g of gclBills) {
  218. if (g.qc_bgl_code) g.qc_bgl_code = g.qc_bgl_code.join(';');
  219. g.final_ratio = ctx.helper.mul(ctx.helper.div(g.end_gather_tp, g.final_ratio), 100);
  220. }
  221. data[tableName] = gclBills;
  222. }
  223. };
  224. const sortGcl = {
  225. name: '工程量清单排序',
  226. hint: '只对一张表,进行工程量清单排序,排序哪张表,根据勾选的清单编号字段决定:\n' +
  227. 'e.g.1 要对mem_stage_bills排序,需要勾选mem_stage_bills下的"清单编号(b_code)"字段\n' +
  228. 'e.g.2 要对mem_stage_im_zl排序,需要勾选mem_stage_im_zl下的"中间计量总量信息_编号(code)"字段\n' +
  229. '特别的,如有"未计入清单章节项": \n' +
  230. ' 1. 默认"未计入清单章节项"排列在最后\n' +
  231. ' 2. 如须"未计入清单章节项"排在100章之后,请在清单编号字段后,依次勾选"章节编号(chapter)", "名称(name)"\n',
  232. intro: '根据选择列,对数据进行工程量清单排序,兼容1000章后清单,以及403-1-a类的非纯数字编号排序',
  233. fun: function (ctx, data, fieldsKey, options) {
  234. if (fieldsKey.length !== 1 && fieldsKey.length !== 3) return;
  235. const code = fieldsKey[0].field;
  236. const chapter = fieldsKey.length > 1 ? fieldsKey[1].field : '';
  237. const name = fieldsKey.length > 2 ? fieldsKey[2].field : '';
  238. const sortData = data[fieldsKey[0].table];
  239. if (!sortData) return;
  240. sortData.sort(function (a, b) {
  241. if (chapter !== '') {
  242. if (a[name] === '未计入清单章节项') {
  243. return b[chapter] === '100' ? 1 : -1;
  244. } else if (b[name] === '未计入清单章节项') {
  245. return a[chapter] === '100' ? -1 : 1
  246. } else {
  247. return ctx.helper.compareCode(a[code], b[code]);
  248. }
  249. } else {
  250. return ctx.helper.compareCode(a[code], b[code]);
  251. }
  252. });
  253. }
  254. };
  255. const gatherChapter = {
  256. name: '汇总章级数据',
  257. hint: '请使用mem_stage_bills/mem_stage_bills_compare/ledger,仅对一张表进行汇总,并生成数据:\n'+
  258. '1. 因为是汇总章级数据,必须在离散数据中添加"章节代码"&"章节名称"\n' +
  259. '2. 需勾选"清单编号(b_code)", "树结构-是否子项(is_leaf)"字段,可以对任何含有这些字段的表汇总\n' +
  260. '注意事项:\n' +
  261. '1. 算法对数据表没有要求,保证有上述字段,且按顺序勾选即可, 仅汇总金额\n' +
  262. '2. 算法计算后,原数据表中非数字类型的字段全部失效(除清单编号、名称外),请勿在指标映射中添加\n' +
  263. '示例:\n' +
  264. 'e.g.1 要对mem_stage_bills汇总,须勾选mem_stage_bills下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  265. 'e.g.2 要对mem_stage_bills_compare汇总,须勾选mem_stage_bills_compare下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  266. '结果:\n' +
  267. '汇总结果可参照 清单汇总--章节合计,但是不过滤1000-1300章数据',
  268. intro: '用于得到类似“清单汇总--章级合计”的数据,可通过options自定义数据',
  269. defaultSetting: {
  270. count: 9,
  271. unChapter: {
  272. name: '未计入清单章节合计',
  273. order: 1,
  274. },
  275. gclSum: {
  276. name: '清单小计',
  277. order: 2,
  278. },
  279. unGcl: {
  280. name: '非清单项费用',
  281. order: 3,
  282. },
  283. sum: {
  284. name: '合计',
  285. order: 4,
  286. }
  287. },
  288. customSetting1: {
  289. count: 7,
  290. gclSum: {
  291. name: '第100章至700章清单合计',
  292. order: 1,
  293. },
  294. custom: [
  295. {name: '已包含在清单合计中的材料、工程设备、专业工程暂估价', order: 2, visible: false},
  296. {name: '清单合计减去材料、工程设备、专业工程暂估价(即8-9=10)', order_calc: 'o1-o2', order: 3},
  297. {name: '计日工合计', node_type: '计日工', order: 4},
  298. {name: '暂列金额(不含计日工总额)(即10×暂列金额比列)', order: 5, match: [{node_type: '暂列金额'}, {field: 'name', part: '暂列金额'}, {field: 'name', all: '暂定金额'}]},
  299. {name: '投标报价、台账价(8+11+12)=13', order_calc: 'o1+o4+o5', order: 6},
  300. ],
  301. rela: [
  302. {
  303. table: 'deal_bills', key: 'code',
  304. fields: {source: 'total_price', target: 'ex_value1'},
  305. },
  306. {
  307. table: 'mem_change_bills', key: 'code',
  308. fields: {source: '', target: 'ex_value2'},
  309. }
  310. ]
  311. },
  312. _getCalcChapter: function (chapter, options) {
  313. const gclChapter = [], otherChapter = [], customChapter = [];
  314. let serialNo = 1;
  315. for (const c of chapter) {
  316. const cc = { code: c.code, name: c.name, cType: 1 };
  317. cc.serialNo = serialNo++;
  318. cc.filter = '^[\\D]*' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}-';
  319. //cc.visible = true;
  320. gclChapter.push(cc);
  321. if (options.activeFields) {
  322. cc.active = cc.serialNo > options.count;
  323. } else {
  324. if (serialNo > options.count) break;
  325. }
  326. }
  327. if (options.unChapter) {
  328. gclChapter.push({
  329. name: options.unChapter.name, cType: 21,
  330. serialNo: serialNo + options.unChapter.order, order: options.unChapter.order,
  331. visible: options.unChapter.visible,
  332. });
  333. }
  334. if (options.gclSum) {
  335. otherChapter.push({
  336. name: options.gclSum.name, cType: 11,
  337. serialNo: serialNo + options.gclSum.order, order: options.gclSum.order,
  338. visible: options.gclSum.visible,
  339. });
  340. }
  341. if (options.unGcl) {
  342. otherChapter.push({
  343. name: options.unGcl.name, cType: 31,
  344. serialNo: serialNo + options.unGcl.order, order: options.unGcl.order,
  345. visible: options.unGcl.visible,
  346. });
  347. }
  348. if (options.sum) {
  349. otherChapter.push({
  350. name: options.sum.name , cType: 41,
  351. serialNo: serialNo + options.sum.order, order: options.sum.order,
  352. visible: options.sum.visible,
  353. });
  354. }
  355. if (options.custom && options.custom instanceof Array) {
  356. for (const c of options.custom) {
  357. const cc = {
  358. name: c.name, serialNo: serialNo + c.order,
  359. order_calc: c.order_calc,
  360. cType: 5, order: c.order,
  361. visible: c.visible,
  362. };
  363. if (c.match) {
  364. cc.match = JSON.parse(JSON.stringify(c.match));
  365. for (const m of cc.match) {
  366. if (m.node_type && m.node_type !== '') {
  367. const nodeType = standard.nodeType.find(function (x) {return x.text === m.node_type});
  368. m.node_type = nodeType.value;
  369. }
  370. }
  371. }
  372. customChapter.push(cc);
  373. }
  374. }
  375. return [gclChapter, otherChapter, customChapter];
  376. },
  377. _completeGatherInfo: function (chapters, source) {
  378. for (const f in source) {
  379. if (/^t_[0-9]*_(id|name)$/.test(f)) {
  380. for (const chapter of chapters) {
  381. chapter[f] = source[f];
  382. }
  383. }
  384. }
  385. },
  386. _completeStageSumInfo: function (chapters, source) {
  387. for (const f in source) {
  388. if (/^s_[0-9]*_order$/.test(f)) {
  389. for (const chapter of chapters) {
  390. chapter[f] = source[f];
  391. }
  392. }
  393. }
  394. },
  395. _getGclChapter: function (chapter, data, field) {
  396. for (const c of chapter) {
  397. if (c.filter) {
  398. const reg = new RegExp(c.filter);
  399. if (reg.test(data[field])) {
  400. return c;
  401. }
  402. } else {
  403. return c;
  404. }
  405. }
  406. },
  407. _checkFilter: function (fullPath, filter) {
  408. for (const f of filter) {
  409. if (fullPath.indexOf(f + '-') === 0 || fullPath === f) return true;
  410. }
  411. return false;
  412. },
  413. _gatherRela: function (ctx, data, rela, gclChapter, otherChapter) {
  414. if (!rela) return;
  415. const gatherRelaFields = function (chapter, source, field) {
  416. const fields = field instanceof Array ? field : [field];
  417. for (const f of fields) {
  418. chapter[f.target] = ctx.helper.add(chapter[f.target], source[f.source]);
  419. }
  420. };
  421. const relaBills = rela instanceof Array ? rela : [rela];
  422. for (const rb of relaBills) {
  423. if (!rb.table) continue;
  424. const relaData = data[rb.table];
  425. if (!relaData) continue;
  426. for (const rd of relaData) {
  427. for (const c of otherChapter) {
  428. if (c.cType === 41) {
  429. gatherRelaFields(c, rd, rb.fields);
  430. } else if (c.cType === 31 && (!rd[rb.key] || rd[rb.key] === '')) {
  431. gatherRelaFields(c, rd, rb.fields);
  432. } else if (c.cType === 11 && (rd[rb.key])) {
  433. gatherRelaFields(c, rd, rb.fields);
  434. }
  435. }
  436. if (rd[rb.key]) {
  437. const c = this._getGclChapter(gclChapter, rd, rb.key);
  438. if (c) gatherRelaFields(c, rd, rb.fields);
  439. }
  440. }
  441. }
  442. },
  443. _orderCalc: function (ctx, chapter, fields) {
  444. const orderMatch = new RegExp('o[0-9]+', 'igm');
  445. for (const c of chapter) {
  446. if (c.order_calc && c.order_calc !== '') {
  447. const matchs = c.order_calc.match(orderMatch);
  448. const calcMatch = [];
  449. for (const m of matchs) {
  450. const order = m.substring(1, m.length);
  451. const orderChapter = chapter.find(function (x) {return x.order == order});
  452. if (orderChapter) {
  453. calcMatch.push({match: m, value: orderChapter})
  454. }
  455. }
  456. for (const f of fields) {
  457. let expr = c.order_calc;
  458. for (const m of calcMatch) {
  459. expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);
  460. }
  461. try {
  462. c[f] = ctx.helper.round(math.eval(expr), 6);
  463. } catch(err) {
  464. }
  465. }
  466. }
  467. }
  468. },
  469. _checkMatch: function (match, d) {
  470. for (const m of match) {
  471. if (m.node_type) {
  472. if (m.node_type === d.node_type) return true;
  473. } else if (m.field) {
  474. const value = d[m.field];
  475. if (m.part && value) {
  476. if (value.indexOf(m.part) >= 0) return true;
  477. } else if (m.all && value) {
  478. if (m.all === value) return true;
  479. }
  480. }
  481. }
  482. return false;
  483. },
  484. fun: function (ctx, data, fieldsKey, options) {
  485. if (!data.tender_info || !data.tender_info.chapter) return;
  486. if (!fieldsKey && fieldsKey.length < 0) return;
  487. const calcFields = [];
  488. const gatherData = function (chapter, data) {
  489. if (!chapter) return;
  490. for (const f in data) {
  491. if (!data[f]) continue;
  492. if ((f.indexOf('tp') >= 0 || f === 'total_price')) {
  493. chapter[f] = ctx.helper.add(chapter[f], data[f]);
  494. if (calcFields.indexOf(f) === -1) calcFields.push(f);
  495. }
  496. }
  497. };
  498. const fields = ctx.helper._.map(fieldsKey, 'field');
  499. const needFields = ['b_code', 'is_leaf'];
  500. for (const nf of needFields) {
  501. if (fields.indexOf(nf) === -1) return;
  502. }
  503. const sourceData = data[fieldsKey[0].table];
  504. if (!sourceData) return;
  505. const [gclChapter, otherChapter, customChapter] = this._getCalcChapter(data.tender_info.chapter, options ? options : this.defaultSetting);
  506. if (fieldsKey[0].table.indexOf('mem_gather') >= 0) {
  507. this._completeGatherInfo(gclChapter, sourceData[0]);
  508. this._completeGatherInfo(otherChapter, sourceData[0]);
  509. this._completeGatherInfo(customChapter, sourceData[0]);
  510. }
  511. if (fieldsKey[0].table.indexOf('mem_stage_sum_') >= 0) {
  512. this._completeStageSumInfo(gclChapter, sourceData[0]);
  513. this._completeStageSumInfo(otherChapter, sourceData[0]);
  514. this._completeStageSumInfo(customChapter, sourceData[0]);
  515. }
  516. const filter = [];
  517. for (const d of sourceData) {
  518. for (const c of customChapter) {
  519. if (c.match && this._checkMatch(c.match, d)) {
  520. gatherData(c, d);
  521. filter.push(d.full_path);
  522. }
  523. }
  524. if (!d.is_leaf || this._checkFilter(d.full_path, filter)) continue;
  525. for (const c of otherChapter) {
  526. if (c.cType === 41) {
  527. gatherData(c, d);
  528. } else if (c.cType === 31 && (!d.b_code || d.b_code === '')) {
  529. gatherData(c, d);
  530. } else if (c.cType === 11 && (d.b_code)) {
  531. gatherData(c, d);
  532. }
  533. }
  534. if (d.b_code) {
  535. const c = this._getGclChapter(gclChapter, d, 'b_code');
  536. if (c) {
  537. gatherData(c, d);
  538. }
  539. }
  540. }
  541. this._gatherRela(ctx, data, options.rela, gclChapter, otherChapter);
  542. const chapter = gclChapter.concat(otherChapter).concat(customChapter);
  543. this._orderCalc(ctx, chapter, calcFields);
  544. chapter.sort(function (a, b) {return a.serialNo - b.serialNo});
  545. data[fieldsKey[0].table] = chapter.filter(function (x) {
  546. if (x.active) {
  547. for (const f of options.activeFields) {
  548. if (!ctx.helper.checkZero(x[f])) return true;
  549. }
  550. return false;
  551. } else {
  552. return (x.visible !== undefined && x.visible !== null) ? x.visible : true;
  553. }
  554. });
  555. },
  556. };
  557. const join = {
  558. name: "连接两张数据表",
  559. hint: "用于处理类似于关联签约清单的情况,会改变主表的数据",
  560. intro: '用于处理类似于关联签约清单的情况,会根据关联表(sub)改变主表(main)的数据',
  561. defaultSetting: {
  562. main: 'mem_stage_bills',
  563. sub: 'deal_bills',
  564. keyFields: [
  565. {main: 'b_code', sub: 'code', type: 'string'},
  566. {main: 'name', sub: 'name',type: 'string'},
  567. {main: 'unit', sub: 'unit',type: 'string'},
  568. {main: 'unit_price', sub: 'unit_price',type: 'number'},
  569. ],
  570. importFields: [
  571. {main: 'ex_value1', sub: 'quantity', type: 'sum'},
  572. {main: 'ex_value2', sub: 'total_price', type: 'sum'}
  573. ],
  574. joinType: 'outer', //'outer', 'main', 'sub', 'inner',
  575. },
  576. fun: function (ctx, data, fields, options) {
  577. if (!options || !options.main || !options.sub || !options.keyFields || options.keyFields.length === 0) return;
  578. const main = data[options.main];
  579. const sub = data[options.sub];
  580. if (!main || !sub) return;
  581. const _ = ctx.helper._, result = _.cloneDeep(main);
  582. for (const r of result) {
  583. r._join_tag = 'main';
  584. for (const i of options.importFields) {
  585. r[i.main] = null;
  586. }
  587. }
  588. for (const s of sub) {
  589. let r = result.find(function (x) {
  590. for (const k of options.keyFields) {
  591. switch (k.type) {
  592. case 'string':
  593. if (x[k.main] !== s[k.sub] && (!_.isNil(x[k.main]) || !_.isNil(s[k.sub]))) return false;
  594. break;
  595. case 'number':
  596. if (!ctx.helper.checkZero(ctx.helper.sub(x[k.main] - s[k.sub]))) return false;
  597. break;
  598. }
  599. }
  600. return true;
  601. });
  602. if (r && r._join_tag === 'main') {
  603. r._join_tag = 'both';
  604. }
  605. if (!r) {
  606. r = {_join_tag: 'sub'};
  607. for (const k of options.keyFields) {
  608. r[k.main] = s[k.sub];
  609. }
  610. result.push(r);
  611. }
  612. for (const i of options.importFields) {
  613. //r[i.main] = s[i.sub];
  614. if (i.type === 'sum') {
  615. r[i.main] = ctx.helper.add(r[i.main], s[i.sub]);
  616. } else {
  617. r[i.main] = s[i.sub];
  618. }
  619. }
  620. }
  621. switch (options.joinType) {
  622. case 'main':
  623. data[options.main] = _.filter(result, function (r) {return ['main', 'both'].indexOf(r._join_tag) >= 0});
  624. break;
  625. case 'sub':
  626. data[options.main] = _.filter(result, function (r) {return ['sub', 'both'].indexOf(r._join_tag) >= 0});
  627. break;
  628. case 'inner':
  629. data[options.main] = _.filter(result, function (r) {return r._join_tag === 'both'});
  630. break;
  631. case 'outer':
  632. data[options.main] = result;
  633. break;
  634. }
  635. }
  636. };
  637. const getChapterCode = {
  638. name: '获取章级编号',
  639. intro: '根据清单编号,计算章级编号,并写入指定字段,适用于未提供chapter字段的数据,例如签约清单',
  640. hint: '',
  641. defaultSetting: {
  642. table: 'mem_stage_bills',
  643. b_code: 'b_code',
  644. chapter: 'chapter',
  645. },
  646. fun: function (ctx, data, fields, options) {
  647. if (!options || !options.table || !options.b_code || !options.chapter) return;
  648. const cData = data[options.table];
  649. if (!cData) return;
  650. for (const d of cData) {
  651. d[options.chapter] = ctx.helper.getChapterCode(d[options.b_code]);
  652. }
  653. }
  654. };
  655. const filter = {
  656. name: '数据过滤',
  657. intro: '根据设置过滤数据,会直接修改数据表',
  658. defaultSetting: {
  659. "table": "pay",
  660. "condition": [
  661. {"field": "minus", "type": "bool", "value": "false", },
  662. {"field": "ptype", "type": "num", "operate": "=", "value": 1, "rela": "and"},
  663. {"field": "name", "type": "str", "operate": "=", "value": "扣回", "rela": "or"}
  664. ],
  665. "f_type": "or'",
  666. },
  667. _typeFun: {
  668. bool: '_checkBoolean',
  669. str: '_checkString',
  670. num: '_checkNumber',
  671. },
  672. _checkBoolean: function (ctx, value, condition) {
  673. switch (condition.value) {
  674. case 'true':
  675. return value ? true : false;
  676. case 'false':
  677. return value ? false : true;
  678. default:
  679. return true;
  680. }
  681. },
  682. _checkNumber: function (ctx, value, condition) {
  683. //if (ctx.helper._.isNil(value)) value = 0;
  684. switch (condition.operate) {
  685. case 'non-zero':
  686. return !ctx.helper.checkZero(value);
  687. case '=':
  688. return !ctx.helper._.isNil(value) ? value === condition.value : false;
  689. case '>':
  690. return !ctx.helper._.isNil(value) ? value > condition.value : false;
  691. case '<':
  692. return !ctx.helper._.isNil(value) ? value < condition.value : false;
  693. case '>=':
  694. return !ctx.helper._.isNil(value) ? value >= condition.value : false;
  695. case '<=':
  696. return !ctx.helper._.isNil(value) ? value <= condition.value : false;
  697. default:
  698. return true;
  699. }
  700. },
  701. _checkString: function (ctx, value, condition) {
  702. switch (condition.operate) {
  703. case '=':
  704. return value === condition.value;
  705. case 'part':
  706. return !ctx.helper._.isNil(value) ? value.indexOf(condition.value) >= 0 : false;
  707. case 'enum':
  708. return (!ctx.helper._.isNil(value) && condition.value instanceof Array) ? condition.value.indexOf(value) >= 0 : false;
  709. default:
  710. return true;
  711. }
  712. },
  713. fun: function (ctx, data, fields, options) {
  714. if (!options || !options.table || !options.condition) return;
  715. const fData = data[options.table], self = this;
  716. if (!fData) return;
  717. for (const c of options.condition) {
  718. c.fun = this._typeFun[c.type];
  719. }
  720. data[options.table] = fData.filter(function (d) {
  721. if (options.condition.length > 0) {
  722. let con = options.condition[0];
  723. let result = self[con.fun](ctx, d[con.field], con);
  724. for (let i = 1, iLen = options.condition.length; i < iLen; i++) {
  725. con = options.condition[i];
  726. result = (con.rela && con.rela === 'or')
  727. ? (result || self[con.fun](ctx, d[con.field], con))
  728. : (result && self[con.fun](ctx, d[con.field], con));
  729. }
  730. return result;
  731. } else {
  732. return true;
  733. }
  734. // const result;
  735. // for (const c of options.condition) {
  736. // if (!self[c.fun](ctx, d[c.field], c)) return false;
  737. // }
  738. // return true;
  739. });
  740. }
  741. };
  742. const gatherStagePay = {
  743. name: '汇总合同支付数据',
  744. intro: '根据付扣款项等,分类汇总合同支付的数据',
  745. defaultSetting: {
  746. table: 'mem_stage_pay',
  747. custom: [
  748. {name: '本期完成计量', ptype: 4, order: 1, visible: false},
  749. {name: '业主违约罚金', match: '业主违约罚金', order: 2},
  750. {name: '迟付款利息', match: '迟付款利息', order: 3},
  751. {flow: true, minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4},
  752. {name: '其他付款', minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4},
  753. {name: '合计', order: 5, order_calc: 'o1+o2+o3+o4', },
  754. {name: '动员预付款', order: 6},
  755. {name: '扣动员预付款', match: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款'], order: 7},
  756. {name: '扣材料预付款', match: '扣材料预付款', order: 8},
  757. {name: '承包人违约罚金', match: '承包人违约罚金', order: 9},
  758. {name: '保留金', match: '保留金', order: 10},
  759. {name: '税金', match: '税金', order: 11},
  760. {name: '质量保证金', match: '质量保证金', order: 12},
  761. {name: '其他扣款', minus: 1, rid: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款', '扣材料预付款', '承包人违约罚金', '保留金', '税金', '质量保证金'], order: 13},
  762. {name: '扣款合计', minus: 1, rid: [], order: 14},
  763. {name: '支付', ptype: 2, order: 15},
  764. ]
  765. },
  766. _filterFields: function (ctx, d, source) {
  767. let filterData = source;
  768. if (d.ptype) {
  769. filterData = filterData.filter(function (x) {
  770. return x.ptype === d.ptype;
  771. });
  772. }
  773. if (d.match) {
  774. filterData = filterData.filter(function (x) {
  775. if (x.name) {
  776. if (d.match instanceof Array) {
  777. for (const m of d.match) {
  778. if (x.name.indexOf(m) >= 0) return true;
  779. }
  780. } else {
  781. return x.name.indexOf(d.match) >= 0;
  782. }
  783. } else {
  784. return false;
  785. }
  786. });
  787. }
  788. if (!ctx.helper._.isNil(d.minus)) {
  789. filterData = filterData.filter(function (x) {
  790. return d.minus === 1 ? x.minus : !x.minus;
  791. });
  792. }
  793. if (d.rid) {
  794. filterData = filterData.filter(function (x) {
  795. if (x.name) {
  796. for (const r of d.rid) {
  797. if (x.name.indexOf(r) >= 0) return false;
  798. }
  799. }
  800. return true;
  801. });
  802. }
  803. return filterData;
  804. },
  805. _gatherFields: function (ctx, d, source, calcFields) {
  806. const filterData = this._filterFields(ctx, d, source);
  807. for (const fd of filterData) {
  808. for (const prop in fd) {
  809. if (prop.indexOf('tp') >= 0) {
  810. d[prop] = ctx.helper.add(d[prop], fd[prop]);
  811. if (calcFields.indexOf(prop) === -1) calcFields.push(prop);
  812. }
  813. }
  814. }
  815. },
  816. _completeGatherInfo(d, source) {
  817. for (const f in source) {
  818. if (/^t_[0-9]*_(id|name)$/.test(f)) {
  819. d[f] = source[f];
  820. }
  821. }
  822. },
  823. _completeStageSumInfo(d, source) {
  824. for (const f in source) {
  825. if (/^t_[0-9]*_order$/.test(f)) {
  826. d[f] = source[f];
  827. }
  828. }
  829. },
  830. fun: function (ctx, data, fields, options) {
  831. if (!options || !options.table || !options.custom) return;
  832. const gatherData = data[options.table];
  833. const result = [], calcFields = [];
  834. for (const c of options.custom) {
  835. const cData = JSON.parse(JSON.stringify(c));
  836. cData.subOrder = 0;
  837. if (cData.flow) {
  838. const fData = this._filterFields(ctx, cData, gatherData);
  839. for (const [i, f] of fData.entries()) {
  840. f.order = cData.order;
  841. f.subOrder = i;
  842. result.push(f);
  843. }
  844. } else {
  845. if (!cData.order_calc && cData.empty !== 1) {
  846. this._gatherFields(ctx, cData, gatherData, calcFields);
  847. }
  848. if (options.table.indexOf('mem_gather') >= 0) this._completeGatherInfo(cData, gatherData[0]);
  849. if (options.table.indexOf('mem_stage_sum_') >= 0) this._completeStageSumInfo(cData, gatherData[0]);
  850. result.push(cData);
  851. }
  852. }
  853. rdaUtils.orderCalc(ctx, result, calcFields);
  854. data[options.table] = result.filter(function (x) {
  855. return x.visible === undefined || x.visible;
  856. });
  857. data[options.table].sort(function (a, b) {
  858. return a.order === b.order ? a.subOrder - b.subOrder : a.order - b.order;
  859. });
  860. },
  861. };
  862. const union = {
  863. name: '合并数据',
  864. intro: '类似sql的union,可合并任意数据,合并结果写到"预留扩展数据-合并(mem_union_data)"中',
  865. defaultSetting: {
  866. union: [
  867. {
  868. table: 'mem_stage_bills',
  869. fields: [
  870. {target: 'str1', source: 'chapter'},
  871. {target: 'str2', source: 'name'},
  872. {target: 'tp1', source: 'total_price'},
  873. {target: 'tp2', source: 'gather_tp'},
  874. {target: 'tp3', source: 'pre_gather_tp'},
  875. {target: 'tp4', source: 'end_gather_tp'},
  876. {target: 'str3', data: '章级合计哟'},
  877. ]
  878. }, {
  879. table: 'mem_stage_pay',
  880. fields: [
  881. {target: 'str2', source: 'name'},
  882. {target: 'tp2', source: 'tp'},
  883. {target: 'tp3', source: 'pre_tp'},
  884. {target: 'tp4', source: 'end_tp'},
  885. {target: 'str3', data: '合同支付哟'},
  886. ]
  887. }
  888. ]
  889. },
  890. fun: function(ctx, data, fields, options) {
  891. if (!options || !options.union) return;
  892. const result = [];
  893. for (const u of options.union) {
  894. const unionData = data[u.table];
  895. for (const d of unionData) {
  896. const nd = {};
  897. for (const f of u.fields) {
  898. if (f.data) {
  899. nd[f.target] = f.data;
  900. } else if (f.source) {
  901. nd[f.target] = d[f.source];
  902. }
  903. }
  904. result.push(nd);
  905. }
  906. }
  907. data.mem_union_data = result;
  908. }
  909. };
  910. const addSumChapter = {
  911. name: '添加章级合计',
  912. intro: '在排序好的工程量清单列表中,根据清单所属章级,添加章级标题行、合计行',
  913. defaultSetting: {
  914. table: 'mem_stage_bills',
  915. code: 'b_code',
  916. chapter: 'chapter',
  917. stdChapter: { title: '%s章', sum: '%s章合计'},
  918. otherChapter: { title: '其他章', sum: '其他章合计'},
  919. sum: { name: '总合计' },
  920. fields: ['ex_value1', 'ex_value2', 'end_gather_tp', 'pre_gather_tp', 'gather_tp'],
  921. },
  922. fun: function (ctx, data, fields, options) {
  923. if (!options || !options.table || !options.code || !options.chapter || !options.stdChapter) return;
  924. const gclData = data[options.table];
  925. if (!gclData || !data.tender_info) return;
  926. const chapters = ctx.helper._.uniq(ctx.helper._.map(gclData, options.chapter));
  927. const finalSum = options.sum ? {name: options.sum.name, chapterNum: 100000, chapterPart: 1} : null;
  928. for (const chapter of chapters) {
  929. const chapterInfo = data.tender_info.chapter.find(function (x) { return x.code === chapter});
  930. const chapterOptions = chapterInfo ? options.stdChapter : options.otherChapter;
  931. // sum
  932. const chapterGcl = gclData.filter(function (x) {
  933. return x.chapter === chapter;
  934. });
  935. const sum = { chapter: chapter, chapterPart: 3, chapterNum: parseInt(chapter) };
  936. for (const cg of chapterGcl) {
  937. cg.chapterPart = 2;
  938. cg.chapterNum = parseInt(cg[options.chapter]);
  939. for (const f of options.fields) {
  940. sum[f] = ctx.helper.add(sum[f], cg[f]);
  941. if (finalSum) {
  942. finalSum[f] = ctx.helper.add(finalSum[f], cg[f]);
  943. }
  944. }
  945. }
  946. if (chapterOptions.sum !== undefined) {
  947. sum.name = chapterOptions.sum.replace('%s', chapter);
  948. gclData.push(sum);
  949. }
  950. // title
  951. if (chapterOptions.title !== undefined) {
  952. const title = { chapter: chapter, chapterPart: 1, chapterNum: parseInt(chapter) };
  953. if (chapterInfo) {
  954. title[options.code] = options.stdChapter.title.replace('%s', chapter);
  955. title.name = chapterInfo.name;
  956. } else {
  957. title.name = chapterOptions.title;
  958. }
  959. gclData.push(title);
  960. }
  961. }
  962. if (finalSum) {
  963. gclData.push(finalSum);
  964. }
  965. gclData.sort(function (a, b) {
  966. if (a.chapter !== b.chapter) {
  967. return a.chapterNum - b.chapterNum;
  968. } else if (a.chapterPart !== b.chapterPart) {
  969. return a.chapterPart - b.chapterPart;
  970. } else {
  971. return ctx.helper.compareCode(a[options.code], b[options.code]);
  972. }
  973. });
  974. }
  975. };
  976. const auditSelect = {
  977. name: '审批人选择',
  978. hint: '需搭配用户交互--审批人选择一起使用',
  979. defaultSetting: {
  980. table: ['mem_stage_bills_compare', 'mem_stage_pos_compare', 'mem_stage_pay'],
  981. },
  982. _stageBillsCompare(data, order) {
  983. const fields = [];
  984. for (const [i, o] of order.entries()) {
  985. const sPrefix = 'r' + o + '_';
  986. const tPrefix = 'as' + i + '_';
  987. fields.push({source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty'});
  988. fields.push({source: sPrefix + 'contract_tp', target: tPrefix + 'contract_tp'});
  989. fields.push({source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty'});
  990. fields.push({source: sPrefix + 'qc_tp', target: tPrefix + 'qc_tp'});
  991. fields.push({source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty'});
  992. fields.push({source: sPrefix + 'gather_tp', target: tPrefix + 'gather_tp'});
  993. }
  994. for (const d of data) {
  995. for (const f of fields) {
  996. d[f.target] = d[f.source];
  997. }
  998. }
  999. },
  1000. _stagePosCompare(data, order) {
  1001. const fields = [];
  1002. for (const [i, o] of order.entries()) {
  1003. const sPrefix = 'r' + o + '_';
  1004. const tPrefix = 'as' + i + '_';
  1005. fields.push({source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty'});
  1006. fields.push({source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty'});
  1007. fields.push({source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty'});
  1008. }
  1009. for (const d of data) {
  1010. for (const f of fields) {
  1011. d[f.target] = d[f.source];
  1012. }
  1013. }
  1014. },
  1015. _stagePay(data, order) {
  1016. const fields = [];
  1017. for (const [i, o] of order.entries()) {
  1018. const sPrefix = 'r' + o + '_';
  1019. const tPrefix = 'as' + i + '_';
  1020. fields.push({source: sPrefix + 'tp', target: tPrefix + 'tp'});
  1021. }
  1022. for (const d of data) {
  1023. for (const f of fields) {
  1024. d[f.target] = d[f.source];
  1025. }
  1026. }
  1027. },
  1028. fun: function (ctx, data, fieldsKey, options, csRela) {
  1029. if (!ctx.tender || !ctx.stage) return;
  1030. if (!csRela.tplDefine) return;
  1031. const asDefine = csRela.tplDefine.audit_select;
  1032. if (!asDefine || !asDefine.enable || !asDefine.setting || asDefine.setting === '') return;
  1033. const asCustom = csRela.cDefine ? csRela.cDefine.audit_select : null;
  1034. const order = [];
  1035. if (asCustom) {
  1036. for (const asc of asCustom) {
  1037. order.push(asc.order);
  1038. }
  1039. }
  1040. for (const t of options.table) {
  1041. switch (t) {
  1042. case 'mem_stage_bills_compare':
  1043. this._stageBillsCompare(data[t], order);
  1044. break;
  1045. case 'mem_stage_pos_compare':
  1046. this._stagePosCompare(data[t], order);
  1047. break;
  1048. case 'mem_stage_pay':
  1049. this._stagePay(data[t], order);
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. };
  1055. const datetimeFormat = {
  1056. name: '日期格式化',
  1057. hint: '参见帮助',
  1058. defaultSetting: {
  1059. tables: [
  1060. {name: 'mem_stage_bonus', fields: [
  1061. {field: 'real_time', formatter: 'yyyy-MM-DD'},
  1062. {field: 'create_time', formatter: 'yyyy-MM-DD'}
  1063. ]},
  1064. ]
  1065. },
  1066. fun: function (ctx, data, fieldsKey, options, csRela) {
  1067. if (!options.tables) return;
  1068. const tables = options.tables instanceof Array ? tables : [options.tables];
  1069. if (tables.length === 0) return;
  1070. for (const table of tables) {
  1071. const formatData = data[table.name];
  1072. for (const fd of formatData) {
  1073. for (const f of table.fields) {
  1074. fd[f.field] = moment(fd[f.field]).format(f.formatter);
  1075. }
  1076. }
  1077. }
  1078. }
  1079. };
  1080. const gatherSelectConverse = {
  1081. name: '交叉汇总数据',
  1082. hint: '需搭配 用户交互--汇总标段选择 一起使用',
  1083. defaultSetting: {
  1084. table: ['mem_gather_stage_bills'],
  1085. },
  1086. _commonConverse: function (helper, data, count) {
  1087. const result = [];
  1088. const reg = new RegExp('^t_[0-9]+_');
  1089. for (let i = 0; i < count; i++) {
  1090. const curReg = new RegExp('^t_' + i + '_');
  1091. for (const [i, d] of data.entries()) {
  1092. const nd = { cross_index: i + 1};
  1093. for (const prop in d) {
  1094. if (reg.test(prop)) {
  1095. if (curReg.test(prop)) {
  1096. nd[prop.replace(curReg, 't_')] = d[prop];
  1097. }
  1098. } else {
  1099. nd[prop] = d[prop];
  1100. }
  1101. }
  1102. result.push(nd);
  1103. }
  1104. }
  1105. // for (const d of data) {
  1106. // for (let i = 0; i < count; i++) {
  1107. // const curReg = new RegExp('^t_' + i + '_');
  1108. // const nd = {};
  1109. // for (const prop in d) {
  1110. // if (reg.test(prop)) {
  1111. // if (curReg.test(prop)) {
  1112. // nd[prop.replace(curReg, 't_')] = d[prop];
  1113. // }
  1114. // } else {
  1115. // nd[prop] = d[prop];
  1116. // }
  1117. // }
  1118. // result.push(nd);
  1119. // }
  1120. // }
  1121. return result;
  1122. },
  1123. fun: function (ctx, data, fieldsKey, options, csRela) {
  1124. if (!csRela.tplDefine) return;
  1125. const gsDefine = csRela.tplDefine.gather_select;
  1126. if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;
  1127. const gsCustom = csRela.cDefine ? csRela.cDefine.gather_select : null;
  1128. const count = gsDefine.setting.special
  1129. ? gsCustom.tenders.length - gsDefine.setting.special.length
  1130. : gsCustom.tenders.length;
  1131. for (const t of options.table) {
  1132. switch (t) {
  1133. case 'mem_gather_stage_bills':
  1134. case 'mem_gather_stage_pay':
  1135. case 'mem_gather_deal_bills':
  1136. case 'mem_union_data':
  1137. data[t] = this._commonConverse(ctx.helper, data[t], count);
  1138. break;
  1139. }
  1140. }
  1141. }
  1142. };
  1143. const sortPos = {
  1144. name: '计量单元排序',
  1145. hint: '',
  1146. defaultSetting: {
  1147. bills: 'mem_stage_bills',
  1148. pos: 'mem_stage_pos',
  1149. },
  1150. fun: function (ctx, data, fieldsKey, options, csRela) {
  1151. if (!options || !options.bills || !options.pos) return;
  1152. const bills = data[options.bills];
  1153. const pos = data[options.pos];
  1154. if (!bills || bills.length === 0 || !pos || pos.length === 0) return;
  1155. const billsIndex = {};
  1156. const findBillsIndex = function (billsId) {
  1157. if (!billsIndex[billsId]) {
  1158. billsIndex[billsId] = ctx.helper._.findIndex(bills, function (x) {
  1159. return x.id === billsId;
  1160. });
  1161. }
  1162. return billsIndex[billsId];
  1163. };
  1164. pos.sort(function (x, y) {
  1165. const xIndex = findBillsIndex(x.lid) * 1000 + x.porder;
  1166. const yIndex = findBillsIndex(y.lid) * 1000 + y.porder;
  1167. return xIndex - yIndex;
  1168. });
  1169. }
  1170. };
  1171. const unionPos = {
  1172. name: '拼接计量单元',
  1173. hint: '',
  1174. defaultSetting: {
  1175. bills: 'mem_stage_bills',
  1176. pos: 'mem_stage_pos',
  1177. },
  1178. fun: function (ctx, data, fieldsKey, options, csRela) {
  1179. if (!options || !options.bills || !options.pos) return;
  1180. const bills = data[options.bills];
  1181. const pos = data[options.pos];
  1182. if (!bills || bills.length === 0 || !pos || pos.length === 0) return;
  1183. const posIndex = new PosIndex({
  1184. id: 'id',
  1185. masterId: 'lid',
  1186. order: 'porder',
  1187. });
  1188. posIndex.loadDatas(pos);
  1189. const unionData = [];
  1190. for (const b of bills) {
  1191. unionData.push(b);
  1192. const posRange = posIndex.getLedgerPos(b.id);
  1193. b.posCount = posRange ? posRange.length : 0;
  1194. if (!posRange || posRange.length === 0) continue;
  1195. for (const p of posRange) {
  1196. unionData.push(p);
  1197. }
  1198. }
  1199. data[options.bills] = unionData;
  1200. }
  1201. };
  1202. const splitXmjCode = {
  1203. name: '拆分项目节编号',
  1204. hint: '',
  1205. defaultSetting: {
  1206. table: 'mem_stage_bills',
  1207. type: '11', // '18', '18-0-1', '18-1', '18-1-1'
  1208. code: 'code',
  1209. targetField: ['xiang', 'mu', 'jie', 'ximu'],
  1210. },
  1211. // 11标准
  1212. _split11Std: function (data, options, helper) {
  1213. for (const d of data) {
  1214. if (!d[options.code]) continue;
  1215. const splitCodes = d[options.code].split('-');
  1216. if (d.level === 2) {
  1217. const field = options.targetField[0];
  1218. if (field && splitCodes.length > 1) {
  1219. d[field] = helper.transFormToChinese(splitCodes[1]);
  1220. }
  1221. } else if (d.level === 3) {
  1222. const field = options.targetField[1];
  1223. if (field && splitCodes.length > 2) {
  1224. d[field] = splitCodes[2];
  1225. }
  1226. } else if (d.level === 4) {
  1227. const field = options.targetField[2];
  1228. if (field && splitCodes.length > 3) {
  1229. d[field] = splitCodes[3];
  1230. }
  1231. } else if (d.level > 4) {
  1232. const field = options.targetField[3];
  1233. if (field && splitCodes.length > 4) {
  1234. d[field] = splitCodes.reduce(function (s, v, i) { return i >= 4 ? (s ? s + '-' + v : v + '') : ''});
  1235. }
  1236. }
  1237. }
  1238. },
  1239. _getParentByLevel: function (data, node, level) {
  1240. if (node.full_path) {
  1241. const pid = node.full_path.split('-')[level-1];
  1242. return data.find(function (x) {
  1243. return x.level === level && x.ledger_id == pid;
  1244. });
  1245. } else {
  1246. return null;
  1247. }
  1248. },
  1249. // 18标准
  1250. _split18Std: function (data, options, helper) {
  1251. const types = options.type.split('-');
  1252. const isSplitSub = types[1] && types[1] === '1' ? true : false;
  1253. const isKeepGD = types[2] && types[2] === '1' ? true : false;
  1254. for (const d of data) {
  1255. if (!d[options.code]) continue;
  1256. let target = '';
  1257. if (d.level === 2) {
  1258. target = options.targetField[0] ? options.targetField[0]: '';
  1259. } else if (d.level === 3) {
  1260. target = options.targetField[1] ? options.targetField[1]: '';
  1261. } else if (d.level === 4) {
  1262. target = options.targetField[2] ? options.targetField[2]: '';
  1263. } else if (d.level > 4) {
  1264. target = options.targetField[3] ? options.targetField[3]: '';
  1265. }
  1266. if (!target) continue;
  1267. if (helper.check18MainCode(d[options.code])) {
  1268. const parent = helper._.find(data, {ledger_id: d.ledger_pid});
  1269. const splitParent = !parent || parent.level < 5 ? parent : this._getParentByLevel(data, d, 4);
  1270. d[target] = !splitParent
  1271. ? d[options.code]
  1272. : d[options.code].replace(splitParent[options.code], '');
  1273. } else if (helper.check18SubCode(d[options.code]) && isSplitSub) {
  1274. const parent = helper._.find(data, {ledger_id: d.ledger_pid});
  1275. const splitParent = !parent || parent.level < 5 ? parent : this._getParentByLevel(data, d, 4);
  1276. d[target] = !splitParent || helper.check18MainCode(splitParent.code)
  1277. ? d[options.code]
  1278. : d[options.code].replace(splitParent[options.code], '');
  1279. } else {
  1280. d[target] = d[options.code];
  1281. }
  1282. if (!isKeepGD) d[target] = d[target].replace(/^G[D]?/, '');
  1283. }
  1284. },
  1285. // 根据层次填入
  1286. _splitDefault: function (data, options) {
  1287. for (const d of data) {
  1288. if (!d[options.code]) continue;
  1289. if (d.level === 2) {
  1290. if (options.targetField[0]) d[options.targetField[0]] = d[options.code];
  1291. } else if (d.level === 3) {
  1292. if (options.targetField[1]) d[options.targetField[1]] = d[options.code];
  1293. } else if (d.level === 4) {
  1294. if (options.targetField[2]) d[options.targetField[2]] = d[options.code];
  1295. } else if (d.level > 4) {
  1296. if (options.targetField[3]) d[options.targetField[3]] = d[options.code];
  1297. }
  1298. }
  1299. },
  1300. fun: function (ctx, data, fieldsKey, options, csRela) {
  1301. if (!options || !options.table || !options.code || !options.targetField) return;
  1302. if (!data[options.table]) return;
  1303. if (options.type === '11') {
  1304. this._split11Std(data[options.table], options, ctx.helper);
  1305. } else if (/^18(-[0,1]){0,2}$/.test(options.type)) {
  1306. this._split18Std(data[options.table], options, ctx.helper);
  1307. } else {
  1308. this._splitDefault(data[options.table], options);
  1309. }
  1310. }
  1311. };
  1312. const stageSelectConverse = {
  1313. name: '交叉多期汇总数据',
  1314. hint: '需搭配 用户交互--单标段多期汇总选择 一起使用',
  1315. defaultSetting: {
  1316. table: ['mem_stage_sum_bills'],
  1317. },
  1318. _commonConverse: function (helper, data, stages) {
  1319. const result = [];
  1320. const reg = new RegExp('^s_[0-9]+_');
  1321. for (const s of stages) {
  1322. const curReg = new RegExp('^s_' + s + '_');
  1323. for (const [i, d] of data.entries()) {
  1324. const nd = { cross_index: i + 1};
  1325. for (const prop in d) {
  1326. if (reg.test(prop)) {
  1327. if (curReg.test(prop)) {
  1328. nd[prop.replace(curReg, 's_')] = d[prop];
  1329. }
  1330. } else {
  1331. nd[prop] = d[prop];
  1332. }
  1333. }
  1334. result.push(nd);
  1335. }
  1336. }
  1337. return result;
  1338. },
  1339. fun: function (ctx, data, fieldsKey, options, csRela) {
  1340. if (!csRela.tplDefine) return;
  1341. const gsDefine = csRela.tplDefine.stage_select;
  1342. if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;
  1343. const gsCustom = csRela.cDefine ? csRela.cDefine.stage_select : null;
  1344. if (gsCustom) {
  1345. console.log(gsCustom[0]);
  1346. console.log(data.mem_stage_sum_bills[0]);
  1347. for (const t of options.table) {
  1348. switch (t) {
  1349. case 'mem_stage_sum_bills':
  1350. case 'mem_stage_sum_pay':
  1351. case 'mem_union_data':
  1352. data[t] = this._commonConverse(ctx.helper, data[t], gsCustom.stages);
  1353. break;
  1354. }
  1355. }
  1356. console.log(data.mem_stage_sum_bills[0]);
  1357. } else {
  1358. console.log('gsCustom is null!');
  1359. }
  1360. }
  1361. };
  1362. const analysisObj = {
  1363. changeSort,
  1364. gatherGcl,
  1365. sortGcl,
  1366. gatherChapter,
  1367. join,
  1368. getChapterCode,
  1369. filter,
  1370. union,
  1371. gatherStagePay,
  1372. addSumChapter,
  1373. auditSelect,
  1374. datetimeFormat,
  1375. gatherSelectConverse,
  1376. stageSelectConverse,
  1377. sortPos,
  1378. unionPos,
  1379. splitXmjCode,
  1380. };
  1381. const analysisDefine = (function (obj) {
  1382. const result = [];
  1383. for (const o in obj) {
  1384. const analysis = obj[o];
  1385. if (analysis.name && analysis.fun) {
  1386. result.push({
  1387. name: analysis.name,
  1388. key: o,
  1389. hint: analysis.hint,
  1390. intro: analysis.intro,
  1391. url: '/help?m=report&s=analysis&d=' + o,
  1392. })
  1393. }
  1394. }
  1395. return result;
  1396. })(analysisObj);
  1397. module.exports = {
  1398. analysisObj,
  1399. analysisDefine
  1400. };