rpt_data_analysis.js 56 KB

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