pay.js 8.2 KB

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