2025-03-21 17:25:39 +08:00
|
|
|
|
import { Injectable ,Logger} from '@nestjs/common';
|
2024-12-31 15:57:32 +08:00
|
|
|
|
import { BaseService } from '../base/base.service';
|
2025-03-21 17:25:39 +08:00
|
|
|
|
|
2024-12-31 15:57:32 +08:00
|
|
|
|
import {
|
|
|
|
|
UserProfile,
|
|
|
|
|
db,
|
|
|
|
|
ObjectType,
|
|
|
|
|
Prisma,
|
2025-01-03 09:24:46 +08:00
|
|
|
|
Resource,
|
2025-01-06 18:30:16 +08:00
|
|
|
|
ResourceStatus,
|
2025-01-06 08:45:23 +08:00
|
|
|
|
} from '@nice/common';
|
2025-01-03 09:24:46 +08:00
|
|
|
|
|
2024-12-31 15:57:32 +08:00
|
|
|
|
@Injectable()
|
|
|
|
|
export class ResourceService extends BaseService<Prisma.ResourceDelegate> {
|
2025-03-21 17:25:39 +08:00
|
|
|
|
private readonly logger = new Logger(ResourceService.name);
|
2024-12-31 15:57:32 +08:00
|
|
|
|
constructor() {
|
|
|
|
|
super(db, ObjectType.RESOURCE);
|
|
|
|
|
}
|
2025-01-03 09:24:46 +08:00
|
|
|
|
async create(
|
|
|
|
|
args: Prisma.ResourceCreateArgs,
|
|
|
|
|
params?: { staff?: UserProfile },
|
|
|
|
|
): Promise<Resource> {
|
|
|
|
|
if (params?.staff) {
|
2025-01-08 00:57:19 +08:00
|
|
|
|
args.data.ownerId = params?.staff?.id;
|
2025-01-03 09:24:46 +08:00
|
|
|
|
}
|
2025-01-06 18:30:16 +08:00
|
|
|
|
return super.create(args);
|
2025-01-03 09:24:46 +08:00
|
|
|
|
}
|
2025-01-06 18:30:16 +08:00
|
|
|
|
async softDeleteByFileId(fileId: string) {
|
|
|
|
|
return this.update({
|
|
|
|
|
where: {
|
|
|
|
|
fileId,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
deletedAt: new Date(),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-03-21 17:25:39 +08:00
|
|
|
|
// 添加保存文件名的方法
|
|
|
|
|
async saveFileName(fileId: string, fileName: string): Promise<void> {
|
|
|
|
|
try {
|
|
|
|
|
this.logger.log(`尝试保存文件名 "${fileName}" 到文件 ${fileId}`);
|
|
|
|
|
|
|
|
|
|
// 首先检查是否已存在 ShareCode 记录
|
|
|
|
|
const existingShareCode = await db.shareCode.findUnique({
|
|
|
|
|
where: { fileId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (existingShareCode) {
|
|
|
|
|
// 如果记录存在,更新文件名
|
|
|
|
|
await db.shareCode.update({
|
|
|
|
|
where: { fileId },
|
|
|
|
|
data: { fileName },
|
|
|
|
|
});
|
|
|
|
|
this.logger.log(`更新了现有记录的文件名 "${fileName}" 到文件 ${fileId}`);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果记录不存在,创建新记录
|
|
|
|
|
await db.shareCode.create({
|
|
|
|
|
data: {
|
|
|
|
|
fileId,
|
|
|
|
|
fileName,
|
|
|
|
|
code: null, // 这里可以设置为 null 或生成一个临时码
|
|
|
|
|
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24小时后过期
|
|
|
|
|
isUsed: false,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
this.logger.log(`创建了新记录并保存文件名 "${fileName}" 到文件 ${fileId}`);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.logger.error(`保存文件名失败,文件ID: ${fileId}`, error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加获取文件名的方法
|
|
|
|
|
async getFileName(fileId: string): Promise<string | null> {
|
|
|
|
|
try {
|
|
|
|
|
const shareCode = await db.shareCode.findUnique({
|
|
|
|
|
where: { fileId },
|
|
|
|
|
select: { fileName: true },
|
|
|
|
|
});
|
|
|
|
|
return shareCode?.fileName || null;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.logger.error(`Failed to get filename for ${fileId}`, error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-08 00:57:19 +08:00
|
|
|
|
}
|