diff --git a/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts b/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts index 91a33bda..665a9582 100644 --- a/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts +++ b/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts @@ -286,19 +286,17 @@ export class ContributionCalculationService { new ContributionAmount(0), ); await this.systemAccountRepository.addContribution( - 'HEADQUARTERS', 'HEADQUARTERS', null, totalUnallocatedAmount, ); // 发布 HEADQUARTERS 账户同步事件 - const headquartersAccount = await this.systemAccountRepository.findByType('HEADQUARTERS'); + const headquartersAccount = await this.systemAccountRepository.findByTypeAndRegion('HEADQUARTERS', null); if (headquartersAccount) { const hqEvent = new SystemAccountSyncedEvent( 'HEADQUARTERS', - 'HEADQUARTERS', // 新增:基础类型 - null, // 新增:区域代码(总部没有区域) + null, // 区域代码(总部没有区域) headquartersAccount.name, headquartersAccount.contributionBalance.value.toString(), headquartersAccount.createdAt, 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 b9f47bb9..23a2eee1 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 @@ -39,14 +39,16 @@ export class SystemAccountRepository { /** * 根据 accountType + regionCode 查找系统账户 + * 注意:由于 regionCode 是可选字段,使用 findFirst 替代 findUnique */ async findByTypeAndRegion( accountType: SystemAccountType, regionCode: string | null, ): Promise { - const record = await this.client.systemAccount.findUnique({ + const record = await this.client.systemAccount.findFirst({ where: { - accountType_regionCode: { accountType, regionCode }, + accountType, + regionCode: regionCode === null ? { equals: null } : regionCode, }, }); @@ -87,19 +89,25 @@ export class SystemAccountRepository { ]; for (const account of accounts) { - await this.client.systemAccount.upsert({ + // 由于 regionCode 是 nullable,使用 findFirst + create 替代 upsert + const existing = await this.client.systemAccount.findFirst({ where: { - accountType_regionCode: { accountType: account.accountType, regionCode: null }, - }, - create: { accountType: account.accountType, - regionCode: null, - name: account.name, - contributionBalance: 0, - contributionNeverExpires: true, + regionCode: { equals: null }, }, - update: {}, }); + + if (!existing) { + await this.client.systemAccount.create({ + data: { + accountType: account.accountType, + regionCode: null, + name: account.name, + contributionBalance: 0, + contributionNeverExpires: true, + }, + }); + } } } @@ -113,21 +121,32 @@ export class SystemAccountRepository { ): Promise { const name = this.getAccountName(accountType, regionCode); - await this.client.systemAccount.upsert({ + // 由于 regionCode 是 nullable,使用 findFirst + create/update 替代 upsert + const existing = await this.client.systemAccount.findFirst({ where: { - accountType_regionCode: { accountType, regionCode }, - }, - create: { accountType, - regionCode, - name, - contributionBalance: amount.value, - contributionNeverExpires: true, - }, - update: { - contributionBalance: { increment: amount.value }, + regionCode: regionCode === null ? { equals: null } : regionCode, }, }); + + if (existing) { + await this.client.systemAccount.update({ + where: { id: existing.id }, + data: { + contributionBalance: { increment: amount.value }, + }, + }); + } else { + await this.client.systemAccount.create({ + data: { + accountType, + regionCode, + name, + contributionBalance: amount.value, + contributionNeverExpires: true, + }, + }); + } } /**