refactor: migrate database layer to Prisma ORM
This commit is contained in:
@@ -1,235 +1,281 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import * as QRCode from 'qrcode';
|
||||
import { Serial, SerialListItem } from './dto';
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { DatabaseService } from "../database/database.service";
|
||||
import * as QRCode from "qrcode";
|
||||
import { Serial, SerialListItem } from "./dto";
|
||||
|
||||
@Injectable()
|
||||
export class SerialsService {
|
||||
constructor(private dbService: DatabaseService) {}
|
||||
|
||||
async generate(companyName: string, quantity: number, validDays: number, userId: number, serialPrefix?: string): Promise<SerialListItem[]> {
|
||||
async generate(
|
||||
companyName: string,
|
||||
quantity: number,
|
||||
validDays: number,
|
||||
userId: number,
|
||||
serialPrefix?: string,
|
||||
): Promise<SerialListItem[]> {
|
||||
const prisma = this.dbService.getPrisma();
|
||||
const validUntil = new Date();
|
||||
validUntil.setDate(validUntil.getDate() + validDays);
|
||||
|
||||
const existingCompany = await this.dbService.get('SELECT * FROM companies WHERE company_name = ?', [companyName]);
|
||||
const existingCompany = await prisma.company.findUnique({
|
||||
where: { companyName },
|
||||
});
|
||||
if (!existingCompany) {
|
||||
await this.dbService.run('INSERT INTO companies (company_name, is_active) VALUES (?, 1)', [companyName]);
|
||||
await prisma.company.create({
|
||||
data: { companyName, isActive: true },
|
||||
});
|
||||
}
|
||||
|
||||
const serials: SerialListItem[] = [];
|
||||
const prefix = serialPrefix ? serialPrefix.toUpperCase().replace(/[^A-Z0-9]/g, '') : 'BF' + new Date().getFullYear().toString().substr(2);
|
||||
const prefix = serialPrefix
|
||||
? serialPrefix.toUpperCase().replace(/[^A-Z0-9]/g, "")
|
||||
: "BF" + new Date().getFullYear().toString().substr(2);
|
||||
|
||||
for (let i = 0; i < quantity; i++) {
|
||||
const randomPart = Math.floor(Math.random() * 1000000).toString().padStart(6, '0');
|
||||
const randomPart = Math.floor(Math.random() * 1000000)
|
||||
.toString()
|
||||
.padStart(6, "0");
|
||||
const serialNumber = `${prefix}${randomPart}`;
|
||||
|
||||
await this.dbService.run(
|
||||
'INSERT INTO serials (serial_number, company_name, valid_until, created_by) VALUES (?, ?, ?, ?)',
|
||||
[serialNumber, companyName, validUntil.toISOString().slice(0, 19).replace('T', ' '), userId]
|
||||
);
|
||||
|
||||
await prisma.serial.create({
|
||||
data: {
|
||||
serialNumber,
|
||||
companyName,
|
||||
validUntil,
|
||||
createdBy: userId,
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
|
||||
serials.push({
|
||||
serialNumber,
|
||||
companyName,
|
||||
validUntil: validUntil.toISOString(),
|
||||
isActive: true,
|
||||
createdAt: new Date().toISOString()
|
||||
createdAt: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
return serials;
|
||||
}
|
||||
|
||||
async generateQRCode(serialNumber: string, baseUrl?: string, requestHost?: string, protocol?: string) {
|
||||
const serial = await this.dbService.get<{ serial_number: string; company_name: string; is_active: number; valid_until: string | null }>(
|
||||
'SELECT s.*, u.name as created_by_name FROM serials s LEFT JOIN users u ON s.created_by = u.id WHERE s.serial_number = ?',
|
||||
[serialNumber.toUpperCase()]
|
||||
);
|
||||
async generateQRCode(
|
||||
serialNumber: string,
|
||||
baseUrl?: string,
|
||||
requestHost?: string,
|
||||
protocol?: string,
|
||||
) {
|
||||
const prisma = this.dbService.getPrisma();
|
||||
const serial = await prisma.serial.findUnique({
|
||||
where: { serialNumber: serialNumber.toUpperCase() },
|
||||
include: {
|
||||
user: {
|
||||
select: { name: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!serial) {
|
||||
throw new Error('序列号不存在');
|
||||
throw new Error("序列号不存在");
|
||||
}
|
||||
|
||||
if (!serial.is_active) {
|
||||
throw new Error('序列号已被禁用');
|
||||
if (!serial.isActive) {
|
||||
throw new Error("序列号已被禁用");
|
||||
}
|
||||
|
||||
if (serial.valid_until && new Date(serial.valid_until) < new Date()) {
|
||||
throw new Error('序列号已过期');
|
||||
if (serial.validUntil && new Date(serial.validUntil) < new Date()) {
|
||||
throw new Error("序列号已过期");
|
||||
}
|
||||
|
||||
if (!baseUrl) {
|
||||
baseUrl = `${protocol}://${requestHost}/query.html`;
|
||||
}
|
||||
|
||||
const queryUrl = baseUrl.includes('?')
|
||||
? `${baseUrl}&serial=${serial.serial_number}`
|
||||
: `${baseUrl}?serial=${serial.serial_number}`;
|
||||
|
||||
const queryUrl = baseUrl.includes("?")
|
||||
? `${baseUrl}&serial=${serial.serialNumber}`
|
||||
: `${baseUrl}?serial=${serial.serialNumber}`;
|
||||
|
||||
const qrCodeData = await QRCode.toDataURL(queryUrl, {
|
||||
width: 200,
|
||||
color: {
|
||||
dark: '#165DFF',
|
||||
light: '#ffffff'
|
||||
}
|
||||
dark: "#165DFF",
|
||||
light: "#ffffff",
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
message: '二维码生成成功',
|
||||
message: "二维码生成成功",
|
||||
qrCodeData,
|
||||
queryUrl,
|
||||
serialNumber: serial.serial_number,
|
||||
companyName: serial.company_name,
|
||||
validUntil: serial.valid_until
|
||||
serialNumber: serial.serialNumber,
|
||||
companyName: serial.companyName,
|
||||
validUntil: serial.validUntil,
|
||||
};
|
||||
}
|
||||
|
||||
async query(serialNumber: string) {
|
||||
const serial = await this.dbService.get<{ serial_number: string; company_name: string; valid_until: string | null; is_active: number; created_at: string; created_by_name: string }>(
|
||||
'SELECT s.*, u.name as created_by_name FROM serials s LEFT JOIN users u ON s.created_by = u.id WHERE s.serial_number = ?',
|
||||
[serialNumber.toUpperCase()]
|
||||
);
|
||||
const prisma = this.dbService.getPrisma();
|
||||
const serial = await prisma.serial.findUnique({
|
||||
where: { serialNumber: serialNumber.toUpperCase() },
|
||||
include: {
|
||||
user: {
|
||||
select: { name: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!serial) {
|
||||
throw new Error('序列号不存在');
|
||||
throw new Error("序列号不存在");
|
||||
}
|
||||
|
||||
if (serial.valid_until && new Date(serial.valid_until) < new Date()) {
|
||||
throw new Error('序列号已过期');
|
||||
if (serial.validUntil && new Date(serial.validUntil) < new Date()) {
|
||||
throw new Error("序列号已过期");
|
||||
}
|
||||
|
||||
return {
|
||||
message: '查询成功',
|
||||
message: "查询成功",
|
||||
serial: {
|
||||
serialNumber: serial.serial_number,
|
||||
companyName: serial.company_name,
|
||||
validUntil: serial.valid_until,
|
||||
status: serial.is_active ? 'active' : 'disabled',
|
||||
isActive: !!serial.is_active,
|
||||
createdAt: serial.created_at,
|
||||
createdBy: serial.created_by_name
|
||||
}
|
||||
serialNumber: serial.serialNumber,
|
||||
companyName: serial.companyName,
|
||||
validUntil: serial.validUntil,
|
||||
status: serial.isActive ? "active" : "disabled",
|
||||
isActive: serial.isActive,
|
||||
createdAt: serial.createdAt,
|
||||
createdBy: serial.user?.name,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async findAll(page: number, limit: number, search: string) {
|
||||
const prisma = this.dbService.getPrisma();
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
let query = 'SELECT s.*, u.name as created_by_name FROM serials s LEFT JOIN users u ON s.created_by = u.id';
|
||||
let countQuery = 'SELECT COUNT(*) as total FROM serials s';
|
||||
let params: any[] = [];
|
||||
const where = search
|
||||
? {
|
||||
OR: [
|
||||
{ serialNumber: { contains: search } },
|
||||
{ companyName: { contains: search } },
|
||||
],
|
||||
}
|
||||
: undefined;
|
||||
|
||||
if (search) {
|
||||
query += ' WHERE s.serial_number LIKE ? OR s.company_name LIKE ?';
|
||||
countQuery += ' WHERE s.serial_number LIKE ? OR s.company_name LIKE ?';
|
||||
const searchParam = `%${search}%`;
|
||||
params.push(searchParam, searchParam);
|
||||
}
|
||||
|
||||
query += ' ORDER BY s.created_at DESC LIMIT ? OFFSET ?';
|
||||
params.push(parseInt(limit.toString()), parseInt(offset.toString()));
|
||||
|
||||
const [serials, countResult] = await Promise.all([
|
||||
this.dbService.all(query, params),
|
||||
this.dbService.get<{ total: number }>(countQuery, params.slice(0, -2))
|
||||
const [serials, total] = await Promise.all([
|
||||
prisma.serial.findMany({
|
||||
where,
|
||||
include: {
|
||||
user: {
|
||||
select: { name: true },
|
||||
},
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
skip: offset,
|
||||
take: limit,
|
||||
}),
|
||||
prisma.serial.count({ where }),
|
||||
]);
|
||||
|
||||
const total = countResult?.total || 0;
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
return {
|
||||
message: '获取序列号列表成功',
|
||||
data: serials.map((s: any) => ({
|
||||
serialNumber: s.serial_number,
|
||||
companyName: s.company_name,
|
||||
validUntil: s.valid_until,
|
||||
isActive: s.is_active,
|
||||
createdAt: s.created_at,
|
||||
createdBy: s.created_by_name
|
||||
message: "获取序列号列表成功",
|
||||
data: serials.map((s) => ({
|
||||
serialNumber: s.serialNumber,
|
||||
companyName: s.companyName,
|
||||
validUntil: s.validUntil,
|
||||
isActive: s.isActive,
|
||||
createdAt: s.createdAt,
|
||||
createdBy: s.user?.name,
|
||||
})),
|
||||
pagination: {
|
||||
page: parseInt(page.toString()),
|
||||
limit: parseInt(limit.toString()),
|
||||
total,
|
||||
totalPages
|
||||
}
|
||||
totalPages,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async update(serialNumber: string, updateData: { companyName?: string; validUntil?: string; isActive?: boolean }) {
|
||||
const existingSerial = await this.dbService.get<{ is_active: number }>('SELECT * FROM serials WHERE serial_number = ?', [serialNumber.toUpperCase()]);
|
||||
async update(
|
||||
serialNumber: string,
|
||||
updateData: {
|
||||
companyName?: string;
|
||||
validUntil?: string;
|
||||
isActive?: boolean;
|
||||
},
|
||||
) {
|
||||
const prisma = this.dbService.getPrisma();
|
||||
const existingSerial = await prisma.serial.findUnique({
|
||||
where: { serialNumber: serialNumber.toUpperCase() },
|
||||
});
|
||||
|
||||
if (!existingSerial) {
|
||||
throw new Error('序列号不存在');
|
||||
throw new Error("序列号不存在");
|
||||
}
|
||||
|
||||
const updateFields: string[] = [];
|
||||
const params: any[] = [];
|
||||
|
||||
const updateFields: any = {};
|
||||
if (updateData.companyName !== undefined) {
|
||||
updateFields.push('company_name = ?');
|
||||
params.push(updateData.companyName);
|
||||
updateFields.companyName = updateData.companyName;
|
||||
}
|
||||
|
||||
if (updateData.validUntil !== undefined) {
|
||||
updateFields.push('valid_until = ?');
|
||||
params.push(updateData.validUntil);
|
||||
updateFields.validUntil = new Date(updateData.validUntil);
|
||||
}
|
||||
|
||||
if (updateData.isActive !== undefined) {
|
||||
updateFields.push('is_active = ?');
|
||||
params.push(updateData.isActive ? 1 : 0);
|
||||
updateFields.isActive = updateData.isActive;
|
||||
}
|
||||
|
||||
if (updateFields.length === 0) {
|
||||
throw new Error('没有提供更新字段');
|
||||
if (Object.keys(updateFields).length === 0) {
|
||||
throw new Error("没有提供更新字段");
|
||||
}
|
||||
|
||||
updateFields.push('updated_at = CURRENT_TIMESTAMP');
|
||||
params.push(serialNumber.toUpperCase());
|
||||
|
||||
await this.dbService.run(
|
||||
`UPDATE serials SET ${updateFields.join(', ')} WHERE serial_number = ?`,
|
||||
params
|
||||
);
|
||||
|
||||
const updatedSerial = await this.dbService.get('SELECT s.*, u.name as created_by_name FROM serials s LEFT JOIN users u ON s.created_by = u.id WHERE s.serial_number = ?', [serialNumber.toUpperCase()]);
|
||||
const updatedSerial = await prisma.serial.update({
|
||||
where: { serialNumber: serialNumber.toUpperCase() },
|
||||
data: updateFields,
|
||||
include: {
|
||||
user: {
|
||||
select: { name: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
message: '序列号更新成功',
|
||||
message: "序列号更新成功",
|
||||
serial: {
|
||||
serialNumber: (updatedSerial as any).serial_number,
|
||||
companyName: (updatedSerial as any).company_name,
|
||||
validUntil: (updatedSerial as any).valid_until,
|
||||
isActive: (updatedSerial as any).is_active,
|
||||
createdAt: (updatedSerial as any).created_at,
|
||||
updatedAt: (updatedSerial as any).updated_at,
|
||||
createdBy: (updatedSerial as any).created_by_name
|
||||
}
|
||||
serialNumber: updatedSerial.serialNumber,
|
||||
companyName: updatedSerial.companyName,
|
||||
validUntil: updatedSerial.validUntil,
|
||||
isActive: updatedSerial.isActive,
|
||||
createdAt: updatedSerial.createdAt,
|
||||
updatedAt: updatedSerial.updatedAt,
|
||||
createdBy: updatedSerial.user?.name,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async revoke(serialNumber: string) {
|
||||
const existingSerial = await this.dbService.get<{ is_active: number }>('SELECT * FROM serials WHERE serial_number = ?', [serialNumber.toUpperCase()]);
|
||||
const prisma = this.dbService.getPrisma();
|
||||
const existingSerial = await prisma.serial.findUnique({
|
||||
where: { serialNumber: serialNumber.toUpperCase() },
|
||||
});
|
||||
|
||||
if (!existingSerial) {
|
||||
throw new Error('序列号不存在');
|
||||
throw new Error("序列号不存在");
|
||||
}
|
||||
|
||||
if (!existingSerial.is_active) {
|
||||
throw new Error('序列号已被吊销');
|
||||
if (!existingSerial.isActive) {
|
||||
throw new Error("序列号已被吊销");
|
||||
}
|
||||
|
||||
await this.dbService.run(
|
||||
'UPDATE serials SET is_active = 0, updated_at = CURRENT_TIMESTAMP WHERE serial_number = ?',
|
||||
[serialNumber.toUpperCase()]
|
||||
);
|
||||
await prisma.serial.update({
|
||||
where: { serialNumber: serialNumber.toUpperCase() },
|
||||
data: { isActive: false },
|
||||
});
|
||||
|
||||
return {
|
||||
message: '序列号已吊销',
|
||||
message: "序列号已吊销",
|
||||
data: {
|
||||
serialNumber: serialNumber.toUpperCase()
|
||||
}
|
||||
serialNumber: serialNumber.toUpperCase(),
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user