stage_other.js 9.0 KB

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