stage_bonus.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 = ? ORDER BY `sorder`, `order`';
  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. nd.doc_co = d.doc_co ? d.doc_co : null;
  68. insertData.push(nd);
  69. }
  70. await this.db.insert(this.tableName, insertData);
  71. return insertData;
  72. }
  73. async _delDatas (data) {
  74. const datas = data instanceof Array ? data : [data];
  75. const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')}});
  76. for (const od of orgDatas) {
  77. console.log(od);
  78. console.log(this.ctx.stage.id);
  79. if (od.sid !== this.ctx.stage.id) throw '非本期新增数据,不可删除';
  80. }
  81. await this.db.delete(this.tableName, {id: datas});
  82. return datas;
  83. }
  84. async _updateDatas (data) {
  85. const datas = data instanceof Array ? data : [data];
  86. const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});
  87. const uDatas = [];
  88. for (const d of datas) {
  89. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  90. if (!od) continue;
  91. const nd = {id: od.id};
  92. if (d.name !== undefined) nd.name = d.name;
  93. if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, this.ctx.tender.info.decimal.tp);
  94. if (d.code !== undefined) nd.code = d.code;
  95. if (d.proof !== undefined) nd.proof = d.proof;
  96. if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);
  97. if (d.memo !== undefined) nd.memo = d.memo;
  98. if (d.order !== undefined) nd.order = d.order;
  99. if (d.doc_co !== undefined) nd.doc_co = d.doc_co;
  100. uDatas.push(nd);
  101. console.log(nd);
  102. }
  103. if (uDatas.length > 0) {
  104. await this.db.updateRows(this.tableName, uDatas);
  105. return uDatas;
  106. } else {
  107. return [];
  108. }
  109. }
  110. async updateDatas(data) {
  111. const result = {add: [], del: [], update: []};
  112. try {
  113. if (data.add) {
  114. result.add = await this._addDatas(data.add);
  115. }
  116. if (data.update) {
  117. result.update = await this._updateDatas(data.update);
  118. }
  119. if (data.del) {
  120. result.del = await this._delDatas(data.del);
  121. }
  122. return result;
  123. } catch (err) {
  124. if (err) result.err = err;
  125. return result;
  126. }
  127. }
  128. async updateHistory(stage, transaction) {
  129. const datas = await this.getStageData(stage.id);
  130. if (datas.length === 0) return;
  131. const filter = {stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder};
  132. const updateDatas = [];
  133. for (const d of datas) {
  134. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  135. const his = this.ctx.helper._.find(datas, filter);
  136. if (his) {
  137. his.tp = d.tp;
  138. } else {
  139. history.push({ stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp });
  140. }
  141. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  142. }
  143. await transaction.updateRows(this.tableName, updateDatas);
  144. }
  145. }
  146. return StageBonus;
  147. };