spss_stash.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 getLatestSpssStash(spid, type) {
  42. const data = await this.getAllDataByCondition({
  43. where: { spid, spss_type: type },
  44. orders: [['create_time', 'desc']],
  45. limit: 1,
  46. offset: 0,
  47. });
  48. if (data.length === 0) return { spss_select: [], spss_result: null };
  49. data[0].spss_select = JSON.parse(data[0].spss_select);
  50. data[0].spss_result = JSON.parse(data[0].spss_result);
  51. return data[0];
  52. }
  53. async addSpssStash(type, select, result) {
  54. const data = {
  55. id: this.uuid.v4(), spid: this.ctx.subProject.id,
  56. user_id: this.ctx.session.sessionUser.accountId, user_name: this.ctx.session.sessionUser.name,
  57. spss_type: type,
  58. spss_select: JSON.stringify(select),
  59. spss_result: JSON.stringify(result),
  60. };
  61. await this.db.insert(this.tableName, data);
  62. }
  63. }
  64. return SpssStash;
  65. };