pay.js 8.1 KB

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