contract_pay.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. const contractConst = require('../const/contract');
  7. module.exports = app => {
  8. class ContractPay extends BaseService {
  9. /**
  10. * 构造函数
  11. *
  12. * @param {Object} ctx - egg全局变量
  13. * @return {void}
  14. */
  15. constructor(ctx) {
  16. super(ctx);
  17. this.tableName = 'contract_pay';
  18. this.dataId = 'id';
  19. }
  20. async getPays(options, cid) {
  21. const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND `cid` = ? ORDER BY `create_time` DESC';
  22. const sqlParams = [this.tableName, cid];
  23. const list = await this.db.query(sql, sqlParams);
  24. if (list.length > 0) {
  25. const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: list.map(item => item.uid) } });
  26. for (const l of list) {
  27. const userInfo = userList.find(item => item.id === l.uid);
  28. l.username = userInfo ? userInfo.name : '';
  29. l.files = await this.ctx.service.contractPayAtt.getAtt(l.id);
  30. }
  31. }
  32. return list;
  33. }
  34. async add(options, cid, data) {
  35. const node = await this.ctx.service.contract.getDataById(cid);
  36. if (!node) {
  37. throw '合同不存在';
  38. }
  39. const transaction = await this.db.beginTransaction();
  40. try {
  41. const insertData = {
  42. spid: options.spid || null,
  43. tid: options.tid || null,
  44. contract_type: options.contract_type,
  45. cid,
  46. uid: this.ctx.session.sessionUser.accountId,
  47. pay_time: data.pay_time,
  48. pay_price: data.pay_price,
  49. debit_price: data.debit_price,
  50. yf_price: data.yf_price,
  51. sf_price: data.sf_price,
  52. pay_type: data.pay_type,
  53. remark: data.remark,
  54. create_time: new Date(),
  55. };
  56. await transaction.insert(this.tableName, insertData);
  57. await this.calcContract(transaction, node);
  58. await transaction.commit();
  59. } catch (err) {
  60. await transaction.rollback();
  61. throw err;
  62. }
  63. return { pays: await this.getPays(options, cid), node: { update: node } };
  64. }
  65. async save(options, cid, data) {
  66. if (!data.id) {
  67. throw '参数有误';
  68. }
  69. const node = await this.ctx.service.contract.getDataById(cid);
  70. if (!node) {
  71. throw '合同不存在';
  72. }
  73. const cpInfo = await this.getDataById(data.id);
  74. if (!cpInfo) {
  75. throw '合同' + contractConst.typeName[cpInfo.contract_type] + '不存在';
  76. }
  77. const transaction = await this.db.beginTransaction();
  78. try {
  79. await transaction.update(this.tableName, data);
  80. await this.calcContract(transaction, node);
  81. await transaction.commit();
  82. } catch (err) {
  83. await transaction.rollback();
  84. throw err;
  85. }
  86. return { pays: await this.getPays(options, cid), node: { update: node } };
  87. }
  88. async del(options, cid, cpid) {
  89. if (!cpid) {
  90. throw '参数有误';
  91. }
  92. const node = await this.ctx.service.contract.getDataById(cid);
  93. if (!node) {
  94. throw '合同不存在';
  95. }
  96. const cpInfo = await this.getDataById(cpid);
  97. if (!cpInfo) {
  98. throw '合同' + contractConst.typeName[cpInfo.contract_type] + '不存在';
  99. }
  100. const transaction = await this.db.beginTransaction();
  101. try {
  102. await transaction.delete(this.tableName, { id: cpid });
  103. // 删除合同附件
  104. const attList = await this.ctx.service.contractPayAtt.getAllDataByCondition({ where: { cpid } });
  105. await this.ctx.helper.delFiles(attList);
  106. await transaction.delete(this.ctx.service.contractPayAtt.tableName, { cpid });
  107. await this.calcContract(transaction, node);
  108. await transaction.commit();
  109. } catch (err) {
  110. await transaction.rollback();
  111. throw err;
  112. }
  113. return { pays: await this.getPays(options, cid), node: { update: node } };
  114. }
  115. async calcContract(transaction, node) {
  116. const paysList = await transaction.query('SELECT * FROM ?? WHERE `cid` = ?', [this.tableName, node.id]);
  117. let pay_price = 0;
  118. let debit_price = 0;
  119. let yf_price = 0;
  120. let sf_price = 0;
  121. for (const l of paysList) {
  122. pay_price = this.ctx.helper.add(pay_price, l.pay_price);
  123. debit_price = this.ctx.helper.add(debit_price, l.debit_price);
  124. yf_price = this.ctx.helper.add(yf_price, l.yf_price);
  125. sf_price = this.ctx.helper.add(sf_price, l.sf_price);
  126. }
  127. node.pay_price = pay_price;
  128. node.debit_price = debit_price;
  129. node.yf_price = yf_price;
  130. node.sf_price = sf_price;
  131. node.exist_pay = paysList.length === 0 ? 0 : 1;
  132. await transaction.update(this.ctx.service.contract.tableName, node);
  133. }
  134. }
  135. return ContractPay;
  136. };