workspaceStore.cjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const fs = require('node:fs');
  2. const path = require('node:path');
  3. const { getDuplicateCheckDir, getDuplicateCheckFilePath, getTechnicalPlanFilePath } = require('../utils/paths.cjs');
  4. const { deleteImportedImageBatches } = require('../utils/importedImages.cjs');
  5. function createWorkspaceStore(app) {
  6. const technicalPlanFile = getTechnicalPlanFilePath(app);
  7. const duplicateCheckFile = getDuplicateCheckFilePath(app);
  8. const duplicateCheckDir = getDuplicateCheckDir(app);
  9. return {
  10. getTechnicalPlanFilePath() {
  11. return technicalPlanFile;
  12. },
  13. loadTechnicalPlan() {
  14. if (!fs.existsSync(technicalPlanFile)) {
  15. return null;
  16. }
  17. try {
  18. const raw = fs.readFileSync(technicalPlanFile, 'utf-8');
  19. return JSON.parse(raw);
  20. } catch (error) {
  21. throw new Error(`技术方案缓存读取失败:${error.message}`);
  22. }
  23. },
  24. saveTechnicalPlan(state) {
  25. try {
  26. fs.mkdirSync(path.dirname(technicalPlanFile), { recursive: true });
  27. fs.writeFileSync(technicalPlanFile, JSON.stringify(state, null, 2), 'utf-8');
  28. return { success: true, message: '技术方案缓存已保存', file_path: technicalPlanFile };
  29. } catch (error) {
  30. throw new Error(`技术方案缓存保存失败:${error.message}`);
  31. }
  32. },
  33. updateTechnicalPlan(partial) {
  34. const prev = this.loadTechnicalPlan() || {};
  35. const next = { ...prev, ...partial };
  36. this.saveTechnicalPlan(next);
  37. return next;
  38. },
  39. clearTechnicalPlan() {
  40. try {
  41. if (fs.existsSync(technicalPlanFile)) {
  42. fs.unlinkSync(technicalPlanFile);
  43. }
  44. deleteImportedImageBatches(app, 'technical-plan');
  45. return { success: true, message: '技术方案缓存已清空', file_path: technicalPlanFile };
  46. } catch (error) {
  47. throw new Error(`技术方案缓存清空失败:${error.message}`);
  48. }
  49. },
  50. loadDuplicateCheck() {
  51. if (!fs.existsSync(duplicateCheckFile)) {
  52. return null;
  53. }
  54. try {
  55. const raw = fs.readFileSync(duplicateCheckFile, 'utf-8');
  56. return JSON.parse(raw);
  57. } catch (error) {
  58. throw new Error(`标书查重缓存读取失败:${error.message}`);
  59. }
  60. },
  61. saveDuplicateCheck(state) {
  62. try {
  63. fs.mkdirSync(path.dirname(duplicateCheckFile), { recursive: true });
  64. fs.writeFileSync(duplicateCheckFile, JSON.stringify(state, null, 2), 'utf-8');
  65. return { success: true, message: '标书查重缓存已保存', file_path: duplicateCheckFile };
  66. } catch (error) {
  67. throw new Error(`标书查重缓存保存失败:${error.message}`);
  68. }
  69. },
  70. updateDuplicateCheck(partial) {
  71. const prev = this.loadDuplicateCheck() || {};
  72. const next = { ...prev, ...partial };
  73. this.saveDuplicateCheck(next);
  74. return next;
  75. },
  76. clearDuplicateCheck() {
  77. try {
  78. if (fs.existsSync(duplicateCheckFile)) {
  79. fs.unlinkSync(duplicateCheckFile);
  80. }
  81. if (fs.existsSync(duplicateCheckDir)) {
  82. fs.rmSync(duplicateCheckDir, { recursive: true, force: true });
  83. }
  84. deleteImportedImageBatches(app, 'duplicate-check-content');
  85. return { success: true, message: '标书查重缓存已清空', file_path: duplicateCheckFile };
  86. } catch (error) {
  87. throw new Error(`标书查重缓存清空失败:${error.message}`);
  88. }
  89. },
  90. };
  91. }
  92. module.exports = {
  93. createWorkspaceStore,
  94. };