stage_other.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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(stage) {
  23. const data = await this.getAllDataByCondition({where: { sid: stage.id }});
  24. if (stage && stage.readOnly && 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: stage.curTimes, sorder: stage.curOrder
  29. });
  30. d.tp = h ? h.tp : null;
  31. }
  32. }
  33. return data;
  34. }
  35. async getPreStageData(sorder) {
  36. const sql = 'SELECT o.uuid, Sum(o.tp) as tp ' +
  37. ' From ' + this.tableName + ' o ' +
  38. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON s.id = o.sid' +
  39. ' WHERE s.order < ? And o.tid = ?' +
  40. ' GROUP By uuid';
  41. const sqlParam = [sorder, this.ctx.tender.id];
  42. const data = await this.db.query(sql, sqlParam);
  43. return data;
  44. }
  45. async getEndStageData(sorder) {
  46. const sql = 'SELECT o.uuid, Sum(o.tp) as tp ' +
  47. ' From ' + this.tableName + ' o ' +
  48. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON s.id = o.sid' +
  49. ' WHERE s.order <= ? And o.tid = ?' +
  50. ' GROUP By uuid';
  51. const sqlParam = [sorder, this.ctx.tender.id];
  52. const data = await this.db.query(sql, sqlParam);
  53. return data;
  54. }
  55. async _addDatas(data) {
  56. const tpDecimal = this.ctx.tender.info.decimal.extra
  57. ? this.ctx.tender.info.decimal.extraTp
  58. : this.ctx.tender.info.decimal.tp;
  59. const datas = data instanceof Array ? data : [data];
  60. const insertData = [];
  61. for (const d of datas) {
  62. if (!d.name || !d.order) throw '新增其他数据,提交的数据错误';
  63. const nd = {
  64. uuid: this.uuid.v4(),
  65. add_sid: this.ctx.stage.id,
  66. add_uid: this.ctx.session.sessionUser.accountId,
  67. add_time: new Date(),
  68. sid: this.ctx.stage.id,
  69. sorder: this.ctx.stage.order,
  70. tid: this.ctx.tender.id,
  71. };
  72. nd.name = d.name;
  73. nd.order = d.order;
  74. if (d.o_type) nd.o_type = d.o_type;
  75. if (d.total_price) nd.total_price = this.ctx.helper.round(d.total_price, tpDecimal);
  76. if (d.tp) nd.tp = this.ctx.helper.round(d.tp, tpDecimal);
  77. if (d.real_time) nd.real_time = d.real_time;
  78. if (d.memo) nd.memo = d.memo;
  79. insertData.push(nd);
  80. }
  81. await this.db.insert(this.tableName, insertData);
  82. return await this.getAllDataByCondition({
  83. where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') }
  84. });
  85. }
  86. async _delDatas (data) {
  87. const datas = data instanceof Array ? data : [data];
  88. const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')} });
  89. for (const od of orgDatas) {
  90. if (od.pre_used) throw '往期已经计量,不可删除';
  91. }
  92. await this.db.delete(this.tableName, {id: datas});
  93. return datas;
  94. }
  95. async _updateDatas (data) {
  96. const tpDecimal = this.ctx.tender.info.decimal.extra
  97. ? this.ctx.tender.info.decimal.extraTp
  98. : this.ctx.tender.info.decimal.tp;
  99. const datas = data instanceof Array ? data : [data];
  100. const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});
  101. const uDatas = [];
  102. for (const d of datas) {
  103. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  104. if (!od) continue;
  105. const nd = {id: od.id};
  106. if (d.name !== undefined) {
  107. if (od.pre_used) throw '往期已使用,不可修改名称';
  108. nd.name = d.name;
  109. }
  110. if (d.order !== undefined) nd.order = d.order;
  111. if (d.o_type !== undefined) nd.o_type = d.o_type;
  112. if (d.total_price !== undefined) {
  113. if (od.pre_used) throw '往期已使用,不可修改金额';
  114. nd.total_price = this.ctx.helper.round(d.total_price, tpDecimal);
  115. }
  116. if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, tpDecimal);
  117. if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);
  118. if (d.memo !== undefined) nd.memo = d.memo;
  119. uDatas.push(nd);
  120. }
  121. if (uDatas.length > 0) {
  122. await this.db.updateRows(this.tableName, uDatas);
  123. return uDatas;
  124. } else {
  125. return [];
  126. }
  127. }
  128. async updateDatas(data) {
  129. const result = {add: [], del: [], update: []};
  130. try {
  131. if (data.add) {
  132. result.add = await this._addDatas(data.add);
  133. }
  134. if (data.update) {
  135. result.update = await this._updateDatas(data.update);
  136. }
  137. if (data.del) {
  138. result.del = await this._delDatas(data.del);
  139. }
  140. return result;
  141. } catch (err) {
  142. if (err.stack) {
  143. throw err;
  144. } else {
  145. result.err = err.toString();
  146. return result;
  147. }
  148. }
  149. }
  150. async updateHistory(stage, transaction) {
  151. const datas = await this.getStageData(stage);
  152. if (datas.length === 0) return;
  153. const updateDatas = [];
  154. const times = this.ctx.stage.curTimes, order = this.ctx.stage.curOrder;
  155. for (const d of datas) {
  156. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  157. const his = history.find(function (x) {
  158. return x.stimes && x.stimes === times
  159. && x.sorder && x.sorder === order;
  160. });
  161. if (his) {
  162. his.tp = d.tp;
  163. if (d.sid === d.add_sid) his.total_price = d.total_price;
  164. } else {
  165. const nHis = { stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp };
  166. if (d.sid === d.add_sid) nHis.total_price = d.total_price;
  167. history.push(nHis);
  168. }
  169. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  170. }
  171. await transaction.updateRows(this.tableName, updateDatas);
  172. }
  173. async addInitialStageData(stage, preStage, transaction) {
  174. if (!stage || !preStage) {
  175. throw '标段数据有误';
  176. }
  177. const preDatas = await this.getAllDataByCondition({
  178. columns: ['uuid', 'tid', 'add_uid', 'add_sid', 'add_time', 'name', 'o_type', 'total_price', 'order', 'memo', 'real_time'],
  179. where: { sid: preStage.id }
  180. });
  181. if (preDatas.length > 0) {
  182. for (const pd of preDatas) {
  183. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.tp);
  184. pd.sid = stage.id;
  185. pd.sorder = stage.order;
  186. }
  187. const result = await transaction.insert(this.tableName, preDatas);
  188. return result.affectedRows === preDatas.length;
  189. } else {
  190. return true;
  191. }
  192. }
  193. async deleteStageTimesData(sid, times, transaction) {
  194. const datas = await this.getAllDataByCondition({where: { sid: sid }});
  195. if (datas.length === 0) return;
  196. const updateDatas = [];
  197. for (const d of datas) {
  198. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  199. const his = history.filter(function (x) {
  200. return x.stimes && x.stimes < times;
  201. });
  202. his.sort(function (x, y) {
  203. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  204. });
  205. updateDatas.push({
  206. id: d.id,
  207. shistory: JSON.stringify(his),
  208. tp: his.length > 0 ? his[his.length - 1].tp : null,
  209. });
  210. }
  211. await transaction.updateRows(this.tableName, updateDatas);
  212. }
  213. }
  214. return StageOther;
  215. };