perf: 老客户. 明细; -->zustand
parent
d9fccc5433
commit
259afa80f1
@ -0,0 +1,171 @@
|
|||||||
|
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 { formatDate, isEmpty, sortBy } from '@haina/utils-commons';
|
||||||
|
import { groupsMappedByKey, sitesMappedByCode, pivotBy } from './../libs/ht';
|
||||||
|
|
||||||
|
const defaultParams = {};
|
||||||
|
|
||||||
|
export const fetchRegularCustomer = async (params) => {
|
||||||
|
const _params = {
|
||||||
|
Website: params.WebCode || '', // CHT,AH,JH,GH,ZWQD,GH_ZWQD_HW,GHKYZG,GHKYHW,HTravel
|
||||||
|
DEI_SNList: params.DepartmentList || '', // 1,2,28,7
|
||||||
|
ApplydateCheck: params.DateType === 'applyDate' ? 1 : 0, //
|
||||||
|
EntrancedateCheck: params.DateType === 'startDate' ? 1 : 0, //
|
||||||
|
ConfirmDateCheck: params.DateType === 'confirmDate' ? 1 : 0, //
|
||||||
|
ApplydateStart: params.Date1 || '',
|
||||||
|
ApplydateEnd: params.Date2 || '',
|
||||||
|
EntrancedateStart: params.Date1 || '',
|
||||||
|
EntrancedateEnd: params.Date2 || '',
|
||||||
|
ConfirmdateStart: params.Date1 || '',
|
||||||
|
ConfirmdateEnd: params.Date2 || '',
|
||||||
|
IsDetail: '', //
|
||||||
|
IncludeTickets: '', //
|
||||||
|
...params,
|
||||||
|
};
|
||||||
|
const { WebCode, DepartmentList, DateType, Date1, Date2, ...readyParams } = _params;
|
||||||
|
const [result1, result2] = await Promise.all([
|
||||||
|
fetchJSON(HT_HOST + '/service-tourdesign/RegularCusOrder', { ...defaultParams, ...readyParams }),
|
||||||
|
...(params.DateDiff1 && params.IsDetail === 0
|
||||||
|
? [
|
||||||
|
fetchJSON(HT_HOST + '/service-tourdesign/RegularCusOrder', {
|
||||||
|
...defaultParams,
|
||||||
|
...readyParams,
|
||||||
|
ApplydateStart: params.DateDiff1 || '',
|
||||||
|
ApplydateEnd: params.DateDiff2 || '',
|
||||||
|
EntrancedateStart: params.DateDiff1 || '',
|
||||||
|
EntrancedateEnd: params.DateDiff2 || '',
|
||||||
|
ConfirmdateStart: params.DateDiff1 || '',
|
||||||
|
ConfirmdateEnd: params.DateDiff2 || '',
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
: []),
|
||||||
|
]);
|
||||||
|
if (params.IsDetail === 1) {
|
||||||
|
return { result1, result2 };
|
||||||
|
}
|
||||||
|
const ret = {};
|
||||||
|
const result1Mapped = result1.reduce((r, v) => ({ ...r, [v.ItemName]: v }), {});
|
||||||
|
const allKeys = [...new Set([...result1.map((e) => e.ItemName), ...result2.map((e) => e.ItemName)])];
|
||||||
|
const result2Mapped = result2.reduce((r, v) => ({ ...r, [v.ItemName]: v }), {});
|
||||||
|
const x = {};
|
||||||
|
allKeys.forEach((key) => {
|
||||||
|
x[key] = { ...(result1Mapped?.[key] || { ItemName: key }), diff: result2Mapped[key] || {} };
|
||||||
|
});
|
||||||
|
ret.result1 = Object.values(x);
|
||||||
|
console.log(ret);
|
||||||
|
return { result1: ret.result1 }; // { result1, result2 };
|
||||||
|
};
|
||||||
|
|
||||||
|
// 老客户: 日期对应的数据字段
|
||||||
|
const dateTypeDataHelper = {
|
||||||
|
applyDate: 'SumOrder',
|
||||||
|
startDate: 'ConfirmOrder',
|
||||||
|
confirmDate: 'ConfirmOrder',
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建 老客户系列数据
|
||||||
|
* * 用明细数据计算
|
||||||
|
* @param {[]} details
|
||||||
|
* @param {string} pivotByOrder
|
||||||
|
* @param {string} pivotByDate
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const buildSeriesDataFromDetails = (details, pivotByOrder, pivotByDate) => {
|
||||||
|
const dataDetail = (details || []).map((ele) => ({
|
||||||
|
...ele,
|
||||||
|
key: ele.COLI_ID,
|
||||||
|
orderState: ele.OrderState,
|
||||||
|
applyDate: formatDate(new Date(ele.COLI_ApplyDate)),
|
||||||
|
startDate: ele.COLI_OrderStartDate,
|
||||||
|
confirmDate: formatDate(new Date(ele.COLI_ConfirmDate)),
|
||||||
|
}));
|
||||||
|
const { data: IsOldData } = pivotBy(
|
||||||
|
dataDetail.filter((ele) => ele.COLI_IsOld === '是'),
|
||||||
|
[['COLI_IsOld'], [], pivotByDate]
|
||||||
|
);
|
||||||
|
const { data: isCusCommendData } = pivotBy(
|
||||||
|
dataDetail.filter((ele) => ele.COLI_IsCusCommend === '是'),
|
||||||
|
[['COLI_IsCusCommend'], [], pivotByDate]
|
||||||
|
);
|
||||||
|
// console.log('IsOldData====', IsOldData, '\nisCusCommend', isCusCommendData);
|
||||||
|
// 合并成两个系列
|
||||||
|
const seriesData = []
|
||||||
|
.concat(
|
||||||
|
IsOldData.map((ele) => ({ ...ele, _ylabel: '老客户' })),
|
||||||
|
isCusCommendData.map((ele) => ({ ...ele, _ylabel: '老客户推荐' }))
|
||||||
|
)
|
||||||
|
.sort(sortBy(pivotByDate));
|
||||||
|
return seriesData;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* --------------------------------------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
const initialState = {
|
||||||
|
loading: false,
|
||||||
|
loading2: false,
|
||||||
|
searchValues: {
|
||||||
|
DepartmentList: ['1', '2', '28', '7'].map((kk) => groupsMappedByKey[kk]),
|
||||||
|
WebCode: ['CHT', 'AH', 'JH', 'GH', 'ZWQD', 'GH_ZWQD_HW', 'GHKYZG', 'GHKYHW', 'HTravel'].map((kk) => sitesMappedByCode[kk]),
|
||||||
|
DateType: { key: 'applyDate', label: '提交日期' },
|
||||||
|
IncludeTickets: { key: '0', label: '不含门票' },
|
||||||
|
},
|
||||||
|
searchValuesToSub: {},
|
||||||
|
|
||||||
|
regular: { data: [], details: [], total_data_tips: '', pivotData: [], pivotY: 'SumOrder', pivotX: '' },
|
||||||
|
};
|
||||||
|
|
||||||
|
const useCustomerRelationsStore = create(
|
||||||
|
devtools(
|
||||||
|
immer((set, get) => ({
|
||||||
|
...initialState,
|
||||||
|
reset: () => set(initialState),
|
||||||
|
|
||||||
|
setLoading: (loading) => set({ loading }),
|
||||||
|
setLoading2: (loading2) => set({ loading2 }),
|
||||||
|
setSearchValues: (obj, values) => set((state) => ({ searchValues: values, searchValuesToSub: obj })),
|
||||||
|
setSearchValuesToSub: (values) => set((state) => ({ searchValuesToSub: values })),
|
||||||
|
|
||||||
|
// 获取数据 ---------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// 老客户
|
||||||
|
getRegularCustomer: async (params) => {
|
||||||
|
const { setLoading, setLoading2 } = get();
|
||||||
|
const { IsDetail } = params;
|
||||||
|
setLoading(true);
|
||||||
|
setLoading2(IsDetail === 1);
|
||||||
|
const pivotByOrder = dateTypeDataHelper[params.DateType];
|
||||||
|
const pivotByDate = params.DateType;
|
||||||
|
try {
|
||||||
|
const {result1, result2} = await fetchRegularCustomer(params);
|
||||||
|
set((state) => {
|
||||||
|
if (IsDetail === 1) {
|
||||||
|
state.regular.details = result1;
|
||||||
|
|
||||||
|
const dump_l = (result1 || []).filter((ele) => ele.COLI_IsOld !== '' && ele.COLI_IsCusCommend !== '').length;
|
||||||
|
state.regular.total_data_tips = dump_l > 0 ? `包含 ${dump_l} 条同时勾选的数据` : '';
|
||||||
|
/** 使用明细数据画图 */
|
||||||
|
const seriesData = buildSeriesDataFromDetails(result1, pivotByOrder, pivotByDate);
|
||||||
|
state.regular.pivotData = seriesData;
|
||||||
|
state.regular.pivotX = pivotByDate;
|
||||||
|
state.regular.pivotY = pivotByOrder;
|
||||||
|
} else {
|
||||||
|
state.regular.data = result1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
IsDetail === 1 && setLoading2(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
{ name: 'CustomerRelations' }
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
export default useCustomerRelationsStore;
|
||||||
Loading…
Reference in New Issue