stage_bonus.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use strict';
  2. /**
  3. * 奖罚金
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class StageBonus extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'stage_bonus';
  20. }
  21. async getStageData(sid) {
  22. const data = await this.getAllDataByCondition({where: { sid: sid }});
  23. return data;
  24. }
  25. async getPreStageData(sorder) {
  26. const sql = 'SELECT * From ' + this.tableName + ' WHERE sorder < ? And tid = ?';
  27. const sqlParam = [sorder, this.ctx.tender.id];
  28. const data = await this.db.query(sql, sqlParam);
  29. return data;
  30. }
  31. async getEndStageData(sorder) {
  32. const sql = 'SELECT * From ' + this.tableName + ' WHERE sorder <= ? And tid = ?';
  33. const sqlParam = [sorder, this.ctx.tender.id];
  34. const data = await this.db.query(sql, sqlParam);
  35. return data;
  36. }
  37. async _addDatas(data) {
  38. const datas = data instanceof Array ? data : [data];
  39. const insertData = [];
  40. for (const d of datas) {
  41. if (!d.name || !d.order) throw '新增甲供材料,提交的数据错误';
  42. const nd = {
  43. id: this.uuid.v4(),
  44. tid: this.ctx.tender.id,
  45. sid: this.ctx.stage.id,
  46. sorder: this.ctx.stage.order,
  47. uid: this.ctx.session.sessionUser.accountId,
  48. create_time: new Date(),
  49. name: d.name,
  50. order: d.order,
  51. };
  52. nd.tp = d.tp ? this.ctx.helper.round(d.to, this.ctx.tender.info.decimal.tp) : 0;
  53. nd.code = d.code ? d.code: null;
  54. nd.proof = d.proof ? d.proof : null;
  55. nd.real_time = d.real_time ? d.real_time : null;
  56. nd.memo = d.memo ? d.memo : null;
  57. insertData.push(nd);
  58. }
  59. await this.db.insert(this.tableName, insertData);
  60. return insertData;
  61. }
  62. async _delDatas (data) {
  63. const datas = data instanceof Array ? data : [data];
  64. const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});
  65. for (const od of orgDatas) {
  66. if (od.sid !== this.ctx.stage.id) throw '非本期新增数据,不可删除';
  67. }
  68. await this.db.delete(this.tableName, {id: datas});
  69. return datas;
  70. }
  71. async _updateDatas (data) {
  72. const datas = data instanceof Array ? data : [data];
  73. const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});
  74. const uDatas = [];
  75. for (const d of datas) {
  76. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  77. if (!od) continue;
  78. const nd = {id: od.id};
  79. if (d.name) nd.name = d.name;
  80. if (d.tp) nd.tp = this.ctx.helper.round(d.tp, this.ctx.tender.info.decimal.tp);
  81. if (d.code) nd.code = d.code;
  82. if (d.proof) nd.proof = d.proof;
  83. if (d.real_time) nd.real_time = d.real_time;
  84. if (d.memo) nd.memo = d.memo;
  85. if (d.order) nd.order = d.order;
  86. uDatas.push(nd);
  87. console.log(nd);
  88. }
  89. if (uDatas.length > 0) {
  90. await this.db.updateRows(this.tableName, uDatas);
  91. return uDatas;
  92. } else {
  93. return [];
  94. }
  95. }
  96. async updateDatas(data) {
  97. const result = {add: [], del: [], update: []};
  98. try {
  99. if (data.add) {
  100. result.add = await this._addDatas(data.add);
  101. }
  102. if (data.update) {
  103. result.update = await this._updateDatas(data.update);
  104. }
  105. if (data.del) {
  106. result.del = await this._delDatas(data.del);
  107. }
  108. return result;
  109. } catch (err) {
  110. if (err) result.err = err;
  111. return result;
  112. }
  113. }
  114. async updateHistory(stage, transaction) {
  115. const datas = await this.getStageData(stage.id);
  116. if (datas.length === 0) return;
  117. const filter = {stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder};
  118. const updateDatas = [];
  119. for (const d of datas) {
  120. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  121. const his = this.ctx.helper._.find(datas, filter);
  122. if (his) {
  123. his.tp = d.tp;
  124. } else {
  125. history.push({ stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp });
  126. }
  127. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  128. }
  129. await transaction.updateRows(this.tableName, updateDatas);
  130. }
  131. }
  132. return StageBonus;
  133. };