stage_bonus.js 9.6 KB

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