stage_bonus.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 StageBonus 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_bonus';
  21. }
  22. async getStageData(sid) {
  23. const data = await this.getAllDataByCondition({where: { sid: sid }});
  24. if (this.ctx.stage && this.ctx.stage.readOnly && this.ctx.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: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder
  29. });
  30. d.tp = h ? h.tp : null;
  31. }
  32. }
  33. return data;
  34. }
  35. async getPreStageData(sorder) {
  36. const sql = 'SELECT * From ' + this.tableName + ' WHERE sorder < ? And tid = ?';
  37. const sqlParam = [sorder, this.ctx.tender.id];
  38. const data = await this.db.query(sql, sqlParam);
  39. return data;
  40. }
  41. async getEndStageData(sorder) {
  42. const sql = 'SELECT * From ' + this.tableName + ' WHERE sorder <= ? And tid = ?';
  43. const sqlParam = [sorder, this.ctx.tender.id];
  44. const data = await this.db.query(sql, sqlParam);
  45. return data;
  46. }
  47. async _addDatas(data) {
  48. const datas = data instanceof Array ? data : [data];
  49. const insertData = [];
  50. for (const d of datas) {
  51. if (!d.name || !d.order) throw '新增甲供材料,提交的数据错误';
  52. const nd = {
  53. id: this.uuid.v4(),
  54. tid: this.ctx.tender.id,
  55. sid: this.ctx.stage.id,
  56. sorder: this.ctx.stage.order,
  57. uid: this.ctx.session.sessionUser.accountId,
  58. create_time: new Date(),
  59. name: d.name,
  60. order: d.order,
  61. };
  62. nd.tp = d.tp ? this.ctx.helper.round(d.to, this.ctx.tender.info.decimal.tp) : 0;
  63. nd.code = d.code ? d.code: null;
  64. nd.proof = d.proof ? d.proof : null;
  65. nd.real_time = d.real_time ? d.real_time : null;
  66. nd.memo = d.memo ? d.memo : null;
  67. insertData.push(nd);
  68. }
  69. await this.db.insert(this.tableName, insertData);
  70. return insertData;
  71. }
  72. async _delDatas (data) {
  73. const datas = data instanceof Array ? data : [data];
  74. const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')}});
  75. for (const od of orgDatas) {
  76. console.log(od);
  77. console.log(this.ctx.stage.id);
  78. if (od.sid !== this.ctx.stage.id) throw '非本期新增数据,不可删除';
  79. }
  80. await this.db.delete(this.tableName, {id: datas});
  81. return datas;
  82. }
  83. async _updateDatas (data) {
  84. const datas = data instanceof Array ? data : [data];
  85. const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});
  86. const uDatas = [];
  87. for (const d of datas) {
  88. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  89. if (!od) continue;
  90. const nd = {id: od.id};
  91. if (d.name !== undefined) nd.name = d.name;
  92. if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, this.ctx.tender.info.decimal.tp);
  93. if (d.code !== undefined) nd.code = d.code;
  94. if (d.proof !== undefined) nd.proof = d.proof;
  95. if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);
  96. if (d.memo !== undefined) nd.memo = d.memo;
  97. if (d.order !== undefined) nd.order = d.order;
  98. uDatas.push(nd);
  99. console.log(nd);
  100. }
  101. if (uDatas.length > 0) {
  102. await this.db.updateRows(this.tableName, uDatas);
  103. return uDatas;
  104. } else {
  105. return [];
  106. }
  107. }
  108. async updateDatas(data) {
  109. const result = {add: [], del: [], update: []};
  110. try {
  111. if (data.add) {
  112. result.add = await this._addDatas(data.add);
  113. }
  114. if (data.update) {
  115. result.update = await this._updateDatas(data.update);
  116. }
  117. if (data.del) {
  118. result.del = await this._delDatas(data.del);
  119. }
  120. return result;
  121. } catch (err) {
  122. if (err) result.err = err;
  123. return result;
  124. }
  125. }
  126. async updateHistory(stage, transaction) {
  127. const datas = await this.getStageData(stage.id);
  128. if (datas.length === 0) return;
  129. const filter = {stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder};
  130. const updateDatas = [];
  131. for (const d of datas) {
  132. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  133. const his = this.ctx.helper._.find(datas, filter);
  134. if (his) {
  135. his.tp = d.tp;
  136. } else {
  137. history.push({ stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp });
  138. }
  139. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  140. }
  141. await transaction.updateRows(this.tableName, updateDatas);
  142. }
  143. }
  144. return StageBonus;
  145. };