payment_safe_bills.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. 'use strict';
  2. /**
  3. *
  4. * 支付审批-安全生产
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const defaultBills = require('../const/payment').defaultSafeBills;
  10. const billsUtils = require('../lib/bills_utils');
  11. const SafeBillsFields = {
  12. textFields: ['b_code', 'name', 'unit', 'spec', 'invoice_code', 'memo'],
  13. calcFields: ['unit_price', 'cur_qty', 'cur_tp', 'end_qty', 'end_tp'],
  14. fixedFields: ['safe_id', 'tender_id', 'pre_qty', 'pre_tp', 'cur_read_qty', 'cur_read_tp', 'cur_his', 'add_user_id', 'add_time'],
  15. treeFields: ['id', 'detail_id', 'safe_id', 'tree_id', 'tree_pid', 'tree_level', 'tree_order', 'tree_full_path', 'tree_is_leaf'],
  16. };
  17. SafeBillsFields.editQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, ...SafeBillsFields.calcFields, 'pre_qty', 'pre_tp'];
  18. SafeBillsFields.readQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, 'unit_price', 'pre_qty', 'pre_tp', 'cur_read_qty', 'cur_read_tp'];
  19. SafeBillsFields.compareQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, 'unit_price', 'cur_his', 'cur_qty', 'cur_tp'];
  20. SafeBillsFields.preQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, 'tender_id', 'unit_price', 'end_qty', 'end_tp', 'add_user_id', 'add_time', 'update_user_id', 'update_time'];
  21. const auditConst = require('../const/audit').stage;
  22. module.exports = app => {
  23. class PaymentSafeBills extends app.BaseTreeService {
  24. /**
  25. * 构造函数
  26. *
  27. * @param {Object} ctx - egg全局变量
  28. * @return {void}
  29. */
  30. constructor(ctx) {
  31. super(ctx, {
  32. mid: 'detail_id',
  33. kid: 'tree_id',
  34. pid: 'tree_pid',
  35. order: 'tree_order',
  36. level: 'tree_level',
  37. isLeaf: 'tree_is_leaf',
  38. fullPath: 'tree_full_path',
  39. keyPre: 'safe_bills_maxLid:',
  40. uuid: true,
  41. });
  42. // this.depart = 10;
  43. this.tableName = 'payment_safe_bills';
  44. this.decimal = { tp: 2, up: 2, qty: 3 };
  45. }
  46. // 继承方法
  47. clearParentingData(data) {
  48. for (const f of SafeBillsFields.calcFields) {
  49. data[f] = 0;
  50. }
  51. }
  52. _getDefaultData(data, detail) {
  53. data.id = this.uuid.v4();
  54. data.safe_id = data.id;
  55. data.tender_id = detail.tender_id;
  56. data.detail_id = detail.id;
  57. data.add_user_id = this.ctx.session.sessionUser.accountId;
  58. }
  59. async getEditData(detail) {
  60. return await this.getAllDataByCondition({
  61. where: { detail_id: detail.id },
  62. columns: SafeBillsFields.editQueryFields
  63. });
  64. }
  65. async getReadData(detail) {
  66. const helper = this.ctx.helper;
  67. const result = await this.getAllDataByCondition({
  68. where: { detail_id: detail.id },
  69. columns: SafeBillsFields.readQueryFields
  70. });
  71. result.forEach(x => {
  72. x.cur_qty = x.cur_read_qty;
  73. x.cur_tp = x.cur_read_tp;
  74. x.end_qty = helper.add(x.pre_qty, x.cur_qty);
  75. x.end_tp = helper.add(x.pre_tp, x.cur_tp);
  76. });
  77. return result;
  78. }
  79. async getCompareData(detail) {
  80. const result = await this.getAllDataByCondition({
  81. where: { detail_id: detail.id },
  82. columns: SafeBillsFields.compareQueryFields
  83. });
  84. result.forEach(x => {
  85. x.cur_his = x.cur_his ? JSON.parse(x.cur_his) : [];
  86. });
  87. return result;
  88. }
  89. async init(detail, transaction) {
  90. if (!detail || !transaction) throw '安全生产费数据错误';
  91. const insertData = [];
  92. for (const b of defaultBills) {
  93. const bills = JSON.parse(JSON.stringify(b));
  94. this._getDefaultData(bills, detail);
  95. insertData.push(bills);
  96. }
  97. const operate = await transaction.insert(this.tableName, insertData);
  98. return operate.affectedRows === insertData.length;
  99. }
  100. async initByPre(detail, preDetail, transaction) {
  101. if (!detail || !preDetail || !transaction) throw '安全生产费数据错误';
  102. const preBills = await this.getAllDataByCondition({
  103. where: { detail_id: preDetail.id },
  104. columns: SafeBillsFields.preQueryFields
  105. });
  106. const insertData = [];
  107. for (const bills of preBills) {
  108. //const bills = JSON.parse(JSON.stringify(b));
  109. bills.id = this.uuid.v4();
  110. bills.detail_id = detail.id;
  111. delete bills.invoice_code;
  112. bills.pre_qty = bills.end_qty;
  113. bills.pre_tp = bills.end_tp;
  114. insertData.push(bills);
  115. }
  116. const operate = await transaction.insert(this.tableName, insertData);
  117. return operate.affectedRows === insertData.length;
  118. }
  119. /**
  120. * 新增数据(供内部或其他service类调用, controller不可直接使用)
  121. * @param {Array|Object} data - 新增数据
  122. * @param {Number} detailId - 标段id
  123. * @param {Object} transaction - 新增事务
  124. * @return {Promise<boolean>} - {Promise<是否正确新增成功>}
  125. */
  126. async innerAdd(data, detailId, transaction) {
  127. const datas = data instanceof Array ? data : [data];
  128. if (detailId <= 0) {
  129. throw '标段id错误';
  130. }
  131. if (datas.length <= 0) {
  132. throw '插入数据为空';
  133. }
  134. if (!transaction) {
  135. throw '内部错误';
  136. }
  137. // 整理数据
  138. const insertData = [];
  139. for (const tmp of datas) {
  140. tmp[this.setting.id] = tmp.template_id;
  141. tmp[this.setting.pid] = tmp.pid;
  142. tmp[this.setting.mid] = detailId;
  143. delete tmp.template_id;
  144. delete tmp.pid;
  145. tmp.id = this.uuid.v4();
  146. insertData.push(tmp);
  147. }
  148. const operate = await transaction.insert(this.tableName, insertData);
  149. return operate.affectedRows === datas.length;
  150. }
  151. /**
  152. * 新增数据
  153. *
  154. * @param {Object} data - 新增的数据(可批量)
  155. * @param {Number} detailId - 支付审批期id
  156. * @return {Boolean} - 返回新增的结果
  157. */
  158. async add(data, detailId) {
  159. this.transaction = await this.db.beginTransaction();
  160. let result = false;
  161. try {
  162. result = await this.innerAdd(data, detailId, this.transaction);
  163. if (!result) {
  164. throw '新增数据错误';
  165. }
  166. await this.transaction.commit();
  167. } catch (error) {
  168. await this.transaction.rollback();
  169. result = false;
  170. }
  171. return result;
  172. }
  173. /**
  174. * 根据节点Id获取数据
  175. *
  176. * @param {Number} detailId - 标段id
  177. * @param {Number} nodeId - 项目节/工程量清单节点id
  178. * @return {Object} - 返回查询到的节点数据
  179. */
  180. async getDataByNodeId(detailId, nodeId) {
  181. if ((nodeId <= 0) || (detailId <= 0)) {
  182. return undefined;
  183. }
  184. const where = {};
  185. where[this.setting.mid] = detailId;
  186. where[this.setting.id] = nodeId;
  187. const data = await this.db.getDataByCondition(where);
  188. return data;
  189. }
  190. /**
  191. * 根据节点Id获取数据
  192. * @param {Number} detailId - 期Id
  193. * @param {Array} nodesIds - 节点Id
  194. * @return {Array}
  195. */
  196. async getDataByNodeIds(detailId, nodesIds) {
  197. if (detailId <= 0) {
  198. return [];
  199. }
  200. const where = {};
  201. where[this.setting.mid] = detailId;
  202. where[this.setting.id] = nodesIds;
  203. const data = await this.db.getAllDataByCondition({ where });
  204. return this._.sortBy(data, function(d) {
  205. return nodesIds.indexOf(d.ledger_id);
  206. });
  207. }
  208. /**
  209. * 根据主键id获取数据
  210. * @param {Array|Number} id - 主键id
  211. * @return {Promise<*>}
  212. */
  213. async getDataByIds(id) {
  214. if (!id) {
  215. return [];
  216. }
  217. const ids = id instanceof Array ? id : [id];
  218. if (ids.length === 0) {
  219. return [];
  220. }
  221. const data = await this.db.getAllDataByCondition({ where: { id: ids } });
  222. return data;
  223. }
  224. /**
  225. * 根据 父节点id 获取子节点
  226. * @param detailId
  227. * @param nodeId
  228. * @return {Promise<*>}
  229. */
  230. async getChildrenByParentId(detailId, nodeId) {
  231. if (detailId <= 0 || !nodeId) {
  232. return undefined;
  233. }
  234. const nodeIds = nodeId instanceof Array ? nodeId : [nodeId];
  235. if (nodeIds.length === 0) {
  236. return [];
  237. }
  238. const where = {};
  239. where[this.setting.mid] = detailId;
  240. where[this.setting.pid] = nodeIds;
  241. const data = await this.getAllDataByCondition({ where, orders: [[this.setting.order, 'ASC']] });
  242. return data;
  243. }
  244. async pasteBlockData(detailId, targetId, pasteData, defaultData) {
  245. const setting = this.setting;
  246. if ((detailId <= 0) || (sid <= 0)) return [];
  247. if (!pasteData || pasteData.length <= 0) throw '复制数据错误';
  248. for (const pd of pasteData) {
  249. if (!pd || pd.length <= 0) throw '复制数据错误';
  250. pd.sort(function (x, y) {
  251. return x[setting.level] - y[setting.level]
  252. });
  253. if (pd[0][this.setting.pid] !== pasteData[0][0][this.setting.pid]) throw '复制数据错误:仅可操作同层节点';
  254. }
  255. this.newBills = false;
  256. const targetData = await this.getDataByKid(detailId, targetId);
  257. if (!targetData) throw '粘贴数据错误';
  258. const newParentPath = targetData.full_path.replace(targetData.ledger_id, '');
  259. const tpDecimal = this.ctx.detail && this.ctx.detail.decimal ? this.ctx.detail.decimal : this.decimal;
  260. const pasteBillsData = [], leafBillsId = [];
  261. let maxId = await this._getMaxLid(this.ctx.paymentTender.id);
  262. for (const [i, pd] of pasteData.entries()) {
  263. for (const d of pd) {
  264. d.children = pd.filter(function (x) {
  265. return x[setting.pid] === d[setting.id];
  266. });
  267. }
  268. const pbd = [];
  269. for (const [j, d] of pd.entries()) {
  270. const newBills = {
  271. id: this.uuid.v4(),
  272. safe_id: this.uuid.v4(),
  273. b_code: d.b_code,
  274. name: d.name,
  275. unit: d.unit,
  276. unit_price: this.ctx.helper.round(d.unit_price, tpDecimal.up),
  277. memo: d.memo,
  278. };
  279. newBills[setting.mid] = detailId;
  280. newBills[setting.id] = maxId + j + 1;
  281. newBills[setting.pid] = j === 0 ? targetData[setting.pid] : d[setting.pid];
  282. newBills[setting.level] = d[setting.level] + targetData[setting.level] - pd[0][setting.level];
  283. newBills[setting.order] = j === 0 ? targetData[setting.order] + i + 1 : d[setting.order];
  284. newBills[setting.isLeaf] = d[setting.isLeaf];
  285. for (const c of d.children) {
  286. c[setting.pid] = newBills[setting.id];
  287. }
  288. newBills.quantity = this.ctx.helper.round(d.quantity, tpDecimal.qty);
  289. newBills.total_price = this.ctx.helper.mul(newBills.quantity, newBills.unit_price, tpDecimal.tp);
  290. if (defaultData) this.ctx.helper._.assignIn(newBills, defaultData);
  291. pbd.push(newBills);
  292. }
  293. for (const d of pbd) {
  294. const parent = pbd.find(function (x) {
  295. return x[setting.id] === d[setting.pid];
  296. });
  297. d[setting.fullPath] = parent
  298. ? parent[setting.fullPath] + '-' + d[setting.id]
  299. : newParentPath + d[setting.id];
  300. if (defaultData) this.ctx.helper._.assignIn(pbd, defaultData);
  301. pasteBillsData.push(d);
  302. }
  303. maxId = maxId + pbd.length;
  304. }
  305. this.transaction = await this.db.beginTransaction();
  306. try {
  307. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  308. await this._updateChildrenOrder(tid, targetData[setting.pid], targetData[setting.order] + 1, pasteData.length);
  309. // 数据库创建新增节点数据
  310. if (pasteBillsData.length > 0) {
  311. const newData = await this.transaction.insert(this.tableName, pasteBillsData);
  312. }
  313. this._cacheMaxLid(tid, maxId);
  314. await this.transaction.commit();
  315. } catch (err) {
  316. await this.transaction.rollback();
  317. throw err;
  318. }
  319. // 查询应返回的结果
  320. const updateData = await this.getNextsData(targetData[setting.mid], targetData[setting.pid], targetData[setting.order] + pasteData.length);
  321. return { create: pasteBillsData, update: updateData };
  322. }
  323. async addSafeBillsNode(detail, targetId, count) {
  324. if (!detail) return null;
  325. const select = targetId ? await this.getDataByKid(detail.id, targetId) : null;
  326. if (targetId && !select) throw '新增节点数据错误';
  327. this.transaction = await this.db.beginTransaction();
  328. try {
  329. if (select) await this._updateChildrenOrder(detail.id, select[this.setting.pid], select[this.setting.order] + 1, count);
  330. const newDatas = [];
  331. const maxId = await this._getMaxLid(detail.id);
  332. for (let i = 1; i < count + 1; i++) {
  333. const newData = {};
  334. newData[this.setting.kid] = maxId + i;
  335. newData[this.setting.pid] = select ? select[this.setting.pid] : this.rootId;
  336. newData[this.setting.mid] = detail.id;
  337. newData[this.setting.level] = select ? select[this.setting.level] : 1;
  338. newData[this.setting.order] = select ? select[this.setting.order] + i : i;
  339. newData[this.setting.fullPath] = newData[this.setting.level] > 1
  340. ? select[this.setting.fullPath].replace('-' + select[this.setting.kid], '-' + newData[this.setting.kid])
  341. : newData[this.setting.kid] + '';
  342. newData[this.setting.isLeaf] = true;
  343. this._getDefaultData(newData, detail);
  344. newDatas.push(newData);
  345. }
  346. const insertResult = await this.transaction.insert(this.tableName, newDatas);
  347. this._cacheMaxLid(detail.id, maxId + count);
  348. if (insertResult.affectedRows !== count) throw '新增节点数据错误';
  349. await this.transaction.commit();
  350. this.transaction = null;
  351. } catch (err) {
  352. await this.transaction.rollback();
  353. this.transaction = null;
  354. throw err;
  355. }
  356. if (select) {
  357. const createData = await this.getChildBetween(detail.id, select[this.setting.pid], select[this.setting.order], select[this.setting.order] + count + 1);
  358. const updateData = await this.getNextsData(detail.id, select[this.setting.pid], select[this.setting.order] + count);
  359. return {create: createData, update: updateData};
  360. } else {
  361. const createData = await this.getChildBetween(detail.id, -1, 0, count + 1);
  362. return {create: createData};
  363. }
  364. }
  365. async addStdNodeWithParent(detail, targetId, stdData) {
  366. const findPreData = function(list, a) {
  367. if (!list || list.length === 0) { return null; }
  368. for (let i = 0, iLen = list.length; i < iLen; i++) {
  369. if (billsUtils.compareCode(list[i].b_code, a.b_code) > 0) {
  370. return i > 0 ? list[i - 1] : null;
  371. }
  372. }
  373. return list[list.length - 1];
  374. };
  375. let parent = await this.getDataByKid(detail.id, targetId);
  376. if (targetId && !parent) throw '新增节点数据错误,请刷新页面重试';
  377. const orgParentId = parent.id;
  378. let children = await this.getChildrenByParentId(detail.id, targetId);
  379. const updateParent = children.length === 0;
  380. const insertData = [];
  381. const maxId = await this._getMaxLid(detail.id);
  382. this.transaction = await this.db.beginTransaction();
  383. try {
  384. if (updateParent) {
  385. const updateData = { id: parent.id };
  386. updateData[this.setting.isLeaf] = false;
  387. this.clearParentingData(updateData);
  388. await this.transaction.update(this.tableName, updateData);
  389. }
  390. // 从最顶层节点依次查询是否存在,否则添加
  391. for (let i = 0, len = stdData.length; i < len; i++) {
  392. const newData = { b_code: stdData[i].b_code, name: stdData[i].name, unit: stdData[i].unit };
  393. newData[this.setting.kid] = maxId + i + 1;
  394. newData[this.setting.pid] = parent ? parent[this.setting.kid] : this.rootId;
  395. newData[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
  396. newData[this.setting.fullPath] = parent ? `${parent[this.setting.fullPath]}-${newData[this.setting.kid]}` : `${newData[this.setting.kid]}`;
  397. const pre = findPreData(children, newData);
  398. newData[this.setting.order] = pre ? pre[this.setting.order] + 1 : 1;
  399. if (!pre || children.indexOf(pre) < children.length - 1) {
  400. await this._updateChildrenOrder(detail.id, parent ? parent[this.setting.kid] : this.rootId, pre ? pre[this.setting.order] + 1 : 1);
  401. }
  402. newData[this.setting.isLeaf] = (i === len - 1);
  403. this._getDefaultData(newData, detail);
  404. insertData.push(newData);
  405. parent = newData;
  406. children = [];
  407. }
  408. await this.transaction.insert(this.tableName, insertData);
  409. await this.transaction.commit();
  410. } catch (err) {
  411. await this.transaction.rollback();
  412. throw err;
  413. }
  414. this._cacheMaxLid(detail.id, maxId + stdData.length);
  415. // 查询应返回的结果
  416. const createData = await this.getDataByFullPath(detail.id, insertData[0][this.setting.fullPath] + '%');
  417. const updateData = await this.getNextsData(detail.id, targetId, insertData[0][this.setting.order]);
  418. if (updateParent) {
  419. updateData.push(await this.getDataByKid(detail.id, targetId));
  420. }
  421. return { create: createData, update: updateData };
  422. }
  423. async updateCalc(detail, data) {
  424. const helper = this.ctx.helper;
  425. const decimal = this.ctx.detail && this.ctx.detail.decimal ? this.ctx.detail.decimal : this.decimal;
  426. // 简单验证数据
  427. if (!detail) throw '安全生产费不存在';
  428. if (!data) throw '提交数据错误';
  429. const datas = data instanceof Array ? data : [data];
  430. const ids = datas.map(x => { return x.id; });
  431. const orgData = await this.getAllDataByCondition({ where: { id: ids }});
  432. const updateData = [];
  433. for (const row of datas) {
  434. const oData = orgData.find(x => { return x.id === row.id });
  435. if (!oData || oData.detail_id !== detail.id || oData.tree_id !== row.tree_id) throw '提交数据错误';
  436. let nData = { id: oData.id, tree_id: oData.tree_id, update_user_id: this.ctx.session.sessionUser.accountId };
  437. // 计算相关
  438. if (row.cur_qty !== undefined || row.unit_price !== undefined || row.cur_tp !== undefined) {
  439. nData.unit_price = row.unit_price !== undefined ? helper.round(row.unit_price, decimal.up) : oData.unit_price;
  440. nData.cur_qty = row.cur_qty !== undefined ? helper.round(row.cur_qty, decimal.qty) : oData.cur_qty;
  441. nData.cur_tp = row.cur_tp !== undefined ? helper.round(row.cur_tp, decimal.tp) : helper.mul(nData.unit_price, nData.cur_qty, decimal.tp);
  442. nData.end_qty = helper.add(nData.cur_qty, oData.pre_qty);
  443. nData.end_tp = helper.add(nData.cur_tp, oData.pre_tp);
  444. }
  445. for (const field of SafeBillsFields.textFields) {
  446. if (row[field] !== undefined) nData[field] = row[field];
  447. }
  448. updateData.push(nData);
  449. }
  450. await this.db.updateRows(this.tableName, updateData);
  451. return { update: updateData };
  452. }
  453. async auditCache(transaction, detailId, auditInfo) {
  454. const leaf = await this.getAllDataByCondition({ where: { detail_id: detailId, tree_is_leaf: true} });
  455. const updateData = [];
  456. for (const l of leaf) {
  457. if (l.cur_read_qty !== l.cur_qty || l.cur_read_tp !== l.cur_tp) {
  458. const data = { id: l.id, cur_read_qty: l.cur_qty, cur_read_tp: l.cur_tp };
  459. const his = l.cur_his ? JSON.parse(l.cur_his) : [];
  460. his.push({ ...auditInfo, qty: l.cur_qty, tp: l.cur_tp, up: l.unit_price });
  461. data.cur_his = JSON.stringify(his);
  462. data.cur_read_qty = l.cur_qty;
  463. data.cur_read_tp = l.cur_tp;
  464. updateData.push(data);
  465. }
  466. }
  467. if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
  468. }
  469. async setDecimal(decimal) {
  470. if (!this.ctx.detail) throw '读取数据错误';
  471. if (this.ctx.detail.order !== this.ctx.detail.highOrder) throw '往期不可修改小数位数';
  472. if (this.ctx.detail.status !== auditConst.status.uncheck && this.ctx.detail.status !== auditConst.status.checkNo) throw '仅原报可修改小数位数';
  473. const orgDecimal = this.ctx.detail.decimal ? this.ctx.detail.decimal : this.decimal;
  474. const calcQty = decimal.qty < orgDecimal.qty;
  475. const calcUp = decimal.up < orgDecimal.up;
  476. const calcTp = decimal.tp < orgDecimal.tp;
  477. this.ctx.detail.decimal = { up: decimal.up, tp: decimal.tp, qty: decimal.qty };
  478. const updateData = [];
  479. if (calcQty || calcUp || calcTp) {
  480. const calcData = await this.getAllDataByCondition({ where: {detail_id: this.ctx.detail.id, tree_is_leaf: 1 } });
  481. for (const cd of calcData) {
  482. const nd = { id: cd.id, tree_id: cd.tree_id };
  483. nd.unit_price = calcUp ? this.ctx.helper.round(cd.unit_price, decimal.up) : cd.unit_price;
  484. nd.cur_qty = calcQty ? this.ctx.helper.round(cd.cur_qty, decimal.qty) : cd.cur_qty;
  485. nd.cur_tp = this.ctx.helper.mul(nd.unit_price, nd.cur_qty, decimal.tp);
  486. nd.end_qty = this.ctx.helper.add(cd.pre_qty, nd.cur_qty);
  487. nd.end_tp = this.ctx.helper.add(cd.pre_tp, nd.cur_tp);
  488. updateData.push(nd);
  489. }
  490. }
  491. const conn = await this.db.beginTransaction();
  492. try {
  493. await conn.update(this.ctx.service.paymentDetail.tableName, { id: this.ctx.detail.id, bills_decimal: JSON.stringify(this.ctx.detail.decimal)});
  494. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  495. await conn.commit();
  496. return { calc: calcQty || calcUp || calcTp, update: updateData };
  497. } catch(err) {
  498. await conn.rollback();
  499. return { calc: false, update: [] };
  500. }
  501. }
  502. }
  503. return PaymentSafeBills;
  504. };