import { deleteByPrimaryKeyForProUser, selectByPrimaryKeyForProUser, insertForProUser, updateForProUser, deleteByMapForProUser,updateByMapForProUser, getOneForProUser,getAllForProUser,queryPageForProUser, countForProUser, insertBatchForProUser, deleteBatchForProUser,updateBatchForProUser, resetPwdForProUser } from '@/services/system/api_prouser';

export default {
    namespace: 'envInformationMonthly',

    state: {
        params: {},
        data: {
            list: [],
            pagination: {},
        },
    },

    effects: {
            *delete_by_primarykey_for_prouser({ payload, callback }, { select, call, put }) {
                const response = yield call(deleteByPrimaryKeyForProUser, payload)
                yield put({
                    type: 'deleteByPrimaryKeyForProUser',
                    payload: response
                })

                if (!response.success) {
                    callback && callback(response)
                    return
                }

                const params = yield select(state => state.prouser.params)
                const responseData = yield call(queryPageForProUser, params)
                yield put({
                    type: 'queryPageForProUser',
                    payload: responseData
                })

                if (callback) callback(response)
            },
            *select_by_primarykey_for_prouser({ payload, callback }, { call, put }) {
                const response = yield call(selectByPrimaryKeyForProUser, payload)
                yield put({
                    type: 'selectByPrimaryKeyForProUser',
                    payload: response
                })

                if (callback) callback(response)
            },
            *insert_for_prouser({ payload, callback }, { select, call, put }) {
                const response = yield call(insertForProUser, payload)
                yield put({
                    type: 'insertForProUser',
                    payload: response
                })

                if (!response.success) {
                    callback && callback(response)
                    return
                }

                const params = yield select(state => state.prouser.params)
                const responseData = yield call(queryPageForProUser, params)
                yield put({
                    type: 'queryPageForProUser',
                    payload: responseData
                })

                if (callback) callback(response)
            },
            *update_for_prouser({ payload, callback }, { select, call, put }) {
                const response = yield call(updateForProUser, payload)
                yield put({
                    type: 'updateForProUser',
                    payload: response
                })

                if (!response.success) {
                    callback && callback(response)
                    return
                }

                const params = yield select(state => state.prouser.params)
                const responseData = yield call(queryPageForProUser, params)
                yield put({
                    type: 'queryPageForProUser',
                    payload: responseData
                })

                if (callback) callback(response)
            },
            *delete_by_map_for_prouser({ payload, callback }, { select, call, put }) {
                const response = yield call(deleteByMapForProUser, payload);
                yield put({
                    type: 'deleteByMapForProUser',
                    payload: response,
                });

                const params = yield select(state => state.prouser.params);
                const responsedata = yield call(queryPageForProUser, params);
                yield put({
                    type: 'queryPageForProUser',
                    payload: responsedata,
                });

                if (callback) callback(response);
            },
            *update_by_map_for_prouser({ payload, callback }, { select, call, put }) {
                const response = yield call(updateByMapForProUser, payload);
                yield put({
                    type: 'updateByMapForProUser',
                    payload: response,
                });

                const params = yield select(state => state.prouser.params);
                const responsedata = yield call(queryPageForProUser, params);
                yield put({
                    type: 'queryPageForProUser',
                    payload: responsedata,
                });

                if (callback) callback(response);
            },
            *get_one_for_prouser({ payload, callback }, { call, put }) {
                const response = yield call(getOneForProUser, payload);
                yield put({
                    type: 'getOneForProUser',
                    payload: response,
                });

                if (callback) callback(response);
            },
            *get_all_for_prouser({ payload, callback }, { call, put }) {
                const response = yield call(getAllForProUser, payload);
                yield put({
                    type: 'getAllForProUser',
                    payload: response,
                });

                if (callback) callback(response);
            },
            *query_page_for_prouser({ payload, callback }, { select, call, put }) {
                const params = yield select(state => state.prouser.params);
                const newParams = payload?.resetFlag ? payload : {...params, ...payload};
                yield put({
                    type: 'setQueryPageByParams',
                    payload: newParams,
                });
                const response = yield call(queryPageForProUser, newParams);
                yield put({
                    type: 'queryPageForProUser',
                    payload: response,
                });

                if (callback) callback(response);
            },
            *count_for_prouser({ payload, callback }, { call, put }) {
                const response = yield call(countForProUser, payload);
                yield put({
                    type: 'countForProUser',
                    payload: response,
                });

                if (callback) callback(response);
            },
            *insert_batch_for_prouser({ payload, callback }, { select, call, put }) {
                const response = yield call(insertBatchForProUser, payload);
                yield put({
                    type: 'insertBatchForProUser',
                    payload: response,
                });

                const params = yield select(state => state.prouser.params);
                const responsedata = yield call(queryPageForProUser, params);
                yield put({
                    type: 'queryPageForProUser',
                    payload: responsedata,
                });

                if (callback) callback(response);
            },
            *delete_batch_for_prouser({ payload, callback }, { select, call, put }) {
                const response = yield call(deleteBatchForProUser, payload);
                yield put({
                    type: 'deleteBatchForProUser',
                    payload: response,
                });

                const params = yield select(state => state.prouser.params);
                const responsedata = yield call(queryPageForProUser, params);
                yield put({
                    type: 'queryPageForProUser',
                    payload: responsedata,
                });

                if (callback) callback(response);
            },
            *update_batch_for_prouser({ payload, callback }, { select, call, put }) {
                const response = yield call(updateBatchForProUser, payload);
                yield put({
                    type: 'updateBatchForProUser',
                    payload: response,
                });

                const params = yield select(state => state.prouser.params);
                const responsedata = yield call(queryPageForProUser, params);
                yield put({
                    type: 'queryPageForProUser',
                    payload: responsedata,
                });

                if (callback) callback(response);
            },
            *resetpwd_for_prouser({ payload, callback }, { select, call, put }) {
                const response = yield call(resetPwdForProUser, payload);
                yield put({
                    type: 'resetPwdForProUser',
                    payload: response,
                });

                const params = yield select(state => state.prouser.params);
                const responsedata = yield call(queryPageForProUser, params);
                yield put({
                    type: 'queryPageForProUser',
                    payload: responsedata,
                });

                if (callback) callback(response);
            },
    },

    reducers: {
        setQueryPageByParams(state, { payload }) {
            return {
                ...state,
                params: {...payload},
            };
        },
        deleteByPrimaryKeyForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        selectByPrimaryKeyForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        insertForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        deleteByMapForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateByMapForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        getOneForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        getAllForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        queryPageForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        countForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        insertBatchForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        deleteBatchForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateBatchForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        resetPwdForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
    },
};
