pay.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const payConst = require('../const/deal_pay.js');
  10. const writableFields = ['name', 'minus', 'sprice', 'sexpr', 'rprice', 'rexpr', 'is_yf', 'dl_type', 'dl_count', 'dl_tp_type', 'dl_tp'];
  11. module.exports = app => {
  12. class Pay extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'pay';
  22. }
  23. /**
  24. * 初始化 合同支付数据
  25. * @param transaction
  26. * @returns {Promise<boolean>}
  27. */
  28. async addDefaultPayData(tid, transaction) {
  29. const pays = JSON.parse(JSON.stringify(payConst.payTemplate));
  30. for (const p of pays) {
  31. p.tid = tid;
  32. p.csid = 0;
  33. p.cstimes = 0;
  34. p.csorder = 0;
  35. p.csaorder = 0;
  36. }
  37. let result;
  38. if (transaction) {
  39. result = await transaction.insert(this.tableName, pays);
  40. } else {
  41. result = await this.db.insert(this.tableName, pays);
  42. }
  43. return result.affectedRows === pays.length;
  44. }
  45. async _getMaxOrder(tenderId) {
  46. const sql = 'SELECT Max(??) As value FROM ?? Where tid = ' + tenderId;
  47. const sqlParam = ['order', this.tableName];
  48. const queryResult = await this.db.queryOne(sql, sqlParam);
  49. return queryResult.value;
  50. }
  51. /**
  52. * 新增合同支付项
  53. * @returns {Promise<*>}
  54. */
  55. async add() {
  56. if (!this.ctx.tender || !this.ctx.stage) {
  57. throw '数据错误';
  58. }
  59. const order = await this._getMaxOrder(this.ctx.tender.id);
  60. const pay = {
  61. tid: this.ctx.tender.id,
  62. csid: this.ctx.stage.id,
  63. cstimes: this.ctx.stage.times,
  64. csorder: this.ctx.stage.order,
  65. csaorder: this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor : 0,
  66. uid: this.ctx.session.sessionUser.accountId,
  67. minus: false,
  68. ptype: payConst.payType.normal,
  69. order: order + 1,
  70. };
  71. const transaction = await this.db.beginTransaction();
  72. try {
  73. const result = await transaction.insert(this.tableName, pay);
  74. if (result.affectedRows !== 1) {
  75. throw '新增合同支付项失败'
  76. }
  77. pay.id = result.insertId;
  78. const stagePay = await this.ctx.service.stagePay.syncAdd(pay, transaction);
  79. await transaction.commit();
  80. return this._.assign(pay, stagePay);
  81. } catch(err) {
  82. await transaction.rollback();
  83. throw err;
  84. }
  85. }
  86. /**
  87. * 删除合同支付项
  88. * @param id
  89. * @returns {Promise<void>}
  90. */
  91. async del(id) {
  92. if (!this.ctx.tender || !this.ctx.stage) {
  93. throw '数据错误';
  94. }
  95. // 检查是否可以删除
  96. const pay = await this.getDataByCondition({id: id});
  97. if (!pay) {
  98. throw '合同支付项不存在';
  99. } else if (pay.uid === -1) {
  100. throw '默认合同支付项不可删除';
  101. }
  102. // 删除合同支付
  103. const transaction = await this.db.beginTransaction();
  104. try {
  105. // 假删除
  106. //const result = await transaction.delete(this.tableName, {id: id});
  107. const result = await transaction.update(this.tableName, {
  108. id: id,
  109. valid: false,
  110. });
  111. if (result.affectedRows !== 1) {
  112. throw '删除合同支付项失败'
  113. }
  114. await transaction.commit();
  115. } catch(err) {
  116. await transaction.rollback();
  117. throw err;
  118. }
  119. }
  120. /**
  121. * 交换两个合同支付项的顺序
  122. * @param {Number} id1 - 合同支付项1的id
  123. * @param {Number} id2 - 合同支付项1的id
  124. * @returns {Promise<void>}
  125. */
  126. async changeOrder(id1, id2) {
  127. if (!this.ctx.tender || !this.ctx.stage) {
  128. throw '数据错误';
  129. }
  130. const pay1 = await this.getDataByCondition({tid: this.ctx.tender.id, id: id1});
  131. const pay2 = await this.getDataByCondition({tid: this.ctx.tender.id, id: id2});
  132. if (!pay1 || !pay2) {
  133. throw '数据错误';
  134. }
  135. const transaction = await this.db.beginTransaction();
  136. try {
  137. const order = pay1.order;
  138. pay1.order = pay2.order;
  139. pay2.order = order;
  140. await transaction.update(this.tableName, {id: pay1.id, order: pay1.order});
  141. await transaction.update(this.tableName, {id: pay2.id, order: pay2.order});
  142. await transaction.commit();
  143. } catch (err) {
  144. await transaction.rollback();
  145. throw err;
  146. }
  147. }
  148. /**
  149. * 保存合同支付项数据
  150. * @param data
  151. * @returns {Promise<boolean>}
  152. */
  153. async save(data) {
  154. if (!this.ctx.tender || !this.ctx.stage) { return false; }
  155. const pay = await this.getDataByCondition({tid: this.ctx.tender.id, id: data.id});
  156. if(!pay) {
  157. throw '数据错误';
  158. }
  159. const updateData = this.ctx.helper.filterValidFields(data, writableFields);
  160. updateData.id = data.id;
  161. const result = await this.db.update(this.tableName, updateData);
  162. if (result.affectedRows !== 1) {
  163. throw '更新数据失败';
  164. }
  165. return updateData;
  166. }
  167. }
  168. return Pay;
  169. };