完善评查详情

This commit is contained in:
2025-04-18 15:41:43 +08:00
parent 119f9197b2
commit 01d93522b8
25 changed files with 1731 additions and 511 deletions
+249
View File
@@ -0,0 +1,249 @@
import { postgrestGet, type PostgrestParams } from "../postgrest-client";
import dayjs from 'dayjs';
/**
* 从不同格式的 API 响应中提取数据
* @param responseData API 响应数据
* @returns 提取后的数据或 null
*/
function extractApiData<T>(responseData: unknown): T | null {
if (!responseData) return null;
// 格式1: { code: number, msg: string, data: T }
if (typeof responseData === 'object' && responseData !== null &&
'code' in responseData &&
'data' in responseData &&
(responseData as { data: unknown }).data) {
return (responseData as { data: T }).data;
}
// 格式2: 直接是数据对象
return responseData as T;
}
/**
* 评估结果类型定义
*/
interface EvaluationResult {
result: boolean;
rule_id?: string;
rule_name?: string;
description?: string;
[key: string]: unknown;
}
/**
* 首页数据统计响应类型
*/
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;
};
}
/**
* 获取主页数据
* @returns 主页数据
*/
export async function getHomeData(): Promise<HomeStatistics> {
try {
// 获取当前日期和时间相关值
const startOfToday = dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss');
const startOfThisMonth = dayjs().startOf('month').format('YYYY-MM-DD HH:mm:ss');
const endOfThisMonth = dayjs().endOf('month').format('YYYY-MM-DD HH:mm:ss');
const startOfLastMonth = dayjs().subtract(1, 'month').startOf('month').format('YYYY-MM-DD HH:mm:ss');
const endOfLastMonth = dayjs().subtract(1, 'month').endOf('month').format('YYYY-MM-DD HH:mm:ss');
// 1. 今日待审核文件 - 获取今天的待审核文件数量 (audit_status = 0 或 2)
const todayPendingParams: PostgrestParams = {
select: 'count',
filter: {
or: `(audit_status.eq.0,audit_status.eq.2)`,
created_at: `gte.${startOfToday}`
}
};
const todayPendingResponse = await postgrestGet('documents', todayPendingParams);
const todayPendingCount = extractApiData<{count: number}[]>(todayPendingResponse.data);
const todayPendingFiles = todayPendingCount?.[0]?.count || 0;
// 2. 本月已审核文件 - 获取本月已审核文件数量 (audit_status != 0 且 != 2)
const thisMonthReviewedParams: PostgrestParams = {
select: 'count',
filter: {
and: `(audit_status.neq.0,audit_status.neq.2)`,
created_at: `gte.${startOfThisMonth}`,
created_at_lte: `lte.${endOfThisMonth}`
}
};
const thisMonthReviewedResponse = await postgrestGet('documents', thisMonthReviewedParams);
const thisMonthReviewedCount = extractApiData<{count: number}[]>(thisMonthReviewedResponse.data);
const monthlyReviewedFiles = thisMonthReviewedCount?.[0]?.count || 0;
// 上月已审核文件
const lastMonthReviewedParams: PostgrestParams = {
select: 'count',
filter: {
and: `(audit_status.neq.0,audit_status.neq.2)`,
created_at: `gte.${startOfLastMonth}`,
created_at_lte: `lte.${endOfLastMonth}`
}
};
const lastMonthReviewedResponse = await postgrestGet('documents', lastMonthReviewedParams);
const lastMonthReviewedCount = extractApiData<{count: number}[]>(lastMonthReviewedResponse.data);
const lastMonthReviewed = lastMonthReviewedCount?.[0]?.count || 0;
// 计算同比增长
let reviewGrowthValue = 0;
let reviewGrowthIsUp = true;
if (lastMonthReviewed > 0) {
const growthRate = ((monthlyReviewedFiles - lastMonthReviewed) / lastMonthReviewed) * 100;
reviewGrowthValue = Math.abs(parseFloat(growthRate.toFixed(1)));
reviewGrowthIsUp = growthRate >= 0;
}
// 3. 审核通过率 - 本月审核通过率 (已审核文件 / 总待审核文件 + 已审核文件)
const thisMonthTotalParams: PostgrestParams = {
select: 'count',
filter: {
created_at: `gte.${startOfThisMonth}`,
created_at_lte: `lte.${endOfThisMonth}`
}
};
const thisMonthTotalResponse = await postgrestGet('documents', thisMonthTotalParams);
const thisMonthTotalCount = extractApiData<{count: number}[]>(thisMonthTotalResponse.data);
const thisMonthTotal = thisMonthTotalCount?.[0]?.count || 0;
// 本月审核通过率
const monthlyPassRate = thisMonthTotal > 0
? parseFloat(((monthlyReviewedFiles / thisMonthTotal) * 100).toFixed(1))
: 0;
// 上月审核通过率
const lastMonthTotalParams: PostgrestParams = {
select: 'count',
filter: {
created_at: `gte.${startOfLastMonth}`,
created_at_lte: `lte.${endOfLastMonth}`
}
};
const lastMonthTotalResponse = await postgrestGet('documents', lastMonthTotalParams);
const lastMonthTotalCount = extractApiData<{count: number}[]>(lastMonthTotalResponse.data);
const lastMonthTotal = lastMonthTotalCount?.[0]?.count || 0;
const lastMonthPassRate = (lastMonthTotal > 0 && lastMonthReviewed > 0)
? (lastMonthReviewed / lastMonthTotal) * 100
: 0;
// 计算通过率同比增长
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;
}
// 4. 检查出的问题总数(从评估结果表中统计)
const thisMonthIssuesParams: PostgrestParams = {
select: 'evaluated_results',
filter: {
created_at: `gte.${startOfThisMonth}`,
created_at_lte: `lte.${endOfThisMonth}`
}
};
const thisMonthIssuesResponse = await postgrestGet('evaluation_results', thisMonthIssuesParams);
const thisMonthIssuesData = extractApiData<{ evaluated_results: EvaluationResult[] }[]>(thisMonthIssuesResponse.data);
// 累计本月问题数量
let thisMonthIssuesCount = 0;
if (thisMonthIssuesData && thisMonthIssuesData.length > 0) {
thisMonthIssuesData.forEach(row => {
if (row.evaluated_results && Array.isArray(row.evaluated_results)) {
thisMonthIssuesCount += row.evaluated_results.filter((result: EvaluationResult) =>
result && result.result === false
).length;
}
});
}
// 上月问题数量
const lastMonthIssuesParams: PostgrestParams = {
select: 'evaluated_results',
filter: {
created_at: `gte.${startOfLastMonth}`,
created_at_lte: `lte.${endOfLastMonth}`
}
};
const lastMonthIssuesResponse = await postgrestGet('evaluation_results', lastMonthIssuesParams);
const lastMonthIssuesData = extractApiData<{ evaluated_results: EvaluationResult[] }[]>(lastMonthIssuesResponse.data);
let lastMonthIssuesCount = 0;
if (lastMonthIssuesData && lastMonthIssuesData.length > 0) {
lastMonthIssuesData.forEach(row => {
if (row.evaluated_results && Array.isArray(row.evaluated_results)) {
lastMonthIssuesCount += row.evaluated_results.filter((result: EvaluationResult) =>
result && result.result === false
).length;
}
});
}
// 计算问题数量同比增长
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,
monthlyReviewedFiles,
monthlyReviewGrowth: {
value: reviewGrowthValue,
isUp: reviewGrowthIsUp
},
monthlyPassRate,
passRateGrowth: {
value: passRateGrowthValue,
isUp: passRateGrowthIsUp
},
issuesDetected: thisMonthIssuesCount,
issuesGrowth: {
value: issuesGrowthValue,
isUp: issuesGrowthIsUp
}
};
} catch (error) {
console.error('获取首页数据失败:', error);
// 返回默认值以防止页面崩溃
return {
todayPendingFiles: 0,
monthlyReviewedFiles: 0,
monthlyReviewGrowth: { value: 0, isUp: true },
monthlyPassRate: 0,
passRateGrowth: { value: 0, isUp: true },
issuesDetected: 0,
issuesGrowth: { value: 0, isUp: true }
};
}
}