pay.js 6.2 KB

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