rpt_data_analysis.js 54 KB

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