You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dashboard/src/zustand/HostCase.js

104 lines
3.3 KiB
JavaScript

import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import { fetchJSON } from '../utils/request';
import { HT_HOST } from '../config';
import moment from 'moment';
/**
* Transforms an array of row data into an array of objects,
* using a separate array of column names as keys.
*
* @param {string[]} cols - The array of column names (keys).
* @param {any[][]} rows - The array of row data (values).
* @returns {Object[]} The transformed array of objects.
*/
export const transformRows = (cols, rows) => {
return rows.map((row) => {
return row.reduce((acc, val, i) => {
acc[cols[i].display_name] = val;
return acc;
}, {});
});
};
export const fetchCaseSummary = async (params) => {
const searchParams = {
...params,
vei_sn: params.agency,
};
const { errcode, result } = await fetchJSON(`${HT_HOST}/service-Analyse2/dong_dao_zhu_total`, searchParams);
return errcode !== 0 ? [] : (result || []);
};
export const fetchCaseSummaryByGuide = async (params) => {
const searchParams = {
...params,
vei_sn: params.agency,
};
const { errcode, result } = await fetchJSON(`${HT_HOST}/service-Analyse2/dong_dao_zhu_tour_guide`, searchParams);
return errcode !== 0 ? [] : (result || []); // .sort(sortDescBy('case_count_dongdaozhu'));
};
export const fetchCaseFeatured = async (params) => {
const searchParams = {
...params,
vei_sn: params.agency,
};
const { errcode, result } = await fetchJSON(`${HT_HOST}/service-Analyse2/dong_dao_zhu_case`, searchParams);
return errcode !== 0 ? [] : (result || []);
};
/**
* 东道主报告----------------------------------------------------------------------------------------------
*/
const initialState = {
loading: false,
loadingCase: false,
caseSummary: [],
caseSummaryByGuide: [],
caseFeatured: [],
forExport: {
agencyName: '',
title: '',
}
};
const useHostCaseStore = create(
devtools(
immer((set, get) => ({
...initialState,
searchValues: {},
searchValuesToSub: {},
reset: () => set(initialState),
setLoading: (loading) => set({ loading }),
setSearchValues: (obj, values) => set((state) => ({ searchValues: values, searchValuesToSub: obj })),
setLoadingCase: (loadingCase) => set({ loadingCase }),
setCaseFeatured: (caseFeatured) => set({ caseFeatured }),
setCaseSummary: (caseSummary) => set({ caseSummary }),
setCaseSummaryByGuide: (caseSummaryByGuide) => set({ caseSummaryByGuide }),
async getCaseReport(params) {
const { setLoading, searchValues } = get();
setLoading(true);
const [summary, guideSummary, featured] = await Promise.all([
fetchCaseSummary(params),
fetchCaseSummaryByGuide(params),
fetchCaseFeatured(params)
]);
set({
caseSummary: summary,
caseSummaryByGuide: guideSummary,
caseFeatured: featured,
forExport: { title: `${moment(params.Date1).format('YYYY年MM月')}-${moment(params.Date2).format('YYYY年MM月')}总结`, agencyName: searchValues.agency.label },
});
setLoading(false);
},
})),
{ name: 'hostCaseReportStore' }
)
);
export default useHostCaseStore;