import { createEntityListSlices } from '../../../list_template/createListSlices'; import { createFetchThunk, createDeleteThunk, } from '../../../list_template/thunk.factory'; import { work, workAnimal } from '../types/worklist'; import { WorkFilter } from '../types/workfilter'; import { createAsyncThunk, Draft, PayloadAction } from '@reduxjs/toolkit'; import { setId, setName, setAccNo, setStartTime, setEndTime, setStatus, setPage, setPageSize, } from '../slices/searchSlice'; // Define the fetch thunk import { deleteStudies, fetchTaskList, lockStudy, } from '../../../../API/patient/workActions'; import { editTaskThunk } from '../../edit/editFormSlice'; import store from '@/states/store'; import { EntitiesState } from '@/states/list_template/type.model'; export const fetchWorkThunk = createFetchThunk( 'historylist', async ({ page, pageSize, filters }) => { // const filtersEx: WorkFilter = { ...filters, status: 'Arrived' } const { items, total } = await fetchTaskList(page, pageSize, filters); return { data: items, total }; } ); // Define the delete thunk export const deleteWorkThunk = createDeleteThunk( 'historylist', async (ids: string[]) => { await deleteStudies(ids); store.dispatch(selectionSlice.actions.clearSelection()); } ); const extraReducersForFilter = (builder) => { builder.addCase( setId.type, (state: WorkFilter, action: PayloadAction) => { state.patient_id = action.payload; } ); builder.addCase( setName.type, (state: WorkFilter, action: PayloadAction) => { state.patient_name = action.payload; } ); builder.addCase( setAccNo.type, (state: WorkFilter, action: PayloadAction) => { state.access_number = action.payload; } ); builder.addCase( setStartTime.type, (state: WorkFilter, action: PayloadAction) => { state.start_time = action.payload; } ); builder.addCase( setEndTime.type, (state: WorkFilter, action: PayloadAction) => { state.end_time = action.payload; } ); builder.addCase( setStatus.type, (state: WorkFilter, action: PayloadAction) => { state.status = action.payload; } ); builder.addCase( setPage.type, (state: WorkFilter, action: PayloadAction) => { state.page = action.payload; } ); builder.addCase( setPageSize.type, (state: WorkFilter, action: PayloadAction) => { state.page_size = action.payload; } ); }; // 锁定/解锁研究的 thunk export const lockWorkInhistorylistThunk = createAsyncThunk( 'historylist/lock', async ({ studyId, lock }: { studyId: string; lock: 'Locked' | 'Unlocked' }) => { console.log(`锁定,从thunk调用api,目标 studyid是 ${studyId},新状态是 ${lock}`); const result = await lockStudy(studyId, lock); return { studyId, lock, result }; } ); // 创建锁定操作的 handlers const createLockHandlers = () => ({ fulfilled: ( state: Draft>, action: PayloadAction<{ studyId: string; lock: 'Locked' | 'Unlocked'; result: any }> ) => { const { studyId, lock } = action.payload; console.log(`锁定,thunk fulfilled,目标 studyid是 ${studyId},新状态是 ${lock}`); const item = state.data.find((item) => item.StudyID === studyId); if (item) { item.StudyLock = lock; } }, }); // Create the worklist slices const { entitiesSlice, filtersSlice, paginationSlice, selectionSlice, uiSlice, } = createEntityListSlices( 'historylist', fetchWorkThunk, deleteWorkThunk, 'StudyID', extraReducersForFilter, { patient_id: '', patient_name: '', start_time: '', end_time: '', access_number: '', status: 'Completed', page: 1, page_size: 10, } satisfies WorkFilter, { lock: { thunk: lockWorkInhistorylistThunk, handlers: createLockHandlers(), }, // 监听 editFormSlice 的编辑 thunk editFromEditForm: { thunk: editTaskThunk, handlers: { fulfilled: ( state: Draft>, action: PayloadAction ) => { const { studyId, result } = action.payload; console.log(`historylist 监听到编辑成功,更新列表数据,studyId: ${studyId}`); const item = state.data.find((item) => item.StudyID === studyId); if (item && result.code === '0x000000') { // 更新列表中的项目数据 const updatedData = result.data; item.PatientName = updatedData.patient_name; item.PatientID = updatedData.patient_id; item.PatientSex = updatedData.patient_sex; item.PatientAge = updatedData.patient_age; item.PatientSize = updatedData.patient_size; item.AccessionNumber = updatedData.accession_number; item.OperatorID = updatedData.operator_name; // 根据产品类型更新特定字段 if ('owner_name' in item) { (item as any).owner_name = (updatedData as any).owner_name; (item as any).chip_number = (updatedData as any).chip_number; (item as any).variety = (updatedData as any).variety; (item as any).is_anaesthesia = (updatedData as any).is_anaesthesia; (item as any).is_sedation = (updatedData as any).is_sedation; } } }, }, }, } ); export const historyEntitiesSlice = entitiesSlice; export const historyFiltersSlice = filtersSlice; export const historyPaginationSlice = paginationSlice; export const historySelectionSlice = selectionSlice; export const historyUISlice = uiSlice;