phase_pay_detail.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const payType = {
  10. bqsf: 'bqsf', bqyf: 'bqyf', gcjl: 'gcjl', qtfk: 'qtfk', qtkk: 'qtkk'
  11. };
  12. const defaultPays = [
  13. { tree_id: 1, tree_pid: -1, tree_level: 1, tree_order: 1, tree_is_leaf: 1, tree_full_path: '1', pay_type: 'bqyf', is_minus: 0, is_fixed: 1, name: '本期应付', expr: '' },
  14. { tree_id: 2, tree_pid: -1, tree_level: 1, tree_order: 2, tree_is_leaf: 1, tree_full_path: '2', pay_type: 'bqsf', is_minus: 0, is_fixed: 1, name: '本期实付', expr: '' },
  15. { tree_id: 3, tree_pid: -1, tree_level: 1, tree_order: 3, tree_is_leaf: 0, tree_full_path: '3', pay_type: 'gcjl', is_minus: 0, is_fixed: 1, name: '工程计量款', expr: '' },
  16. { tree_id: 4, tree_pid: -1, tree_level: 1, tree_order: 4, tree_is_leaf: 0, tree_full_path: '4', pay_type: 'qtfk', is_minus: 0, is_fixed: 1, name: '其他付款项', expr: '' },
  17. { tree_id: 5, tree_pid: -1, tree_level: 1, tree_order: 5, tree_is_leaf: 0, tree_full_path: '5', pay_type: 'qtkk', is_minus: 1, is_fixed: 1, name: '其他扣款项', expr: '' },
  18. { tree_id: 6, tree_pid: 3, tree_level: 2, tree_order: 1, tree_is_leaf: 1, tree_full_path: '3-6', pay_type: '', is_minus: 0, is_fixed: 0, name: '本期完成计量', expr: 'bqwc' },
  19. { tree_id: 7, tree_pid: 4, tree_level: 2, tree_order: 1, tree_is_leaf: 1, tree_full_path: '4-7', pay_type: '', is_minus: 0, is_fixed: 0, name: '新增付款项', expr: '' },
  20. { tree_id: 8, tree_pid: 5, tree_level: 2, tree_order: 1, tree_is_leaf: 1, tree_full_path: '5-8', pay_type: '', is_minus: 1, is_fixed: 0, name: '新增扣款项', expr: '' },
  21. ];
  22. const TreeService = require('../base/base_tree_service');
  23. const Ledger = require('../lib/ledger');
  24. const deadlineType = {
  25. phaseCount: { key: 'phaseCount', type: '计量期数', name: '合同支付期数' },
  26. stageCount: { key: 'stageCount', type: '计量期数', name: '计量期数' },
  27. gather: { key: 'gather', type: '计量金额', name: '累计完成金额' },
  28. contract: { key: 'contract', type: '计量金额', name: '累计合同金额' },
  29. qc: { key: 'qc', type: '计量金额', name: '累计变更金额' },
  30. };
  31. const math = require('mathjs');
  32. math.config({
  33. number: 'BigNumber',
  34. });
  35. const validField = ['name', 'is_pause', 'is_gather', 'start_tp', 'start_expr', 'range_tp', 'range_expr', 'tp', 'expr', 'postil', 'dl_type', 'dl_value'];
  36. const infoField = ['name', 'postil'];
  37. const calcField = validField.filter(x => { return infoField.indexOf(x) < 0; });
  38. class PayCalculator {
  39. constructor (ctx, phasePay) {
  40. this.ctx = ctx;
  41. this.phasePay = phasePay;
  42. this.percentReg = /((\d+)|((\d+)(\.\d+)))%/g;
  43. this.tenderInfo = ctx.tender.info;
  44. this.decimal = this.tenderInfo.decimal.pay ? this.tenderInfo.decimal.payTp : this.tenderInfo.decimal.tp;
  45. this.orderReg = /f\d+/ig;
  46. this.nodeReg = /<<[a-z0-9\-]+>>/ig;
  47. /* 以下变量在调用calculate方法后获得
  48. this.add;
  49. this.pre;
  50. this.cur;
  51. this.yf;
  52. */
  53. }
  54. /**
  55. * 获取 计算基数
  56. * @returns {Promise<void>}
  57. */
  58. getCalcBase () {
  59. if (this.bases) return;
  60. const bases = this.ctx.service.phasePay.getPhasePayCalcBase(this.phasePay, this.tenderInfo);
  61. this.bases = bases.sort(function (a, b) {
  62. return a.sort - b.sort;
  63. });
  64. for (const b of this.bases) {
  65. b.reg = new RegExp(b.code, 'igm');
  66. }
  67. }
  68. getCalcAdd() {
  69. if (this.addBase) return;
  70. const calc_base = this.phasePay.calc_base;
  71. this.addBase = {
  72. contract_tp: this.ctx.helper.add(calc_base.contract_tp, calc_base.pre_contract_tp),
  73. qc_tp: this.ctx.helper.add(calc_base.qc_tp, calc_base.pre_qc_tp),
  74. gather_tp: this.ctx.helper.add(calc_base.gather_tp, calc_base.pre_gather_tp),
  75. };
  76. }
  77. _calculateTpExpr(pay, pays) {
  78. let formula = pay.expr;
  79. const nodeParam = pay.expr.match(this.nodeReg);
  80. if (nodeParam) {
  81. for (const op of nodeParam) {
  82. const id = op.substring(2, op.length - 2);
  83. const payNode = pays.find(x => { return x.uuid === id; });
  84. formula = formula.replace(op, payNode ? payNode.tp || 0 : 0);
  85. }
  86. }
  87. for (const b of this.bases) {
  88. const value = b.checkStart && (!pay.pre_used && pay.start_tp)
  89. ? this.ctx.helper.sub(this.addBase[pay.checkStart], pay.start_tp)
  90. : b.value;
  91. formula = formula.replace(b.reg, value);
  92. }
  93. const percent = formula.match(this.percentReg);
  94. if (percent) {
  95. for (const p of percent) {
  96. const v = math.eval(p.replace(new RegExp('%', 'gm'), '/100'));
  97. formula = formula.replace(p, v);
  98. }
  99. }
  100. try {
  101. // 使用mathjs计算 math.eval('17259401.95*0.9') = 15533461.754999999,正确应为 15533461.755
  102. // const value = math.eval(formula);
  103. // 使用逆波兰法四则运算,可防止出现误差,但是只支持四则运算,不支持科学运算
  104. // const value = this.ctx.helper.calcExprStrRpn(formula);
  105. // 使用mathjs的大数运算,可支持所有
  106. const value = parseFloat(math.eval(formula));
  107. return Number.isFinite(value) ? value : 0;
  108. } catch(err) {
  109. return 0;
  110. }
  111. }
  112. _calculateExpr(expr) {
  113. let formula = expr;
  114. for (const b of this.bases) {
  115. formula = formula.replace(b.reg, b.value ? b.value : 0);
  116. }
  117. const percent = formula.match(this.percentReg);
  118. if (percent) {
  119. for (const p of percent) {
  120. const v = math.eval(p.replace(new RegExp('%', 'gm'), '/100'));
  121. formula = formula.replace(p, v);
  122. }
  123. }
  124. try {
  125. // 使用mathjs计算 math.eval('17259401.95*0.9') = 15533461.754999999,正确应为 15533461.755
  126. // const value = math.eval(formula);
  127. // 使用逆波兰法四则运算,可防止出现误差,但是只支持四则运算,不支持科学运算
  128. // const value = this.ctx.helper.calcExprStrRpn(formula);
  129. // 使用mathjs的大数运算,可支持所有
  130. const value = parseFloat(math.eval(formula));
  131. return Number.isFinite(value) ? value : 0;
  132. } catch(err) {
  133. return 0;
  134. }
  135. }
  136. /**
  137. * 计算起扣金额、付(扣)款限额
  138. *
  139. * @param {Array} pays - (标段)合同支付数据
  140. */
  141. calculateStartRangePrice (payTree) {
  142. for (const p of payTree.nodes) {
  143. // 上一期已计量的合同支付项,不予计算起扣金额、扣款限额
  144. if (p.pre_used) continue;
  145. if (p.start_expr) {
  146. p.start_tp = this.ctx.helper.round(this._calculateExpr(p.start_expr), this.decimal);
  147. } else if (p.start_tp && !p.start_expr) {
  148. p.start_tp = this.ctx.helper.round(p.start_tp, this.decimal);
  149. }
  150. if (p.range_expr) {
  151. p.range_tp = this.ctx.helper.round(this._calculateExpr(p.range_expr), this.decimal);
  152. } else if (p.range_tp && !p.range_expr) {
  153. p.range_tp = this.ctx.helper.round(p.range_tp, this.decimal);
  154. }
  155. }
  156. }
  157. /**
  158. * 检查是否到达 计提期限
  159. * @param pay
  160. */
  161. _checkDeadline(pay) {
  162. switch (pay.dl_type) {
  163. case deadlineType.phaseCount.key:
  164. return this.phasePay.phase_order >= pay.dl_value;
  165. case deadlineType.stageCount.key:
  166. const maxStageOrder = this.ctx.helper._.max(this.phasePay.rela_stage.map(x => { return x.stage_order; }));
  167. return maxStageOrder >= pay.dl_value;
  168. case deadlineType.gather.key:
  169. case deadlineType.contract.key:
  170. case deadlineType.qc.key:
  171. const deadlineTp = this.addBase[pay.dl_type + '_tp'];
  172. return deadlineTp >= pay.dl_value;
  173. default :
  174. return false;
  175. }
  176. }
  177. getLeafOrder(data, pays, parentId) {
  178. if (!data || !data.expr) return [];
  179. const nodeParam = data.expr.match(this.nodeReg);
  180. if (!nodeParam || nodeParam.length === 0) return [];
  181. const result = [...nodeParam];
  182. for (const op of nodeParam) {
  183. const id = op.substring(2, op.length - 2);
  184. if (id === data.uuid || id === parentId) {
  185. result.push(op);
  186. } else {
  187. const subPay = pays.find(x => { return x.uuid === id; });
  188. const sub = this.getLeafOrder(subPay, pays, data.uuid);
  189. if (sub.length > 0) {
  190. result.push(...sub);
  191. } else {
  192. result.push(op);
  193. }
  194. }
  195. }
  196. return this.ctx.helper._.uniq(result);
  197. }
  198. sortPaysByCalc(payTree) {
  199. const calcArr = [];
  200. for (const pay of payTree.nodes) {
  201. if (pay.pay_type === payType.bqyf) {
  202. pay.calcSort = 3;
  203. } else if (pay.pay_type === payType.bqsf) {
  204. pay.calcSort = 4;
  205. } else if (pay.children && pay.children.length > 0) {
  206. pay.calcSort = 2;
  207. } else {
  208. pay.calcSort = 1;
  209. pay.calcLeaf = this.getLeafOrder(pay, payTree.nodes);
  210. }
  211. calcArr.push(pay);
  212. }
  213. calcArr.sort((x, y) => {
  214. const calcSort = x.calcSort - y.calcSort;
  215. if (calcSort) return calcSort;
  216. if (x.calcSort === 2) return -(x.tree_level - y.tree_level);
  217. return x.calcLeaf.length - y.calcLeaf.length;
  218. });
  219. return calcArr;
  220. }
  221. _calculateYf(yf, pays) {
  222. yf.tp = 0;
  223. for (const p of pays) {
  224. if (p.pay_type || !p.is_gather || !p.tree_is_leaf) continue;
  225. yf.tp = !p.is_minus ? this.ctx.helper.add(yf.tp, p.tp) : this.ctx.helper.sub(yf.tp, p.tp);
  226. }
  227. const bqyf = this.bases.find(function (x) {return x.code === 'bqyf'});
  228. if (bqyf) bqyf.value = yf.tp;
  229. }
  230. _calculateSf(sf, pays) {
  231. if (sf.expr) {
  232. sf.tp = this.ctx.helper.round(this._calculateExpr(sf.expr, pays), this.decimal);
  233. } else {
  234. const yf = pays.find(x => { return x.pay_type === payType.bqyf; });
  235. sf.tp = yf.tp;
  236. }
  237. sf.tp = sf.range_tp ? Math.min(this.ctx.helper.sub(sf.range_tp, sf.pre_tp), sf.tp) : sf.tp;
  238. }
  239. _calculateGather(pay) {
  240. pay.tp = 0;
  241. for (const c of pay.children) {
  242. if (c.children && c.children.length > 0) this._calculateGather(c);
  243. if (!c.is_gather) continue;
  244. pay.tp = this.ctx.helper.add(pay.tp, c.tp);
  245. }
  246. }
  247. _calculateCommon(pay, pays) {
  248. // 暂停计量|未达起扣金额时,不计算
  249. if (pay.is_pause || (pay.start_tp && this.addBase.gather_tp < pay.start_tp)) {
  250. pay.tp = 0;
  251. return;
  252. }
  253. if (this._checkDeadline(pay)) {
  254. pay.tp = this.ctx.helper.sub(pay.range_tp, pay.pre_tp);
  255. } else if (pay.expr) {
  256. pay.tp = this.ctx.helper.round(this._calculateTpExpr(pay, pays), this.decimal);
  257. } else {
  258. pay.tp = this.ctx.helper.round(pay.tp || 0, this.decimal);
  259. }
  260. pay.tp = pay.range_tp ? Math.min(this.ctx.helper.sub(pay.range_tp, pay.pre_tp), pay.tp) : pay.tp;
  261. }
  262. /**
  263. * 计算本期、截止本期金额
  264. * @param {Array} pays - (标段&期)合同支付数据
  265. */
  266. calculate(pays) {
  267. for (const p of pays) {
  268. switch (p.pay_type) {
  269. case payType.bqyf:
  270. this._calculateYf(p, pays); break;
  271. case payType.bqsf:
  272. this._calculateSf(p, pays); break;
  273. case payType.gcjl:
  274. case payType.qtfk:
  275. case payType.qtkk:
  276. this._calculateGather(p); break;
  277. default:
  278. this._calculateCommon(p, pays); break;
  279. }
  280. p.end_tp = this.ctx.helper.add(p.pre_tp, p.tp);
  281. }
  282. }
  283. calculateAll(payTree) {
  284. payTree.nodes.forEach(x => {
  285. x.org_tp = x.tp || 0;
  286. x.org_start_tp = x.start_tp || 0;
  287. x.org_range_tp = x.range_tp || 0;
  288. x.org_end_tp = x.end_tp || 0;
  289. });
  290. this.getCalcBase();
  291. this.getCalcAdd();
  292. this.calculateStartRangePrice(payTree);
  293. const calcPays = this.sortPaysByCalc(payTree);
  294. this.calculate(calcPays);
  295. payTree.nodes.forEach(p => {
  296. p.calcUpdate = !this.ctx.helper.numEqual(p.org_tp, p.tp) || !this.ctx.helper.numEqual(p.org_end_tp, p.end_tp)
  297. || !this.ctx.helper.numEqual(p.org_start_tp, p.start_tp) || !this.ctx.helper.numEqual(p.org_range_tp, p.range_tp) ;
  298. });
  299. }
  300. }
  301. class PhasePayDetail extends TreeService {
  302. /**
  303. * 构造函数
  304. *
  305. * @param {Object} ctx - egg全局变量
  306. * @param {String} tableName - 表名
  307. * @return {void}
  308. */
  309. constructor(ctx, setting) {
  310. super(ctx, {
  311. mid: 'master_id',
  312. kid: 'tree_id',
  313. pid: 'tree_pid',
  314. order: 'tree_order',
  315. level: 'tree_level',
  316. isLeaf: 'tree_is_leaf',
  317. fullPath: 'tree_full_path',
  318. keyPre: '',
  319. uuid: true,
  320. });
  321. this.tableName = 'phase_pay_detail';
  322. this.deadlineType = deadlineType;
  323. }
  324. getMasterKey(phasePay) {
  325. return phasePay.curTimes
  326. ? `${phasePay.id}-${phasePay.curTimes}-${phasePay.curSort}`
  327. : `${phasePay.id}-${phasePay.audit_times}-${phasePay.audit_max_sort}`;
  328. }
  329. async initPhaseDataEmpty(conn, phasePay) {
  330. const user_id = this.ctx.session.sessionUser.accountId;
  331. const insertData = [];
  332. for (const dp of defaultPays) {
  333. insertData.push({
  334. tid: phasePay.tid, phase_id: phasePay.id, create_phase_id: phasePay.id, master_id: phasePay.id + '-1-0',
  335. create_user_id: user_id, update_user_id: user_id,
  336. uuid: this.uuid.v4(), ...dp,
  337. });
  338. }
  339. await conn.insert(this.tableName, insertData);
  340. }
  341. async initPhaseDataByPre(conn, phasePay, prePhase) {
  342. const preData = await this.getAllDataByCondition({
  343. where: { phase_id: prePhase.id, audit_times: prePhase.audit_times, audit_sort: prePhase.audit_max_sort },
  344. });
  345. for (const pd of preData) {
  346. delete pd.id;
  347. pd.tp = 0;
  348. pd.pre_tp = pd.end_tp;
  349. pd.phase_id = phasePay.id;
  350. pd.master_id = phasePay.id + '-1-0';
  351. pd.audit_times = 1;
  352. pd.audit_sort = 0;
  353. }
  354. await conn.insert(this.tableName, preData);
  355. }
  356. async initPhaseDataByAudit(conn, phasePay, newTimes, newSort) {
  357. const preData = await conn.select(this.tableName, { where: { master_id: this.getMasterKey(phasePay)}});
  358. for (const pd of preData) {
  359. delete pd.id;
  360. pd.master_id = `${phasePay.id}-${newTimes}-${newSort}`;
  361. pd.audit_times = newTimes;
  362. pd.audit_sort = newSort;
  363. }
  364. await conn.insert(this.tableName, preData);
  365. }
  366. async initPhaseDataByAuditCancel(conn, phasePay, oldTimes, oldSort, newTimes, newSort) {
  367. const masterId = `${phasePay.id}-${oldTimes}-${oldSort}`;
  368. const preData = await conn.select(this.tableName, { where: { master_id: masterId } });
  369. for (const pd of preData) {
  370. delete pd.id;
  371. pd.master_id = `${phasePay.id}-${newTimes}-${newSort}`;
  372. pd.audit_times = newTimes;
  373. pd.audit_sort = newSort;
  374. }
  375. await conn.insert(this.tableName, preData);
  376. }
  377. async initPhaseData(conn, phasePay, prePhase){
  378. if (!conn) throw '内部错误';
  379. if (prePhase) {
  380. await this.initPhaseDataByPre(conn, phasePay, prePhase);
  381. } else {
  382. await this.initPhaseDataEmpty(conn, phasePay);
  383. }
  384. }
  385. async getDetailData(phasePay) {
  386. return await this.getAllDataByCondition({ where: { master_id: this.getMasterKey(phasePay)} });
  387. }
  388. calculate(phasePay, details) {
  389. const payTree = new Ledger.baseTree(this.ctx, {
  390. id: 'tree_id', pid: 'tree_pid', order: 'tree_order',
  391. level: 'tree_level', isLeaf: 'tree_is_leaf', fullPath: 'tree_full_path',
  392. rootId: -1, calcField: [],
  393. });
  394. payTree.loadDatas(details);
  395. const payCalculator = new PayCalculator(this.ctx, phasePay);
  396. payCalculator.calculateAll(payTree);
  397. return payTree.getDefaultDatas();
  398. }
  399. getPayTp(datas, type) {
  400. const pay = datas.find(x => { return x.pay_type === type });
  401. return pay ? pay.tp || 0 : 0;
  402. }
  403. getPaySum(datas) {
  404. const result = {};
  405. result.yf_tp = this.getPayTp(datas, payType.bqyf);
  406. result.sf_tp = this.getPayTp(datas, payType.bqsf);
  407. result.calc_tp = this.getPayTp(datas, payType.gcjl);
  408. result.pay_tp = this.ctx.helper.add(this.getPayTp(datas, payType.qtfk), result.calc_tp);
  409. result.cut_tp = this.getPayTp(datas, payType.qtkk);
  410. return result;
  411. }
  412. async calculateSave(phasePay, transaction) {
  413. const details = await this.getDetailData(phasePay);
  414. if (details.length === 0) return false;
  415. const calcResult = this.calculate(phasePay, details);
  416. const updateData = calcResult.filter(x => { return x.calcUpdate; }).map(x => {
  417. return { id: x.id, tp: x.tp, start_tp: x.start_tp, range_tp: x.range_tp, end_tp: x.end_tp };
  418. });
  419. if (updateData.length === 0) return this.getPaySum(calcResult);
  420. if (transaction) {
  421. await transaction.updateRows(this.tableName, updateData);
  422. } else {
  423. await this.defaultUpdateRows(updateData);
  424. }
  425. return this.getPaySum(calcResult);
  426. }
  427. _getDefaultData(data, phasePay, parent) {
  428. data.uuid = this.uuid.v4();
  429. data.tid = phasePay.tid;
  430. data.phase_id = phasePay.id;
  431. data.create_user_id = this.ctx.session.sessionUser.accountId;
  432. data.update_user_id = this.ctx.session.sessionUser.accountId;
  433. data.audit_times = phasePay.audit_times;
  434. data.audit_sort = phasePay.audit_max_sort;
  435. data.master_id = this.getMasterKey(phasePay);
  436. if (parent) {
  437. data.is_minus = parent.is_minus;
  438. }
  439. }
  440. async addChild(phasePay, select, count = 1) {
  441. if (select.pay_type === payType.bqsf || select.pay_type === payType.bqyf) throw '不可新增子项';
  442. if (select.tree_level >= 2) throw '不可新增子项';
  443. const masterId = this.getMasterKey(phasePay);
  444. const children = await this.getChildrenByParentId(masterId, select[this.setting.kid]);
  445. const maxId = await this._getMaxLid(masterId);
  446. const insertData = [];
  447. for (let i = 1; i <= count ; i++) {
  448. const data = {
  449. tree_id: maxId + i, tree_pid: select.tree_id, tree_order: children.length + i,
  450. tree_level: select.tree_level + 1, tree_is_leaf: 1
  451. };
  452. data.tree_full_path = select.tree_full_path + '-' + data.tree_id;
  453. this._getDefaultData(data, phasePay, select);
  454. insertData.push(data);
  455. }
  456. const conn = await this.db.beginTransaction();
  457. try {
  458. const result = await conn.insert(this.tableName, insertData);
  459. if (children.length === 0) {
  460. await conn.update(this.tableName, { id: select.id, is_leaf: false });
  461. }
  462. await conn.commit();
  463. } catch(err) {
  464. conn.rollback();
  465. throw err;
  466. }
  467. this._cacheMaxLid(masterId, maxId + 1);
  468. // 查询应返回的结果
  469. const resultData = {};
  470. resultData.create = await this.getNextsData(masterId, select.tree_id, children.length);
  471. if (children.length === 0) resultData.update = await this.getDataByKid(masterId, select.tree_id);
  472. return resultData;
  473. }
  474. async addNext(phasePay, select, count = 1) {
  475. const masterId = this.getMasterKey(phasePay);
  476. this.transaction = await this.db.beginTransaction();
  477. try {
  478. if (select) await this._updateChildrenOrder(masterId, select[this.setting.pid], select[this.setting.order] + 1, count);
  479. const newDatas = [];
  480. const maxId = await this._getMaxLid(masterId);
  481. for (let i = 1; i < count + 1; i++) {
  482. const newData = {};
  483. newData[this.setting.kid] = maxId + i;
  484. newData[this.setting.pid] = select ? select[this.setting.pid] : this.rootId;
  485. newData[this.setting.level] = select ? select[this.setting.level] : 1;
  486. newData[this.setting.order] = select ? select[this.setting.order] + i : i;
  487. newData[this.setting.fullPath] = newData[this.setting.level] > 1
  488. ? select[this.setting.fullPath].replace('-' + select[this.setting.kid], '-' + newData[this.setting.kid])
  489. : newData[this.setting.kid] + '';
  490. newData[this.setting.isLeaf] = 1;
  491. this._getDefaultData(newData, phasePay);
  492. newDatas.push(newData);
  493. }
  494. const insertResult = await this.transaction.insert(this.tableName, newDatas);
  495. this._cacheMaxLid(masterId, maxId + count);
  496. if (insertResult.affectedRows !== count) throw '新增节点数据错误';
  497. await this.transaction.commit();
  498. this.transaction = null;
  499. } catch (err) {
  500. await this.transaction.rollback();
  501. this.transaction = null;
  502. throw err;
  503. }
  504. if (select) {
  505. const createData = await this.getChildBetween(masterId, select[this.setting.pid], select[this.setting.order], select[this.setting.order] + count + 1);
  506. const updateData = await this.getNextsData(masterId, select[this.setting.pid], select[this.setting.order] + count);
  507. return {create: createData, update: updateData};
  508. } else {
  509. const createData = await this.getChildBetween(masterId, -1, 0, count + 1);
  510. return {create: createData};
  511. }
  512. }
  513. async addDetailNode(phasePay, targetId, count = 1) {
  514. if (!phasePay) return null;
  515. const select = targetId ? await this.getDataByKid(this.getMasterKey(phasePay), targetId) : null;
  516. if (targetId && !select) throw '新增节点数据错误';
  517. if (select[this.setting.level] === 1) {
  518. return await this.addChild(phasePay, select, count);
  519. } else {
  520. return await this.addNext(phasePay, select, count);
  521. }
  522. }
  523. async upMoveDetailNode(phasePay, targetId, count = 1) {
  524. const masterId = this.getMasterKey(phasePay);
  525. return await this.upMoveNode(masterId, targetId, count);
  526. }
  527. async downMoveDetailNode(phasePay, targetId, count = 1) {
  528. const masterId = this.getMasterKey(phasePay);
  529. return await this.downMoveNode(masterId, targetId, count);
  530. }
  531. async deleteDetailNode(phasePay, targetId, count = 1) {
  532. const masterId = this.getMasterKey(phasePay);
  533. return await this.delete(masterId, targetId, count);
  534. }
  535. _filterValidField(id, data) {
  536. const ud = { id };
  537. for (const prop in data) {
  538. if (validField.indexOf(prop) >= 0) {
  539. ud[prop] = data[prop];
  540. }
  541. }
  542. return ud;
  543. }
  544. checkCalc(data) {
  545. const datas = data instanceof Array ? data : [data];
  546. for (const d of datas) {
  547. for (const prop in d) {
  548. if (calcField.indexOf(prop) >= 0) return true;
  549. }
  550. }
  551. return false;
  552. }
  553. async updateDetail(phasePay, data) {
  554. const masterId = this.getMasterKey(phasePay);
  555. if (Array.isArray(data)) {
  556. const orgData = await this.getAllDataByCondition({ where: { id: data.map(d => { return d.id; }) } });
  557. const updateDatas = [];
  558. for (const d of data) {
  559. const node = orgData.find(x => { return x.id === d.id; });
  560. if (!node || masterId !== node[this.setting.mid]) throw '提交数据错误';
  561. const ud = this._filterValidField(node.id, d);
  562. ud.update_user_id = this.ctx.session.sessionUser.accountId;
  563. updateDatas.push(ud);
  564. }
  565. if (updateDatas.length > 0) await this.db.updateRows(this.tableName, updateDatas);
  566. } else {
  567. const node = await this.getDataById(data.id);
  568. if (!node || masterId !== node[this.setting.mid]) throw '提交数据错误';
  569. const updateData = this._filterValidField(node.id, data);
  570. updateData.update_user_id = this.ctx.session.sessionUser.accountId;
  571. await this.db.update(this.tableName, updateData);
  572. }
  573. }
  574. }
  575. module.exports = PhasePayDetail;