phase_pay_detail.js 25 KB

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