rpt_data_analysis.js 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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 = '^[\\D]*' + 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. _completeGatherInfo: function (chapters, source) {
  355. for (const f in source) {
  356. if (/^t_[0-9]*_(id|name)$/.test(f)) {
  357. for (const chapter of chapters) {
  358. chapter[f] = source[f];
  359. }
  360. }
  361. }
  362. },
  363. _getGclChapter: function (chapter, data, field) {
  364. for (const c of chapter) {
  365. if (c.filter) {
  366. const reg = new RegExp(c.filter);
  367. if (reg.test(data[field])) {
  368. return c;
  369. }
  370. } else {
  371. return c;
  372. }
  373. }
  374. },
  375. _checkFilter: function (fullPath, filter) {
  376. for (const f of filter) {
  377. if (fullPath.indexOf(f + '-') === 0 || fullPath === f) return true;
  378. }
  379. return false;
  380. },
  381. _gatherRela: function (ctx, data, rela, gclChapter, otherChapter) {
  382. if (!rela) return;
  383. const gatherRelaFields = function (chapter, source, field) {
  384. const fields = field instanceof Array ? field : [field];
  385. for (const f of fields) {
  386. chapter[f.target] = ctx.helper.add(chapter[f.target], source[f.source]);
  387. }
  388. };
  389. const relaBills = rela instanceof Array ? rela : [rela];
  390. for (const rb of relaBills) {
  391. if (!rb.table) continue;
  392. const relaData = data[rb.table];
  393. if (!relaData) continue;
  394. for (const rd of relaData) {
  395. for (const c of otherChapter) {
  396. if (c.cType === 41) {
  397. gatherRelaFields(c, rd, rb.fields);
  398. } else if (c.cType === 31 && (!rd[rb.key] || rd[rb.key] === '')) {
  399. gatherRelaFields(c, rd, rb.fields);
  400. } else if (c.cType === 11 && (rd[rb.key])) {
  401. gatherRelaFields(c, rd, rb.fields);
  402. }
  403. }
  404. if (rd[rb.key]) {
  405. const c = this._getGclChapter(gclChapter, rd, rb.key);
  406. if (c) gatherRelaFields(c, rd, rb.fields);
  407. }
  408. }
  409. }
  410. },
  411. _orderCalc: function (ctx, chapter, fields) {
  412. const orderMatch = new RegExp('o[0-9]+', 'igm');
  413. for (const c of chapter) {
  414. if (c.order_calc && c.order_calc !== '') {
  415. const matchs = c.order_calc.match(orderMatch);
  416. const calcMatch = [];
  417. for (const m of matchs) {
  418. const order = m.substring(1, m.length);
  419. const orderChapter = chapter.find(function (x) {return x.order == order});
  420. if (orderChapter) {
  421. calcMatch.push({match: m, value: orderChapter})
  422. }
  423. }
  424. for (const f of fields) {
  425. let expr = c.order_calc;
  426. for (const m of calcMatch) {
  427. expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);
  428. }
  429. try {
  430. c[f] = ctx.helper.round(math.eval(expr), 6);
  431. } catch(err) {
  432. }
  433. }
  434. }
  435. }
  436. },
  437. _checkMatch: function (match, d) {
  438. for (const m of match) {
  439. if (m.node_type) {
  440. if (m.node_type === d.node_type) return true;
  441. } else if (m.field) {
  442. const value = d[m.field];
  443. if (m.part && value) {
  444. if (value.indexOf(m.part) >= 0) return true;
  445. } else if (m.all && value) {
  446. if (m.all === value) return true;
  447. }
  448. }
  449. }
  450. return false;
  451. },
  452. fun: function (ctx, data, fieldsKey, options) {
  453. if (!data.tender_info || !data.tender_info.chapter) return;
  454. if (!fieldsKey && fieldsKey.length < 0) return;
  455. const calcFields = [];
  456. const gatherData = function (chapter, data) {
  457. if (!chapter) return;
  458. for (const f in data) {
  459. if (!data[f]) continue;
  460. if ((f.indexOf('tp') >= 0 || f === 'total_price')) {
  461. chapter[f] = ctx.helper.add(chapter[f], data[f]);
  462. if (calcFields.indexOf(f) === -1) calcFields.push(f);
  463. }
  464. }
  465. };
  466. const fields = ctx.helper._.map(fieldsKey, 'field');
  467. const needFields = ['b_code', 'is_leaf'];
  468. for (const nf of needFields) {
  469. if (fields.indexOf(nf) === -1) return;
  470. }
  471. const sourceData = data[fieldsKey[0].table];
  472. if (!sourceData) return;
  473. const [gclChapter, otherChapter, customChapter] = this._getCalcChapter(data.tender_info.chapter, options ? options : this.defaultSetting);
  474. if (fieldsKey[0].table.indexOf('mem_gather') >= 0) {
  475. this._completeGatherInfo(gclChapter, sourceData[0]);
  476. this._completeGatherInfo(otherChapter, sourceData[0]);
  477. this._completeGatherInfo(customChapter, sourceData[0]);
  478. }
  479. const filter = [];
  480. for (const d of sourceData) {
  481. for (const c of customChapter) {
  482. if (c.match && this._checkMatch(c.match, d)) {
  483. gatherData(c, d);
  484. filter.push(d.full_path);
  485. }
  486. }
  487. if (!d.is_leaf || this._checkFilter(d.full_path, filter)) continue;
  488. for (const c of otherChapter) {
  489. if (c.cType === 41) {
  490. gatherData(c, d);
  491. } else if (c.cType === 31 && (!d.b_code || d.b_code === '')) {
  492. gatherData(c, d);
  493. } else if (c.cType === 11 && (d.b_code)) {
  494. gatherData(c, d);
  495. }
  496. }
  497. if (d.b_code) {
  498. const c = this._getGclChapter(gclChapter, d, 'b_code');
  499. if (c) {
  500. gatherData(c, d);
  501. }
  502. }
  503. }
  504. this._gatherRela(ctx, data, options.rela, gclChapter, otherChapter);
  505. const chapter = gclChapter.concat(otherChapter).concat(customChapter);
  506. this._orderCalc(ctx, chapter, calcFields);
  507. chapter.sort(function (a, b) {return a.serialNo - b.serialNo});
  508. data[fieldsKey[0].table] = chapter.filter(function (x) {
  509. if (x.active) {
  510. for (const f of options.activeFields) {
  511. if (!ctx.helper.checkZero(x[f])) return true;
  512. }
  513. return false;
  514. } else {
  515. return (x.visible !== undefined && x.visible !== null) ? x.visible : true;
  516. }
  517. });
  518. },
  519. };
  520. const join = {
  521. name: "连接两张数据表",
  522. hint: "用于处理类似于关联签约清单的情况,会改变主表的数据",
  523. intro: '用于处理类似于关联签约清单的情况,会根据关联表(sub)改变主表(main)的数据',
  524. defaultSetting: {
  525. main: 'mem_stage_bills',
  526. sub: 'deal_bills',
  527. keyFields: [
  528. {main: 'b_code', sub: 'code', type: 'string'},
  529. {main: 'name', sub: 'name',type: 'string'},
  530. {main: 'unit', sub: 'unit',type: 'string'},
  531. {main: 'unit_price', sub: 'unit_price',type: 'number'},
  532. ],
  533. importFields: [
  534. {main: 'ex_value1', sub: 'quantity', type: 'sum'},
  535. {main: 'ex_value2', sub: 'total_price', type: 'sum'}
  536. ],
  537. joinType: 'outer', //'outer', 'main', 'sub', 'inner',
  538. },
  539. fun: function (ctx, data, fields, options) {
  540. if (!options || !options.main || !options.sub || !options.keyFields || options.keyFields.length === 0) return;
  541. const main = data[options.main];
  542. const sub = data[options.sub];
  543. if (!main || !sub) return;
  544. const _ = ctx.helper._, result = _.cloneDeep(main);
  545. for (const r of result) {
  546. r._join_tag = 'main';
  547. for (const i of options.importFields) {
  548. r[i.main] = null;
  549. }
  550. }
  551. for (const s of sub) {
  552. let r = result.find(function (x) {
  553. for (const k of options.keyFields) {
  554. switch (k.type) {
  555. case 'string':
  556. if (x[k.main] !== s[k.sub] && (!_.isNil(x[k.main]) || !_.isNil(s[k.sub]))) return false;
  557. break;
  558. case 'number':
  559. if (!ctx.helper.checkZero(ctx.helper.sub(x[k.main] - s[k.sub]))) return false;
  560. break;
  561. }
  562. }
  563. return true;
  564. });
  565. if (r && r._join_tag === 'main') {
  566. r._join_tag = 'both';
  567. }
  568. if (!r) {
  569. r = {_join_tag: 'sub'};
  570. for (const k of options.keyFields) {
  571. r[k.main] = s[k.sub];
  572. }
  573. result.push(r);
  574. }
  575. for (const i of options.importFields) {
  576. //r[i.main] = s[i.sub];
  577. if (i.type === 'sum') {
  578. r[i.main] = ctx.helper.add(r[i.main], s[i.sub]);
  579. } else {
  580. r[i.main] = s[i.sub];
  581. }
  582. }
  583. }
  584. switch (options.joinType) {
  585. case 'main':
  586. data[options.main] = _.filter(result, function (r) {return ['main', 'both'].indexOf(r._join_tag) >= 0});
  587. break;
  588. case 'sub':
  589. data[options.main] = _.filter(result, function (r) {return ['sub', 'both'].indexOf(r._join_tag) >= 0});
  590. break;
  591. case 'inner':
  592. data[options.main] = _.filter(result, function (r) {return r._join_tag === 'both'});
  593. break;
  594. case 'outer':
  595. data[options.main] = result;
  596. break;
  597. }
  598. }
  599. };
  600. const getChapterCode = {
  601. name: '获取章级编号',
  602. intro: '根据清单编号,计算章级编号,并写入指定字段,适用于未提供chapter字段的数据,例如签约清单',
  603. hint: '',
  604. defaultSetting: {
  605. table: 'mem_stage_bills',
  606. b_code: 'b_code',
  607. chapter: 'chapter',
  608. },
  609. fun: function (ctx, data, fields, options) {
  610. if (!options || !options.table || !options.b_code || !options.chapter) return;
  611. const cData = data[options.table];
  612. if (!cData) return;
  613. for (const d of cData) {
  614. d[options.chapter] = ctx.helper.getChapterCode(d[options.b_code]);
  615. }
  616. }
  617. };
  618. const filter = {
  619. name: '数据过滤',
  620. intro: '根据设置过滤数据,会直接修改数据表',
  621. defaultSetting: {
  622. "table": "pay",
  623. "condition": [
  624. {"field": "minus", "type": "bool", "value": "false", },
  625. {"field": "ptype", "type": "num", "operate": "=", "value": 1, "rela": "and"},
  626. {"field": "name", "type": "str", "operate": "=", "value": "扣回", "rela": "or"}
  627. ],
  628. "f_type": "or'",
  629. },
  630. _typeFun: {
  631. bool: '_checkBoolean',
  632. str: '_checkString',
  633. num: '_checkNumber',
  634. },
  635. _checkBoolean: function (ctx, value, condition) {
  636. switch (condition.value) {
  637. case 'true':
  638. return value ? true : false;
  639. case 'false':
  640. return value ? false : true;
  641. default:
  642. return true;
  643. }
  644. },
  645. _checkNumber: function (ctx, value, condition) {
  646. //if (ctx.helper._.isNil(value)) value = 0;
  647. switch (condition.operate) {
  648. case 'non-zero':
  649. return !ctx.helper.checkZero(value);
  650. case '=':
  651. return !ctx.helper._.isNil(value) ? value === condition.value : false;
  652. case '>':
  653. return !ctx.helper._.isNil(value) ? value > condition.value : false;
  654. case '<':
  655. return !ctx.helper._.isNil(value) ? value < condition.value : false;
  656. case '>=':
  657. return !ctx.helper._.isNil(value) ? value >= condition.value : false;
  658. case '<=':
  659. return !ctx.helper._.isNil(value) ? value <= condition.value : false;
  660. default:
  661. return true;
  662. }
  663. },
  664. _checkString: function (ctx, value, condition) {
  665. switch (condition.operate) {
  666. case '=':
  667. return value === condition.value;
  668. case 'part':
  669. return !ctx.helper._.isNil(value) ? value.indexOf(condition.value) >= 0 : false;
  670. case 'enum':
  671. return (!ctx.helper._.isNil(value) && condition.value instanceof Array) ? condition.value.indexOf(value) >= 0 : false;
  672. default:
  673. return true;
  674. }
  675. },
  676. fun: function (ctx, data, fields, options) {
  677. if (!options || !options.table || !options.condition) return;
  678. const fData = data[options.table], self = this;
  679. if (!fData) return;
  680. for (const c of options.condition) {
  681. c.fun = this._typeFun[c.type];
  682. }
  683. data[options.table] = fData.filter(function (d) {
  684. if (options.condition.length > 0) {
  685. let con = options.condition[0];
  686. let result = self[con.fun](ctx, d[con.field], con);
  687. for (let i = 1, iLen = options.condition.length; i < iLen; i++) {
  688. con = options.condition[i];
  689. result = (con.rela && con.rela === 'or')
  690. ? (result || self[con.fun](ctx, d[con.field], con))
  691. : (result && self[con.fun](ctx, d[con.field], con));
  692. }
  693. return result;
  694. } else {
  695. return true;
  696. }
  697. // const result;
  698. // for (const c of options.condition) {
  699. // if (!self[c.fun](ctx, d[c.field], c)) return false;
  700. // }
  701. // return true;
  702. });
  703. }
  704. };
  705. const gatherStagePay = {
  706. name: '汇总合同支付数据',
  707. intro: '根据付扣款项等,分类汇总合同支付的数据',
  708. defaultSetting: {
  709. table: 'mem_stage_pay',
  710. custom: [
  711. {name: '本期完成计量', ptype: 4, order: 1, visible: false},
  712. {name: '业主违约罚金', match: '业主违约罚金', order: 2},
  713. {name: '迟付款利息', match: '迟付款利息', order: 3},
  714. {flow: true, minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4},
  715. {name: '其他付款', minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4},
  716. {name: '合计', order: 5, order_calc: 'o1+o2+o3+o4', },
  717. {name: '动员预付款', order: 6},
  718. {name: '扣动员预付款', match: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款'], order: 7},
  719. {name: '扣材料预付款', match: '扣材料预付款', order: 8},
  720. {name: '承包人违约罚金', match: '承包人违约罚金', order: 9},
  721. {name: '保留金', match: '保留金', order: 10},
  722. {name: '税金', match: '税金', order: 11},
  723. {name: '质量保证金', match: '质量保证金', order: 12},
  724. {name: '其他扣款', minus: 1, rid: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款', '扣材料预付款', '承包人违约罚金', '保留金', '税金', '质量保证金'], order: 13},
  725. {name: '扣款合计', minus: 1, rid: [], order: 14},
  726. {name: '支付', ptype: 2, order: 15},
  727. ]
  728. },
  729. _filterFields: function (ctx, d, source) {
  730. let filterData = source;
  731. if (d.ptype) {
  732. filterData = filterData.filter(function (x) {
  733. return x.ptype === d.ptype;
  734. });
  735. }
  736. if (d.match) {
  737. filterData = filterData.filter(function (x) {
  738. if (x.name) {
  739. if (d.match instanceof Array) {
  740. for (const m of d.match) {
  741. if (x.name.indexOf(m) >= 0) return true;
  742. }
  743. } else {
  744. return x.name.indexOf(d.match) >= 0;
  745. }
  746. } else {
  747. return false;
  748. }
  749. });
  750. }
  751. if (!ctx.helper._.isNil(d.minus)) {
  752. filterData = filterData.filter(function (x) {
  753. return d.minus === 1 ? x.minus : !x.minus;
  754. });
  755. }
  756. if (d.rid) {
  757. filterData = filterData.filter(function (x) {
  758. if (x.name) {
  759. for (const r of d.rid) {
  760. if (x.name.indexOf(r) >= 0) return false;
  761. }
  762. }
  763. return true;
  764. });
  765. }
  766. return filterData;
  767. },
  768. _gatherFields: function (ctx, d, source, calcFields) {
  769. const filterData = this._filterFields(ctx, d, source);
  770. for (const fd of filterData) {
  771. for (const prop in fd) {
  772. if (prop.indexOf('tp') >= 0) {
  773. d[prop] = ctx.helper.add(d[prop], fd[prop]);
  774. if (calcFields.indexOf(prop) === -1) calcFields.push(prop);
  775. }
  776. }
  777. }
  778. },
  779. _completeGatherInfo(d, source) {
  780. for (const f in source) {
  781. if (/^t_[0-9]*_(id|name)$/.test(f)) {
  782. d[f] = source[f];
  783. }
  784. }
  785. },
  786. fun: function (ctx, data, fields, options) {
  787. if (!options || !options.table || !options.custom) return;
  788. const gatherData = data[options.table];
  789. const result = [], calcFields = [];
  790. for (const c of options.custom) {
  791. const cData = JSON.parse(JSON.stringify(c));
  792. cData.subOrder = 0;
  793. if (cData.flow) {
  794. const fData = this._filterFields(ctx, cData, gatherData);
  795. for (const [i, f] of fData.entries()) {
  796. f.order = cData.order;
  797. f.subOrder = i;
  798. result.push(f);
  799. }
  800. } else {
  801. if (!cData.order_calc && cData.empty !== 1) {
  802. this._gatherFields(ctx, cData, gatherData, calcFields);
  803. }
  804. if (options.table.indexOf('mem_gather') >= 0) this._completeGatherInfo(cData, gatherData[0]);
  805. result.push(cData);
  806. }
  807. }
  808. rdaUtils.orderCalc(ctx, result, calcFields);
  809. data[options.table] = result.filter(function (x) {
  810. return x.visible === undefined || x.visible;
  811. });
  812. data[options.table].sort(function (a, b) {
  813. return a.order === b.order ? a.subOrder - b.subOrder : a.order - b.order;
  814. });
  815. },
  816. };
  817. const union = {
  818. name: '合并数据',
  819. intro: '类似sql的union,可合并任意数据,合并结果写到"预留扩展数据-合并(mem_union_data)"中',
  820. defaultSetting: {
  821. union: [
  822. {
  823. table: 'mem_stage_bills',
  824. fields: [
  825. {target: 'str1', source: 'chapter'},
  826. {target: 'str2', source: 'name'},
  827. {target: 'tp1', source: 'total_price'},
  828. {target: 'tp2', source: 'gather_tp'},
  829. {target: 'tp3', source: 'pre_gather_tp'},
  830. {target: 'tp4', source: 'end_gather_tp'},
  831. {target: 'str3', data: '章级合计哟'},
  832. ]
  833. }, {
  834. table: 'mem_stage_pay',
  835. fields: [
  836. {target: 'str2', source: 'name'},
  837. {target: 'tp2', source: 'tp'},
  838. {target: 'tp3', source: 'pre_tp'},
  839. {target: 'tp4', source: 'end_tp'},
  840. {target: 'str3', data: '合同支付哟'},
  841. ]
  842. }
  843. ]
  844. },
  845. fun: function(ctx, data, fields, options) {
  846. if (!options || !options.union) return;
  847. const result = [];
  848. for (const u of options.union) {
  849. const unionData = data[u.table];
  850. for (const d of unionData) {
  851. const nd = {};
  852. for (const f of u.fields) {
  853. if (f.data) {
  854. nd[f.target] = f.data;
  855. } else if (f.source) {
  856. nd[f.target] = d[f.source];
  857. }
  858. }
  859. result.push(nd);
  860. }
  861. }
  862. data.mem_union_data = result;
  863. }
  864. };
  865. const addSumChapter = {
  866. name: '添加章级合计',
  867. intro: '在排序好的工程量清单列表中,根据清单所属章级,添加章级标题行、合计行',
  868. defaultSetting: {
  869. table: 'mem_stage_bills',
  870. code: 'b_code',
  871. chapter: 'chapter',
  872. stdChapter: { title: '%s章', sum: '%s章合计'},
  873. otherChapter: { title: '其他章', sum: '其他章合计'},
  874. sum: { name: '总合计' },
  875. fields: ['ex_value1', 'ex_value2', 'end_gather_tp', 'pre_gather_tp', 'gather_tp'],
  876. },
  877. fun: function (ctx, data, fields, options) {
  878. if (!options || !options.table || !options.code || !options.chapter || !options.stdChapter) return;
  879. const gclData = data[options.table];
  880. if (!gclData || !data.tender_info) return;
  881. const chapters = ctx.helper._.uniq(ctx.helper._.map(gclData, options.chapter));
  882. const finalSum = options.sum ? {name: options.sum.name, chapterNum: 100000, chapterPart: 1} : null;
  883. for (const chapter of chapters) {
  884. const chapterInfo = data.tender_info.chapter.find(function (x) { return x.code === chapter});
  885. const chapterOptions = chapterInfo ? options.stdChapter : options.otherChapter;
  886. // sum
  887. const chapterGcl = gclData.filter(function (x) {
  888. return x.chapter === chapter;
  889. });
  890. const sum = { chapter: chapter, chapterPart: 3, chapterNum: parseInt(chapter) };
  891. for (const cg of chapterGcl) {
  892. cg.chapterPart = 2;
  893. cg.chapterNum = parseInt(cg[options.chapter]);
  894. for (const f of options.fields) {
  895. sum[f] = ctx.helper.add(sum[f], cg[f]);
  896. if (finalSum) {
  897. finalSum[f] = ctx.helper.add(finalSum[f], cg[f]);
  898. }
  899. }
  900. }
  901. if (chapterOptions.sum !== undefined) {
  902. sum.name = chapterOptions.sum.replace('%s', chapter);
  903. gclData.push(sum);
  904. }
  905. // title
  906. if (chapterOptions.title !== undefined) {
  907. const title = { chapter: chapter, chapterPart: 1, chapterNum: parseInt(chapter) };
  908. if (chapterInfo) {
  909. title[options.code] = options.stdChapter.title.replace('%s', chapter);
  910. title.name = chapterInfo.name;
  911. } else {
  912. title.name = chapterOptions.title;
  913. }
  914. gclData.push(title);
  915. }
  916. }
  917. if (finalSum) {
  918. gclData.push(finalSum);
  919. }
  920. gclData.sort(function (a, b) {
  921. if (a.chapter !== b.chapter) {
  922. return a.chapterNum - b.chapterNum;
  923. } else if (a.chapterPart !== b.chapterPart) {
  924. return a.chapterPart - b.chapterPart;
  925. } else {
  926. return ctx.helper.compareCode(a[options.code], b[options.code]);
  927. }
  928. });
  929. }
  930. };
  931. const auditSelect = {
  932. name: '审批人选择',
  933. hint: '需搭配用户交互--审批人选择一起使用',
  934. defaultSetting: {
  935. table: ['mem_stage_bills_compare', 'mem_stage_pos_compare', 'mem_stage_pay'],
  936. },
  937. _stageBillsCompare(data, order) {
  938. const fields = [];
  939. for (const [i, o] of order.entries()) {
  940. const sPrefix = 'r' + o + '_';
  941. const tPrefix = 'as' + i + '_';
  942. fields.push({source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty'});
  943. fields.push({source: sPrefix + 'contract_tp', target: tPrefix + 'contract_tp'});
  944. fields.push({source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty'});
  945. fields.push({source: sPrefix + 'qc_tp', target: tPrefix + 'qc_tp'});
  946. fields.push({source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty'});
  947. fields.push({source: sPrefix + 'gather_tp', target: tPrefix + 'gather_tp'});
  948. }
  949. for (const d of data) {
  950. for (const f of fields) {
  951. d[f.target] = d[f.source];
  952. }
  953. }
  954. },
  955. _stagePosCompare(data, order) {
  956. const fields = [];
  957. for (const [i, o] of order.entries()) {
  958. const sPrefix = 'r' + o + '_';
  959. const tPrefix = 'as' + i + '_';
  960. fields.push({source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty'});
  961. fields.push({source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty'});
  962. fields.push({source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty'});
  963. }
  964. for (const d of data) {
  965. for (const f of fields) {
  966. d[f.target] = d[f.source];
  967. }
  968. }
  969. },
  970. _stagePay(data, order) {
  971. const fields = [];
  972. for (const [i, o] of order.entries()) {
  973. const sPrefix = 'r' + o + '_';
  974. const tPrefix = 'as' + i + '_';
  975. fields.push({source: sPrefix + 'tp', target: tPrefix + 'tp'});
  976. }
  977. for (const d of data) {
  978. for (const f of fields) {
  979. d[f.target] = d[f.source];
  980. }
  981. }
  982. },
  983. fun: function (ctx, data, fieldsKey, options, csRela) {
  984. if (!ctx.tender || !ctx.stage) return;
  985. if (!csRela.tplDefine) return;
  986. const asDefine = csRela.tplDefine.audit_select;
  987. if (!asDefine || !asDefine.enable || !asDefine.setting || asDefine.setting === '') return;
  988. const asCustom = csRela.cDefine ? csRela.cDefine.audit_select : null;
  989. const order = [];
  990. if (asCustom) {
  991. for (const asc of asCustom) {
  992. order.push(asc.order);
  993. }
  994. }
  995. for (const t of options.table) {
  996. switch (t) {
  997. case 'mem_stage_bills_compare':
  998. this._stageBillsCompare(data[t], order);
  999. break;
  1000. case 'mem_stage_pos_compare':
  1001. this._stagePosCompare(data[t], order);
  1002. break;
  1003. case 'mem_stage_pay':
  1004. this._stagePay(data[t], order);
  1005. break;
  1006. }
  1007. }
  1008. }
  1009. };
  1010. const datetimeFormat = {
  1011. name: '日期格式化',
  1012. hint: '参见帮助',
  1013. defaultSetting: {
  1014. tables: [
  1015. {name: 'mem_stage_bonus', fields: [
  1016. {field: 'real_time', formatter: 'yyyy-MM-DD'},
  1017. {field: 'create_time', formatter: 'yyyy-MM-DD'}
  1018. ]},
  1019. ]
  1020. },
  1021. fun: function (ctx, data, fieldsKey, options, csRela) {
  1022. if (!options.tables) return;
  1023. const tables = options.tables instanceof Array ? tables : [options.tables];
  1024. if (tables.length === 0) return;
  1025. for (const table of tables) {
  1026. const formatData = data[table.name];
  1027. for (const fd of formatData) {
  1028. for (const f of table.fields) {
  1029. fd[f.field] = moment(fd[f.field]).format(f.formatter);
  1030. }
  1031. }
  1032. }
  1033. }
  1034. };
  1035. const gatherSelectConverse = {
  1036. name: '交叉汇总数据',
  1037. hint: '需搭配 用户交互--汇总标段选择 一起使用',
  1038. defaultSetting: {
  1039. table: ['mem_gather_stage_bills'],
  1040. },
  1041. _commonConverse: function (helper, data, count) {
  1042. const result = [];
  1043. const reg = new RegExp('^t_[0-9]+_');
  1044. for (let i = 0; i < count; i++) {
  1045. const curReg = new RegExp('^t_' + i + '_');
  1046. for (const [i, d] of data.entries()) {
  1047. const nd = { cross_index: i + 1};
  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. // for (const d of data) {
  1061. // for (let i = 0; i < count; i++) {
  1062. // const curReg = new RegExp('^t_' + i + '_');
  1063. // const nd = {};
  1064. // for (const prop in d) {
  1065. // if (reg.test(prop)) {
  1066. // if (curReg.test(prop)) {
  1067. // nd[prop.replace(curReg, 't_')] = d[prop];
  1068. // }
  1069. // } else {
  1070. // nd[prop] = d[prop];
  1071. // }
  1072. // }
  1073. // result.push(nd);
  1074. // }
  1075. // }
  1076. return result;
  1077. },
  1078. fun: function (ctx, data, fieldsKey, options, csRela) {
  1079. if (!csRela.tplDefine) return;
  1080. const gsDefine = csRela.tplDefine.gather_select;
  1081. if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;
  1082. const gsCustom = csRela.cDefine ? csRela.cDefine.gather_select : null;
  1083. const count = gsDefine.setting.special
  1084. ? gsCustom.tenders.length - gsDefine.setting.special.length
  1085. : gsCustom.tenders.length;
  1086. for (const t of options.table) {
  1087. switch (t) {
  1088. case 'mem_gather_stage_bills':
  1089. case 'mem_gather_stage_pay':
  1090. case 'mem_gather_deal_bills':
  1091. case 'mem_union_data':
  1092. data[t] = this._commonConverse(ctx.helper, data[t], count);
  1093. break;
  1094. }
  1095. }
  1096. }
  1097. };
  1098. const sortPos = {
  1099. name: '计量单元排序',
  1100. hint: '',
  1101. defaultSetting: {
  1102. bills: 'mem_stage_bills',
  1103. pos: 'mem_stage_pos',
  1104. },
  1105. fun: function (ctx, data, fieldsKey, options, csRela) {
  1106. if (!options || !options.bills || !options.pos) return;
  1107. const bills = data[options.bills];
  1108. const pos = data[options.pos];
  1109. if (!bills || bills.length === 0 || !pos || pos.length === 0) return;
  1110. const billsIndex = {};
  1111. const findBillsIndex = function (billsId) {
  1112. if (!billsIndex[billsId]) {
  1113. billsIndex[billsId] = ctx.helper._.findIndex(bills, function (x) {
  1114. return x.id === billsId;
  1115. });
  1116. }
  1117. return billsIndex[billsId];
  1118. };
  1119. pos.sort(function (x, y) {
  1120. const xIndex = findBillsIndex(x.lid) * 1000 + x.porder;
  1121. const yIndex = findBillsIndex(y.lid) * 1000 + y.porder;
  1122. return xIndex - yIndex;
  1123. });
  1124. }
  1125. };
  1126. const unionPos = {
  1127. name: '拼接计量单元',
  1128. hint: '',
  1129. defaultSetting: {
  1130. bills: 'mem_stage_bills',
  1131. pos: 'mem_stage_pos',
  1132. },
  1133. fun: function (ctx, data, fieldsKey, options, csRela) {
  1134. if (!options || !options.bills || !options.pos) return;
  1135. const bills = data[options.bills];
  1136. const pos = data[options.pos];
  1137. if (!bills || bills.length === 0 || !pos || pos.length === 0) return;
  1138. const posIndex = new PosIndex({
  1139. id: 'id',
  1140. masterId: 'lid',
  1141. order: 'porder',
  1142. });
  1143. posIndex.loadDatas(pos);
  1144. const unionData = [];
  1145. for (const b of bills) {
  1146. unionData.push(b);
  1147. const posRange = posIndex.getLedgerPos(b.id);
  1148. b.posCount = posRange ? posRange.length : 0;
  1149. if (!posRange || posRange.length === 0) continue;
  1150. for (const p of posRange) {
  1151. unionData.push(p);
  1152. }
  1153. }
  1154. data[options.bills] = unionData;
  1155. }
  1156. };
  1157. const splitXmjCode = {
  1158. name: '拆分项目节编号',
  1159. hint: '',
  1160. defaultSetting: {
  1161. table: 'mem_stage_bills',
  1162. type: '11', // '18', '18-0-1', '18-1', '18-1-1'
  1163. code: 'code',
  1164. targetField: ['xiang', 'mu', 'jie', 'ximu'],
  1165. },
  1166. // 11标准
  1167. _split11Std: function (data, options, helper) {
  1168. for (const d of data) {
  1169. if (!d[options.code]) continue;
  1170. const splitCodes = d[options.code].split('-');
  1171. if (d.level === 2) {
  1172. const field = options.targetField[0];
  1173. if (field && splitCodes.length > 1) {
  1174. d[field] = helper.transFormToChinese(splitCodes[1]);
  1175. }
  1176. } else if (d.level === 3) {
  1177. const field = options.targetField[1];
  1178. if (field && splitCodes.length > 2) {
  1179. d[field] = splitCodes[2];
  1180. }
  1181. } else if (d.level === 4) {
  1182. const field = options.targetField[2];
  1183. if (field && splitCodes.length > 3) {
  1184. d[field] = splitCodes[3];
  1185. }
  1186. } else if (d.level > 4) {
  1187. const field = options.targetField[3];
  1188. if (field && splitCodes.length > 4) {
  1189. d[field] = splitCodes.reduce(function (s, v, i) { return i >= 4 ? (s ? s + '-' + v : v + '') : ''});
  1190. }
  1191. }
  1192. }
  1193. },
  1194. _getParentByLevel: function (data, node, level) {
  1195. if (node.full_path) {
  1196. const pid = node.full_path.split('-')[level-1];
  1197. return data.find(function (x) {
  1198. return x.level === level && x.ledger_id == pid;
  1199. });
  1200. } else {
  1201. return null;
  1202. }
  1203. },
  1204. // 18标准
  1205. _split18Std: function (data, options, helper) {
  1206. const types = options.type.split('-');
  1207. const isSplitSub = types[1] && types[1] === '1' ? true : false;
  1208. const isKeepGD = types[2] && types[2] === '1' ? true : false;
  1209. for (const d of data) {
  1210. if (!d[options.code]) continue;
  1211. let target = '';
  1212. if (d.level === 2) {
  1213. target = options.targetField[0] ? options.targetField[0]: '';
  1214. } else if (d.level === 3) {
  1215. target = options.targetField[1] ? options.targetField[1]: '';
  1216. } else if (d.level === 4) {
  1217. target = options.targetField[2] ? options.targetField[2]: '';
  1218. } else if (d.level > 4) {
  1219. target = options.targetField[3] ? options.targetField[3]: '';
  1220. }
  1221. if (!target) continue;
  1222. if (helper.check18MainCode(d[options.code])) {
  1223. const parent = helper._.find(data, {ledger_id: d.ledger_pid});
  1224. const splitParent = !parent || parent.level < 5 ? parent : this._getParentByLevel(data, d, 4);
  1225. d[target] = !splitParent
  1226. ? d[options.code]
  1227. : d[options.code].replace(splitParent[options.code], '');
  1228. } else if (helper.check18SubCode(d[options.code]) && isSplitSub) {
  1229. const parent = helper._.find(data, {ledger_id: d.ledger_pid});
  1230. const splitParent = !parent || parent.level < 5 ? parent : this._getParentByLevel(data, d, 4);
  1231. d[target] = !splitParent || helper.check18MainCode(splitParent.code)
  1232. ? d[options.code]
  1233. : d[options.code].replace(splitParent[options.code], '');
  1234. } else {
  1235. d[target] = d[options.code];
  1236. }
  1237. if (!isKeepGD) d[target] = d[target].replace(/^G[D]?/, '');
  1238. }
  1239. },
  1240. // 根据层次填入
  1241. _splitDefault: function (data, options) {
  1242. for (const d of data) {
  1243. if (!d[options.code]) continue;
  1244. if (d.level === 2) {
  1245. if (options.targetField[0]) d[options.targetField[0]] = d[options.code];
  1246. } else if (d.level === 3) {
  1247. if (options.targetField[1]) d[options.targetField[1]] = d[options.code];
  1248. } else if (d.level === 4) {
  1249. if (options.targetField[2]) d[options.targetField[2]] = d[options.code];
  1250. } else if (d.level > 4) {
  1251. if (options.targetField[3]) d[options.targetField[3]] = d[options.code];
  1252. }
  1253. }
  1254. },
  1255. fun: function (ctx, data, fieldsKey, options, csRela) {
  1256. if (!options || !options.table || !options.code || !options.targetField) return;
  1257. if (!data[options.table]) return;
  1258. if (options.type === '11') {
  1259. this._split11Std(data[options.table], options, ctx.helper);
  1260. } else if (/^18(-[0,1]){0,2}$/.test(options.type)) {
  1261. this._split18Std(data[options.table], options, ctx.helper);
  1262. } else {
  1263. this._splitDefault(data[options.table], options);
  1264. }
  1265. }
  1266. };
  1267. const analysisObj = {
  1268. changeSort,
  1269. gatherGcl,
  1270. sortGcl,
  1271. gatherChapter,
  1272. join,
  1273. getChapterCode,
  1274. filter,
  1275. union,
  1276. gatherStagePay,
  1277. addSumChapter,
  1278. auditSelect,
  1279. datetimeFormat,
  1280. gatherSelectConverse,
  1281. sortPos,
  1282. unionPos,
  1283. splitXmjCode,
  1284. };
  1285. const analysisDefine = (function (obj) {
  1286. const result = [];
  1287. for (const o in obj) {
  1288. const analysis = obj[o];
  1289. if (analysis.name && analysis.fun) {
  1290. result.push({
  1291. name: analysis.name,
  1292. key: o,
  1293. hint: analysis.hint,
  1294. intro: analysis.intro,
  1295. url: '/help?m=report&s=analysis&d=' + o,
  1296. })
  1297. }
  1298. }
  1299. return result;
  1300. })(analysisObj);
  1301. module.exports = {
  1302. analysisObj,
  1303. analysisDefine
  1304. };