From eeaa43e0446bf5dc3334b182c7100617563a04c6 Mon Sep 17 00:00:00 2001 From: hailin Date: Wed, 21 Jan 2026 03:09:41 -0800 Subject: [PATCH] =?UTF-8?q?feat(contribution):=20=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E8=B4=A6=E6=88=B7=E6=98=8E=E7=BB=86=E8=AE=B0=E5=BD=95=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E8=BD=AF=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 SystemContributionRecord 模型添加 deleted_at 字段 - 修改 deleteContributionRecordsByAdoption 方法为软删除(设置 deleted_at) - 修改 findContributionRecords 方法过滤已删除记录(deletedAt: null) - 添加数据库迁移文件 Co-Authored-By: Claude Opus 4.5 --- .../migration.sql | 5 +++++ .../contribution-service/prisma/schema.prisma | 4 +++- .../repositories/system-account.repository.ts | 17 +++++++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 backend/services/contribution-service/prisma/migrations/0002_add_soft_delete_to_system_contribution_records/migration.sql diff --git a/backend/services/contribution-service/prisma/migrations/0002_add_soft_delete_to_system_contribution_records/migration.sql b/backend/services/contribution-service/prisma/migrations/0002_add_soft_delete_to_system_contribution_records/migration.sql new file mode 100644 index 00000000..3a75610e --- /dev/null +++ b/backend/services/contribution-service/prisma/migrations/0002_add_soft_delete_to_system_contribution_records/migration.sql @@ -0,0 +1,5 @@ +-- AlterTable: Add soft delete column to system_contribution_records +ALTER TABLE "system_contribution_records" ADD COLUMN "deleted_at" TIMESTAMP(3); + +-- CreateIndex +CREATE INDEX "system_contribution_records_deleted_at_idx" ON "system_contribution_records"("deleted_at"); diff --git a/backend/services/contribution-service/prisma/schema.prisma b/backend/services/contribution-service/prisma/schema.prisma index 5c76580e..f63775b1 100644 --- a/backend/services/contribution-service/prisma/schema.prisma +++ b/backend/services/contribution-service/prisma/schema.prisma @@ -334,12 +334,14 @@ model SystemContributionRecord { expireDate DateTime? @map("expire_date") @db.Date isExpired Boolean @default(false) @map("is_expired") - createdAt DateTime @default(now()) @map("created_at") + createdAt DateTime @default(now()) @map("created_at") + deletedAt DateTime? @map("deleted_at") // 软删除标记 systemAccount SystemAccount @relation(fields: [systemAccountId], references: [id]) @@index([systemAccountId]) @@index([sourceAdoptionId]) + @@index([deletedAt]) @@map("system_contribution_records") } diff --git a/backend/services/contribution-service/src/infrastructure/persistence/repositories/system-account.repository.ts b/backend/services/contribution-service/src/infrastructure/persistence/repositories/system-account.repository.ts index 05382b47..4a4f3d4c 100644 --- a/backend/services/contribution-service/src/infrastructure/persistence/repositories/system-account.repository.ts +++ b/backend/services/contribution-service/src/infrastructure/persistence/repositories/system-account.repository.ts @@ -191,7 +191,7 @@ export class SystemAccountRepository { } /** - * 删除指定来源认种的明细记录 + * 软删除指定来源认种的明细记录 */ async deleteContributionRecordsByAdoption( accountType: SystemAccountType, @@ -204,11 +204,15 @@ export class SystemAccountRepository { return 0; } - const result = await this.client.systemContributionRecord.deleteMany({ + const result = await this.client.systemContributionRecord.updateMany({ where: { systemAccountId: systemAccount.id, sourceAdoptionId, sourceAccountSequence, + deletedAt: null, // 只软删除未删除的记录 + }, + data: { + deletedAt: new Date(), }, }); @@ -256,15 +260,20 @@ export class SystemAccountRepository { return { data: [], total: 0 }; } + const whereClause = { + systemAccountId: systemAccount.id, + deletedAt: null, // 过滤已软删除的记录 + }; + const [records, total] = await Promise.all([ this.client.systemContributionRecord.findMany({ - where: { systemAccountId: systemAccount.id }, + where: whereClause, skip: (page - 1) * pageSize, take: pageSize, orderBy: { createdAt: 'desc' }, }), this.client.systemContributionRecord.count({ - where: { systemAccountId: systemAccount.id }, + where: whereClause, }), ]);