From d8a6b346abd88451ea7628ae3f54430e4f3198ee Mon Sep 17 00:00:00 2001 From: puriphatt Date: Wed, 4 Sep 2024 14:45:53 +0700 Subject: [PATCH] refactor(04): calculate stat --- src/pages/04_product-service/MainPage.vue | 35 +++++++++++++++++++---- src/stores/product-service/index.ts | 14 +++++++-- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/pages/04_product-service/MainPage.vue b/src/pages/04_product-service/MainPage.vue index a43ce9d3..2b2beb86 100644 --- a/src/pages/04_product-service/MainPage.vue +++ b/src/pages/04_product-service/MainPage.vue @@ -1257,7 +1257,23 @@ function openPropertiesDialog(type: 'service' | 'work') { propertiesDialog.value = true; } -async function calculateStats() { +async function calculateStats(type?: 'service' | 'product') { + if (type === 'service' && productMode.value === 'service') { + const resStatsService = await fetchStatsService({ + productGroupId: currentIdGrop.value, + }); + stat.value[1].count = resStatsService ?? 0; + return; + } + + if (type === 'product' && productMode.value === 'product') { + const resStatsProduct = await fetchStatsProduct({ + productGroupId: currentIdGrop.value, + }); + stat.value[2].count = resStatsProduct ?? 0; + return; + } + const resStatsGroup = await fetchStatsProductGroup(); const resStatsService = await fetchStatsService(); const resStatsProduct = await fetchStatsProduct(); @@ -1321,6 +1337,7 @@ async function enterGroup( toService?: boolean, ) { expandedTree.value = []; + filterStat.value = []; expandedTree.value.push(id); pathGroupName.value = name; currentIdType.value = ''; @@ -1368,6 +1385,7 @@ async function enterNext(type: 'service' | 'product') { } if (type === 'service') { + console.log('service'); productMode.value = 'service'; productAndServiceTab.value = 'service'; currentIdType.value = 'type'; @@ -1384,7 +1402,6 @@ async function enterNext(type: 'service' | 'product') { filterStat.value.push('group'); filterStat.value.push('service'); } - flowStore.rotate(); } @@ -1555,8 +1572,17 @@ watch( }, ); -watch([currentStatusList, productMode], () => { +watch([currentStatusList, productMode], async () => { currentNoAction.value = handleStatus(); + if (productMode.value === 'group') { + await calculateStats(); + } + if (productMode.value === 'service') { + await calculateStats('service'); + } + if (productMode.value === 'product') { + await calculateStats('product'); + } }); @@ -1716,7 +1742,6 @@ watch([currentStatusList, productMode], () => { currentIdType = ''; currentStatusList = []; productMode = 'group'; - return; } } @@ -1743,10 +1768,8 @@ watch([currentStatusList, productMode], () => { if (currentIdType === v.id) { expandedTree.pop(); currentIdType = ''; - filterStat = []; productMode = 'group'; - return; } } diff --git a/src/stores/product-service/index.ts b/src/stores/product-service/index.ts index e7c940c8..bf5d465d 100644 --- a/src/stores/product-service/index.ts +++ b/src/stores/product-service/index.ts @@ -413,8 +413,18 @@ const useProductServiceStore = defineStore('api-product-service', () => { } // Service - async function fetchStatsService() { - const res = await api.get('/service/stats'); + async function fetchStatsService(opts?: { productGroupId?: string }) { + const params = new URLSearchParams(); + + for (const [k, v] of Object.entries(opts || {})) { + v !== undefined && params.append(k, v.toString()); + } + + const query = params.toString(); + + const res = await api.get( + `/service/stats${(params && '?'.concat(query)) || ''}`, + ); if (!res) return false;