From 202d64efd6638135311a4b659945e3daef1d13a8 Mon Sep 17 00:00:00 2001 From: PingChuan <1259732256@qq.com> Date: Fri, 30 May 2025 16:14:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=B3=BB=E7=BB=9F=E6=A6=82?= =?UTF-8?q?=E8=A7=88=E4=B8=AD=E7=BB=9F=E8=AE=A1=E4=BF=A1=E6=81=AF=E7=9A=84?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/home/home.ts | 88 +++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 38 deletions(-) diff --git a/app/api/home/home.ts b/app/api/home/home.ts index 225acdc..eb8f969 100644 --- a/app/api/home/home.ts +++ b/app/api/home/home.ts @@ -1,3 +1,4 @@ +import { log } from "node:console"; import { postgrestGet, type PostgrestParams } from "../postgrest-client"; import dayjs from 'dayjs'; @@ -20,8 +21,8 @@ function extractApiData(responseData: unknown): T | null { const code = (responseData as { code: number }).code; // 如果有错误码且不是成功状态 if (code !== 0 && code !== 200) { - const errorMsg = 'msg' in responseData - ? (responseData as { msg: string }).msg + const errorMsg = 'msg' in responseData + ? (responseData as { msg: string }).msg : '未知错误'; console.error(`API响应错误: [${code}] ${errorMsg}`); return null; @@ -58,22 +59,22 @@ function extractApiData(responseData: unknown): T | null { * 首页数据统计响应类型 */ interface HomeStatistics { - todayPendingFiles: number; - monthlyReviewedFiles: number; - monthlyReviewGrowth: { - value: number; - isUp: boolean; - }; - monthlyPassRate: number; - passRateGrowth: { - value: number; - isUp: boolean; - }; - issuesDetected: number; - issuesGrowth: { - value: number; - isUp: boolean; - }; + todayPendingFiles: number; + monthlyReviewedFiles: number; + monthlyReviewGrowth: { + value: number; + isUp: boolean; + }; + monthlyPassRate: number; + passRateGrowth: { + value: number; + isUp: boolean; + }; + issuesDetected: number; + issuesGrowth: { + value: number; + isUp: boolean; + }; } /** @@ -91,13 +92,13 @@ export async function getHomeData(): Promise { // 通用API响应处理函数 const handleApiResponse = async ( - apiCall: Promise<{ - data?: unknown; + apiCall: Promise<{ + data?: unknown; headers?: Record; - error?: string; - status?: number - }>, - errorMessage: string, + error?: string; + status?: number + }>, + errorMessage: string, defaultValue: T ): Promise => { try { @@ -127,7 +128,7 @@ export async function getHomeData(): Promise { is_test_document: `eq.false` } }; - const todayPendingCount = await handleApiResponse<{count: number}[]>( + const todayPendingCount = await handleApiResponse<{ count: number }[]>( postgrestGet('documents', todayPendingParams), '获取今日待审核文件数量失败', [] @@ -143,7 +144,7 @@ export async function getHomeData(): Promise { is_test_document: `eq.false` } }; - const thisMonthReviewedCount = await handleApiResponse<{count: number}[]>( + const thisMonthReviewedCount = await handleApiResponse<{ count: number }[]>( postgrestGet('documents', thisMonthReviewedParams), '获取本月已审核文件数量失败', [] @@ -160,23 +161,24 @@ export async function getHomeData(): Promise { is_test_document: `eq.false` } }; - const lastMonthReviewedCount = await handleApiResponse<{count: number}[]>( + const lastMonthReviewedCount = await handleApiResponse<{ count: number }[]>( postgrestGet('documents', lastMonthReviewedParams), '获取上月已审核文件数量失败', [] ); // 上月已审核文件数量 const lastMonthReviewed = lastMonthReviewedCount[0]?.count || 0; - + // 计算同比增长 let reviewGrowthValue = 0; let reviewGrowthIsUp = true; - // console.log('lastMonthReviewed-------', lastMonthReviewed); - // console.log('monthlyReviewedFiles-------', monthlyReviewedFiles); if (lastMonthReviewed > 0) { const growthRate = ((monthlyReviewedFiles - lastMonthReviewed) / lastMonthReviewed) * 100; reviewGrowthValue = Math.abs(parseFloat(growthRate.toFixed(1))); reviewGrowthIsUp = growthRate >= 0; + } else if (lastMonthReviewed == 0 && monthlyReviewedFiles > 0) { + reviewGrowthValue = 100; + reviewGrowthIsUp = true; } // 3. 审核通过率 - 本月审核通过率 @@ -188,7 +190,7 @@ export async function getHomeData(): Promise { is_test_document: `eq.false` } }; - const thisMonthTotalCount = await handleApiResponse<{count: number}[]>( + const thisMonthTotalCount = await handleApiResponse<{ count: number }[]>( postgrestGet('documents', thisMonthTotalParams), '获取本月审核通过数量失败', [] @@ -198,7 +200,7 @@ export async function getHomeData(): Promise { // 本月审核通过率 const monthlyPassRate = (thisMonthPassTotal > 0 && monthlyReviewedFiles > 0) - ? parseFloat(((thisMonthPassTotal / monthlyReviewedFiles) * 100).toFixed(1)) + ? parseFloat(((thisMonthPassTotal / monthlyReviewedFiles) * 100).toFixed(1)) : 0; // 上月审核通过率 @@ -210,7 +212,7 @@ export async function getHomeData(): Promise { is_test_document: `eq.false` } }; - const lastMonthTotalCount = await handleApiResponse<{count: number}[]>( + const lastMonthTotalCount = await handleApiResponse<{ count: number }[]>( postgrestGet('documents', lastMonthTotalParams), '获取上月审核通过数量失败', [] @@ -223,16 +225,26 @@ export async function getHomeData(): Promise { ? parseFloat(((lastMonthTotal / lastMonthReviewed) * 100).toFixed(1)) : 0; + console.log('上个月-------', lastMonthPassRate); + // 计算通过率同比增长 let passRateGrowthValue = 0; let passRateGrowthIsUp = true; - + + + if (lastMonthPassRate > 0) { const passRateGrowth = ((monthlyPassRate - lastMonthPassRate) / lastMonthPassRate) * 100; passRateGrowthValue = Math.abs(parseFloat(passRateGrowth.toFixed(1))); passRateGrowthIsUp = passRateGrowth >= 0; + } else if (lastMonthPassRate == 0 && monthlyPassRate > 0) { + passRateGrowthValue = 100; + passRateGrowthIsUp = true; } + console.log('上月通过率-------', lastMonthPassRate); + console.log('本月通过率-------', monthlyPassRate); + // 4. 检查出的问题总数(从评估结果表中统计) const thisMonthIssuesParams: PostgrestParams = { select: 'count', @@ -241,7 +253,7 @@ export async function getHomeData(): Promise { 'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段 } }; - const thisMonthIssuesResponse = await handleApiResponse<{count: number}[]>( + const thisMonthIssuesResponse = await handleApiResponse<{ count: number }[]>( postgrestGet('evaluation_results', thisMonthIssuesParams), '获取本月问题数据失败', [] @@ -257,7 +269,7 @@ export async function getHomeData(): Promise { 'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段 } }; - const lastMonthIssuesResponse = await handleApiResponse<{count: number}[]>( + const lastMonthIssuesResponse = await handleApiResponse<{ count: number }[]>( postgrestGet('evaluation_results', lastMonthIssuesParams), '获取上月问题数据失败', [] @@ -268,13 +280,13 @@ export async function getHomeData(): Promise { // 计算问题数量同比增长 let issuesGrowthValue = 0; let issuesGrowthIsUp = true; - + + if (lastMonthIssuesCount > 0) { const issuesGrowth = ((thisMonthIssuesCount - lastMonthIssuesCount) / lastMonthIssuesCount) * 100; issuesGrowthValue = Math.abs(parseFloat(issuesGrowth.toFixed(1))); issuesGrowthIsUp = issuesGrowth >= 0; } - // 返回统计结果 return { todayPendingFiles,