s2b_proj.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2021/6/17
  7. * @version
  8. */
  9. const thirdPartyConst = require('../const/third_party');
  10. module.exports = app => {
  11. class S2bProj extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 's2b_proj';
  21. }
  22. async getDataByPid(pid) {
  23. const sp = await this.getDataByCondition({ pid });
  24. if (sp) {
  25. sp.gxby_option = sp.gxby_option ? JSON.parse(sp.gxby_option) : null;
  26. sp.dagl_option = sp.dagl_option ? JSON.parse(sp.dagl_option) : null;
  27. sp.gxby_status = sp.gxby_status ? JSON.parse(sp.gxby_status) : thirdPartyConst.gxby;
  28. sp.dagl_status = sp.dagl_status ? JSON.parse(sp.dagl_status) : thirdPartyConst.dagl;
  29. }
  30. return sp;
  31. }
  32. async refreshSessionS2b() {
  33. const s2bProj = await this.getDataByPid(this.ctx.session.sessionProject.id);
  34. this.ctx.session.sessionProject.gxby = s2bProj ? s2bProj.gxby : false;
  35. this.ctx.session.sessionProject.gxby_status = s2bProj ? s2bProj.gxby_status : thirdPartyConst.gxby;
  36. this.ctx.session.sessionProject.dagl = s2bProj ? s2bProj.dagl : false;
  37. this.ctx.session.sessionProject.dagl_status = s2bProj ? s2bProj.dagl_status : thirdPartyConst.dagl;
  38. }
  39. async updateGxbyStatus(pid, status, limit, ratio) {
  40. const sp = await this.getDataByPid(pid);
  41. const gs = sp.gxby_status.find(x => { return x.value === status; });
  42. if (!gs) throw '提交数据错误';
  43. if (limit !== undefined && limit !== null) gs.limit = limit;
  44. if (ratio !== undefined && ratio !== null) gs.ratio = this.ctx.helper.round(ratio, 2);
  45. await this.db.update(this.tableName, { gxby_status: JSON.stringify(sp.gxby_status) }, { where: { pid } });
  46. return gs;
  47. }
  48. async updateDaglStatus(pid, status, limit, ratio) {
  49. const sp = await this.getDataByPid(pid);
  50. const ds = sp.dagl_status.find(x => { return x.value === status; });
  51. if (!ds) throw '提交数据错误';
  52. if (limit !== undefined && limit !== null) ds.limit = limit;
  53. if (ratio !== undefined && ratio !== null) ds.ratio = this.ctx.helper.round(ratio, 2);
  54. await this.db.update(this.tableName, { dagl_status: JSON.stringify(sp.dagl_status) }, { where: { pid } });
  55. return ds;
  56. }
  57. }
  58. return S2bProj;
  59. };