stage_bonus.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.tender.isTourist && 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. if (od.sid !== this.ctx.stage.id) throw '非本期新增数据,不可删除';
  101. }
  102. await this.db.delete(this.tableName, {id: datas});
  103. return datas;
  104. }
  105. async _updateDatas (data) {
  106. const tpDecimal = this.ctx.tender.info.decimal.extra
  107. ? this.ctx.tender.info.decimal.extraTp
  108. : this.ctx.tender.info.decimal.tp;
  109. const datas = data instanceof Array ? data : [data];
  110. const orgDatas = await this.getAllDataByCondition({
  111. where: { id: this.ctx.helper._.map(datas, 'id') }
  112. });
  113. const uDatas = [];
  114. for (const d of datas) {
  115. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  116. if (!od) continue;
  117. const nd = {id: od.id};
  118. if (d.name !== undefined) nd.name = d.name;
  119. if (d.b_type !== undefined) nd.b_type = d.b_type;
  120. if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, tpDecimal);
  121. if (d.code !== undefined) nd.code = d.code;
  122. if (d.proof !== undefined) nd.proof = d.proof;
  123. if (d.proof_file !== undefined) nd.proof_file = JSON.stringify(d.proof_file);
  124. if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);
  125. if (d.memo !== undefined) nd.memo = d.memo;
  126. if (d.order !== undefined) nd.order = d.order;
  127. if (d.doc_co !== undefined) nd.doc_co = d.doc_co;
  128. uDatas.push(nd);
  129. }
  130. if (uDatas.length > 0) {
  131. await this.db.updateRows(this.tableName, uDatas);
  132. return uDatas;
  133. } else {
  134. return [];
  135. }
  136. }
  137. async updateDatas(data) {
  138. const result = {add: [], del: [], update: []};
  139. try {
  140. if (data.add) {
  141. result.add = await this._addDatas(data.add);
  142. }
  143. if (data.update) {
  144. result.update = await this._updateDatas(data.update);
  145. }
  146. if (data.del) {
  147. result.del = await this._delDatas(data.del);
  148. }
  149. return result;
  150. } catch (err) {
  151. if (err) result.err = err;
  152. return result;
  153. }
  154. }
  155. async updateHistory(stage, transaction) {
  156. const datas = await this.getStageData(stage.id);
  157. if (datas.length === 0) return;
  158. const updateDatas = [];
  159. const times = this.ctx.stage.curTimes, order = this.ctx.stage.curOrder;
  160. for (const d of datas) {
  161. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  162. const his = history.find(function (x) {
  163. return x.stimes && x.stimes === times
  164. && x.sorder && x.sorder === order;
  165. });
  166. if (his) {
  167. his.tp = d.tp;
  168. } else {
  169. history.push({ stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp });
  170. }
  171. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  172. }
  173. await transaction.updateRows(this.tableName, updateDatas);
  174. }
  175. async updateHistory4CheckAgain(stage, transaction) {
  176. const datas = await this.getStageData(stage.id);
  177. if (datas.length === 0) return;
  178. const updateDatas = [];
  179. const times = this.ctx.stage.curTimes, order = this.ctx.stage.curOrder + 1;
  180. for (const d of datas) {
  181. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  182. const his = history.find(function (x) {
  183. return x.stimes && x.stimes === times
  184. && x.sorder && x.sorder === order;
  185. });
  186. if (his) {
  187. his.tp = d.tp;
  188. } else {
  189. history.push({ stimes: times, sorder: order, tp: d.tp });
  190. }
  191. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  192. }
  193. await transaction.updateRows(this.tableName, updateDatas);
  194. }
  195. async deleteStageTimesData(sid, times, transaction) {
  196. const datas = await this.getAllDataByCondition({where: { sid: sid }});
  197. if (datas.length === 0) return;
  198. const updateDatas = [];
  199. for (const d of datas) {
  200. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  201. const his = history.filter(function (x) {
  202. return x.stimes && x.stimes < times;
  203. });
  204. his.sort(function (x, y) {
  205. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  206. });
  207. updateDatas.push({
  208. id: d.id,
  209. shistory: JSON.stringify(his),
  210. tp: his.length > 0 ? his[his.length - 1].tp : null,
  211. });
  212. }
  213. await transaction.updateRows(this.tableName, updateDatas);
  214. }
  215. }
  216. return StageBonus;
  217. };