change_settle_list.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. module.exports = app => {
  10. class ChangeSettleList extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'change_settle_list';
  20. }
  21. async isSettle(cid) {
  22. const result = await this.getDataByCondition({ cid });
  23. return result && result.length > 0;
  24. }
  25. async updateChangeList(cid, settleBills, settlePos, list) {
  26. const removeList = [];
  27. const updateList = [];
  28. const changeSettleListData = await this.getAllDataByCondition({ where: { cid } });
  29. const settleStatus = this.ctx.service.settle.settleStatus;
  30. const transaction = await this.db.beginTransaction();
  31. try {
  32. for (const cl of list) {
  33. const settleInfo = cl.mx_id ? this._.find(settlePos, { lid: cl.gcl_id, pid: cl.mx_id }) : this._.find(settleBills, { lid: cl.gcl_id });
  34. if (settleInfo && settleInfo.settle_status === settleStatus.finish) {
  35. const changeSettleInfo = cl.mx_id ? this._.find(changeSettleListData, { gcl_id: cl.gcl_id, mx_id: cl.mx_id }) : this._.find(changeSettleListData, { gcl_id: cl.gcl_id });
  36. if (!changeSettleInfo) {
  37. removeList.push(cl.id);
  38. } else {
  39. if (changeSettleInfo.amount !== cl.spamount || changeSettleInfo.amount !== cl.camount) {
  40. cl.camount = changeSettleInfo.amount;
  41. cl.spamount = changeSettleInfo.amount;
  42. updateList.push({ id: cl.id, camount: cl.camount, spamount: cl.spamount });
  43. }
  44. }
  45. }
  46. }
  47. // list根据removeList对应删除
  48. if (removeList.length > 0) {
  49. for (const id of removeList) {
  50. const index = this._.findIndex(list, { id });
  51. if (index !== -1) {
  52. list.splice(index, 1);
  53. }
  54. }
  55. await transaction.delete(this.ctx.service.changeAuditList.tableName, { id: removeList });
  56. }
  57. if (updateList.length > 0) await transaction.update(this.ctx.service.changeAuditList.tableName, updateList);
  58. // 重算变更令金额
  59. if (removeList.length > 0 || updateList.length > 0) {
  60. await this.ctx.service.changeAuditList.reCalcTp(transaction, cid);
  61. }
  62. await transaction.commit();
  63. } catch (err) {
  64. console.log(err);
  65. await transaction.rollback();
  66. throw err;
  67. }
  68. return removeList.length;
  69. }
  70. }
  71. return ChangeSettleList;
  72. };