spss_stash.js 2.5 KB

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