phase_pay_detail.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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: '本期应付' },
  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: '本期实付' },
  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: '工程计量款' },
  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: '其他付款项' },
  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: '其他扣款项' },
  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: '新增付款项' },
  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: '新增扣款项' },
  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_price = this.ctx.helper.round(this._calculateExpr(p.range_expr), this.decimal);
  152. } else if (p.range_price && !p.range_expr) {
  153. p.range_price = this.ctx.helper.round(p.range_price, 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.payType || !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, 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. });
  289. this.getCalcBase();
  290. this.getCalcAdd();
  291. this.calculateStartRangePrice(payTree);
  292. const calcPays = this.sortPaysByCalc(payTree);
  293. this.calculate(calcPays);
  294. payTree.nodes.forEach(p => {
  295. p.calcUpdate = !this.ctx.helper.numEqual(p.org_tp, p.tp) || !this.ctx.helper.numEqual(p.org_start_tp, p.start_tp) || !this.ctx.helper.numEqual(p.org_range_tp, p.range_tp);
  296. });
  297. }
  298. }
  299. class PhasePayDetail extends TreeService {
  300. /**
  301. * 构造函数
  302. *
  303. * @param {Object} ctx - egg全局变量
  304. * @param {String} tableName - 表名
  305. * @return {void}
  306. */
  307. constructor(ctx, setting) {
  308. super(ctx, {
  309. mid: 'master_id',
  310. kid: 'tree_id',
  311. pid: 'tree_pid',
  312. order: 'tree_order',
  313. level: 'tree_level',
  314. isLeaf: 'tree_is_leaf',
  315. fullPath: 'tree_full_path',
  316. keyPre: '',
  317. uuid: true,
  318. });
  319. this.tableName = 'phase_pay_detail';
  320. this.deadlineType = deadlineType;
  321. }
  322. getMasterKey(phasePay) {
  323. return phasePay.curTimes
  324. ? `${phasePay.id}-${phasePay.curTimes}-${phasePay.curSort}`
  325. : `${phasePay.id}-${phasePay.audit_times}-${phasePay.audit_max_sort}`;
  326. }
  327. async initPhaseDataEmpty(conn, phasePay) {
  328. const user_id = this.ctx.session.sessionUser.accountId;
  329. const insertData = [];
  330. for (const dp of defaultPays) {
  331. insertData.push({
  332. tid: phasePay.tid, phase_id: phasePay.id, create_user_id: user_id, update_user_id: user_id,
  333. uuid: this.uuid.v4(), ...dp,
  334. });
  335. }
  336. await conn.insert(this.tableName, insertData);
  337. }
  338. async initPhaseDataByPre(conn, phasePay, prePhase) {
  339. const preData = await this.getAllDataByCondition({
  340. where: { phase_id: prePhase.id, audit_times: prePhase.audit_times, audit_sort: audit_max_sort },
  341. });
  342. for (const pd of preData) {
  343. delete pd.id;
  344. pd.audit_times = 1;
  345. pd.audit_sort = 0;
  346. }
  347. await conn.insert(this.tableName, preData);
  348. }
  349. async initPhaseDataByAudit(conn, phasePay, newTimes, newSort) {
  350. const preData = await transaction.getAllDataByCondition({ where: { master_id: this.getMasterKey(phasePay)}});
  351. for (const pd of preData) {
  352. delete pd.id;
  353. pd.master_id = `${phasePay.id}-${newTimes}-${newSort}`;
  354. pd.audit_times = newTimes;
  355. pd.audit_sort = newSort;
  356. }
  357. await conn.insert(this.tableName, preData);
  358. }
  359. async initPhaseData(conn, phasePay){
  360. if (!conn) throw '内部错误';
  361. const prePhase = await this.ctx.service.phasePay.getPhasePayByOrder(phasePay.tid, phasePay.phase_order - 1);
  362. if (prePhase) {
  363. await this.initPhaseDataByPre(conn, phasePay, prePhase);
  364. } else {
  365. await this.initPhaseDataEmpty(conn, phasePay);
  366. }
  367. }
  368. async getDetailData(phasePay) {
  369. return await this.getAllDataByCondition({ where: { master_id: this.getMasterKey(phasePay)} });
  370. }
  371. calculate(phasePay, details) {
  372. const payTree = new Ledger.baseTree(this.ctx, {
  373. id: 'tree_id', pid: 'tree_pid', order: 'tree_order',
  374. level: 'tree_level', isLeaf: 'tree_is_leaf', fullPath: 'tree_full_path',
  375. rootId: -1, calcField: [],
  376. });
  377. payTree.loadDatas(details);
  378. const payCalculator = new PayCalculator(this.ctx, phasePay);
  379. payCalculator.calculateAll(payTree);
  380. return payTree.getDefaultDatas();
  381. }
  382. getPayTp(datas, type) {
  383. const pay = datas.find(x => { return x.pay_type === type });
  384. return pay ? pay.tp || 0 : 0;
  385. }
  386. getPaySum(datas) {
  387. const result = {};
  388. result.yf_tp = this.getPayTp(datas, payType.bqyf);
  389. result.sf_tp = this.getPayTp(datas, payType.bqsf);
  390. result.calc_tp = this.getPayTp(datas, payType.gcjl);
  391. result.pay_tp = this.ctx.helper.add(this.getPayTp(datas, payType.qtfk), result.calc_tp);
  392. result.cut_tp = this.getPayTp(datas, payType.qtkk);
  393. return result;
  394. }
  395. async calculateSave(phasePay, transaction) {
  396. const details = await this.getDetailData(phasePay);
  397. if (details.length === 0) return false;
  398. const calcResult = this.calculate(phasePay, details);
  399. const updateData = calcResult.filter(x => { return x.calcUpdate; }).map(x => {
  400. return { id: x.id, tp: x.tp, start_tp: x.start_tp, range_tp: x.range_tp, end_tp: x.end_tp };
  401. });
  402. if (updateData.length === 0) return false;
  403. if (transaction) {
  404. await transaction.updateRows(this.tableName, updateData);
  405. } else {
  406. await this.defaultUpdateRows(updateData);
  407. }
  408. return this.getPaySum(calcResult);
  409. }
  410. _getDefaultData(data, phasePay, parent) {
  411. data.uuid = this.uuid.v4();
  412. data.tid = phasePay.tid;
  413. data.phase_id = phasePay.id;
  414. data.create_user_id = this.ctx.session.sessionUser.accountId;
  415. data.update_user_id = this.ctx.session.sessionUser.accountId;
  416. data.audit_times = phasePay.audit_times;
  417. data.audit_sort = phasePay.audit_max_sort;
  418. data.master_id = this.getMasterKey(phasePay);
  419. if (parent) {
  420. data.is_minus = parent.is_minus;
  421. }
  422. }
  423. async addChild(phasePay, select, count = 1) {
  424. if (select.payType === payType.bqsf || select.payType === payType.bqyf) throw '不可新增子项';
  425. if (select.tree_level >= 2) throw '不可新增子项';
  426. const masterId = this.getMasterKey(phasePay);
  427. const children = await this.getChildrenByParentId(masterId, select[this.setting.kid]);
  428. const maxId = await this._getMaxLid(masterId);
  429. const insertData = [];
  430. for (let i = 1; i <= count ; i++) {
  431. const data = {
  432. tree_id: maxId + i, tree_pid: select.tree_id, tree_order: children.length + 1 + i,
  433. tree_level: select.tree_level + 1, tree_is_leaf: 1
  434. };
  435. data.tree_full_path = select.tree_full_path + '-' + data.tree_id;
  436. this._getDefaultData(data, phasePay, select);
  437. insertData.push(data);
  438. }
  439. const conn = await this.db.beginTransaction();
  440. try {
  441. const result = await conn.insert(this.tableName, insertData);
  442. if (children.length === 0) {
  443. await conn.update(this.tableName, { id: select.id, is_leaf: false });
  444. }
  445. await conn.commit();
  446. } catch(err) {
  447. conn.rollback();
  448. throw err;
  449. }
  450. this._cacheMaxLid(masterId, maxId + 1);
  451. // 查询应返回的结果
  452. const resultData = {};
  453. resultData.create = await this.getNextsData(masterId, select.tree_id, children.length);
  454. if (children.length === 0) resultData.update = await this.getDataByKid(masterId, select.tree_id);
  455. return resultData;
  456. }
  457. async addNext(phasePay, select, count = 1) {
  458. const masterId = this.getMasterKey(phasePay);
  459. this.transaction = await this.db.beginTransaction();
  460. try {
  461. if (select) await this._updateChildrenOrder(masterId, select[this.setting.pid], select[this.setting.order] + 1, count);
  462. const newDatas = [];
  463. const maxId = await this._getMaxLid(masterId);
  464. for (let i = 1; i < count + 1; i++) {
  465. const newData = {};
  466. newData[this.setting.kid] = maxId + i;
  467. newData[this.setting.pid] = select ? select[this.setting.pid] : this.rootId;
  468. newData[this.setting.level] = select ? select[this.setting.level] : 1;
  469. newData[this.setting.order] = select ? select[this.setting.order] + i : i;
  470. newData[this.setting.fullPath] = newData[this.setting.level] > 1
  471. ? select[this.setting.fullPath].replace('-' + select[this.setting.kid], '-' + newData[this.setting.kid])
  472. : newData[this.setting.kid] + '';
  473. newData[this.setting.isLeaf] = 1;
  474. this._getDefaultData(newData, phasePay);
  475. newDatas.push(newData);
  476. }
  477. const insertResult = await this.transaction.insert(this.tableName, newDatas);
  478. this._cacheMaxLid(masterId, maxId + count);
  479. if (insertResult.affectedRows !== count) throw '新增节点数据错误';
  480. await this.transaction.commit();
  481. this.transaction = null;
  482. } catch (err) {
  483. await this.transaction.rollback();
  484. this.transaction = null;
  485. throw err;
  486. }
  487. if (select) {
  488. const createData = await this.getChildBetween(masterId, select[this.setting.pid], select[this.setting.order], select[this.setting.order] + count + 1);
  489. const updateData = await this.getNextsData(masterId, select[this.setting.pid], select[this.setting.order] + count);
  490. return {create: createData, update: updateData};
  491. } else {
  492. const createData = await this.getChildBetween(masterId, -1, 0, count + 1);
  493. return {create: createData};
  494. }
  495. }
  496. async addDetailNode(phasePay, targetId, count = 1) {
  497. if (!phasePay) return null;
  498. const select = targetId ? await this.getDataByKid(this.getMasterKey(phasePay), targetId) : null;
  499. if (targetId && !select) throw '新增节点数据错误';
  500. if (select[this.setting.level] === 1) {
  501. return await this.addChild(phasePay, select, count);
  502. } else {
  503. return await this.addNext(phasePay, select, count);
  504. }
  505. }
  506. async upMoveDetailNode(phasePay, targetId, count = 1) {
  507. const masterId = this.getMasterKey(phasePay);
  508. return await this.upMoveNode(masterId, targetId, count);
  509. }
  510. async downMoveDetailNode(phasePay, targetId, count = 1) {
  511. const masterId = this.getMasterKey(phasePay);
  512. return await this.downMoveNode(masterId, targetId, count);
  513. }
  514. async deleteDetailNode(phasePay, targetId, count = 1) {
  515. const masterId = this.getMasterKey(phasePay);
  516. return await this.delete(masterId, targetId, count);
  517. }
  518. _filterValidField(id, data) {
  519. const ud = { id };
  520. for (const prop in data) {
  521. if (validField.indexOf(prop) >= 0) {
  522. ud[prop] = data[prop];
  523. }
  524. }
  525. return ud;
  526. }
  527. checkCalc(data) {
  528. const datas = data instanceof Array ? data : [data];
  529. for (const d of datas) {
  530. for (const prop in d) {
  531. if (calcField.indexOf(prop) >= 0) return true;
  532. }
  533. }
  534. return false;
  535. }
  536. async updateDetail(phasePay, data) {
  537. const masterId = this.getMasterKey(phasePay);
  538. if (Array.isArray(data)) {
  539. const orgData = await this.getAllDataByCondition({ where: { id: data.map(d => { return d.id; }) } });
  540. const updateDatas = [];
  541. for (const d of data) {
  542. const node = orgData.find(x => { return x.id === d.id; });
  543. if (!node || masterId !== node[this.setting.mid]) throw '提交数据错误';
  544. const ud = this._filterValidField(node.id, d);
  545. ud.update_user_id = this.ctx.session.sessionUser.accountId;
  546. updateDatas.push(ud);
  547. }
  548. if (updateDatas.length > 0) await this.db.updateRows(this.tableName, updateDatas);
  549. } else {
  550. const node = await this.getDataById(data.id);
  551. if (!node || masterId !== node[this.setting.mid]) throw '提交数据错误';
  552. const updateData = this._filterValidField(node.id, data);
  553. updateData.update_user_id = this.ctx.session.sessionUser.accountId;
  554. await this.db.update(this.tableName, updateData);
  555. }
  556. }
  557. }
  558. module.exports = PhasePayDetail;