添加合同和卷宗数据隔离
This commit is contained in:
+243
-20
@@ -1,4 +1,3 @@
|
||||
import { log } from "node:console";
|
||||
import { postgrestGet, type PostgrestParams } from "../postgrest-client";
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
@@ -77,11 +76,27 @@ interface HomeStatistics {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过传入的 reviewType 参数构建类型过滤条件
|
||||
* @param reviewType 文档类型
|
||||
* @returns 过滤条件字符串
|
||||
*/
|
||||
function buildTypeFilter(reviewType: string | null): string {
|
||||
let typeFilter = '';
|
||||
if (reviewType === 'contract') {
|
||||
typeFilter = 'type_id.eq.1';
|
||||
} else if (reviewType === 'record') {
|
||||
typeFilter = '(type_id.eq.2,type_id.eq.3)';
|
||||
}
|
||||
return typeFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主页数据
|
||||
* @param reviewType 从客户端传入的 reviewType 值
|
||||
* @returns 主页数据
|
||||
*/
|
||||
export async function getHomeData(): Promise<HomeStatistics> {
|
||||
export async function getHomeData(reviewType?: string | null): Promise<HomeStatistics> {
|
||||
try {
|
||||
// 获取当前日期和时间相关值
|
||||
const startOfToday = dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss');
|
||||
@@ -90,6 +105,12 @@ export async function getHomeData(): Promise<HomeStatistics> {
|
||||
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');
|
||||
|
||||
// console.log('传入的 reviewType', reviewType);
|
||||
|
||||
// 基于 reviewType 构建类型过滤条件
|
||||
const typeFilter = buildTypeFilter(reviewType || null);
|
||||
// console.log('构建的 typeFilter', typeFilter);
|
||||
|
||||
// 通用API响应处理函数
|
||||
const handleApiResponse = async <T>(
|
||||
apiCall: Promise<{
|
||||
@@ -128,6 +149,24 @@ export async function getHomeData(): Promise<HomeStatistics> {
|
||||
is_test_document: `eq.false`
|
||||
}
|
||||
};
|
||||
|
||||
// 添加类型过滤条件
|
||||
if (typeFilter) {
|
||||
if (typeFilter.startsWith('(')) {
|
||||
// 确保 filter 已初始化
|
||||
if (!todayPendingParams.filter) {
|
||||
todayPendingParams.filter = {};
|
||||
}
|
||||
todayPendingParams.filter.or = typeFilter + ',' + todayPendingParams.filter.or;
|
||||
} else {
|
||||
const [field, op, value] = typeFilter.split('.');
|
||||
if (!todayPendingParams.filter) {
|
||||
todayPendingParams.filter = {};
|
||||
}
|
||||
todayPendingParams.filter[field] = `${op}.${value}`;
|
||||
}
|
||||
}
|
||||
|
||||
const todayPendingCount = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet('documents', todayPendingParams),
|
||||
'获取今日待审核文件数量失败',
|
||||
@@ -144,6 +183,20 @@ export async function getHomeData(): Promise<HomeStatistics> {
|
||||
is_test_document: `eq.false`
|
||||
}
|
||||
};
|
||||
|
||||
// 添加类型过滤条件
|
||||
if (typeFilter) {
|
||||
if (typeFilter.startsWith('(')) {
|
||||
thisMonthReviewedParams.or = typeFilter;
|
||||
} else {
|
||||
const [field, op, value] = typeFilter.split('.');
|
||||
if (!thisMonthReviewedParams.filter) {
|
||||
thisMonthReviewedParams.filter = {};
|
||||
}
|
||||
thisMonthReviewedParams.filter[field] = `${op}.${value}`;
|
||||
}
|
||||
}
|
||||
|
||||
const thisMonthReviewedCount = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet('documents', thisMonthReviewedParams),
|
||||
'获取本月已审核文件数量失败',
|
||||
@@ -161,6 +214,24 @@ export async function getHomeData(): Promise<HomeStatistics> {
|
||||
is_test_document: `eq.false`
|
||||
}
|
||||
};
|
||||
|
||||
// 添加类型过滤条件
|
||||
if (typeFilter) {
|
||||
if (typeFilter.startsWith('(')) {
|
||||
// 确保 filter 已初始化
|
||||
if (!lastMonthReviewedParams.filter) {
|
||||
lastMonthReviewedParams.filter = {};
|
||||
}
|
||||
lastMonthReviewedParams.filter.or = lastMonthReviewedParams.filter.or + ',' + typeFilter;
|
||||
} else {
|
||||
const [field, op, value] = typeFilter.split('.');
|
||||
if (!lastMonthReviewedParams.filter) {
|
||||
lastMonthReviewedParams.filter = {};
|
||||
}
|
||||
lastMonthReviewedParams.filter[field] = `${op}.${value}`;
|
||||
}
|
||||
}
|
||||
|
||||
const lastMonthReviewedCount = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet('documents', lastMonthReviewedParams),
|
||||
'获取上月已审核文件数量失败',
|
||||
@@ -190,6 +261,20 @@ export async function getHomeData(): Promise<HomeStatistics> {
|
||||
is_test_document: `eq.false`
|
||||
}
|
||||
};
|
||||
|
||||
// 添加类型过滤条件
|
||||
if (typeFilter) {
|
||||
if (typeFilter.startsWith('(')) {
|
||||
thisMonthTotalParams.or = typeFilter;
|
||||
} else {
|
||||
const [field, op, value] = typeFilter.split('.');
|
||||
if (!thisMonthTotalParams.filter) {
|
||||
thisMonthTotalParams.filter = {};
|
||||
}
|
||||
thisMonthTotalParams.filter[field] = `${op}.${value}`;
|
||||
}
|
||||
}
|
||||
|
||||
const thisMonthTotalCount = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet('documents', thisMonthTotalParams),
|
||||
'获取本月审核通过数量失败',
|
||||
@@ -212,6 +297,20 @@ export async function getHomeData(): Promise<HomeStatistics> {
|
||||
is_test_document: `eq.false`
|
||||
}
|
||||
};
|
||||
|
||||
// 添加类型过滤条件
|
||||
if (typeFilter) {
|
||||
if (typeFilter.startsWith('(')) {
|
||||
lastMonthTotalParams.or = typeFilter;
|
||||
} else {
|
||||
const [field, op, value] = typeFilter.split('.');
|
||||
if (!lastMonthTotalParams.filter) {
|
||||
lastMonthTotalParams.filter = {};
|
||||
}
|
||||
lastMonthTotalParams.filter[field] = `${op}.${value}`;
|
||||
}
|
||||
}
|
||||
|
||||
const lastMonthTotalCount = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet('documents', lastMonthTotalParams),
|
||||
'获取上月审核通过数量失败',
|
||||
@@ -246,36 +345,157 @@ export async function getHomeData(): Promise<HomeStatistics> {
|
||||
// console.log('本月通过率-------', monthlyPassRate);
|
||||
|
||||
// 4. 检查出的问题总数(从评估结果表中统计)
|
||||
const thisMonthIssuesParams: PostgrestParams = {
|
||||
// 使用新的数据库函数 count_evaluation_results_by_type 获取指定类型文档的问题数量
|
||||
let thisMonthIssuesCount = 0;
|
||||
let lastMonthIssuesCount = 0;
|
||||
|
||||
// 根据 reviewType 设置要查询的文档类型
|
||||
if (reviewType === 'contract') {
|
||||
// 合同类型 - 直接查询类型 1
|
||||
const typeToQuery = 1;
|
||||
|
||||
// 调用数据库函数获取本月指定类型的问题数量
|
||||
const thisMonthIssuesResponse = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet(`rpc/count_evaluation_results_by_type?type_val=${typeToQuery}&start_time=${startOfThisMonth}&end_time=${endOfThisMonth}`, {
|
||||
select: '*',
|
||||
filter: {}
|
||||
}),
|
||||
'获取本月问题数据失败',
|
||||
[]
|
||||
);
|
||||
|
||||
// 本月问题数量
|
||||
thisMonthIssuesCount = thisMonthIssuesResponse[0]?.count || 0;
|
||||
|
||||
// 调用数据库函数获取上月指定类型的问题数量
|
||||
const lastMonthIssuesResponse = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet(`rpc/count_evaluation_results_by_type?type_val=${typeToQuery}&start_time=${startOfLastMonth}&end_time=${endOfLastMonth}`, {
|
||||
select: '*',
|
||||
filter: {}
|
||||
}),
|
||||
'获取上月问题数据失败',
|
||||
[]
|
||||
);
|
||||
|
||||
// 上月问题数量
|
||||
lastMonthIssuesCount = lastMonthIssuesResponse[0]?.count || 0;
|
||||
|
||||
} else if (reviewType === 'record') {
|
||||
// 记录类型 - 需要查询类型 2 和类型 3,并合并结果
|
||||
|
||||
// 查询类型 2 的本月问题数量
|
||||
const thisMonthType2Response = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet(`rpc/count_evaluation_results_by_type?type_val=2&start_time=${startOfThisMonth}&end_time=${endOfThisMonth}`, {
|
||||
select: '*',
|
||||
filter: {}
|
||||
}),
|
||||
'获取本月类型2问题数据失败',
|
||||
[]
|
||||
);
|
||||
|
||||
// 查询类型 3 的本月问题数量
|
||||
const thisMonthType3Response = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet(`rpc/count_evaluation_results_by_type?type_val=3&start_time=${startOfThisMonth}&end_time=${endOfThisMonth}`, {
|
||||
select: '*',
|
||||
filter: {}
|
||||
}),
|
||||
'获取本月类型3问题数据失败',
|
||||
[]
|
||||
);
|
||||
|
||||
// 合并本月两种类型的问题数量
|
||||
const thisMonthType2Count = thisMonthType2Response[0]?.count || 0;
|
||||
const thisMonthType3Count = thisMonthType3Response[0]?.count || 0;
|
||||
thisMonthIssuesCount = thisMonthType2Count + thisMonthType3Count;
|
||||
|
||||
// 查询类型 2 的上月问题数量
|
||||
const lastMonthType2Response = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet(`rpc/count_evaluation_results_by_type?type_val=2&start_time=${startOfLastMonth}&end_time=${endOfLastMonth}`, {
|
||||
select: '*',
|
||||
filter: {}
|
||||
}),
|
||||
'获取上月类型2问题数据失败',
|
||||
[]
|
||||
);
|
||||
|
||||
// 查询类型 3 的上月问题数量
|
||||
const lastMonthType3Response = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet(`rpc/count_evaluation_results_by_type?type_val=3&start_time=${startOfLastMonth}&end_time=${endOfLastMonth}`, {
|
||||
select: '*',
|
||||
filter: {}
|
||||
}),
|
||||
'获取上月类型3问题数据失败',
|
||||
[]
|
||||
);
|
||||
|
||||
// 合并上月两种类型的问题数量
|
||||
const lastMonthType2Count = lastMonthType2Response[0]?.count || 0;
|
||||
const lastMonthType3Count = lastMonthType3Response[0]?.count || 0;
|
||||
lastMonthIssuesCount = lastMonthType2Count + lastMonthType3Count;
|
||||
|
||||
} else {
|
||||
// 如果没有指定类型,则使用原来的查询方式获取所有类型的问题数量
|
||||
const thisMonthIssuesParams: PostgrestParams = {
|
||||
select: 'count',
|
||||
filter: {
|
||||
and: `(created_at.gte.${startOfThisMonth},created_at.lte.${endOfThisMonth})`,
|
||||
'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段
|
||||
and: `(created_at.gte.${startOfThisMonth},created_at.lte.${endOfThisMonth})`,
|
||||
'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段
|
||||
}
|
||||
};
|
||||
const thisMonthIssuesResponse = await handleApiResponse<{ count: number }[]>(
|
||||
};
|
||||
|
||||
// 添加类型过滤条件
|
||||
if (typeFilter) {
|
||||
if (typeFilter.startsWith('(')) {
|
||||
thisMonthIssuesParams.or = typeFilter;
|
||||
} else {
|
||||
const [field, op, value] = typeFilter.split('.');
|
||||
if (!thisMonthIssuesParams.filter) {
|
||||
thisMonthIssuesParams.filter = {};
|
||||
}
|
||||
thisMonthIssuesParams.filter[field] = `${op}.${value}`;
|
||||
}
|
||||
}
|
||||
|
||||
const thisMonthIssuesResponse = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet('evaluation_results', thisMonthIssuesParams),
|
||||
'获取本月问题数据失败',
|
||||
[]
|
||||
);
|
||||
// 本月问题数量
|
||||
const thisMonthIssuesCount = thisMonthIssuesResponse[0]?.count || 0;
|
||||
|
||||
// 上月问题数量
|
||||
const lastMonthIssuesParams: PostgrestParams = {
|
||||
);
|
||||
|
||||
// 本月问题数量
|
||||
thisMonthIssuesCount = thisMonthIssuesResponse[0]?.count || 0;
|
||||
|
||||
// 上月问题数量
|
||||
const lastMonthIssuesParams: PostgrestParams = {
|
||||
select: 'count',
|
||||
filter: {
|
||||
and: `(created_at.gte.${startOfLastMonth},created_at.lte.${endOfLastMonth})`,
|
||||
'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段
|
||||
and: `(created_at.gte.${startOfLastMonth},created_at.lte.${endOfLastMonth})`,
|
||||
'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段
|
||||
}
|
||||
};
|
||||
const lastMonthIssuesResponse = await handleApiResponse<{ count: number }[]>(
|
||||
};
|
||||
|
||||
// 添加类型过滤条件
|
||||
if (typeFilter) {
|
||||
if (typeFilter.startsWith('(')) {
|
||||
lastMonthIssuesParams.or = typeFilter;
|
||||
} else {
|
||||
const [field, op, value] = typeFilter.split('.');
|
||||
if (!lastMonthIssuesParams.filter) {
|
||||
lastMonthIssuesParams.filter = {};
|
||||
}
|
||||
lastMonthIssuesParams.filter[field] = `${op}.${value}`;
|
||||
}
|
||||
}
|
||||
|
||||
const lastMonthIssuesResponse = await handleApiResponse<{ count: number }[]>(
|
||||
postgrestGet('evaluation_results', lastMonthIssuesParams),
|
||||
'获取上月问题数据失败',
|
||||
[]
|
||||
);
|
||||
// 上月问题数量
|
||||
const lastMonthIssuesCount = lastMonthIssuesResponse[0]?.count || 0;
|
||||
);
|
||||
|
||||
// 上月问题数量
|
||||
lastMonthIssuesCount = lastMonthIssuesResponse[0]?.count || 0;
|
||||
}
|
||||
|
||||
// 计算问题数量同比增长
|
||||
let issuesGrowthValue = 0;
|
||||
@@ -286,6 +506,9 @@ export async function getHomeData(): Promise<HomeStatistics> {
|
||||
const issuesGrowth = ((thisMonthIssuesCount - lastMonthIssuesCount) / lastMonthIssuesCount) * 100;
|
||||
issuesGrowthValue = Math.abs(parseFloat(issuesGrowth.toFixed(1)));
|
||||
issuesGrowthIsUp = issuesGrowth >= 0;
|
||||
}else if(lastMonthIssuesCount == 0 && thisMonthIssuesCount > 0){
|
||||
issuesGrowthValue = 100;
|
||||
issuesGrowthIsUp = true;
|
||||
}
|
||||
// 返回统计结果
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user