feat: support batch wallet balance changes

This commit is contained in:
2026-07-16 10:22:23 +08:00
parent 8ba51f48c0
commit d037787346
4 changed files with 124 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
import { IsIn, IsInt, IsNumber, IsOptional, IsString, MaxLength, NotEquals } from 'class-validator';
import { Type } from 'class-transformer';
import { ArrayNotEmpty, IsArray, IsIn, IsInt, IsNumber, IsOptional, IsString, MaxLength, NotEquals } from 'class-validator';
export class ChangeWalletBalanceDto {
@IsInt()
@@ -16,3 +17,24 @@ export class ChangeWalletBalanceDto {
@MaxLength(300)
description?: string;
}
export class BatchChangeWalletBalanceDto {
@IsArray()
@ArrayNotEmpty()
@IsInt({ each: true })
@Type(() => Number)
studentIds: number[];
@IsNumber({ maxDecimalPlaces: 2 })
@NotEquals(0)
amount: number;
@IsIn(['recharge', 'adjustment'])
type: 'recharge' | 'adjustment';
@IsOptional()
@IsString()
@MaxLength(300)
description?: string;
}

View File

@@ -3,7 +3,7 @@ import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../auth/decorators/permission.decorator';
import { OperationLogsService } from '../operation-logs/operation-logs.service';
import { extractRequestInfo } from '../common/request-utils';
import { ChangeWalletBalanceDto } from './dto/wallet.dto';
import { BatchChangeWalletBalanceDto, ChangeWalletBalanceDto } from './dto/wallet.dto';
import { WalletsService } from './wallets.service';
@UseGuards(JwtAuthGuard)
@@ -41,4 +41,24 @@ export class WalletsController {
});
return result;
}
@Post('batch-change-balance')
@RequirePermission('wallet:edit')
async batchChangeBalance(@Body() dto: BatchChangeWalletBalanceDto, @Request() req: any) {
const result = await this.service.batchChangeBalance(dto, req.user?.id);
const { ipAddress, userAgent } = extractRequestInfo(req);
await this.logService.log({
userId: req.user?.id,
username: req.user?.username,
module: '学生余额',
action: dto.type === 'recharge' ? '批量余额充值' : '批量余额调账',
targetId: undefined,
targetType: 'student_wallet',
detail: `学生${result.count}人,金额 ¥${dto.amount}${dto.description ? `${dto.description}` : ''}`,
ipAddress,
userAgent,
});
return result;
}
}

View File

@@ -6,7 +6,7 @@ import { Student } from '../entities/student.entity';
import { StudentWallet } from '../entities/student-wallet.entity';
import { WalletTransaction } from '../entities/wallet-transaction.entity';
import { In } from 'typeorm';
import { ChangeWalletBalanceDto } from './dto/wallet.dto';
import { BatchChangeWalletBalanceDto, ChangeWalletBalanceDto } from './dto/wallet.dto';
const money = (value: number | string | null | undefined) => Number(Number(value || 0).toFixed(2));
@@ -92,6 +92,20 @@ export class WalletsService {
});
}
async batchChangeBalance(dto: BatchChangeWalletBalanceDto, recordedBy?: number) {
const uniqueStudentIds = Array.from(new Set(dto.studentIds));
const results: Awaited<ReturnType<WalletsService['changeBalance']>>[] = [];
for (const studentId of uniqueStudentIds) {
results.push(await this.changeBalance({
studentId,
amount: dto.amount,
type: dto.type,
description: dto.description,
}, recordedBy));
}
return { count: uniqueStudentIds.length, results };
}
async debitBill(manager: EntityManager, bill: Bill, recordedBy?: number) {
if (bill.status === 'cancelled') return bill;