pay.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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({ where: { 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 valid = 1 and 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. async _save(data, transaction) {
  133. const pay = await this.getDataByCondition({tid: this.ctx.tender.id, id: data.id});
  134. if(!pay) {
  135. throw '数据错误';
  136. }
  137. const updateData = this.ctx.helper.filterValidFields(data, writableFields);
  138. updateData.id = data.id;
  139. if (transaction) {
  140. const result = await transaction.update(this.tableName, updateData);
  141. if (result.affectedRows !== 1) {
  142. throw '更新数据失败';
  143. }
  144. } else {
  145. const result = await this.db.update(this.tableName, updateData);
  146. if (result.affectedRows !== 1) {
  147. throw '更新数据失败';
  148. }
  149. }
  150. return updateData;
  151. }
  152. /**
  153. * 保存合同支付项数据
  154. * @param data
  155. * @returns {Promise<boolean>}
  156. */
  157. async save(data) {
  158. if (!this.ctx.tender || !this.ctx.stage) { return false; }
  159. if (data instanceof Array) {
  160. const transaction = await this.db.beginTransaction();
  161. const result = [];
  162. try {
  163. for (const d of data) {
  164. const updateData = await this._save(d, transaction);
  165. result.push(updateData);
  166. }
  167. await transaction.commit();
  168. } catch(err) {
  169. await transaction.rollback();
  170. throw err;
  171. }
  172. return result;
  173. } else {
  174. const pay = await this.getDataByCondition({tid: this.ctx.tender.id, id: data.id});
  175. if(!pay) {
  176. throw '数据错误';
  177. }
  178. const updateData = this.ctx.helper.filterValidFields(data, writableFields);
  179. updateData.id = data.id;
  180. const result = await this.db.update(this.tableName, updateData);
  181. if (result.affectedRows !== 1) {
  182. throw '更新数据失败';
  183. }
  184. return updateData;
  185. }
  186. }
  187. async doDeleteStage(stage, transaction) {
  188. await transaction.delete(this.tableName, { csid: stage.id });
  189. if (stage.order > 1) {
  190. const preStage = await this.ctx.service.stage.getDataByCondition({ tid: stage.tid, order: stage.order - 1});
  191. const max = await this.db.queryOne('SELECT MAX(stimes) as stimes, MAX(sorder) as sorder FROM ?? WHERE sid = ?', [this.ctx.service.stagePay.tableName, preStage.id]);
  192. const resortSql = `UPDATE ${this.tableName} p LEFT JOIN ${this.ctx.service.stagePay.tableName} sp ON p.id = sp.pid`+
  193. ' SET p.`order` = sp.porder, p.`expr` = sp.`expr`, p.valid = 1 WHERE p.tid = ? and sp.sid = ? and sp.stimes = ? and sp.sorder = ?';
  194. await transaction.query(resortSql, [stage.tid, preStage.id, max.stimes, max.sorder]);
  195. }
  196. }
  197. }
  198. return Pay;
  199. };