rpt_data_analysis.js 22 KB

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