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/SalesInsight.js

123 lines
4.2 KiB
JavaScript

import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import { groupsMappedByCode } from '../libs/ht';
import { fetchJSON } from '@haina/utils-request';
import { HT_HOST } from '../config';
import { groupBy, isEmpty, } from '@haina/utils-commons';
/**
* 顾问业绩 (例会数据)
*/
const defaultParams = { OrderType: 227001, IsDYTJ: '', IncludeTickets: 1, Team: '', WebCodeFX: '', CusType: '', OldCus: 0, lineClass: '', IsDetail: -1 };
export const fetchMeetingDataSales = async (params) => {
const { errcode, errmsg, result } = await fetchJSON(HT_HOST + '/service-web/QueryData/WLCountForMeetingNew', {
...defaultParams,
...params,
WebCode: (params.WebCode || '').replace('all', ''),
OPI_SN: params.operator || '',
});
const ret =
errcode !== 0
? []
: (result || [])
// .filter((ele) =>
// Object.keys(ele)
// .filter((col) => !['OPI_SN', 'OPI_Name', 'COLI_LineClass', 'LineClass', 'vi'].includes(col))
// .some((col) => !isEmpty(ele[col])),
// )
.map((ele) => ({ ...ele, key: `${ele.OPI_SN}_${ele.COLI_LineClass || ''}_${ele.vi || ''}` }));
const byOPI = groupBy(structuredClone(ret), 'OPI_SN');
const OPIValue = Object.keys(byOPI).reduce((r, opisn) => {
byOPI[opisn].forEach((ele, xi) => {
ele.rowSpan = xi === 0 ? byOPI[opisn].length : 0;
});
return [...r, ...byOPI[opisn]];
}, []);
const byLineClass = groupBy(structuredClone(ret), 'LineClass');
const LineClassValue = Object.keys(byLineClass).reduce((r, gkey) => {
byLineClass[gkey].forEach((ele, xi) => {
ele.rowSpan = xi === 0 ? byLineClass[gkey].length : 0;
});
return [...r, ...byLineClass[gkey]];
}, []);
return { result: ret, OPIValue, LineClassValue };
};
/**
* --------------------------------------------------------------------------------------------------------
*/
const initialState = {
loading: false,
typeLoading: false,
searchValues: {
// DateType: { key: 'applyDate', label: '提交日期' },
WebCode: { key: 'all', label: '所有来源' },
IncludeTickets: { key: '1', label: '含门票' },
DepartmentList: groupsMappedByCode.GH, // { key: 'All', label: '所有来源' }, //
},
searchValuesToSub: {
// DateType: 'applyDate',
WebCode: 'all',
IncludeTickets: '1',
DepartmentList: -1, // -1: All
},
salesDataTotal: [],
salesData: [],
matrixData: {},
matrixtableMajorKey: 'opi',
matrixTableData: [],
// 二级页面
};
const useSalesInsightStore = create(
devtools(
immer((set, get) => ({
...initialState,
reset: () => set(initialState),
setLoading: (loading) => set({ loading }),
setTypeLoading: (typeLoading) => set({ typeLoading }),
setSearchValues: (obj, values) => set((state) => ({ searchValues: values, searchValuesToSub: obj })),
setSearchValuesToSub: (values) => set((state) => ({ searchValuesToSub: values })),
setActiveTab: (tab) => set({ activeTab: tab }),
// site effects
getMeetingDataSales: async (params) => {
const { setTypeLoading, } = get();
setTypeLoading(true);
try {
const res = await fetchMeetingDataSales(params);
if (params.IsDetail === 1) {
set({ matrixData: res, matrixTableData: res.OPIValue, matrixtableMajorKey: 'opi', salesData: res.result });
}
else {
set({ salesDataTotal: res.result });
}
} catch (error) {
console.error(error);
} finally {
setTypeLoading(false);
}
},
onMatrixChange: () => {
const { matrixtableMajorKey, matrixData } = get();
const newKey = matrixtableMajorKey === 'opi' ? 'lineclass' : 'opi';
const dataKey = matrixtableMajorKey === 'opi' ? 'LineClassValue' : 'OPIValue' ;
set({ matrixtableMajorKey: newKey, matrixTableData: matrixData?.[dataKey] });
},
// sub
})),
{ name: 'SalesInsight' }
)
);
export default useSalesInsightStore;