rpt_data_analysis.js 40 KB

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