feat: 实现出库导入、区块链存证、查询日志、看板与系统设置

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nixevol
2026-06-28 16:25:03 +08:00
co-authored by Cursor
parent 63c0488ff7
commit 23cbf37aa1
20 changed files with 720 additions and 6 deletions
@@ -0,0 +1,16 @@
import { Controller, Get } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { DashboardService } from './dashboard.service';
@ApiTags('数据看板')
@ApiBearerAuth()
@Controller('dashboard')
export class DashboardController {
constructor(private readonly dashboardService: DashboardService) {}
@Get('overview')
@ApiOperation({ summary: '看板总览' })
overview() {
return this.dashboardService.overview();
}
}
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { DashboardService } from './dashboard.service';
import { DashboardController } from './dashboard.controller';
@Module({
controllers: [DashboardController],
providers: [DashboardService],
})
export class DashboardModule {}
@@ -0,0 +1,76 @@
import { Injectable } from '@nestjs/common';
import dayjs from 'dayjs';
import { QueryResult, TraceCodeStatus } from '@artveris/shared';
import { PrismaService } from '../prisma/prisma.service';
@Injectable()
export class DashboardService {
constructor(private readonly prisma: PrismaService) {}
async overview() {
const todayStart = dayjs().startOf('day').toDate();
const [
totalArtworks,
boundCodes,
activatedCodes,
totalQueries,
todayQueries,
abnormalQueries,
] = await this.prisma.$transaction([
this.prisma.artwork.count({ where: { deletedAt: null } }),
this.prisma.traceCode.count({ where: { artworkId: { not: null } } }),
this.prisma.traceCode.count({ where: { status: TraceCodeStatus.ACTIVE } }),
this.prisma.queryLog.count(),
this.prisma.queryLog.count({ where: { queriedAt: { gte: todayStart } } }),
this.prisma.queryLog.count({
where: { queryResult: { in: [QueryResult.INVALID, QueryResult.DISABLED, QueryResult.ABNORMAL] } },
}),
]);
return {
totalArtworks,
boundCodes,
activatedCodes,
totalQueries,
todayQueries,
abnormalQueries,
trend: await this.trend(7),
topArtworks: await this.topArtworks(5),
};
}
// 近 n 天查询趋势
async trend(days: number) {
const start = dayjs().startOf('day').subtract(days - 1, 'day');
const logs = await this.prisma.queryLog.findMany({
where: { queriedAt: { gte: start.toDate() } },
select: { queriedAt: true },
});
const buckets: Record<string, number> = {};
for (let i = 0; i < days; i++) {
buckets[start.add(i, 'day').format('MM-DD')] = 0;
}
for (const l of logs) {
const k = dayjs(l.queriedAt).format('MM-DD');
if (k in buckets) buckets[k] += 1;
}
return Object.entries(buckets).map(([date, count]) => ({ date, count }));
}
// 高频查询作品榜
async topArtworks(limit: number) {
const codes = await this.prisma.traceCode.findMany({
where: { artworkId: { not: null }, queryCount: { gt: 0 } },
orderBy: { queryCount: 'desc' },
take: limit,
include: { artwork: { select: { id: true, title: true } } },
});
return codes.map((c) => ({
artworkId: c.artworkId,
title: c.artwork?.title ?? '-',
shortCode: c.shortCode,
queryCount: c.queryCount,
}));
}
}