stage_bonus.js 9.7 KB

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