stage_other.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 _insertDatas(data, result) {
  87. const select = await this.getDataById(data.select);
  88. if (!select) throw '选中的数据不存在';
  89. const insertData = [];
  90. for (let i = 0; i < data.count; i++) {
  91. insertData.push({
  92. uuid: this.uuid.v4(),
  93. add_sid: this.ctx.stage.id,
  94. add_uid: this.ctx.session.sessionUser.accountId,
  95. add_time: new Date(),
  96. tid: this.ctx.tender.id,
  97. sid: this.ctx.stage.id,
  98. sorder: this.ctx.stage.order,
  99. name: '',
  100. order: select.order + i,
  101. });
  102. }
  103. const transaction = await this.db.beginTransaction();
  104. try {
  105. await transaction.query('Update ?? SET `order` = `order` + ? WHERE sid = ? and `order` >= ?', [this.tableName, data.count, this.ctx.stage.id, select.order]);
  106. await transaction.insert(this.tableName, insertData);
  107. await transaction.commit();
  108. } catch(err) {
  109. this.ctx.log(err);
  110. await transaction.rollback();
  111. throw '插入数据错误';
  112. }
  113. result.add = await this.getAllDataByCondition({
  114. where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') }
  115. });
  116. result.update = await this.db.query('SELECT * FROM ?? where sid = ? and `order` >= ?', [this.tableName, this.ctx.stage.id, select.order + data.count]);
  117. }
  118. async _delDatas (data) {
  119. const datas = data instanceof Array ? data : [data];
  120. const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')} });
  121. for (const od of orgDatas) {
  122. if (od.pre_used) throw '往期已经计量,不可删除';
  123. }
  124. await this.db.delete(this.tableName, {id: datas});
  125. return datas;
  126. }
  127. async _updateDatas (data) {
  128. const tpDecimal = this.ctx.tender.info.decimal.extra
  129. ? this.ctx.tender.info.decimal.extraTp
  130. : this.ctx.tender.info.decimal.tp;
  131. const datas = data instanceof Array ? data : [data];
  132. const orgDatas = await this.getAllDataByCondition({
  133. where: { sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id') }
  134. });
  135. const uDatas = [];
  136. for (const d of datas) {
  137. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  138. if (!od) continue;
  139. const nd = {id: od.id};
  140. if (d.name !== undefined) {
  141. if (od.pre_used) throw '往期已使用,不可修改名称';
  142. nd.name = d.name;
  143. }
  144. if (d.order !== undefined) nd.order = d.order;
  145. if (d.o_type !== undefined) nd.o_type = d.o_type;
  146. if (d.total_price !== undefined) {
  147. if (od.pre_used) throw '往期已使用,不可修改金额';
  148. nd.total_price = this.ctx.helper.round(d.total_price, tpDecimal);
  149. }
  150. if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, tpDecimal);
  151. if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);
  152. if (d.memo !== undefined) nd.memo = d.memo;
  153. uDatas.push(nd);
  154. }
  155. if (uDatas.length > 0) {
  156. await this.db.updateRows(this.tableName, uDatas);
  157. return uDatas;
  158. } else {
  159. return [];
  160. }
  161. }
  162. async updateDatas(data) {
  163. const result = {add: [], del: [], update: []};
  164. try {
  165. if (data.add) {
  166. result.add = await this._addDatas(data.add);
  167. }
  168. if (data.update) {
  169. result.update = await this._updateDatas(data.update);
  170. }
  171. if (data.del) {
  172. result.del = await this._delDatas(data.del);
  173. }
  174. if (data.insert) await this._insertDatas(data.insert, result);
  175. return result;
  176. } catch (err) {
  177. if (err.stack) {
  178. throw err;
  179. } else {
  180. result.err = err.toString();
  181. return result;
  182. }
  183. }
  184. }
  185. async updateHistory4TimesOrder(stage, times, order, transaction) {
  186. const datas = await this.getStageData(stage);
  187. if (datas.length === 0) return;
  188. const updateDatas = [];
  189. for (const d of datas) {
  190. for (const curOrder of order) {
  191. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  192. const his = history.find(function (x) {
  193. return x.stimes && x.stimes === times
  194. && x.sorder !== undefined && x.sorder !== null && x.sorder === curOrder;
  195. });
  196. if (his) {
  197. his.tp = d.tp;
  198. if (d.sid === d.add_sid) his.total_price = d.total_price;
  199. } else {
  200. const nHis = { stimes: times, sorder: curOrder, tp: d.tp };
  201. if (d.sid === d.add_sid) nHis.total_price = d.total_price;
  202. history.push(nHis);
  203. }
  204. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  205. }
  206. }
  207. await transaction.updateRows(this.tableName, updateDatas);
  208. }
  209. async updateHistory(stage, transaction) {
  210. await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder], transaction);
  211. }
  212. async updateHistory4CheckAgain(stage, transaction) {
  213. await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder + 1], transaction);
  214. }
  215. async addInitialStageData(stage, preStage, transaction) {
  216. if (!stage || !preStage) {
  217. throw '标段数据有误';
  218. }
  219. const preDatas = await this.getAllDataByCondition({
  220. columns: ['uuid', 'tid', 'add_uid', 'add_sid', 'add_time', 'name', 'o_type', 'tp', 'total_price', 'order', 'memo', 'real_time'],
  221. where: { sid: preStage.id }
  222. });
  223. if (preDatas.length > 0) {
  224. for (const pd of preDatas) {
  225. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.tp);
  226. delete pd.tp;
  227. pd.sid = stage.id;
  228. pd.sorder = stage.order;
  229. }
  230. await transaction.delete(this.tableName, { sid: stage.id });
  231. const result = await transaction.insert(this.tableName, preDatas);
  232. return result.affectedRows === preDatas.length;
  233. } else {
  234. return true;
  235. }
  236. }
  237. async deleteStageTimesData(sid, times, transaction) {
  238. const datas = await this.getAllDataByCondition({where: { sid: sid }});
  239. if (datas.length === 0) return;
  240. const updateDatas = [];
  241. for (const d of datas) {
  242. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  243. const his = history.filter(function (x) {
  244. return x.stimes && x.stimes < times;
  245. });
  246. his.sort(function (x, y) {
  247. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  248. });
  249. updateDatas.push({
  250. id: d.id,
  251. shistory: JSON.stringify(his),
  252. tp: his.length > 0 ? his[his.length - 1].tp : null,
  253. });
  254. }
  255. await transaction.updateRows(this.tableName, updateDatas);
  256. }
  257. async getSumTp(stage) {
  258. const stages = stage instanceof Array ? stage : [stage];
  259. const condition = {};
  260. condition.sid = stage.length === 1 ? stage[0].id : stages.map(x => { return x.id; }).join(',');
  261. const sql = `SELECT SUM(tp) AS sum_tp FROM ${this.tableName} ` + this.ctx.helper.whereSql(condition);
  262. return await this.db.queryOne(sql);
  263. }
  264. }
  265. return StageOther;
  266. };