stage_other.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. module.exports = app => {
  11. class StageOther extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'stage_other';
  21. }
  22. async getStageData(sid) {
  23. const data = await this.getAllDataByCondition({where: { sid: sid }});
  24. if (this.ctx.stage && this.ctx.stage.readOnly && this.ctx.stage.status !== auditConst.status.checked) {
  25. for (const d of data) {
  26. const his = d.shistory ? JSON.parse(d.shistory) : [];
  27. const h = this.ctx.helper._.find(his, {
  28. stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder
  29. });
  30. d.tp = h ? h.tp : null;
  31. }
  32. }
  33. return data;
  34. }
  35. async getPreStageData(sorder) {
  36. const sql = 'SELECT uuid, Sum(tp) as tp From ' + this.tableName + ' WHERE sorder < ? And tid = ? GROUP By uuid';
  37. const sqlParam = [sorder, this.ctx.tender.id];
  38. const data = await this.db.query(sql, sqlParam);
  39. return data;
  40. }
  41. async getEndStageData(sorder) {
  42. const sql = 'SELECT uuid, Sum(tp) as tp From' + this.tableName + ' WHERE sorder <= ? And tid = ? GROUP By uuid';
  43. const sqlParam = [sorder, this.ctx.tender.id];
  44. const data = await this.db.query(sql, sqlParam);
  45. return data;
  46. }
  47. async _addDatas(data) {
  48. const datas = data instanceof Array ? data : [data];
  49. const insertData = [];
  50. for (const d of datas) {
  51. if (!d.name || !d.order) throw '新增其他数据,提交的数据错误';
  52. const nd = {
  53. uuid: this.uuid.v4(),
  54. add_sid: this.ctx.stage.id,
  55. add_uid: this.ctx.session.sessionUser.accountId,
  56. add_time: new Date(),
  57. sid: this.ctx.stage.id,
  58. sorder: this.ctx.stage.order,
  59. tid: this.ctx.tender.id,
  60. };
  61. nd.name = d.name;
  62. nd.order = d.order;
  63. if (d.total_price) nd.total_price = this.ctx.helper.round(d.total_price, this.ctx.tender.info.decimal.tp);
  64. if (d.tp) nd.tp = this.ctx.helper.round(d.tp, this.ctx.tender.info.decimal.tp);
  65. if (d.real_time) nd.real_time = d.real_time;
  66. if (d.memo) nd.memo = d.memo;
  67. insertData.push(nd);
  68. }
  69. await this.db.insert(this.tableName, insertData);
  70. return await this.getAllDataByCondition({
  71. where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') }
  72. });
  73. }
  74. async _delDatas (data) {
  75. const datas = data instanceof Array ? data : [data];
  76. const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')} });
  77. for (const od of orgDatas) {
  78. if (od.pre_used) throw '往期已经计量,不可删除';
  79. }
  80. await this.db.delete(this.tableName, {id: datas});
  81. return datas;
  82. }
  83. async _updateDatas (data) {
  84. const datas = data instanceof Array ? data : [data];
  85. const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});
  86. const uDatas = [];
  87. for (const d of datas) {
  88. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  89. if (!od) continue;
  90. const nd = {id: od.id};
  91. if (d.name !== undefined) {
  92. if (od.pre_used) throw '往期已使用,不可修改名称';
  93. nd.name = d.name;
  94. }
  95. if (d.order !== undefined) nd.order = d.order;
  96. if (d.total_price !== undefined) {
  97. if (od.pre_used) throw '往期已使用,不可修改金额';
  98. nd.total_price = this.ctx.helper.round(d.total_price, this.ctx.tender.info.decimal.tp);
  99. }
  100. if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, this.ctx.tender.info.decimal.tp);
  101. if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);
  102. if (d.memo !== undefined) nd.memo = d.memo;
  103. uDatas.push(nd);
  104. }
  105. if (uDatas.length > 0) {
  106. await this.db.updateRows(this.tableName, uDatas);
  107. return uDatas;
  108. } else {
  109. return [];
  110. }
  111. }
  112. async updateDatas(data) {
  113. const result = {add: [], del: [], update: []};
  114. try {
  115. if (data.add) {
  116. result.add = await this._addDatas(data.add);
  117. }
  118. if (data.update) {
  119. result.update = await this._updateDatas(data.update);
  120. }
  121. if (data.del) {
  122. result.del = await this._delDatas(data.del);
  123. }
  124. return result;
  125. } catch (err) {
  126. if (err.stack) {
  127. throw err;
  128. } else {
  129. result.err = err.toString();
  130. return result;
  131. }
  132. }
  133. }
  134. async updateHistory(stage, transaction) {
  135. const datas = await this.getStageData(stage.id);
  136. if (datas.length === 0) return;
  137. const filter = {stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder};
  138. const updateDatas = [];
  139. for (const d of datas) {
  140. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  141. const his = this.ctx.helper._.find(datas, filter);
  142. if (his) {
  143. his.tp = d.tp;
  144. if (d.sid === d.add_sid) his.total_price = d.total_price;
  145. } else {
  146. const nHis = { stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp };
  147. if (d.sid === d.add_sid) his.total_price = d.total_price;
  148. history.push(nHis);
  149. }
  150. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  151. }
  152. await transaction.updateRows(this.tableName, updateDatas);
  153. }
  154. async addInitialStageData(stage, preStage, transaction) {
  155. if (!stage || !preStage) {
  156. throw '标段数据有误';
  157. }
  158. const preDatas = await this.getStageData(preStage.id);
  159. if (preDatas.length > 0) {
  160. for (const pd of preDatas) {
  161. delete pd.id;
  162. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.tp);
  163. delete pd.tp;
  164. pd.sid = stage.id;
  165. }
  166. const result = await transaction.insert(this.tableName, preDatas);
  167. return result.affectedRows === preDatas.length;
  168. } else {
  169. return true;
  170. }
  171. }
  172. }
  173. return StageOther;
  174. };