spss_stash.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2025/12/05
  7. * @version
  8. */
  9. const maxStashCount = 5;
  10. module.exports = app => {
  11. class SpssStash extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'spss_stash';
  21. }
  22. async getSpssStashList(spid, spssType) {
  23. const result = await this.getAllDataByCondition({
  24. where: { spid, spss_type: spssType },
  25. columns: [ 'id', 'user_id', 'user_name', 'create_time' ],
  26. orders: [['create_time', 'desc']],
  27. });
  28. if (result.length > maxStashCount) {
  29. const id = [...result].reverse().slice(0, result.length - maxStashCount).map(x => { return x.id; });
  30. await this.db.delete(this.tableName, { id });
  31. return result.slice(0, maxStashCount);
  32. }
  33. return result;
  34. }
  35. async getSpssStash(id) {
  36. const data = await this.getDataById(id);
  37. data.spss_select = JSON.parse(data.spss_select);
  38. data.spss_result = JSON.parse(data.spss_result);
  39. return data;
  40. }
  41. async addSpssStash(type, select, result) {
  42. const data = {
  43. id: this.uuid.v4(), spid: this.ctx.subProject.id,
  44. user_id: this.ctx.session.sessionUser.accountId, user_name: this.ctx.session.sessionUser.name,
  45. spss_type: type,
  46. spss_select: JSON.stringify(select),
  47. spss_result: JSON.stringify(result),
  48. };
  49. await this.db.insert(this.tableName, data);
  50. }
  51. }
  52. return SpssStash;
  53. };