import { setBusinessFlow } from '../../states/BusinessFlowSlice'; import store from '@/states/store'; import { clearWorks, addWork } from '../../states/exam/examWorksCacheSlice'; import { prepareWorksForExam } from './worklistToExam'; import { transformWorksToBodyPositions, setBodyPositions, } from '../../states/exam/bodyPositionListSlice'; /** * 从历史记录进入打印页面 * @param selectedIds - 选中的 StudyID 数组 */ const historyToPrint = async (selectedIds: string[]) => { const dispatch = store.dispatch; try { if (selectedIds.length === 0) { console.warn('[historyToPrint] No studies selected'); return; } console.log('[historyToPrint] Loading data for print page:', selectedIds); // Clear existing works in the cache await dispatch(clearWorks()); // 获取选中的 Task 对象 const state = store.getState(); const selectedTasks = state.historyEntities.data.filter((task) => selectedIds.includes(task.StudyID) ); if (selectedTasks.length === 0) { console.warn('[historyToPrint] No matching tasks found'); return; } // 使用公共函数准备数据(获取详细信息) const preparedTasks = await prepareWorksForExam(selectedTasks); // 保存到 examWorksCache preparedTasks.forEach((task) => { dispatch(addWork(task)); }); // 转换为体位列表 const bodyPositions = await transformWorksToBodyPositions(preparedTasks); await dispatch(setBodyPositions(bodyPositions)); console.log( '[historyToPrint] Data loaded successfully, navigating to print page' ); // 切换到打印页面 await dispatch(setBusinessFlow('print')); } catch (error) { console.error('[historyToPrint] Error:', error); throw error; } }; export default historyToPrint;