stage_other.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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({
  101. where: { sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id') }
  102. });
  103. const uDatas = [];
  104. for (const d of datas) {
  105. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  106. if (!od) continue;
  107. const nd = {id: od.id};
  108. if (d.name !== undefined) {
  109. if (od.pre_used) throw '往期已使用,不可修改名称';
  110. nd.name = d.name;
  111. }
  112. if (d.order !== undefined) nd.order = d.order;
  113. if (d.o_type !== undefined) nd.o_type = d.o_type;
  114. if (d.total_price !== undefined) {
  115. if (od.pre_used) throw '往期已使用,不可修改金额';
  116. nd.total_price = this.ctx.helper.round(d.total_price, tpDecimal);
  117. }
  118. if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, tpDecimal);
  119. if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);
  120. if (d.memo !== undefined) nd.memo = d.memo;
  121. uDatas.push(nd);
  122. }
  123. if (uDatas.length > 0) {
  124. await this.db.updateRows(this.tableName, uDatas);
  125. return uDatas;
  126. } else {
  127. return [];
  128. }
  129. }
  130. async updateDatas(data) {
  131. const result = {add: [], del: [], update: []};
  132. try {
  133. if (data.add) {
  134. result.add = await this._addDatas(data.add);
  135. }
  136. if (data.update) {
  137. result.update = await this._updateDatas(data.update);
  138. }
  139. if (data.del) {
  140. result.del = await this._delDatas(data.del);
  141. }
  142. return result;
  143. } catch (err) {
  144. if (err.stack) {
  145. throw err;
  146. } else {
  147. result.err = err.toString();
  148. return result;
  149. }
  150. }
  151. }
  152. async updateHistory(stage, transaction) {
  153. const datas = await this.getStageData(stage);
  154. if (datas.length === 0) return;
  155. const updateDatas = [];
  156. const times = this.ctx.stage.curTimes, order = this.ctx.stage.curOrder;
  157. for (const d of datas) {
  158. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  159. const his = history.find(function (x) {
  160. return x.stimes && x.stimes === times
  161. && x.sorder && x.sorder === order;
  162. });
  163. if (his) {
  164. his.tp = d.tp;
  165. if (d.sid === d.add_sid) his.total_price = d.total_price;
  166. } else {
  167. const nHis = { stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp };
  168. if (d.sid === d.add_sid) nHis.total_price = d.total_price;
  169. history.push(nHis);
  170. }
  171. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  172. }
  173. await transaction.updateRows(this.tableName, updateDatas);
  174. }
  175. async addInitialStageData(stage, preStage, transaction) {
  176. if (!stage || !preStage) {
  177. throw '标段数据有误';
  178. }
  179. const preDatas = await this.getAllDataByCondition({
  180. columns: ['uuid', 'tid', 'add_uid', 'add_sid', 'add_time', 'name', 'o_type', 'tp', 'total_price', 'order', 'memo', 'real_time'],
  181. where: { sid: preStage.id }
  182. });
  183. if (preDatas.length > 0) {
  184. for (const pd of preDatas) {
  185. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.tp);
  186. delete pd.tp;
  187. pd.sid = stage.id;
  188. pd.sorder = stage.order;
  189. }
  190. const result = await transaction.insert(this.tableName, preDatas);
  191. return result.affectedRows === preDatas.length;
  192. } else {
  193. return true;
  194. }
  195. }
  196. async deleteStageTimesData(sid, times, transaction) {
  197. const datas = await this.getAllDataByCondition({where: { sid: sid }});
  198. if (datas.length === 0) return;
  199. const updateDatas = [];
  200. for (const d of datas) {
  201. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  202. const his = history.filter(function (x) {
  203. return x.stimes && x.stimes < times;
  204. });
  205. his.sort(function (x, y) {
  206. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  207. });
  208. updateDatas.push({
  209. id: d.id,
  210. shistory: JSON.stringify(his),
  211. tp: his.length > 0 ? his[his.length - 1].tp : null,
  212. });
  213. }
  214. await transaction.updateRows(this.tableName, updateDatas);
  215. }
  216. }
  217. return StageOther;
  218. };