rpt_data_analysis.js 52 KB

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