stage_bonus.js 10 KB

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