// import { create } from 'dua'; import { fetchList, fetchDetail } from '../services/alarm'; import { fetchDeviceInfo } from '../services'; import { forEach } from 'lodash'; import { Reducer, Effect } from 'umi'; import { Alarm, AlarmTimelineType, AlarmStatus, Device, } from '../common/types/alarm'; export interface AlarmState { list?: Alarm[]; total?: number; actionTypes?: any[]; spaces: { id: string; name: string }[]; detail?: Alarm | undefined; device?: Partial; } interface ModelType { namespace: string; state: AlarmState; effects: { fetch: Effect; addTo: Effect; fetchDetail: Effect; fetchDevice: Effect; }; reducers: { addToState: Reducer; updateState: Reducer; updateAlarmStatus: Reducer; // 其他模块用到预警记录数据时,会有缓存问题,这里清除一下 clearList: Reducer; }; } const Model: ModelType = { namespace: 'alarm', state: { list: [], total: 0, actionTypes: [], spaces: [], detail: undefined, device: { deployment: {} }, }, effects: { *fetch({ payload }, { call, put }) { const response = yield call(fetchList, payload); if (response?.data) { yield put({ type: 'updateState', payload: response.data, }); } return response; }, *fetchDetail({ payload }, { call, put }) { const { id } = payload; const response = yield call(fetchDetail, id); if (response?.data) { yield put({ type: 'updateState', payload: { detail: response.data }, }); } return response; }, *fetchDevice({ payload }, { call, put }) { const { id } = payload; const response = yield call(fetchDeviceInfo, id); if (response?.data) { yield put({ type: 'updateState', payload: { device: response.data }, }); } }, *addTo({ payload }, { call, put }) { const response = yield call(fetchList, payload); if (response?.data) { yield put({ type: 'addToState', payload: response.data, }); } return response; }, }, reducers: { addToState(state, { payload }) { const { spaces = [], list, ...rest } = payload; const data = { ...state, ...rest, spaces: spaces.concat(state?.spaces || []), list: (state?.list || []).concat(list), }; return data; }, updateState(state, { payload }) { const { spaces = [], ...rest } = payload; const data = { ...state, ...rest, spaces: spaces.concat(state?.spaces || []), }; return data; }, updateAlarmStatus(state, { payload }) { const { id, type, data } = payload; const { list, detail } = state; const statusMap = { [AlarmTimelineType.ACCEPT]: AlarmStatus.unprocessed, [AlarmTimelineType.PROCESS]: AlarmStatus.processing, [AlarmTimelineType.FINISH]: AlarmStatus.finished, [AlarmTimelineType.SUPPLEMENT]: AlarmStatus.finished, }; const currentStatus = statusMap[type]; forEach(list, item => { if (item.id === id && currentStatus) { item.status = currentStatus; } }); if (detail && detail.id === id) { detail.status = currentStatus; // 处理操作更新确认类型及处理信息 if (data && type === AlarmTimelineType.PROCESS) { detail.type = data.data.type; detail.lastProcess = data; } if (data && type === AlarmTimelineType.FINISH) { detail.lastFinish = data; } } return { ...state, list, detail, }; }, clearList(state, { payload }) { return { ...state, spaces: [], list: [], }; }, }, }; export default Model;