stage_other.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.o_type) nd.o_type = d.o_type;
  64. if (d.total_price) nd.total_price = this.ctx.helper.round(d.total_price, this.ctx.tender.info.decimal.tp);
  65. if (d.tp) nd.tp = this.ctx.helper.round(d.tp, this.ctx.tender.info.decimal.tp);
  66. if (d.real_time) nd.real_time = d.real_time;
  67. if (d.memo) nd.memo = d.memo;
  68. insertData.push(nd);
  69. }
  70. await this.db.insert(this.tableName, insertData);
  71. return await this.getAllDataByCondition({
  72. where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') }
  73. });
  74. }
  75. async _delDatas (data) {
  76. const datas = data instanceof Array ? data : [data];
  77. const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')} });
  78. for (const od of orgDatas) {
  79. if (od.pre_used) throw '往期已经计量,不可删除';
  80. }
  81. await this.db.delete(this.tableName, {id: datas});
  82. return datas;
  83. }
  84. async _updateDatas (data) {
  85. const datas = data instanceof Array ? data : [data];
  86. const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});
  87. const uDatas = [];
  88. for (const d of datas) {
  89. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  90. if (!od) continue;
  91. const nd = {id: od.id};
  92. if (d.name !== undefined) {
  93. if (od.pre_used) throw '往期已使用,不可修改名称';
  94. nd.name = d.name;
  95. }
  96. if (d.order !== undefined) nd.order = d.order;
  97. if (d.o_type !== undefined) nd.o_type = d.o_type;
  98. if (d.total_price !== undefined) {
  99. if (od.pre_used) throw '往期已使用,不可修改金额';
  100. nd.total_price = this.ctx.helper.round(d.total_price, this.ctx.tender.info.decimal.tp);
  101. }
  102. if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, this.ctx.tender.info.decimal.tp);
  103. if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);
  104. if (d.memo !== undefined) nd.memo = d.memo;
  105. uDatas.push(nd);
  106. }
  107. if (uDatas.length > 0) {
  108. await this.db.updateRows(this.tableName, uDatas);
  109. return uDatas;
  110. } else {
  111. return [];
  112. }
  113. }
  114. async updateDatas(data) {
  115. const result = {add: [], del: [], update: []};
  116. try {
  117. if (data.add) {
  118. result.add = await this._addDatas(data.add);
  119. }
  120. if (data.update) {
  121. result.update = await this._updateDatas(data.update);
  122. }
  123. if (data.del) {
  124. result.del = await this._delDatas(data.del);
  125. }
  126. return result;
  127. } catch (err) {
  128. if (err.stack) {
  129. throw err;
  130. } else {
  131. result.err = err.toString();
  132. return result;
  133. }
  134. }
  135. }
  136. async updateHistory(stage, transaction) {
  137. const datas = await this.getStageData(stage.id);
  138. if (datas.length === 0) return;
  139. const filter = {stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder};
  140. const updateDatas = [];
  141. for (const d of datas) {
  142. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  143. const his = this.ctx.helper._.find(datas, filter);
  144. if (his) {
  145. his.tp = d.tp;
  146. if (d.sid === d.add_sid) his.total_price = d.total_price;
  147. } else {
  148. const nHis = { stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp };
  149. if (d.sid === d.add_sid) nHis.total_price = d.total_price;
  150. history.push(nHis);
  151. }
  152. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  153. }
  154. await transaction.updateRows(this.tableName, updateDatas);
  155. }
  156. async addInitialStageData(stage, preStage, transaction) {
  157. if (!stage || !preStage) {
  158. throw '标段数据有误';
  159. }
  160. const preDatas = await this.getStageData(preStage.id);
  161. if (preDatas.length > 0) {
  162. for (const pd of preDatas) {
  163. delete pd.id;
  164. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.tp);
  165. delete pd.tp;
  166. pd.sid = stage.id;
  167. }
  168. const result = await transaction.insert(this.tableName, preDatas);
  169. return result.affectedRows === preDatas.length;
  170. } else {
  171. return true;
  172. }
  173. }
  174. }
  175. return StageOther;
  176. };