rpt_data_analysis.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. // 通用方法
  14. const rdaUtils = {
  15. orderCalc: function (ctx, data, fields) {
  16. const orderMatch = new RegExp('o[0-9]+', 'igm');
  17. for (const c of data) {
  18. if (c.order_calc && c.order_calc !== '') {
  19. const matchs = c.order_calc.match(orderMatch);
  20. const calcMatch = [];
  21. for (const m of matchs) {
  22. const order = m.substring(1, m.length);
  23. const orderData = data.find(function (x) {return (x.order + '') === order});
  24. if (orderData) {
  25. calcMatch.push({match: m, value: orderData})
  26. }
  27. }
  28. for (const f of fields) {
  29. let expr = c.order_calc;
  30. for (const m of calcMatch) {
  31. expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);
  32. }
  33. try {
  34. c[f] = ctx.helper.round(math.eval(expr), 6);
  35. } catch(err) {
  36. }
  37. }
  38. }
  39. }
  40. }
  41. };
  42. // 可提供的数据处理方法
  43. const changeSort = {
  44. name: '变更令排序',
  45. hint: '默认的变更令排序,同时对变更令,变更清单进行排序\n' +
  46. '变更令排序勿需勾选任何预定义处理指标,但是在指标映射中,需要添加如下指标:' +
  47. '1. 如果只有变更令数据,则必须添加,"变更令(change)"下的"变更令号"' +
  48. '2. 如果有变更清单需要排序,则必须在1的基础上添加,变更令(change)"下的"变更令uuid"和"变更清单(chang_audit_list)"下的"所属变更令uuid"&"清单编号"',
  49. intro: '报表对于主从表的排序有要求,解决变更令数据中的变更令、变更清单排序问题',
  50. /**
  51. *
  52. * @param ctx - context常量
  53. * @param data - 全部数据源{Array}
  54. * @param fieldsKey - 计算字段
  55. * @param options - 计算设置
  56. */
  57. fun: function(ctx, data, fieldsKey, options) {
  58. const change = options && options.change ? data[options.change] : data.change;
  59. if (!change) return;
  60. // 变更令排序
  61. change.sort(function (a, b) {
  62. return a.code.localeCompare(b.code);
  63. });
  64. const changeBills = options && options.changeBills ? data[options.changeBills] : data.change_audit_list;
  65. if (changeBills) {
  66. changeBills.sort(function (a, b) {
  67. const aCIndex = data.change.findIndex(function (c) {
  68. return c.cid === a.cid;
  69. });
  70. const bCIndex = data.change.findIndex(function (c) {
  71. return c.cid === b.cid;
  72. });
  73. return aCIndex === bCIndex
  74. ? ctx.helper.compareCode(a.code, b.code)
  75. : aCIndex - bCIndex;
  76. });
  77. }
  78. },
  79. };
  80. const gatherGcl = {
  81. name: '汇总工程量清单',
  82. hint: '请使用mem_stage_bills下指标,注意事项:\n' +
  83. '1. 以下字段,不管报表是否实际使用,均应添加至指标映射,且在此处应勾选(不要求顺序):\n' +
  84. ' 清单编号(b_code), 名称(name), 单位(unit), 单价(unit_price), 树结构-是否子项(is_leaf)\n' +
  85. '2. 汇总后,以下字段,均会失效, 请勿使用:\n' +
  86. ' 台账ID(id), 树结构-ID(ledger_id), 树结构父项-ID(ledger_pid),\n' +
  87. ' 树结构-层级(level), 树结构-同层排序(order), 树结构-完整路径(full_path),\n' +
  88. ' 图册号(drawing_code), 备注(memo), 节点类型(node_type), 总额计量(is_tp)\n' +
  89. '3. 如需汇总"未计入清单章节项",请勾选"章节编号(chapter)"字段\n',
  90. intro: '根据三级清单树结构,汇总平面工程量清单,目前仅支持mem_stage_bills表',
  91. fun: function (ctx, data, fieldsKey, options) {
  92. const gatherFields = function(gcl, data, fields) {
  93. for (const f of fields) {
  94. if (data[f]) {
  95. gcl[f] = ctx.helper.add(gcl[f], data[f]);
  96. }
  97. }
  98. };
  99. if (!data.mem_stage_bills) return;
  100. const fields = ctx.helper._.map(fieldsKey, 'field');
  101. const needFields = ['b_code', 'name', 'unit', 'unit_price', 'is_leaf'];
  102. for (const nf of needFields) {
  103. if (fields.indexOf(nf) === -1) return;
  104. }
  105. const gatherOther = fields.indexOf('chapter') >= 0;
  106. const gclBills = [], other = {name: '未计入清单章节项', chapter: '10000'};
  107. for (const b of data.mem_stage_bills) {
  108. const child = ctx.helper._.find(data.mem_stage_bills, {ledger_pid: b.ledger_id});
  109. if (child) continue;
  110. if (b.b_code && b.b_code !== '') {
  111. let gcl = gclBills.find(function (g) {
  112. return g.b_code === b.b_code && g.name === b.name && g.unit === b.unit
  113. && ctx.helper.checkZero(ctx.helper.sub(g.unit_price, b.unit_price));
  114. });
  115. if (!gcl) {
  116. gcl = {
  117. b_code: b.b_code, name: b.name, unit: b.unit,
  118. unit_price: b.unit_price,
  119. qc_bgl_code: [], chapter: b.chapter,
  120. deal_bills_qty: b.deal_bills_qty, deal_bills_tp: b.deal_bills_tp,
  121. };
  122. gclBills.push(gcl);
  123. }
  124. gatherFields(gcl, b, [
  125. 'deal_qty', 'deal_tp',
  126. 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price',
  127. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp',
  128. 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp',
  129. 'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp',
  130. 'final_tp'
  131. ]);
  132. if (b.qc_bgl_code && b.qc_bgl_code !== '') {
  133. gcl.qc_bgl_code = gcl.qc_bgl_code.concat(b.qc_bgl_code.split(';'));
  134. }
  135. } else if (gatherOther) {
  136. gatherFields(other, b, [
  137. 'deal_tp', 'sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price',
  138. 'contract_tp', 'qc_tp', 'gather_tp',
  139. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp',
  140. 'end_contract_tp', 'end_qc_tp', 'end_gather_tp',
  141. 'final_tp'
  142. ]);
  143. }
  144. }
  145. if (gatherOther) gclBills.push(other);
  146. for (const g of gclBills) {
  147. if (g.qc_bgl_code) g.qc_bgl_code = g.qc_bgl_code.join(';');
  148. g.final_ratio = ctx.helper.mul(ctx.helper.div(g.end_gather_tp, g.final_ratio), 100);
  149. }
  150. data.mem_stage_bills = gclBills;
  151. }
  152. };
  153. const sortGcl = {
  154. name: '工程量清单排序',
  155. hint: '只对一张表,进行工程量清单排序,排序哪张表,根据勾选的清单编号字段决定:\n' +
  156. 'e.g.1 要对mem_stage_bills排序,需要勾选mem_stage_bills下的"清单编号(b_code)"字段\n' +
  157. 'e.g.2 要对mem_stage_im_zl排序,需要勾选mem_stage_im_zl下的"中间计量总量信息_编号(code)"字段\n' +
  158. '特别的,如有"未计入清单章节项": \n' +
  159. ' 1. 默认"未计入清单章节项"排列在最后\n' +
  160. ' 2. 如须"未计入清单章节项"排在100章之后,请在清单编号字段后,依次勾选"章节编号(chapter)", "名称(name)"\n',
  161. intro: '根据选择列,对数据进行工程量清单排序,兼容1000章后清单,以及403-1-a类的非纯数字编号排序',
  162. fun: function (ctx, data, fieldsKey, options) {
  163. if (fieldsKey.length !== 1 && fieldsKey.length !== 3) return;
  164. const code = fieldsKey[0].field;
  165. const chapter = fieldsKey.length > 1 ? fieldsKey[1].field : '';
  166. const name = fieldsKey.length > 2 ? fieldsKey[2].field : '';
  167. const sortData = data[fieldsKey[0].table];
  168. if (!sortData) return;
  169. sortData.sort(function (a, b) {
  170. if (chapter !== '') {
  171. if (a[name] === '未计入清单章节项') {
  172. return b[chapter] === '100' ? 1 : -1;
  173. } else if (b[name] === '未计入清单章节项') {
  174. return a[chapter] === '100' ? -1 : 1
  175. } else {
  176. return ctx.helper.compareCode(a[code], b[code]);
  177. }
  178. } else {
  179. return ctx.helper.compareCode(a[code], b[code]);
  180. }
  181. });
  182. }
  183. };
  184. const gatherChapter = {
  185. name: '汇总章级数据',
  186. hint: '请使用mem_stage_bills/mem_stage_bills_compare/ledger,仅对一张表进行汇总,并生成数据:\n'+
  187. '1. 因为是汇总章级数据,必须在离散数据中添加"章节代码"&"章节名称"\n' +
  188. '2. 需勾选"清单编号(b_code)", "树结构-是否子项(is_leaf)"字段,可以对任何含有这些字段的表汇总\n' +
  189. '注意事项:\n' +
  190. '1. 算法对数据表没有要求,保证有上述字段,且按顺序勾选即可, 仅汇总金额\n' +
  191. '2. 算法计算后,原数据表中非数字类型的字段全部失效(除清单编号、名称外),请勿在指标映射中添加\n' +
  192. '示例:\n' +
  193. 'e.g.1 要对mem_stage_bills汇总,须勾选mem_stage_bills下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  194. 'e.g.2 要对mem_stage_bills_compare汇总,须勾选mem_stage_bills_compare下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  195. '结果:\n' +
  196. '汇总结果可参照 清单汇总--章节合计,但是不过滤1000-1300章数据',
  197. intro: '用于得到类似“清单汇总--章级合计”的数据,可通过options自定义数据',
  198. defaultSetting: {
  199. count: 9,
  200. unChapter: {
  201. name: '未计入清单章节合计',
  202. order: 1,
  203. },
  204. gclSum: {
  205. name: '清单小计',
  206. order: 2,
  207. },
  208. unGcl: {
  209. name: '非清单项费用',
  210. order: 3,
  211. },
  212. sum: {
  213. name: '合计',
  214. order: 4,
  215. }
  216. },
  217. customSetting1: {
  218. count: 7,
  219. gclSum: {
  220. name: '第100章至700章清单合计',
  221. order: 1,
  222. },
  223. custom: [
  224. {name: '已包含在清单合计中的材料、工程设备、专业工程暂估价', order: 2, visible: false},
  225. {name: '清单合计减去材料、工程设备、专业工程暂估价(即8-9=10)', order_calc: 'o1-o2', order: 3},
  226. {name: '计日工合计', node_type: '计日工', order: 4},
  227. {name: '暂列金额(不含计日工总额)(即10×暂列金额比列)', order: 5, match: [{node_type: '暂列金额'}, {field: 'name', part: '暂列金额'}, {field: 'name', all: '暂定金额'}]},
  228. {name: '投标报价、台账价(8+11+12)=13', order_calc: 'o1+o4+o5', order: 6},
  229. ],
  230. rela: [
  231. {
  232. table: 'deal_bills', key: 'code',
  233. fields: {source: 'total_price', target: 'ex_value1'},
  234. },
  235. {
  236. table: 'mem_change_bills', key: 'code',
  237. fields: {source: '', target: 'ex_value2'},
  238. }
  239. ]
  240. },
  241. _getCalcChapter: function (chapter, options) {
  242. const gclChapter = [], otherChapter = [], customChapter = [];
  243. let serialNo = 1;
  244. for (const c of chapter) {
  245. const cc = { code: c.code, name: c.name, cType: 1 };
  246. cc.serialNo = serialNo++;
  247. cc.filter = '^' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}-';
  248. //cc.visible = true;
  249. gclChapter.push(cc);
  250. if (options.activeFields) {
  251. cc.active = cc.serialNo > options.count;
  252. } else {
  253. if (serialNo > options.count) break;
  254. }
  255. }
  256. if (options.unChapter) {
  257. gclChapter.push({
  258. name: options.unChapter.name, cType: 21,
  259. serialNo: serialNo + options.unChapter.order, order: options.unChapter.order,
  260. visible: options.unChapter.visible,
  261. });
  262. }
  263. if (options.gclSum) {
  264. otherChapter.push({
  265. name: options.gclSum.name, cType: 11,
  266. serialNo: serialNo + options.gclSum.order, order: options.gclSum.order,
  267. visible: options.gclSum.visible,
  268. });
  269. }
  270. if (options.unGcl) {
  271. otherChapter.push({
  272. name: options.unGcl.name, cType: 31,
  273. serialNo: serialNo + options.unGcl.order, order: options.unGcl.order,
  274. visible: options.unGcl.visible,
  275. });
  276. }
  277. if (options.sum) {
  278. otherChapter.push({
  279. name: options.sum.name , cType: 41,
  280. serialNo: serialNo + options.sum.order, order: options.sum.order,
  281. visible: options.sum.visible,
  282. });
  283. }
  284. if (options.custom && options.custom instanceof Array) {
  285. for (const c of options.custom) {
  286. const cc = {
  287. name: c.name, serialNo: serialNo + c.order,
  288. order_calc: c.order_calc,
  289. cType: 5, order: c.order,
  290. visible: c.visible,
  291. };
  292. if (c.match) {
  293. cc.match = JSON.parse(JSON.stringify(c.match));
  294. for (const m of cc.match) {
  295. if (m.node_type && m.node_type !== '') {
  296. const nodeType = standard.nodeType.find(function (x) {return x.text === m.node_type});
  297. m.node_type = nodeType.value;
  298. }
  299. }
  300. }
  301. customChapter.push(cc);
  302. }
  303. }
  304. return [gclChapter, otherChapter, customChapter];
  305. },
  306. _getGclChapter: function (chapter, data, field) {
  307. for (const c of chapter) {
  308. if (c.filter) {
  309. const reg = new RegExp(c.filter);
  310. if (reg.test(data[field])) {
  311. return c;
  312. }
  313. } else {
  314. return c;
  315. }
  316. }
  317. },
  318. _checkFilter: function (fullPath, filter) {
  319. for (const f of filter) {
  320. if (fullPath.indexOf(f + '-') === 0 || fullPath === f) return true;
  321. }
  322. return false;
  323. },
  324. _gatherRela: function (ctx, data, rela, gclChapter, otherChapter) {
  325. if (!rela) return;
  326. const gatherRelaFields = function (chapter, source, field) {
  327. const fields = field instanceof Array ? field : [field];
  328. for (const f of fields) {
  329. chapter[f.target] = ctx.helper.add(chapter[f.target], source[f.source]);
  330. }
  331. };
  332. const relaBills = rela instanceof Array ? rela : [rela];
  333. for (const rb of relaBills) {
  334. if (!rb.table) continue;
  335. const relaData = data[rb.table];
  336. if (!relaData) continue;
  337. for (const rd of relaData) {
  338. for (const c of otherChapter) {
  339. if (c.cType === 41) {
  340. gatherRelaFields(c, rd, rb.fields);
  341. } else if (c.cType === 31 && (!rd[rb.key] || rd[rb.key] === '')) {
  342. gatherRelaFields(c, rd, rb.fields);
  343. } else if (c.cType === 11 && (rd[rb.key])) {
  344. gatherRelaFields(c, rd, rb.fields);
  345. }
  346. }
  347. if (rd[rb.key]) {
  348. const c = this._getGclChapter(gclChapter, rd, rb.key);
  349. gatherRelaFields(c, rd, rb.fields);
  350. }
  351. }
  352. }
  353. },
  354. _orderCalc: function (ctx, chapter, fields) {
  355. const orderMatch = new RegExp('o[0-9]+', 'igm');
  356. for (const c of chapter) {
  357. if (c.order_calc && c.order_calc !== '') {
  358. const matchs = c.order_calc.match(orderMatch);
  359. const calcMatch = [];
  360. for (const m of matchs) {
  361. const order = m.substring(1, m.length);
  362. const orderChapter = chapter.find(function (x) {return x.order == order});
  363. if (orderChapter) {
  364. calcMatch.push({match: m, value: orderChapter})
  365. }
  366. }
  367. for (const f of fields) {
  368. let expr = c.order_calc;
  369. for (const m of calcMatch) {
  370. expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);
  371. }
  372. try {
  373. c[f] = ctx.helper.round(math.eval(expr), 6);
  374. } catch(err) {
  375. }
  376. }
  377. }
  378. }
  379. },
  380. _checkMatch: function (match, d) {
  381. for (const m of match) {
  382. if (m.node_type) {
  383. if (m.node_type === d.node_type) return true;
  384. } else if (m.field) {
  385. const value = d[m.field];
  386. if (m.part && value) {
  387. if (value.indexOf(m.part) >= 0) return true;
  388. } else if (m.all && value) {
  389. if (m.all === value) return true;
  390. }
  391. }
  392. }
  393. return false;
  394. },
  395. fun: function (ctx, data, fieldsKey, options) {
  396. if (!data.tender_info || !data.tender_info.chapter) return;
  397. if (!fieldsKey && fieldsKey.length < 0) return;
  398. const calcFields = [];
  399. const gatherData = function (chapter, data) {
  400. if (!chapter) return;
  401. for (const f in data) {
  402. if (data[f] && (f.indexOf('tp') >= 0 || f === 'total_price')) {
  403. chapter[f] = ctx.helper.add(chapter[f], data[f]);
  404. if (calcFields.indexOf(f) === -1) calcFields.push(f);
  405. }
  406. }
  407. };
  408. const [gclChapter, otherChapter, customChapter] = this._getCalcChapter(data.tender_info.chapter, options ? options : this.defaultSetting);
  409. const fields = ctx.helper._.map(fieldsKey, 'field');
  410. const needFields = ['b_code', 'is_leaf'];
  411. for (const nf of needFields) {
  412. if (fields.indexOf(nf) === -1) return;
  413. }
  414. const sourceData = data[fieldsKey[0].table];
  415. if (!sourceData) return;
  416. const filter = [];
  417. for (const d of sourceData) {
  418. for (const c of customChapter) {
  419. if (c.match && this._checkMatch(c.match, d)) {
  420. gatherData(c, d);
  421. filter.push(d.full_path);
  422. }
  423. }
  424. if (!d.is_leaf || this._checkFilter(d.full_path, filter)) continue;
  425. for (const c of otherChapter) {
  426. if (c.cType === 41) {
  427. gatherData(c, d);
  428. } else if (c.cType === 31 && (!d.b_code || d.b_code === '')) {
  429. gatherData(c, d);
  430. } else if (c.cType === 11 && (d.b_code)) {
  431. gatherData(c, d);
  432. }
  433. }
  434. if (d.b_code) {
  435. const c = this._getGclChapter(gclChapter, d, 'b_code');
  436. if (c) {
  437. gatherData(c, d);
  438. }
  439. }
  440. }
  441. this._gatherRela(ctx, data, options.rela, gclChapter, otherChapter);
  442. const chapter = gclChapter.concat(otherChapter).concat(customChapter);
  443. this._orderCalc(ctx, chapter, calcFields);
  444. chapter.sort(function (a, b) {return a.serialNo - b.serialNo});
  445. data[fieldsKey[0].table] = chapter.filter(function (x) {
  446. if (x.active) {
  447. for (const f of options.activeFields) {
  448. if (!ctx.helper.checkZero(x[f])) return true;
  449. }
  450. return false;
  451. } else {
  452. return (x.visible !== undefined && x.visible !== null) ? x.visible : true;
  453. }
  454. });
  455. },
  456. };
  457. const join = {
  458. name: "连接两张数据表",
  459. hint: "用于处理类似于关联签约清单的情况,会改变主表的数据",
  460. intro: '用于处理类似于关联签约清单的情况,会根据关联表(sub)改变主表(main)的数据',
  461. defaultSetting: {
  462. main: 'mem_stage_bills',
  463. sub: 'deal_bills',
  464. keyFields: [
  465. {main: 'b_code', sub: 'code', type: 'string'},
  466. {main: 'name', sub: 'name',type: 'string'},
  467. {main: 'unit', sub: 'unit',type: 'string'},
  468. {main: 'unit_price', sub: 'unit_price',type: 'number'},
  469. ],
  470. importFields: [
  471. {main: 'ex_value1', sub: 'quantity', type: 'sum'},
  472. {main: 'ex_value2', sub: 'total_price', type: 'sum'}
  473. ],
  474. joinType: 'outer', //'outer', 'main', 'sub', 'inner',
  475. },
  476. fun: function (ctx, data, fields, options) {
  477. if (!options || !options.main || !options.sub || !options.keyFields || options.keyFields.length === 0) return;
  478. const main = data[options.main];
  479. const sub = data[options.sub];
  480. if (!main || !sub) return;
  481. const _ = ctx.helper._, result = _.cloneDeep(main);
  482. for (const r of result) {
  483. r._join_tag = 'main';
  484. for (const i of options.importFields) {
  485. r[i.main] = null;
  486. }
  487. }
  488. for (const s of sub) {
  489. let r = result.find(function (x) {
  490. for (const k of options.keyFields) {
  491. switch (k.type) {
  492. case 'string':
  493. if (x[k.main] !== s[k.sub] && (!_.isNil(x[k.main]) || !_.isNil(s[k.sub]))) return false;
  494. break;
  495. case 'number':
  496. if (!ctx.helper.checkZero(ctx.helper.sub(x[k.main] - s[k.sub]))) return false;
  497. break;
  498. }
  499. }
  500. return true;
  501. });
  502. if (r && r._join_tag === 'main') {
  503. r._join_tag = 'both';
  504. }
  505. if (!r) {
  506. r = {_join_tag: 'sub'};
  507. for (const k of options.keyFields) {
  508. r[k.main] = s[k.sub];
  509. }
  510. result.push(r);
  511. }
  512. for (const i of options.importFields) {
  513. //r[i.main] = s[i.sub];
  514. if (i.type === 'sum') {
  515. r[i.main] = ctx.helper.add(r[i.main], s[i.sub]);
  516. } else {
  517. r[i.main] = s[i.sub];
  518. }
  519. }
  520. }
  521. switch (options.joinType) {
  522. case 'main':
  523. data[options.main] = _.filter(result, function (r) {return ['main', 'both'].indexOf(r._join_tag) >= 0});
  524. break;
  525. case 'sub':
  526. data[options.main] = _.filter(result, function (r) {return ['sub', 'both'].indexOf(r._join_tag) >= 0});
  527. break;
  528. case 'inner':
  529. data[options.main] = _.filter(result, function (r) {return r._join_tag === 'both'});
  530. break;
  531. case 'outer':
  532. data[options.main] = result;
  533. break;
  534. }
  535. }
  536. };
  537. const getChapterCode = {
  538. name: '获取章级编号',
  539. intro: '根据清单编号,计算章级编号,并写入指定字段,适用于未提供chapter字段的数据,例如签约清单',
  540. hint: '',
  541. defaultSetting: {
  542. table: 'mem_stage_bills',
  543. b_code: 'b_code',
  544. chapter: 'chapter',
  545. },
  546. fun: function (ctx, data, fields, options) {
  547. if (!options || !options.table || !options.b_code || !options.chapter) return;
  548. const cData = data[options.table];
  549. if (!cData) return;
  550. for (const d of cData) {
  551. d[options.chapter] = ctx.helper.getChapterCode(d[options.b_code]);
  552. }
  553. }
  554. };
  555. const filter = {
  556. name: '数据过滤',
  557. intro: '根据设置过滤数据,会直接修改数据表',
  558. defaultSetting: {
  559. "table": "pay",
  560. "condition": [
  561. {"field": "minus", "type": "bool", "value": "false", },
  562. {"field": "ptype", "type": "num", "operate": "=", "value": 1, "rela": "and"},
  563. {"field": "name", "type": "str", "operate": "=", "value": "扣回", "rela": "or"}
  564. ],
  565. "f_type": "or'",
  566. },
  567. _typeFun: {
  568. bool: '_checkBoolean',
  569. str: '_checkString',
  570. num: '_checkNumber',
  571. },
  572. _checkBoolean: function (ctx, value, condition) {
  573. switch (condition.value) {
  574. case 'true':
  575. return value ? true : false;
  576. case 'false':
  577. return value ? false : true;
  578. default:
  579. return true;
  580. }
  581. },
  582. _checkNumber: function (ctx, value, condition) {
  583. //if (ctx.helper._.isNil(value)) value = 0;
  584. switch (condition.operate) {
  585. case 'non-zero':
  586. return !ctx.helper.checkZero(value);
  587. case '=':
  588. return !ctx.helper._.isNil(value) ? value === condition.value : false;
  589. case '>':
  590. return !ctx.helper._.isNil(value) ? value > condition.value : false;
  591. case '<':
  592. return !ctx.helper._.isNil(value) ? value < condition.value : false;
  593. case '>=':
  594. return !ctx.helper._.isNil(value) ? value >= condition.value : false;
  595. case '<=':
  596. return !ctx.helper._.isNil(value) ? value <= condition.value : false;
  597. default:
  598. return true;
  599. }
  600. },
  601. _checkString: function (ctx, value, condition) {
  602. switch (condition.operate) {
  603. case '=':
  604. return value === condition.value;
  605. case 'part':
  606. return !ctx.helper._.isNil(value) ? value.indexOf(condition.value) >= 0 : false;
  607. case 'enum':
  608. return (!ctx.helper._.isNil(value) && condition.value instanceof Array) ? condition.value.indexOf(value) >= 0 : false;
  609. default:
  610. return true;
  611. }
  612. },
  613. fun: function (ctx, data, fields, options) {
  614. if (!options || !options.table || !options.condition) return;
  615. const fData = data[options.table], self = this;
  616. if (!fData) return;
  617. for (const c of options.condition) {
  618. c.fun = this._typeFun[c.type];
  619. }
  620. data[options.table] = fData.filter(function (d) {
  621. if (options.condition.length > 0) {
  622. let con = options.condition[0];
  623. let result = self[con.fun](ctx, d[con.field], con);
  624. for (let i = 1, iLen = options.condition.length; i < iLen; i++) {
  625. con = options.condition[i];
  626. result = (con.rela && con.rela === 'or')
  627. ? (result || self[con.fun](ctx, d[con.field], con))
  628. : (result && self[con.fun](ctx, d[con.field], con));
  629. }
  630. return result;
  631. } else {
  632. return true;
  633. }
  634. // const result;
  635. // for (const c of options.condition) {
  636. // if (!self[c.fun](ctx, d[c.field], c)) return false;
  637. // }
  638. // return true;
  639. });
  640. }
  641. };
  642. const gatherStagePay = {
  643. name: '汇总合同支付数据',
  644. intro: '根据付扣款项等,分类汇总合同支付的数据',
  645. defaultSetting: {
  646. table: 'mem_stage_pay',
  647. custom: [
  648. {name: '本期完成计量', ptype: 4, order: 1, visible: false},
  649. {name: '业主违约罚金', match: '业主违约罚金', order: 2},
  650. {name: '迟付款利息', match: '迟付款利息', order: 3},
  651. {flow: true, minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4},
  652. {name: '其他付款', minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4},
  653. {name: '合计', order: 5, order_calc: 'o1+o2+o3+o4', },
  654. {name: '动员预付款', order: 6},
  655. {name: '扣动员预付款', match: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款'], order: 7},
  656. {name: '扣材料预付款', match: '扣材料预付款', order: 8},
  657. {name: '承包人违约罚金', match: '承包人违约罚金', order: 9},
  658. {name: '保留金', match: '保留金', order: 10},
  659. {name: '税金', match: '税金', order: 11},
  660. {name: '质量保证金', match: '质量保证金', order: 12},
  661. {name: '其他扣款', minus: 1, rid: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款', '扣材料预付款', '承包人违约罚金', '保留金', '税金', '质量保证金'], order: 13},
  662. {name: '扣款合计', minus: 1, rid: [], order: 14},
  663. {name: '支付', ptype: 2, order: 15},
  664. ]
  665. },
  666. _filterFields: function (ctx, d, source) {
  667. let filterData = source;
  668. if (d.ptype) {
  669. filterData = filterData.filter(function (x) {
  670. return x.ptype === d.ptype;
  671. });
  672. }
  673. if (d.match) {
  674. filterData = filterData.filter(function (x) {
  675. if (x.name) {
  676. if (d.match instanceof Array) {
  677. for (const m of d.match) {
  678. if (x.name.indexOf(m) >= 0) return true;
  679. }
  680. } else {
  681. return x.name.indexOf(d.match) >= 0;
  682. }
  683. } else {
  684. return false;
  685. }
  686. });
  687. }
  688. if (!ctx.helper._.isNil(d.minus)) {
  689. filterData = filterData.filter(function (x) {
  690. return d.minus === 1 ? x.minus : !x.minus;
  691. });
  692. }
  693. if (d.rid) {
  694. filterData = filterData.filter(function (x) {
  695. if (x.name) {
  696. for (const r of d.rid) {
  697. if (x.name.indexOf(r) >= 0) return false;
  698. }
  699. }
  700. return true;
  701. });
  702. }
  703. return filterData;
  704. },
  705. _gatherFields: function (ctx, d, source, calcFields) {
  706. const filterData = this._filterFields(ctx, d, source);
  707. for (const fd of filterData) {
  708. for (const prop in fd) {
  709. if (prop.indexOf('tp') >= 0) {
  710. d[prop] = ctx.helper.add(d[prop], fd[prop]);
  711. if (calcFields.indexOf(prop) === -1) calcFields.push(prop);
  712. }
  713. }
  714. }
  715. },
  716. fun: function (ctx, data, fields, options) {
  717. if (!options || !options.table || !options.custom) return;
  718. const gatherData = data[options.table];
  719. const result = [], calcFields = [];
  720. for (const c of options.custom) {
  721. const cData = JSON.parse(JSON.stringify(c));
  722. cData.subOrder = 0;
  723. if (cData.flow) {
  724. const fData = this._filterFields(ctx, cData, gatherData);
  725. for (const [i, f] of fData.entries()) {
  726. f.order = cData.order;
  727. f.subOrder = i;
  728. result.push(f);
  729. }
  730. } else {
  731. if (!cData.order_calc && cData.empty !== 1) {
  732. this._gatherFields(ctx, cData, gatherData, calcFields);
  733. }
  734. result.push(cData);
  735. }
  736. }
  737. rdaUtils.orderCalc(ctx, result, calcFields);
  738. data[options.table] = result.filter(function (x) {
  739. return x.visible === undefined || x.visible;
  740. });
  741. data[options.table].sort(function (a, b) {
  742. return a.order === b.order ? a.subOrder - b.subOrder : a.order - b.order;
  743. });
  744. },
  745. };
  746. const union = {
  747. name: '合并数据',
  748. intro: '类似sql的union,可合并任意数据,合并结果写到"预留扩展数据-合并(mem_union_data)"中',
  749. defaultSetting: {
  750. union: [
  751. {
  752. table: 'mem_stage_bills',
  753. fields: [
  754. {target: 'str1', source: 'chapter'},
  755. {target: 'str2', source: 'name'},
  756. {target: 'tp1', source: 'total_price'},
  757. {target: 'tp2', source: 'gather_tp'},
  758. {target: 'tp3', source: 'pre_gather_tp'},
  759. {target: 'tp4', source: 'end_gather_tp'},
  760. {target: 'str3', data: '章级合计哟'},
  761. ]
  762. }, {
  763. table: 'mem_stage_pay',
  764. fields: [
  765. {target: 'str2', source: 'name'},
  766. {target: 'tp2', source: 'tp'},
  767. {target: 'tp3', source: 'pre_tp'},
  768. {target: 'tp4', source: 'end_tp'},
  769. {target: 'str3', data: '合同支付哟'},
  770. ]
  771. }
  772. ]
  773. },
  774. fun: function(ctx, data, fields, options) {
  775. if (!options || !options.union) return;
  776. const result = [];
  777. for (const u of options.union) {
  778. const unionData = data[u.table];
  779. for (const d of unionData) {
  780. const nd = {};
  781. for (const f of u.fields) {
  782. if (f.data) {
  783. nd[f.target] = f.data;
  784. } else if (f.source) {
  785. nd[f.target] = d[f.source];
  786. }
  787. }
  788. result.push(nd);
  789. }
  790. }
  791. data.mem_union_data = result;
  792. }
  793. };
  794. const addSumChapter = {
  795. name: '添加章级合计',
  796. intro: '在排序好的工程量清单列表中,根据清单所属章级,添加章级标题行、合计行',
  797. defaultSetting: {
  798. table: 'mem_stage_bills',
  799. code: 'b_code',
  800. chapter: 'chapter',
  801. stdChapter: { title: '%s章', sum: '%s章合计'},
  802. otherChapter: { title: '其他章', sum: '其他章合计'},
  803. sum: { name: '总合计' },
  804. fields: ['ex_value1', 'ex_value2', 'end_gather_tp', 'pre_gather_tp', 'gather_tp'],
  805. },
  806. fun: function (ctx, data, fields, options) {
  807. if (!options || !options.table || !options.code || !options.chapter || !options.stdChapter) return;
  808. const gclData = data[options.table];
  809. if (!gclData || !data.tender_info) return;
  810. const chapters = ctx.helper._.uniq(ctx.helper._.map(gclData, options.chapter));
  811. const finalSum = options.sum ? {name: options.sum.name, chapterNum: 100000, chapterPart: 1} : null;
  812. for (const chapter of chapters) {
  813. const chapterInfo = data.tender_info.chapter.find(function (x) { return x.code === chapter});
  814. const chapterOptions = chapterInfo ? options.stdChapter : options.otherChapter;
  815. // sum
  816. const chapterGcl = gclData.filter(function (x) {
  817. return x.chapter === chapter;
  818. });
  819. const sum = { chapter: chapter, chapterPart: 3, chapterNum: parseInt(chapter) };
  820. for (const cg of chapterGcl) {
  821. cg.chapterPart = 2;
  822. cg.chapterNum = parseInt(cg[options.chapter]);
  823. for (const f of options.fields) {
  824. sum[f] = ctx.helper.add(sum[f], cg[f]);
  825. if (finalSum) {
  826. finalSum[f] = ctx.helper.add(finalSum[f], cg[f]);
  827. }
  828. }
  829. }
  830. if (chapterOptions.sum !== undefined) {
  831. sum.name = chapterOptions.sum.replace('%s', chapter);
  832. gclData.push(sum);
  833. }
  834. // title
  835. if (chapterOptions.title !== undefined) {
  836. const title = { chapter: chapter, chapterPart: 1, chapterNum: parseInt(chapter) };
  837. if (chapterInfo) {
  838. title[options.code] = options.stdChapter.title.replace('%s', chapter);
  839. title.name = chapterInfo.name;
  840. } else {
  841. title.name = chapterOptions.title;
  842. }
  843. gclData.push(title);
  844. }
  845. }
  846. if (finalSum) {
  847. gclData.push(finalSum);
  848. }
  849. gclData.sort(function (a, b) {
  850. if (a.chapter !== b.chapter) {
  851. return a.chapterNum - b.chapterNum;
  852. } else if (a.chapterPart !== b.chapterPart) {
  853. return a.chapterPart - b.chapterPart;
  854. } else {
  855. return ctx.helper.compareCode(a[options.code], b[options.code]);
  856. }
  857. });
  858. }
  859. };
  860. const auditSelect = {
  861. name: '审批人选择',
  862. hint: '需搭配用户交互--审批人选择一起使用',
  863. defaultSetting: {
  864. table: ['mem_stage_bills_compare', 'mem_stage_pos_compare', 'mem_stage_pay'],
  865. },
  866. _stageBillsCompare(data, order) {
  867. const fields = [];
  868. for (const [i, o] of order.entries()) {
  869. const sPrefix = 'r' + o + '_';
  870. const tPrefix = 'as' + i + '_';
  871. fields.push({source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty'});
  872. fields.push({source: sPrefix + 'contract_tp', target: tPrefix + 'contract_tp'});
  873. fields.push({source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty'});
  874. fields.push({source: sPrefix + 'qc_tp', target: tPrefix + 'qc_tp'});
  875. fields.push({source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty'});
  876. fields.push({source: sPrefix + 'gather_tp', target: tPrefix + 'gather_tp'});
  877. }
  878. for (const d of data) {
  879. for (const f of fields) {
  880. d[f.target] = d[f.source];
  881. }
  882. }
  883. },
  884. _stagePosCompare(data, order) {
  885. const fields = [];
  886. for (const [i, o] of order.entries()) {
  887. const sPrefix = 'r' + o + '_';
  888. const tPrefix = 'as' + i + '_';
  889. fields.push({source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty'});
  890. fields.push({source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty'});
  891. fields.push({source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty'});
  892. }
  893. for (const d of data) {
  894. for (const f of fields) {
  895. d[f.target] = d[f.source];
  896. }
  897. }
  898. },
  899. _stagePay(data, order) {
  900. const fields = [];
  901. for (const [i, o] of order.entries()) {
  902. const sPrefix = 'r' + o + '_';
  903. const tPrefix = 'as' + i + '_';
  904. fields.push({source: sPrefix + 'tp', target: tPrefix + 'tp'});
  905. }
  906. for (const d of data) {
  907. for (const f of fields) {
  908. d[f.target] = d[f.source];
  909. }
  910. }
  911. },
  912. fun: function (ctx, data, fieldsKey, options, csRela) {
  913. if (!ctx.tender || !ctx.stage) return;
  914. if (!csRela.tplDefine) return;
  915. const asDefine = csRela.tplDefine.audit_select;
  916. if (!asDefine || !asDefine.enable || !asDefine.setting || asDefine.setting === '') return;
  917. const asCustom = csRela.cDefine ? csRela.cDefine.audit_select : null;
  918. const order = [];
  919. if (asCustom) {
  920. for (const asc of asCustom) {
  921. order.push(asc.order);
  922. }
  923. } else {
  924. const setting = JSON.stringify(asDefine.setting);
  925. for (const [i, s] of setting.select.entries()) {
  926. order.push(i);
  927. }
  928. }
  929. for (const t of options.table) {
  930. switch (t) {
  931. case 'mem_stage_bills_compare':
  932. this._stageBillsCompare(data[t], order);
  933. break;
  934. case 'mem_stage_pos_compare':
  935. this._stagePosCompare(data[t], order);
  936. break;
  937. case 'mem_stage_pay':
  938. this._stagePay(data[t], order);
  939. break;
  940. }
  941. }
  942. }
  943. };
  944. const datetimeFormat = {
  945. name: '日期格式化',
  946. hint: '参见帮助',
  947. defaultSetting: {
  948. tables: [
  949. {name: 'mem_stage_bonus', fields: [
  950. {field: 'real_time', formatter: 'yyyy-MM-DD'},
  951. {field: 'create_time', formatter: 'yyyy-MM-DD'}
  952. ]},
  953. ]
  954. },
  955. fun: function (ctx, data, fieldsKey, options, csRela) {
  956. if (!options.tables) return;
  957. const tables = options.tables instanceof Array ? tables : [options.tables];
  958. if (tables.length === 0) return;
  959. for (const table of tables) {
  960. const formatData = data[table.name];
  961. for (const fd of formatData) {
  962. for (const f of table.fields) {
  963. fd[f.field] = moment(fd[f.field]).format(f.formatter);
  964. }
  965. }
  966. }
  967. }
  968. };
  969. const gatherSelectConverse = {
  970. name: '交叉汇总数据',
  971. hint: '需搭配 用户交互--汇总标段选择 一起使用',
  972. defaultSetting: {
  973. table: ['mem_gather_stage_bills'],
  974. },
  975. _commonConverse: function (helper, data, count) {
  976. const result = [];
  977. const reg = new RegExp('^t_[0-9]+_');
  978. for (let i = 0; i < count; i++) {
  979. const curReg = new RegExp('^t_' + i + '_');
  980. for (const [i, d] of data.entries()) {
  981. const nd = { cross_index: i + 1};
  982. for (const prop in d) {
  983. if (reg.test(prop)) {
  984. if (curReg.test(prop)) {
  985. nd[prop.replace(curReg, 't_')] = d[prop];
  986. }
  987. } else {
  988. nd[prop] = d[prop];
  989. }
  990. }
  991. result.push(nd);
  992. }
  993. }
  994. // for (const d of data) {
  995. // for (let i = 0; i < count; i++) {
  996. // const curReg = new RegExp('^t_' + i + '_');
  997. // const nd = {};
  998. // for (const prop in d) {
  999. // if (reg.test(prop)) {
  1000. // if (curReg.test(prop)) {
  1001. // nd[prop.replace(curReg, 't_')] = d[prop];
  1002. // }
  1003. // } else {
  1004. // nd[prop] = d[prop];
  1005. // }
  1006. // }
  1007. // result.push(nd);
  1008. // }
  1009. // }
  1010. return result;
  1011. },
  1012. fun: function (ctx, data, fieldsKey, options, csRela) {
  1013. if (!csRela.tplDefine) return;
  1014. const gsDefine = csRela.tplDefine.gather_select;
  1015. if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;
  1016. const gsCustom = csRela.cDefine ? csRela.cDefine.gather_select : null;
  1017. const count = gsDefine.setting.special
  1018. ? gsCustom.tenders.length - gsDefine.setting.special.length
  1019. : gsCustom.tenders.length;
  1020. for (const t of options.table) {
  1021. switch (t) {
  1022. case 'mem_gather_stage_bills':
  1023. case 'mem_gather_stage_pay':
  1024. case 'mem_gather_deal_bills':
  1025. data[t] = this._commonConverse(ctx.helper, data[t], count);
  1026. break;
  1027. }
  1028. }
  1029. }
  1030. };
  1031. const analysisObj = {
  1032. changeSort,
  1033. gatherGcl,
  1034. sortGcl,
  1035. gatherChapter,
  1036. join,
  1037. getChapterCode,
  1038. filter,
  1039. union,
  1040. gatherStagePay,
  1041. addSumChapter,
  1042. auditSelect,
  1043. datetimeFormat,
  1044. gatherSelectConverse,
  1045. };
  1046. const analysisDefine = (function (obj) {
  1047. const result = [];
  1048. for (const o in obj) {
  1049. const analysis = obj[o];
  1050. if (analysis.name && analysis.fun) {
  1051. result.push({
  1052. name: analysis.name,
  1053. key: o,
  1054. hint: analysis.hint,
  1055. intro: analysis.intro,
  1056. url: '/help?m=report&s=analysis&d=' + o,
  1057. })
  1058. }
  1059. }
  1060. return result;
  1061. })(analysisObj);
  1062. module.exports = {
  1063. analysisObj,
  1064. analysisDefine
  1065. };