import { useQuery, useQueryClient } from '@tanstack/vue-query';
|
import * as standardServiceServices from '@12333/services/apiV2/standardService';
|
import { computed, MaybeRef, unref } from 'vue';
|
import { groupBy } from 'lodash';
|
|
type UseStandardServiceDetailOptions = {
|
id: MaybeRef<string>;
|
onSuccess?: (data: API.GetStandardServiceQueryResult) => any;
|
};
|
|
export function useStandardServiceDetail({ id, onSuccess }: UseStandardServiceDetailOptions) {
|
const { data, refetch, isLoading, isError } = useQuery({
|
queryKey: ['standardServiceServices/getStandardService', id],
|
queryFn: async () => {
|
let params: API.APIgetStandardServiceParams = {
|
id: unref(id),
|
};
|
|
return await standardServiceServices.getStandardService(params, {
|
showLoading: false,
|
});
|
},
|
placeholderData: () => ({} as API.GetStandardServiceQueryResult),
|
onSuccess(data) {
|
onSuccess?.(data);
|
},
|
});
|
|
const minPrice = computed(() => {
|
if (!data.value?.specs?.length) {
|
return 0;
|
} else {
|
return Math.min(...data.value.specs.map((x) => x.price));
|
}
|
});
|
|
return {
|
detail: data,
|
refetch,
|
isLoading,
|
isError,
|
minPrice,
|
};
|
}
|
|
type UseStandardServiceListOptions = {
|
params?: MaybeRef<API.GetOpenStandardServiceListQuery>;
|
onSuccess?: (data: API.GetStandardServicesQueryResultItem[]) => any;
|
};
|
|
export function useStandardServiceList(options: UseStandardServiceListOptions = {}) {
|
const { params = {}, onSuccess } = options;
|
const {
|
data: standardServiceList,
|
refetch,
|
isLoading,
|
isError,
|
} = useQuery({
|
queryKey: ['standardServiceServices/getOpenStandardServiceList', params],
|
queryFn: async () => {
|
return await standardServiceServices.getOpenStandardServiceList(unref(params), {
|
showLoading: false,
|
});
|
},
|
placeholderData: () => [] as API.GetStandardServicesQueryResultItem[],
|
onSuccess(data) {
|
onSuccess?.(data);
|
},
|
});
|
|
const standardServiceListForCategory = computed(() => {
|
return standardServiceList.value.map((x) => ({
|
...x,
|
catName: x.name,
|
}));
|
});
|
|
const standardServiceListForCategoryMap = computed(() => {
|
const group = groupBy(standardServiceListForCategory.value, 'jobCode');
|
return group;
|
});
|
|
const queryClient = useQueryClient();
|
|
function ensureStandardServiceList() {
|
return queryClient.ensureQueryData<API.GetStandardServicesQueryResultItem[]>({
|
queryKey: ['standardServiceServices/getOpenStandardServiceList', params],
|
});
|
}
|
|
return {
|
standardServiceList,
|
ensureStandardServiceList,
|
standardServiceListForCategory,
|
standardServiceListForCategoryMap,
|
};
|
}
|