rpt_data_analysis.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 changeSort = {
  12. name: '变更令排序',
  13. hint: '默认的变更令排序,同时对变更令,变更清单进行排序\n' +
  14. '变更令排序勿需勾选任何预定义处理指标,但是在指标映射中,需要添加如下指标:' +
  15. '1. 如果只有变更令数据,则必须添加,"变更令(change)"下的"变更令号"' +
  16. '2. 如果有变更清单需要排序,则必须在1的基础上添加,变更令(change)"下的"变更令uuid"和"变更清单(chang_audit_list)"下的"所属变更令uuid"&"清单编号"',
  17. /**
  18. *
  19. * @param ctx - context常量
  20. * @param data - 全部数据源{Array}
  21. * @param fieldsKey - 计算字段
  22. * @param options - 计算设置
  23. */
  24. fun: function(ctx, data, fieldsKey, options) {
  25. if (!data.change) return;
  26. // 变更令排序
  27. data.change.sort(function (a, b) {
  28. return a.code.localeCompare(b.code);
  29. });
  30. if (data.change_audit_list) {
  31. data.change_audit_list.sort(function (a, b) {
  32. const aCIndex = data.change.findIndex(function (c) {
  33. return c.cid === a.cid;
  34. });
  35. const bCIndex = data.change.findIndex(function (c) {
  36. return c.cid === b.cid;
  37. });
  38. return aCIndex === bCIndex
  39. ? ctx.helper.compareCode(a.code, b.code)
  40. : aCIndex - bCIndex;
  41. });
  42. }
  43. },
  44. };
  45. const gatherGcl = {
  46. name: '汇总工程量清单',
  47. hint: '请使用mem_stage_bills下指标,注意事项:\n' +
  48. '1. 以下字段,不管报表是否实际使用,均应添加至指标映射,且在此处应勾选(不要求顺序):\n' +
  49. ' 清单编号(b_code), 名称(name), 单位(unit), 单价(unit_price), 树结构-是否子项(is_leaf)\n' +
  50. '2. 汇总后,以下字段,均会失效, 请勿使用:\n' +
  51. ' 台账ID(id), 树结构-ID(ledger_id), 树结构父项-ID(ledger_pid),\n' +
  52. ' 树结构-层级(level), 树结构-同层排序(order), 树结构-完整路径(full_path),\n' +
  53. ' 图册号(drawing_code), 备注(memo), 节点类型(node_type), 总额计量(is_tp)\n' +
  54. '3. 如需汇总"未计入清单章节项",请勾选"章节编号(chapter)"字段\n',
  55. fun: function (ctx, data, fieldsKey, options) {
  56. const gatherFields = function(gcl, data, fields) {
  57. for (const f of fields) {
  58. if (data[f]) {
  59. gcl[f] = ctx.helper.add(gcl[f], data[f]);
  60. }
  61. }
  62. };
  63. if (!data.mem_stage_bills) return;
  64. const fields = ctx.helper._.map(fieldsKey, 'field');
  65. const needFields = ['b_code', 'name', 'unit', 'unit_price', 'is_leaf'];
  66. for (const nf of needFields) {
  67. if (fields.indexOf(nf) === -1) return;
  68. }
  69. const gatherOther = fields.indexOf('chapter') >= 0;
  70. ctx.helper._.pull(data.mem_stage_bills, 'is_leaf');
  71. const gclBills = [], other = {name: '未计入清单章节项', chapter: '10000'};
  72. for (const b of data.mem_stage_bills) {
  73. if (b.b_code && b.b_code !== '') {
  74. let gcl = gclBills.find(function (g) {
  75. return g.b_code === b.b_code && g.name === b.name && g.unit === b.unit
  76. && ctx.helper.checkZero(ctx.helper.sub(g.unit_price, b.unit_price));
  77. });
  78. if (!gcl) {
  79. gcl = {
  80. b_code: b.b_code, name: b.name, unit: b.unit,
  81. unit_price: b.unit_price,
  82. qc_bgl_code: [], chapter: b.chapter,
  83. deal_bills_qty: b.deal_bills_qty, deal_bills_tp: b.deal_bills_tp,
  84. };
  85. gclBills.push(gcl);
  86. }
  87. gatherFields(gcl, b, [
  88. 'deal_qty', 'deal_tp',
  89. 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price',
  90. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp',
  91. 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp',
  92. 'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp',
  93. 'final_tp'
  94. ]);
  95. if (b.qc_bgl_code && b.qc_bgl_code !== '') {
  96. gcl.qc_bgl_code = gcl.qc_bgl_code.concat(b.qc_bgl_code.split(';'));
  97. }
  98. } else if (gatherOther) {
  99. gatherFields(other, b, [
  100. 'deal_tp', 'sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price',
  101. 'contract_tp', 'qc_tp', 'gather_tp',
  102. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp',
  103. 'end_contract_tp', 'end_qc_tp', 'end_gather_tp',
  104. 'final_tp'
  105. ]);
  106. }
  107. }
  108. if (gatherOther) gclBills.push(other);
  109. for (const g of gclBills) {
  110. if (g.qc_bgl_code) g.qc_bgl_code = g.qc_bgl_code.join(';');
  111. g.final_ratio = ctx.helper.mul(ctx.helper.div(g.end_gather_tp, g.final_ratio), 100);
  112. }
  113. data.mem_stage_bills = gclBills;
  114. }
  115. };
  116. const sortGcl = {
  117. name: '工程量清单排序',
  118. hint: '只对一张表,进行工程量清单排序,排序哪张表,根据勾选的清单编号字段决定:\n' +
  119. 'e.g.1 要对mem_stage_bills排序,需要勾选mem_stage_bills下的"清单编号(b_code)"字段\n' +
  120. 'e.g.2 要对mem_stage_im_zl排序,需要勾选mem_stage_im_zl下的"中间计量总量信息_编号(code)"字段\n' +
  121. '特别的,如有"未计入清单章节项": \n' +
  122. ' 1. 默认"未计入清单章节项"排列在最后\n' +
  123. ' 2. 如须"未计入清单章节项"排在100章之后,请在清单编号字段后,依次勾选"章节编号(chapter)", "名称(name)"\n',
  124. fun: function (ctx, data, fieldsKey, options) {
  125. if (fieldsKey.length !== 1 && fieldsKey.length !== 3) return;
  126. const code = fieldsKey[0].field;
  127. const chapter = fieldsKey.length > 1 ? fieldsKey[1].field : '';
  128. const name = fieldsKey.length > 2 ? fieldsKey[2].field : '';
  129. const sortData = data[fieldsKey[0].table];
  130. if (!sortData) return;
  131. sortData.sort(function (a, b) {
  132. if (chapter !== '') {
  133. if (a[name] === '未计入清单章节项') {
  134. return b[chapter] === '100' ? 1 : -1;
  135. } else if (b[name] === '未计入清单章节项') {
  136. return a[chapter] === '100' ? -1 : 1
  137. } else {
  138. return ctx.helper.compareCode(a[code], b[code]);
  139. }
  140. } else {
  141. return ctx.helper.compareCode(a[code], b[code]);
  142. }
  143. });
  144. }
  145. };
  146. const gatherChapter = {
  147. name: '汇总章级数据',
  148. hint: '请使用mem_stage_bills/mem_stage_bills_compare/ledger,仅对一张表进行汇总,并生成数据:\n'+
  149. '1. 因为是汇总章级数据,必须在离散数据中添加"章节代码"&"章节名称"\n' +
  150. '2. 需勾选"清单编号(b_code)", "树结构-是否子项(is_leaf)"字段,可以对任何含有这些字段的表汇总\n' +
  151. '注意事项:\n' +
  152. '1. 算法对数据表没有要求,保证有上述字段,且按顺序勾选即可, 仅汇总金额\n' +
  153. '2. 算法计算后,原数据表中非数字类型的字段全部失效(除清单编号、名称外),请勿在指标映射中添加\n' +
  154. '示例:\n' +
  155. 'e.g.1 要对mem_stage_bills汇总,须勾选mem_stage_bills下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  156. 'e.g.2 要对mem_stage_bills_compare汇总,须勾选mem_stage_bills_compare下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  157. '结果:\n' +
  158. '汇总结果可参照 清单汇总--章节合计,但是不过滤1000-1300章数据',
  159. defaultSetting: {
  160. count: 9,
  161. unChapter: {
  162. name: '未计入清单章节合计',
  163. order: 1,
  164. },
  165. gclSum: {
  166. name: '清单小计',
  167. order: 2,
  168. },
  169. unGcl: {
  170. name: '非清单项费用',
  171. order: 3,
  172. },
  173. sum: {
  174. name: '合计',
  175. order: 4,
  176. }
  177. },
  178. customSetting1: {
  179. count: 7,
  180. gclSum: {
  181. name: '第100章至700章清单合计',
  182. order: 1,
  183. },
  184. custom: [
  185. {name: '已包含在清单合计中的材料、工程设备、专业工程暂估价', order: 2},
  186. {name: '清单合计减去材料、工程设备、专业工程暂估价(即8-9=10)', order_calc: 'o1-o2', order: 3},
  187. {name: '计日工合计', node_type: '计日工', order: 4},
  188. {name: '暂列金额(不含计日工总额)(即10×暂列金额比列)', node_type: '暂列金额', order: 5},
  189. {name: '投标报价、台账价(8+11+12)=13', order_calc: 'o1+o4+o5', order: 6},
  190. ],
  191. },
  192. _getCalcChapter: function (chapter, options) {
  193. const gclChapter = [], otherChapter = [], customChapter = [];
  194. let serialNo = 1;
  195. for (const c of chapter) {
  196. const cc = { code: c.code, name: c.name, cType: 1 };
  197. cc.serialNo = serialNo++;
  198. cc.filter = '^' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}-';
  199. gclChapter.push(cc);
  200. if (serialNo > options.count) break;
  201. }
  202. if (options.unChapter) {
  203. gclChapter.push({ name: options.unChapter.name, cType: 21, serialNo: serialNo + options.unChapter.order, order: options.unChapter.order });
  204. }
  205. if (options.gclSum) {
  206. otherChapter.push({ name: options.gclSum.name, cType: 11, serialNo: serialNo + options.gclSum.order, order: options.gclSum.order });
  207. }
  208. if (options.unGcl) {
  209. otherChapter.push({ name: options.unGcl.name, cType: 31, serialNo: serialNo + options.unGcl.order, order: options.unGcl.order });
  210. }
  211. if (options.sum) {
  212. otherChapter.push({ name: options.sum.name , cType: 41, serialNo: serialNo + options.sum.order, order: options.sum.order });
  213. }
  214. if (options.custom && options.custom instanceof Array) {
  215. for (const c of options.custom) {
  216. const cc = {
  217. name: c.name, serialNo: serialNo + c.order,
  218. order_calc: c.order_calc,
  219. cType: 5, order: c.order,
  220. };
  221. if (c.node_type && c.node_type !== '') {
  222. const nodeType = standard.nodeType.find(function (x) {return x.text === c.node_type});
  223. cc.node_type = nodeType.value;
  224. } else {
  225. cc.node_type = -1;
  226. }
  227. customChapter.push(cc);
  228. }
  229. }
  230. return [gclChapter, otherChapter, customChapter];
  231. },
  232. _getGclChapter: function (chapter, data) {
  233. for (const c of chapter) {
  234. if (c.filter) {
  235. const reg = new RegExp(c.filter);
  236. if (reg.test(data.b_code)) {
  237. return c;
  238. }
  239. } else {
  240. return c;
  241. }
  242. }
  243. },
  244. _checkFilter: function (fullPath, filter) {
  245. for (const f of filter) {
  246. if (fullPath.indexOf(f + '-') === 0 || fullPath === f) return true;
  247. }
  248. return false;
  249. },
  250. _orderCalc: function (ctx, chapter, fields) {
  251. const orderMatch = new RegExp('o[0-9]+', 'igm');
  252. for (const c of chapter) {
  253. if (c.order_calc && c.order_calc !== '') {
  254. const matchs = c.order_calc.match(orderMatch);
  255. const calcMatch = [];
  256. for (const m of matchs) {
  257. const order = m.substring(1, m.length);
  258. const orderChapter = chapter.find(function (x) {return x.order == order});
  259. if (orderChapter) {
  260. calcMatch.push({match: m, value: orderChapter})
  261. }
  262. }
  263. for (const f of fields) {
  264. let expr = c.order_calc;
  265. for (const m of calcMatch) {
  266. expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);
  267. }
  268. try {
  269. c[f] = ctx.helper.round(math.eval(expr), 6);
  270. } catch(err) {
  271. }
  272. }
  273. }
  274. }
  275. },
  276. fun: function (ctx, data, fieldsKey, options) {
  277. if (!data.tender_info || !data.tender_info.chapter) return;
  278. if (!fieldsKey && fieldsKey.length < 0) return;
  279. const calcFields = [];
  280. const gatherData = function (chapter, data) {
  281. if (!chapter) return;
  282. for (const f in data) {
  283. if (data[f] && (f.indexOf('tp') >= 0 || f === 'total_price')) {
  284. chapter[f] = ctx.helper.add(chapter[f], data[f]);
  285. if (calcFields.indexOf(f) === -1) calcFields.push(f);
  286. }
  287. }
  288. };
  289. const [gclChapter, otherChapter, customChapter] = this._getCalcChapter(data.tender_info.chapter, options ? options : this.defaultSetting);
  290. const fields = ctx.helper._.map(fieldsKey, 'field');
  291. const needFields = ['b_code', 'is_leaf'];
  292. for (const nf of needFields) {
  293. if (fields.indexOf(nf) === -1) return;
  294. }
  295. const sourceData = data[fieldsKey[0].table];
  296. if (!sourceData) return;
  297. const filter = [];
  298. for (const d of sourceData) {
  299. for (const c of customChapter) {
  300. if (c.node_type && c.node_type > 0 && c.node_type === d.node_type) {
  301. gatherData(c, d);
  302. filter.push(d.full_path);
  303. }
  304. }
  305. if (!d.is_leaf || this._checkFilter(d.full_path, filter)) continue;
  306. for (const c of otherChapter) {
  307. if (c.cType === 41) {
  308. gatherData(c, d);
  309. } else if (c.cType === 31 && (!d.b_code || d.b_code === '')) {
  310. gatherData(c, d);
  311. } else if (c.cType === 11 && (d.b_code)) {
  312. gatherData(c, d);
  313. }
  314. }
  315. if (d.b_code) {
  316. const c = this._getGclChapter(gclChapter, d);
  317. gatherData(c, d);
  318. }
  319. }
  320. const chapter = gclChapter.concat(otherChapter).concat(customChapter);
  321. this._orderCalc(ctx, chapter, calcFields);
  322. chapter.sort(function (a, b) {return a.serialNo - b.serialNo});
  323. data[fieldsKey[0].table] = chapter;
  324. },
  325. };
  326. const join = {
  327. name: "连接两张数据表",
  328. hint: "用于处理类似于关联签约清单的情况,会改变主表的数据",
  329. defaultSetting: {
  330. main: 'mem_stage_bills',
  331. sub: 'deal_bills',
  332. keyFields: [
  333. {main: 'b_code', sub: 'code', type: 'string'},
  334. {main: 'name', sub: 'name',type: 'string'},
  335. {main: 'unit', sub: 'unit',type: 'string'},
  336. {main: 'unit_price', sub: 'unit_price',type: 'number'},
  337. ],
  338. importFields: [
  339. {main: 'ex_value1', sub: 'quantity', type: 'number'},
  340. {main: 'ex_value2', sub: 'total_price', type: 'number'}
  341. ],
  342. joinType: 'outer', //'outer', 'main', 'sub', 'inner',
  343. },
  344. fun: function (ctx, data, fields, options) {
  345. if (!options || !options.main || !options.sub || !options.keyFields || options.keyFields.length === 0) return;
  346. const main = data[options.main];
  347. const sub = data[options.sub];
  348. if (!main || !sub) return;
  349. const _ = ctx.helper._, result = _.cloneDeep(main);
  350. for (const r of result) {
  351. r._join_tag = 'main';
  352. }
  353. for (const s of sub) {
  354. let r = result.find(function (x) {
  355. for (const k of options.keyFields) {
  356. switch (k.type) {
  357. case 'string':
  358. if (x[k.main] !== s[k.sub] && (!_.isNil(x[k.main]) || !_.isNil(s[k.sub]))) return false;
  359. break;
  360. case 'number':
  361. if (!ctx.helper.checkZero(ctx.helper.sub(x[k.main] - s[k.sub]))) return false;
  362. break;
  363. }
  364. }
  365. return true;
  366. });
  367. if (r && r._join_tag === 'main') {
  368. r._join_tag = 'both';
  369. }
  370. if (!r) {
  371. r = {_join_tag: 'sub'};
  372. for (const k of options.keyFields) {
  373. r[k.main] = s[k.sub];
  374. }
  375. result.push(r);
  376. }
  377. for (const i of options.importFields) {
  378. r[i.main] = s[i.sub];
  379. }
  380. }
  381. switch (options.joinType) {
  382. case 'main':
  383. data[options.main] = _.filter(result, function (r) {return ['main', 'both'].indexOf(r._join_tag) >= 0});
  384. break;
  385. case 'sub':
  386. data[options.main] = _.filter(result, function (r) {return ['sub', 'both'].indexOf(r._join_tag) >= 0});
  387. break;
  388. case 'inner':
  389. data[options.main] = _.filter(result, function (r) {return r._join_tag === 'both'});
  390. break;
  391. case 'outer':
  392. data[options.main] = result;
  393. break;
  394. }
  395. }
  396. };
  397. const getChapterCode = {
  398. name: '获取章级编号',
  399. hint: '',
  400. defaultSetting: {
  401. table: 'mem_stage_bills',
  402. b_code: 'b_code',
  403. chapter: 'chapter',
  404. },
  405. fun: function (ctx, data, fields, options) {
  406. if (!options || !options.table || !options.b_code || !options.chapter) return;
  407. const cData = data[options.table];
  408. if (!cData) return;
  409. for (const d of cData) {
  410. d[options.chapter] = ctx.helper.getChapterCode(d[options.b_code]);
  411. }
  412. }
  413. };
  414. const analysisObj = {
  415. changeSort,
  416. gatherGcl,
  417. sortGcl,
  418. gatherChapter,
  419. join,
  420. getChapterCode,
  421. };
  422. const analysisDefine = (function (obj) {
  423. const result = [];
  424. for (const o in obj) {
  425. const analysis = obj[o];
  426. if (analysis.name && analysis.fun) {
  427. result.push({
  428. name: analysis.name,
  429. key: o,
  430. hint: analysis.hint,
  431. })
  432. }
  433. }
  434. return result;
  435. })(analysisObj);
  436. module.exports = {
  437. analysisObj,
  438. analysisDefine
  439. };