stage_bonus.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 tpDecimal = this.ctx.tender.info.decimal.extra
  68. ? this.ctx.tender.info.decimal.extraTp
  69. : this.ctx.tender.info.decimal.tp;
  70. const datas = data instanceof Array ? data : [data];
  71. const insertData = [];
  72. for (const d of datas) {
  73. if (!d.name || !d.order) throw '新增甲供材料,提交的数据错误';
  74. const nd = {
  75. id: this.uuid.v4(),
  76. tid: this.ctx.tender.id,
  77. sid: this.ctx.stage.id,
  78. sorder: this.ctx.stage.order,
  79. uid: this.ctx.session.sessionUser.accountId,
  80. create_time: new Date(),
  81. name: d.name,
  82. order: d.order,
  83. };
  84. nd.b_type = d.b_type ? d.b_type : null;
  85. nd.tp = d.tp ? this.ctx.helper.round(d.tp, tpDecimal) : 0;
  86. nd.code = d.code ? d.code: null;
  87. nd.proof = d.proof ? d.proof : null;
  88. nd.real_time = d.real_time ? d.real_time : null;
  89. nd.memo = d.memo ? d.memo : null;
  90. nd.doc_co = d.doc_co ? d.doc_co : null;
  91. insertData.push(nd);
  92. }
  93. await this.db.insert(this.tableName, insertData);
  94. return insertData;
  95. }
  96. async _delDatas (data) {
  97. const datas = data instanceof Array ? data : [data];
  98. const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')}});
  99. for (const od of orgDatas) {
  100. console.log(od);
  101. console.log(this.ctx.stage.id);
  102. if (od.sid !== this.ctx.stage.id) throw '非本期新增数据,不可删除';
  103. }
  104. await this.db.delete(this.tableName, {id: datas});
  105. return datas;
  106. }
  107. async _updateDatas (data) {
  108. const tpDecimal = this.ctx.tender.info.decimal.extra
  109. ? this.ctx.tender.info.decimal.extraTp
  110. : this.ctx.tender.info.decimal.tp;
  111. const datas = data instanceof Array ? data : [data];
  112. const orgDatas = await this.getAllDataByCondition({
  113. where: { sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id') }
  114. });
  115. const uDatas = [];
  116. for (const d of datas) {
  117. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  118. if (!od) continue;
  119. const nd = {id: od.id};
  120. if (d.name !== undefined) nd.name = d.name;
  121. if (d.b_type !== undefined) nd.b_type = d.b_type;
  122. if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, tpDecimal);
  123. if (d.code !== undefined) nd.code = d.code;
  124. if (d.proof !== undefined) nd.proof = d.proof;
  125. if (d.proof_file !== undefined) nd.proof_file = JSON.stringify(d.proof_file);
  126. if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);
  127. if (d.memo !== undefined) nd.memo = d.memo;
  128. if (d.order !== undefined) nd.order = d.order;
  129. if (d.doc_co !== undefined) nd.doc_co = d.doc_co;
  130. uDatas.push(nd);
  131. }
  132. if (uDatas.length > 0) {
  133. await this.db.updateRows(this.tableName, uDatas);
  134. return uDatas;
  135. } else {
  136. return [];
  137. }
  138. }
  139. async updateDatas(data) {
  140. const result = {add: [], del: [], update: []};
  141. try {
  142. if (data.add) {
  143. result.add = await this._addDatas(data.add);
  144. }
  145. if (data.update) {
  146. result.update = await this._updateDatas(data.update);
  147. }
  148. if (data.del) {
  149. result.del = await this._delDatas(data.del);
  150. }
  151. return result;
  152. } catch (err) {
  153. if (err) result.err = err;
  154. return result;
  155. }
  156. }
  157. async updateHistory(stage, transaction) {
  158. const datas = await this.getStageData(stage.id);
  159. if (datas.length === 0) return;
  160. const updateDatas = [];
  161. const times = this.ctx.stage.curTimes, order = this.ctx.stage.curOrder;
  162. for (const d of datas) {
  163. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  164. const his = history.find(function (x) {
  165. return x.stimes && x.stimes === times
  166. && x.sorder && x.sorder === order;
  167. });
  168. if (his) {
  169. his.tp = d.tp;
  170. } else {
  171. history.push({ stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp });
  172. }
  173. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  174. }
  175. await transaction.updateRows(this.tableName, updateDatas);
  176. }
  177. async deleteStageTimesData(sid, times, transaction) {
  178. const datas = await this.getAllDataByCondition({where: { sid: sid }});
  179. if (datas.length === 0) return;
  180. const updateDatas = [];
  181. for (const d of datas) {
  182. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  183. const his = history.filter(function (x) {
  184. return x.stimes && x.stimes < times;
  185. });
  186. his.sort(function (x, y) {
  187. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  188. });
  189. updateDatas.push({
  190. id: d.id,
  191. shistory: JSON.stringify(his),
  192. tp: his.length > 0 ? his[his.length - 1].tp : null,
  193. });
  194. }
  195. await transaction.updateRows(this.tableName, updateDatas);
  196. }
  197. }
  198. return StageBonus;
  199. };