history.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { createEntityListSlices } from '../../../list_template/createListSlices';
  2. import {
  3. createFetchThunk,
  4. createDeleteThunk,
  5. } from '../../../list_template/thunk.factory';
  6. import { work, workAnimal } from '../types/worklist';
  7. import { WorkFilter } from '../types/workfilter';
  8. import { createAsyncThunk, Draft, PayloadAction } from '@reduxjs/toolkit';
  9. import {
  10. setId,
  11. setName,
  12. setAccNo,
  13. setStartTime,
  14. setEndTime,
  15. setStatus,
  16. setPage,
  17. setPageSize,
  18. } from '../slices/searchSlice';
  19. // Define the fetch thunk
  20. import {
  21. deleteStudies,
  22. fetchTaskList,
  23. lockStudy,
  24. } from '../../../../API/patient/workActions';
  25. import { editTaskThunk } from '../../edit/editFormSlice';
  26. import store from '@/states/store';
  27. import { EntitiesState } from '@/states/list_template/type.model';
  28. export const fetchWorkThunk = createFetchThunk<WorkFilter, work>(
  29. 'historylist',
  30. async ({ page, pageSize, filters }) => {
  31. // const filtersEx: WorkFilter = { ...filters, status: 'Arrived' }
  32. const { items, total } = await fetchTaskList(page, pageSize, filters);
  33. return { data: items, total };
  34. }
  35. );
  36. // Define the delete thunk
  37. export const deleteWorkThunk = createDeleteThunk(
  38. 'historylist',
  39. async (ids: string[]) => {
  40. await deleteStudies(ids);
  41. store.dispatch(selectionSlice.actions.clearSelection());
  42. }
  43. );
  44. const extraReducersForFilter = (builder) => {
  45. builder.addCase(
  46. setId.type,
  47. (state: WorkFilter, action: PayloadAction<string>) => {
  48. state.patient_id = action.payload;
  49. }
  50. );
  51. builder.addCase(
  52. setName.type,
  53. (state: WorkFilter, action: PayloadAction<string>) => {
  54. state.patient_name = action.payload;
  55. }
  56. );
  57. builder.addCase(
  58. setAccNo.type,
  59. (state: WorkFilter, action: PayloadAction<string>) => {
  60. state.access_number = action.payload;
  61. }
  62. );
  63. builder.addCase(
  64. setStartTime.type,
  65. (state: WorkFilter, action: PayloadAction<string>) => {
  66. state.start_time = action.payload;
  67. }
  68. );
  69. builder.addCase(
  70. setEndTime.type,
  71. (state: WorkFilter, action: PayloadAction<string>) => {
  72. state.end_time = action.payload;
  73. }
  74. );
  75. builder.addCase(
  76. setStatus.type,
  77. (state: WorkFilter, action: PayloadAction<string>) => {
  78. state.status = action.payload;
  79. }
  80. );
  81. builder.addCase(
  82. setPage.type,
  83. (state: WorkFilter, action: PayloadAction<number>) => {
  84. state.page = action.payload;
  85. }
  86. );
  87. builder.addCase(
  88. setPageSize.type,
  89. (state: WorkFilter, action: PayloadAction<number>) => {
  90. state.page_size = action.payload;
  91. }
  92. );
  93. };
  94. // 锁定/解锁研究的 thunk
  95. export const lockWorkInhistorylistThunk = createAsyncThunk(
  96. 'historylist/lock',
  97. async ({ studyId, lock }: { studyId: string; lock: 'Locked' | 'Unlocked' }) => {
  98. console.log(`锁定,从thunk调用api,目标 studyid是 ${studyId},新状态是 ${lock}`);
  99. const result = await lockStudy(studyId, lock);
  100. return { studyId, lock, result };
  101. }
  102. );
  103. // 创建锁定操作的 handlers
  104. const createLockHandlers = () => ({
  105. fulfilled: (
  106. state: Draft<EntitiesState<work | workAnimal>>,
  107. action: PayloadAction<{ studyId: string; lock: 'Locked' | 'Unlocked'; result: any }>
  108. ) => {
  109. const { studyId, lock } = action.payload;
  110. console.log(`锁定,thunk fulfilled,目标 studyid是 ${studyId},新状态是 ${lock}`);
  111. const item = state.data.find((item) => item.StudyID === studyId);
  112. if (item) {
  113. item.StudyLock = lock;
  114. }
  115. },
  116. });
  117. // Create the worklist slices
  118. const {
  119. entitiesSlice,
  120. filtersSlice,
  121. paginationSlice,
  122. selectionSlice,
  123. uiSlice,
  124. } = createEntityListSlices<work, WorkFilter>(
  125. 'historylist',
  126. fetchWorkThunk,
  127. deleteWorkThunk,
  128. 'StudyID',
  129. extraReducersForFilter,
  130. {
  131. patient_id: '',
  132. patient_name: '',
  133. start_time: '',
  134. end_time: '',
  135. access_number: '',
  136. status: 'Completed',
  137. page: 1,
  138. page_size: 10,
  139. } satisfies WorkFilter,
  140. {
  141. lock: {
  142. thunk: lockWorkInhistorylistThunk,
  143. handlers: createLockHandlers(),
  144. },
  145. // 监听 editFormSlice 的编辑 thunk
  146. editFromEditForm: {
  147. thunk: editTaskThunk,
  148. handlers: {
  149. fulfilled: (
  150. state: Draft<EntitiesState<work | workAnimal>>,
  151. action: PayloadAction<any>
  152. ) => {
  153. const { studyId, result } = action.payload;
  154. console.log(`historylist 监听到编辑成功,更新列表数据,studyId: ${studyId}`);
  155. const item = state.data.find((item) => item.StudyID === studyId);
  156. if (item && result.code === '0x000000') {
  157. // 更新列表中的项目数据
  158. const updatedData = result.data;
  159. item.PatientName = updatedData.patient_name;
  160. item.PatientID = updatedData.patient_id;
  161. item.PatientSex = updatedData.patient_sex;
  162. item.PatientAge = updatedData.patient_age;
  163. item.PatientSize = updatedData.patient_size;
  164. item.AccessionNumber = updatedData.accession_number;
  165. item.OperatorID = updatedData.operator_name;
  166. // 根据产品类型更新特定字段
  167. if ('owner_name' in item) {
  168. (item as any).owner_name = (updatedData as any).owner_name;
  169. (item as any).chip_number = (updatedData as any).chip_number;
  170. (item as any).variety = (updatedData as any).variety;
  171. (item as any).is_anaesthesia = (updatedData as any).is_anaesthesia;
  172. (item as any).is_sedation = (updatedData as any).is_sedation;
  173. }
  174. }
  175. },
  176. },
  177. },
  178. }
  179. );
  180. export const historyEntitiesSlice = entitiesSlice;
  181. export const historyFiltersSlice = filtersSlice;
  182. export const historyPaginationSlice = paginationSlice;
  183. export const historySelectionSlice = selectionSlice;
  184. export const historyUISlice = uiSlice;