80 lines
2.5 KiB
TypeScript
80 lines
2.5 KiB
TypeScript
import { Controller, Get, Query } from '@nestjs/common';
|
|
import { ApiBearerAuth, ApiOperation, ApiPropertyOptional, ApiTags } from '@nestjs/swagger';
|
|
import { IsEnum, IsOptional, IsString } from 'class-validator';
|
|
import { Prisma } from '@prisma/client';
|
|
import { QueryResult } from '@artveris/shared';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
import { buildPage } from '../common/utils/pagination.util';
|
|
import { PaginationDto } from '../common/dto/pagination.dto';
|
|
|
|
class QueryLogQueryDto extends PaginationDto {
|
|
@ApiPropertyOptional({ description: '关键词(短码 / 追溯码 / SKU / 档案编号)' })
|
|
@IsOptional()
|
|
@IsString()
|
|
keyword?: string;
|
|
|
|
@ApiPropertyOptional({ description: 'SKU' })
|
|
@IsOptional()
|
|
@IsString()
|
|
sku?: string;
|
|
|
|
@ApiPropertyOptional({ description: '批次编号' })
|
|
@IsOptional()
|
|
@IsString()
|
|
batchNo?: string;
|
|
|
|
@ApiPropertyOptional({ description: '查询结果', enum: QueryResult })
|
|
@IsOptional()
|
|
@IsEnum(QueryResult)
|
|
result?: QueryResult;
|
|
|
|
@ApiPropertyOptional({ description: '开始时间' })
|
|
@IsOptional()
|
|
@IsString()
|
|
startTime?: string;
|
|
|
|
@ApiPropertyOptional({ description: '结束时间' })
|
|
@IsOptional()
|
|
@IsString()
|
|
endTime?: string;
|
|
}
|
|
|
|
@ApiTags('查询日志')
|
|
@ApiBearerAuth()
|
|
@Controller('query-logs')
|
|
export class QueryLogController {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
@Get()
|
|
@ApiOperation({ summary: '查询日志列表' })
|
|
async list(@Query() query: QueryLogQueryDto) {
|
|
const where: Prisma.QueryLogWhereInput = {};
|
|
if (query.keyword) {
|
|
where.OR = [
|
|
{ shortCode: { contains: query.keyword } },
|
|
{ code: { contains: query.keyword } },
|
|
{ sku: { contains: query.keyword } },
|
|
{ archiveNo: { contains: query.keyword } },
|
|
];
|
|
}
|
|
if (query.sku) where.sku = { contains: query.sku };
|
|
if (query.batchNo) where.batchNo = { contains: query.batchNo };
|
|
if (query.result) where.queryResult = query.result;
|
|
if (query.startTime || query.endTime) {
|
|
where.queriedAt = {};
|
|
if (query.startTime) where.queriedAt.gte = new Date(query.startTime);
|
|
if (query.endTime) where.queriedAt.lte = new Date(query.endTime);
|
|
}
|
|
const [list, total] = await this.prisma.$transaction([
|
|
this.prisma.queryLog.findMany({
|
|
where,
|
|
orderBy: { id: 'desc' },
|
|
skip: query.skip,
|
|
take: query.take,
|
|
}),
|
|
this.prisma.queryLog.count({ where }),
|
|
]);
|
|
return buildPage(list, total, query.page, query.pageSize);
|
|
}
|
|
}
|