地接接团 --> zustand

main
Lei OT 3 months ago
parent e6fe06a533
commit d5bc8a639f

@ -0,0 +1,132 @@
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import { fetchJSON } from '@haina/utils-request';
import { HT_HOST } from '../config';
import { isEmpty } from '@haina/utils-commons';
const defaultParams = {};
export const fetchAgentGroupCount = async (params) => {
const { errcode, errmsg, ...result } = await fetchJSON(HT_HOST + '/service-web/QueryData/GetAgentGroupInfoALL', {
...defaultParams,
// ...params,
DateType: params.DateType,
VEI_SN: params.agency || '',
DepList: params.DepartmentList || '',
Country: params.countryArea || '',
Date1: params.Date1,
Date2: params.Date2,
OldDate1: params.DateDiff1 || '',
OldDate2: params.DateDiff2 || '',
});
if (errcode !== 0) {
return {};
}
const { result1, result2, total1, total2 } = result;
const result1Mapped = result1.reduce((r, v) => ({ ...r, [v.EOI_ObjSN]: v }), {});
const ret = {};
if (isEmpty(params.DateDiff1)) {
ret.result1 = Object.values(result1Mapped).filter((row) => row.EOI_ObjSN !== -1);
ret.total1 = { ...result1Mapped['-1'] };
return ret;
} else {
const allKeys = [...new Set([...result1.map((e) => e.EOI_ObjSN), ...result2.map((e) => e.EOI_ObjSN)])];
const result2Mapped = result2.reduce((r, v) => ({ ...r, [v.EOI_ObjSN]: v }), {});
const x = {};
allKeys.forEach((key) => {
x[key] = { ...(result1Mapped?.[key] || { EOI_ObjSN: key, vi: key, key, VendorName: result2Mapped[key]?.VendorName || '--' }), diff: result2Mapped[key] || {} };
});
ret.result1 = Object.values(x).filter((row) => row.EOI_ObjSN !== -1);
ret.total1 = { ...result1Mapped['-1'], diff: result2Mapped['-1'] || {} };
return ret;
}
};
export const fetchGroupListByAgentId = async (params) => {
const { agentId, DateType, Date1, Date2, DateDiff1, DateDiff2, DepartmentList, countryArea } = params;
const { errcode, errmsg, ...result } = await fetchJSON(HT_HOST + '/service-web/QueryData/GetAgentGroupInfo', {
VEI_SN: agentId || '',
DateType: DateType || '',
Date1: Date1 || '',
Date2: Date2 || '',
OldDate1: DateDiff1 || '',
OldDate2: DateDiff2 || '',
DepList: DepartmentList || '',
});
if (errcode !== 0) {
return {};
}
result.title1 = Date1 ? `${Date1}~${Date2}` : '';
result.title2 = DateDiff1 ? `${DateDiff1}~${DateDiff2}` : '';
return result;
};
/**
* --------------------------------------------------------------------------------------------------------
*/
const initialState = {
loading: false,
typeLoading: false,
searchValues: {
DateType: { key: 'departureDate', label: '抵达日期' },
WebCode: { key: 'all', label: '所有来源' },
IncludeTickets: { key: '1', label: '含门票' },
DepartmentList: { key: 'All', label: '所有来源' },
countryArea: { key: 'china', label: '国内' },
},
searchValuesToSub: {
DateType: 'departureDate',
WebCode: 'all',
IncludeTickets: '1',
DepartmentList: 'All',
},
agentCountList: [],
agentCountTotal: {},
agencyName: '',
agencyGroups: { total1: {}, result1: [], total2: {}, result2: [] },
};
const useCustomerServicesStore = create(
devtools(
immer((set, get) => ({
...initialState,
reset: () => set(initialState),
setLoading: (loading) => set({ loading }),
setSearchValues: (obj, values) => set((state) => ({ searchValues: values, searchValuesToSub: obj })),
setSearchValuesToSub: (values) => set((state) => ({ searchValuesToSub: values })),
getAgentGroupCount: async (params) => {
const { setLoading } = get();
setLoading(true);
try {
const res = await fetchAgentGroupCount(params);
set({ agentCountList: res.result1 || [], agentCountTotal: res.total1 || {} });
} catch (error) {
} finally {
setLoading(false);
}
},
getGroupListByAgentId: async (agentId) => {
const { setLoading, searchValuesToSub } = get();
setLoading(true);
try {
const res = await fetchGroupListByAgentId({ ...searchValuesToSub, agentId });
set({ agencyGroups: res, agencyName: res.result1?.[0]?.VendorName || '' });
} catch (error) {
} finally {
setLoading(false);
}
},
})),
{ name: 'CustomerServices' }
)
);
export default useCustomerServicesStore;
Loading…
Cancel
Save