stage_bonus.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. _parseData(data) {
  23. if (!data) return;
  24. const datas = data instanceof Array ? data : [data];
  25. for (const d of datas) {
  26. if (d.proof_file) {
  27. d.proof_file = JSON.parse(d.proof_file);
  28. } else {
  29. d.proof_file = [];
  30. }
  31. }
  32. }
  33. async getStageData(sid) {
  34. const data = await this.getAllDataByCondition({where: { sid: sid }});
  35. if (this.ctx.stage && this.ctx.stage.readOnly && this.ctx.stage.status !== auditConst.status.checked) {
  36. for (const d of data) {
  37. const his = d.shistory ? JSON.parse(d.shistory) : [];
  38. const h = this.ctx.helper._.find(his, {
  39. stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder
  40. });
  41. d.tp = h ? h.tp : null;
  42. }
  43. }
  44. this._parseData(data);
  45. return data;
  46. }
  47. async getPreStageData(sorder) {
  48. const sql = 'SELECT * From ' + this.tableName + ' WHERE sorder < ? And tid = ?';
  49. const sqlParam = [sorder, this.ctx.tender.id];
  50. const data = await this.db.query(sql, sqlParam);
  51. this._parseData(data);
  52. return data;
  53. }
  54. async getEndStageData(sorder) {
  55. const sql = 'SELECT * From ' + this.tableName + ' WHERE sorder <= ? And tid = ? ORDER BY `sorder`, `order`';
  56. const sqlParam = [sorder, this.ctx.tender.id];
  57. const data = await this.db.query(sql, sqlParam);
  58. this._parseData(data);
  59. return data;
  60. }
  61. async getStageDataById(bonusId) {
  62. const data = await this.getAllDataByCondition({ where: { id: bonusId } });
  63. this._parseData(data);
  64. return data[0];
  65. }
  66. async _addDatas(data) {
  67. const datas = data instanceof Array ? data : [data];
  68. const insertData = [];
  69. for (const d of datas) {
  70. if (!d.name || !d.order) throw '新增甲供材料,提交的数据错误';
  71. const nd = {
  72. id: this.uuid.v4(),
  73. tid: this.ctx.tender.id,
  74. sid: this.ctx.stage.id,
  75. sorder: this.ctx.stage.order,
  76. uid: this.ctx.session.sessionUser.accountId,
  77. create_time: new Date(),
  78. name: d.name,
  79. order: d.order,
  80. };
  81. nd.b_type = d.b_type ? d.b_type : null;
  82. nd.tp = d.tp ? this.ctx.helper.round(d.to, this.ctx.tender.info.decimal.tp) : 0;
  83. nd.code = d.code ? d.code: null;
  84. nd.proof = d.proof ? d.proof : null;
  85. nd.real_time = d.real_time ? d.real_time : null;
  86. nd.memo = d.memo ? d.memo : null;
  87. nd.doc_co = d.doc_co ? d.doc_co : null;
  88. insertData.push(nd);
  89. }
  90. await this.db.insert(this.tableName, insertData);
  91. return insertData;
  92. }
  93. async _delDatas (data) {
  94. const datas = data instanceof Array ? data : [data];
  95. const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')}});
  96. for (const od of orgDatas) {
  97. console.log(od);
  98. console.log(this.ctx.stage.id);
  99. if (od.sid !== this.ctx.stage.id) throw '非本期新增数据,不可删除';
  100. }
  101. await this.db.delete(this.tableName, {id: datas});
  102. return datas;
  103. }
  104. async _updateDatas (data) {
  105. const datas = data instanceof Array ? data : [data];
  106. const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});
  107. const uDatas = [];
  108. for (const d of datas) {
  109. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  110. if (!od) continue;
  111. const nd = {id: od.id};
  112. if (d.name !== undefined) nd.name = d.name;
  113. if (d.b_type !== undefined) nd.b_type = d.b_type;
  114. if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, this.ctx.tender.info.decimal.tp);
  115. if (d.code !== undefined) nd.code = d.code;
  116. if (d.proof !== undefined) nd.proof = d.proof;
  117. if (d.proof_file !== undefined) nd.proof_file = JSON.stringify(d.proof_file);
  118. if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);
  119. if (d.memo !== undefined) nd.memo = d.memo;
  120. if (d.order !== undefined) nd.order = d.order;
  121. if (d.doc_co !== undefined) nd.doc_co = d.doc_co;
  122. uDatas.push(nd);
  123. }
  124. if (uDatas.length > 0) {
  125. await this.db.updateRows(this.tableName, uDatas);
  126. return uDatas;
  127. } else {
  128. return [];
  129. }
  130. }
  131. async updateDatas(data) {
  132. const result = {add: [], del: [], update: []};
  133. try {
  134. if (data.add) {
  135. result.add = await this._addDatas(data.add);
  136. }
  137. if (data.update) {
  138. result.update = await this._updateDatas(data.update);
  139. }
  140. if (data.del) {
  141. result.del = await this._delDatas(data.del);
  142. }
  143. return result;
  144. } catch (err) {
  145. if (err) result.err = err;
  146. return result;
  147. }
  148. }
  149. async updateHistory(stage, transaction) {
  150. const datas = await this.getStageData(stage.id);
  151. if (datas.length === 0) return;
  152. const updateDatas = [];
  153. const times = this.ctx.stage.curTimes, order = this.ctx.stage.curOrder;
  154. for (const d of datas) {
  155. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  156. const his = history.find(function (x) {
  157. return x.stimes && x.stimes === times
  158. && x.sorder && x.sorder === order;
  159. });
  160. if (his) {
  161. his.tp = d.tp;
  162. } else {
  163. history.push({ stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp });
  164. }
  165. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  166. }
  167. await transaction.updateRows(this.tableName, updateDatas);
  168. }
  169. async deleteStageTimesData(sid, times, transaction) {
  170. const datas = await this.getAllDataByCondition({where: { sid: sid }});
  171. if (datas.length === 0) return;
  172. const updateDatas = [];
  173. for (const d of datas) {
  174. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  175. const his = history.filter(function (x) {
  176. return x.stimes && x.stimes < times;
  177. });
  178. his.sort(function (x, y) {
  179. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  180. });
  181. updateDatas.push({
  182. id: d.id,
  183. shistory: JSON.stringify(his),
  184. tp: his.length > 0 ? his[his.length - 1].tp : null,
  185. });
  186. }
  187. await transaction.updateRows(this.tableName, updateDatas);
  188. }
  189. }
  190. return StageBonus;
  191. };