historyToPrint.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { setBusinessFlow } from '../../states/BusinessFlowSlice';
  2. import store from '@/states/store';
  3. import { clearWorks, addWork } from '../../states/exam/examWorksCacheSlice';
  4. import { prepareWorksForExam } from './worklistToExam';
  5. import {
  6. transformWorksToBodyPositions,
  7. setBodyPositions,
  8. } from '../../states/exam/bodyPositionListSlice';
  9. /**
  10. * 从历史记录进入打印页面
  11. * @param selectedIds - 选中的 StudyID 数组
  12. */
  13. const historyToPrint = async (selectedIds: string[]) => {
  14. const dispatch = store.dispatch;
  15. try {
  16. if (selectedIds.length === 0) {
  17. console.warn('[historyToPrint] No studies selected');
  18. return;
  19. }
  20. console.log('[historyToPrint] Loading data for print page:', selectedIds);
  21. // Clear existing works in the cache
  22. await dispatch(clearWorks());
  23. // 获取选中的 Task 对象
  24. const state = store.getState();
  25. const selectedTasks = state.historyEntities.data.filter((task) =>
  26. selectedIds.includes(task.StudyID)
  27. );
  28. if (selectedTasks.length === 0) {
  29. console.warn('[historyToPrint] No matching tasks found');
  30. return;
  31. }
  32. // 使用公共函数准备数据(获取详细信息)
  33. const preparedTasks = await prepareWorksForExam(selectedTasks);
  34. // 保存到 examWorksCache
  35. preparedTasks.forEach((task) => {
  36. dispatch(addWork(task));
  37. });
  38. // 转换为体位列表
  39. const bodyPositions = await transformWorksToBodyPositions(preparedTasks);
  40. await dispatch(setBodyPositions(bodyPositions));
  41. console.log(
  42. '[historyToPrint] Data loaded successfully, navigating to print page'
  43. );
  44. // 切换到打印页面
  45. await dispatch(setBusinessFlow('print'));
  46. } catch (error) {
  47. console.error('[historyToPrint] Error:', error);
  48. throw error;
  49. }
  50. };
  51. export default historyToPrint;