84 lines
2.8 KiB
TypeScript
84 lines
2.8 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import type { ActorType, TicketType } from '@prisma/client';
|
|
import { Prisma } from '@prisma/client';
|
|
import { PrismaService } from '../../common/prisma/prisma.module';
|
|
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
|
|
import type { TicketListQueryDto } from './dto/common-query.dto';
|
|
import type { AssignTicketDto, CreateTicketDto, UpdateTicketStatusDto } from './dto/common-mutate.dto';
|
|
|
|
function generateTicketNo() {
|
|
return `TK${Date.now()}${Math.floor(Math.random() * 900 + 100)}`;
|
|
}
|
|
|
|
@Injectable()
|
|
export class TicketService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
async create(dto: CreateTicketDto) {
|
|
const ticket = await this.prisma.commonTicket.create({
|
|
data: {
|
|
ticketNo: generateTicketNo(),
|
|
ticketType: dto.ticketType as TicketType,
|
|
refType: dto.refType,
|
|
refId: BigInt(dto.refId),
|
|
remark: dto.remark,
|
|
param1: dto.param1,
|
|
param1Desc: dto.param1Desc,
|
|
},
|
|
});
|
|
return serializeBigInt(ticket);
|
|
}
|
|
|
|
async list(query: TicketListQueryDto) {
|
|
const page = query.page ?? 1;
|
|
const pageSize = query.pageSize ?? 20;
|
|
const where: Prisma.CommonTicketWhereInput = {};
|
|
if (query.ticketType) where.ticketType = query.ticketType as Prisma.EnumTicketTypeFilter['equals'];
|
|
if (query.status) where.status = query.status;
|
|
if (query.refType) where.refType = query.refType;
|
|
if (query.refId) where.refId = BigInt(query.refId);
|
|
|
|
const [items, total] = await Promise.all([
|
|
this.prisma.commonTicket.findMany({
|
|
where,
|
|
orderBy: { createdAt: 'desc' },
|
|
skip: (page - 1) * pageSize,
|
|
take: pageSize,
|
|
}),
|
|
this.prisma.commonTicket.count({ where }),
|
|
]);
|
|
return serializeBigInt({ items, total, page, pageSize });
|
|
}
|
|
|
|
async detail(id: bigint) {
|
|
const ticket = await this.prisma.commonTicket.findUnique({ where: { id } });
|
|
if (!ticket) throw new NotFoundException('工单不存在');
|
|
return serializeBigInt(ticket);
|
|
}
|
|
|
|
async updateStatus(id: bigint, dto: UpdateTicketStatusDto) {
|
|
await this.detail(id);
|
|
const ticket = await this.prisma.commonTicket.update({
|
|
where: { id },
|
|
data: {
|
|
status: dto.status,
|
|
remark: dto.remark,
|
|
completedAt: ['COMPLETED', 'CLOSED', 'RESOLVED'].includes(dto.status) ? new Date() : undefined,
|
|
},
|
|
});
|
|
return serializeBigInt(ticket);
|
|
}
|
|
|
|
async assign(id: bigint, dto: AssignTicketDto) {
|
|
await this.detail(id);
|
|
const ticket = await this.prisma.commonTicket.update({
|
|
where: { id },
|
|
data: {
|
|
operatorType: dto.operatorType as ActorType,
|
|
operatorId: BigInt(dto.operatorId),
|
|
},
|
|
});
|
|
return serializeBigInt(ticket);
|
|
}
|
|
}
|