stage_other.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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, cancel = false) {
  23. const data = await this.getAllDataByCondition({where: { sid: stage.id }});
  24. if (!cancel && stage && stage.readOnly && !this.ctx.tender.isTourist && 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(tid, 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, tid];
  42. const data = await this.db.query(sql, sqlParam);
  43. return data;
  44. }
  45. async getEndStageData(tid, 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, tid];
  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 updateHistory4TimesOrder(stage, times, order, transaction) {
  153. const datas = await this.getStageData(stage);
  154. if (datas.length === 0) return;
  155. const updateDatas = [];
  156. for (const d of datas) {
  157. for (const curOrder of order) {
  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 !== undefined && x.sorder !== null && x.sorder === curOrder;
  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: times, sorder: 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. }
  174. await transaction.updateRows(this.tableName, updateDatas);
  175. }
  176. async updateHistory(stage, transaction) {
  177. await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder], transaction);
  178. }
  179. async updateHistory4CheckAgain(stage, transaction) {
  180. await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder + 1], transaction);
  181. }
  182. async addInitialStageData(stage, preStage, transaction) {
  183. if (!stage || !preStage) {
  184. throw '标段数据有误';
  185. }
  186. const preDatas = await this.getAllDataByCondition({
  187. columns: ['uuid', 'tid', 'add_uid', 'add_sid', 'add_time', 'name', 'o_type', 'tp', 'total_price', 'order', 'memo', 'real_time'],
  188. where: { sid: preStage.id }
  189. });
  190. if (preDatas.length > 0) {
  191. for (const pd of preDatas) {
  192. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.tp);
  193. delete pd.tp;
  194. pd.sid = stage.id;
  195. pd.sorder = stage.order;
  196. }
  197. await transaction.delete(this.tableName, { sid: stage.id });
  198. const result = await transaction.insert(this.tableName, preDatas);
  199. return result.affectedRows === preDatas.length;
  200. } else {
  201. return true;
  202. }
  203. }
  204. async deleteStageTimesData(sid, times, transaction) {
  205. const datas = await this.getAllDataByCondition({where: { sid: sid }});
  206. if (datas.length === 0) return;
  207. const updateDatas = [];
  208. for (const d of datas) {
  209. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  210. const his = history.filter(function (x) {
  211. return x.stimes && x.stimes < times;
  212. });
  213. his.sort(function (x, y) {
  214. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  215. });
  216. updateDatas.push({
  217. id: d.id,
  218. shistory: JSON.stringify(his),
  219. tp: his.length > 0 ? his[his.length - 1].tp : null,
  220. });
  221. }
  222. await transaction.updateRows(this.tableName, updateDatas);
  223. }
  224. async getSumTp(stage) {
  225. const stages = stage instanceof Array ? stage : [stage];
  226. const condition = {};
  227. condition.sid = stage.length === 1 ? stage[0].id : stages.map(x => { return x.id; }).join(',');
  228. const sql = `SELECT SUM(tp) AS sum_tp FROM ${this.tableName} ` + this.ctx.helper.whereSql(condition);
  229. return await this.db.queryOne(sql);
  230. }
  231. }
  232. return StageOther;
  233. };