stage_other.js 6.0 KB

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