model.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * @Author: your name
  3. * @Date: 2022-01-12 10:12:55
  4. * @LastEditTime: 2023-08-18 21:37:40
  5. * @LastEditors: code4eat awesomedema@gmail.com
  6. * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  7. * @FilePath: /KC-MiddlePlatform/src/pages/platform/setting/userManage/model.ts
  8. */
  9. import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
  10. import {
  11. addHosp,
  12. delHosp,
  13. editHosp,
  14. GetHospYoushuAccountsType,
  15. hospBindMenu,
  16. hospBindYoushuAccount,
  17. YoushuAccountsType,
  18. YoushuUserSaveDTOs,
  19. } from '@/service/hospList';
  20. import type { getUsersParams } from '@/service/user';
  21. import { HospTableItem as TableListItem } from '@/service/hospList';
  22. // import { TableActType } from '.';
  23. enum TableActType {
  24. NOACT,
  25. ADD,
  26. DEL,
  27. EDIT,
  28. EDITMENU,
  29. BINDACCOUNT, //绑定有数账号
  30. }
  31. type CurrentSelectedData = TableListItem & GetHospYoushuAccountsType&{[key:string]:any};
  32. export interface hospManageModelState {
  33. name: string;
  34. tableAct: TableActType;
  35. isShowModal: boolean;
  36. reloadTable: boolean;
  37. currentEdit: CurrentSelectedData | undefined;
  38. }
  39. export interface hospManageModelType {
  40. namespace: 'hospManageModel';
  41. state: hospManageModelState;
  42. effects: {
  43. query: Effect;
  44. postAddData: Effect;
  45. postEditData: Effect;
  46. delRequest: Effect;
  47. };
  48. reducers: {
  49. save: Reducer<hospManageModelState>;
  50. add: Reducer<hospManageModelState>;
  51. cancelTableAct: Reducer<hospManageModelState>;
  52. reloadTable: Reducer<hospManageModelState>;
  53. edit: Reducer<hospManageModelState>;
  54. // 启用 immer 之后
  55. // save: ImmerReducer<IndexModelState>;
  56. };
  57. subscriptions: { setup: Subscription };
  58. }
  59. const hospManageModel: hospManageModelType = {
  60. namespace: 'hospManageModel',
  61. state: {
  62. name: '',
  63. tableAct: TableActType.NOACT,
  64. isShowModal: false,
  65. reloadTable: false,
  66. currentEdit: undefined,
  67. },
  68. effects: {
  69. *query({ payload }, { call, put }) {},
  70. *postAddData({ payload }, { call, put }) {
  71. yield addHosp({ mainHospId: 0, ...payload });
  72. yield put({
  73. type: 'reloadTable',
  74. payload: true,
  75. });
  76. },
  77. *postEditData({ payload }, { call, put, select }) {
  78. // console.log({payload});
  79. const {
  80. tableAct,
  81. currentEdit: currentEditOld,
  82. }: { tableAct: TableActType; currentEdit: TableListItem } = yield select(
  83. ({ hospManageModel: state }: { hospManageModel: hospManageModelState }) => {
  84. return {
  85. tableAct: state.tableAct,
  86. currentEdit: state.currentEdit,
  87. };
  88. },
  89. );
  90. if (tableAct == TableActType.BINDACCOUNT) {
  91. const defaultAccount = payload.dataSource.filter((t: YoushuAccountsType) => t.isDefault);
  92. // return;
  93. yield hospBindYoushuAccount({
  94. hospId: currentEditOld.id,
  95. reportId: payload.reportId,
  96. reportUrl: payload.reportUrl,
  97. userId: defaultAccount.length > 0 ? defaultAccount[0].id : 0,
  98. youshuUserSaveDTOs: payload.dataSource.map((t: YoushuUserSaveDTOs) => ({
  99. account: t.account,
  100. password: t.password,
  101. })),
  102. });
  103. yield put({
  104. type: 'reloadTable',
  105. payload: true,
  106. });
  107. }
  108. if (tableAct == TableActType.EDITMENU) {
  109. //绑定菜单
  110. yield hospBindMenu({
  111. hospId: currentEditOld.id,
  112. menuIds: payload.bindMenuIds,
  113. }); //menuId集合
  114. yield put({
  115. type: 'reloadTable',
  116. payload: true,
  117. });
  118. }
  119. if (tableAct == TableActType.EDIT) {
  120. //编辑院区信息
  121. yield editHosp({ ...currentEditOld, ...payload });
  122. yield put({
  123. type: 'reloadTable',
  124. payload: true,
  125. });
  126. }
  127. },
  128. *delRequest({ payload }, { put }) {
  129. yield delHosp(payload);
  130. yield put({
  131. type: 'reloadTable',
  132. payload: true,
  133. });
  134. },
  135. },
  136. reducers: {
  137. reloadTable(state, action) {
  138. return {
  139. ...state,
  140. reloadTable: action.payload,
  141. };
  142. },
  143. save(state, action) {
  144. return {
  145. ...state,
  146. ...action.payload,
  147. };
  148. },
  149. add(state, action) {
  150. return {
  151. ...state,
  152. ...action.payload,
  153. };
  154. },
  155. cancelTableAct(state) {
  156. return {
  157. ...state,
  158. isShowModal: false,
  159. };
  160. },
  161. edit(state, action) {
  162. return {
  163. ...state,
  164. tableAct: action.payload.act,
  165. isShowModal: action.payload.isShowModal,
  166. currentEdit: action.payload.data,
  167. };
  168. },
  169. // 启用 immer 之后
  170. // save(state, action) {
  171. // state.name = action.payload;
  172. // },
  173. },
  174. subscriptions: {
  175. setup({ dispatch, history }) {
  176. return history.listen(({ pathname }) => {
  177. if (pathname === '/') {
  178. dispatch({
  179. type: 'query',
  180. });
  181. }
  182. });
  183. },
  184. },
  185. };
  186. export default hospManageModel;