s2b_proj.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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) {
  40. const sp = await this.getDataByPid(pid);
  41. const gs = sp.gxby_status.find(x => { return x.value === status; });
  42. if (!gs) throw '提交数据错误';
  43. gs.limit = limit;
  44. await this.db.update(this.tableName, { gxby_status: JSON.stringify(sp.gxby_status) }, { where: { pid } });
  45. return gs;
  46. }
  47. async updateDaglStatus(pid, status, limit, ratio) {
  48. const sp = await this.getDataByPid(pid);
  49. const ds = sp.dagl_status.find(x => { return x.value === status; });
  50. if (!ds) throw '提交数据错误';
  51. if (limit !== undefined && limit !== null) ds.limit = limit;
  52. if (ratio !== undefined && ratio !== null) ds.ratio = this.ctx.helper.round(ratio, 2);
  53. await this.db.update(this.tableName, { dagl_status: JSON.stringify(sp.dagl_status) }, { where: { pid } });
  54. return ds;
  55. }
  56. }
  57. return S2bProj;
  58. };