pay.js 7.7 KB

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