feat(contribution): 系统账户明细记录改为软删除

- 在 SystemContributionRecord 模型添加 deleted_at 字段
- 修改 deleteContributionRecordsByAdoption 方法为软删除(设置 deleted_at)
- 修改 findContributionRecords 方法过滤已删除记录(deletedAt: null)
- 添加数据库迁移文件

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-21 03:09:41 -08:00
parent e0eb734196
commit eeaa43e044
3 changed files with 21 additions and 5 deletions

View File

@ -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");

View File

@ -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")
}

View File

@ -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,
}),
]);