feat: refine deposit room type workflows

This commit is contained in:
2026-07-17 17:36:04 +08:00
parent a5bda6f093
commit b3d0bafc22
11 changed files with 724 additions and 140 deletions

View File

@@ -4,11 +4,38 @@ import { Repository } from 'typeorm';
import { Deposit } from '../entities/deposit.entity';
import { Student } from '../entities/student.entity';
import { DepositInstallment } from '../entities/deposit-installment.entity';
import { Occupancy } from '../entities/occupancy.entity';
import { CreateDepositDto, RefundDepositDto } from './dto/deposit.dto';
import { BatchCreateDepositDto, CreateDepositDto, RefundDepositDto } from './dto/deposit.dto';
const money = (value: number | string | null | undefined) => Number(Number(value || 0).toFixed(2));
const capacityRoomTypeText: Record<number, string> = {
1: '单人间',
2: '二人间',
3: '三人间',
4: '四人间',
5: '五人间',
6: '六人间',
8: '八人间',
};
const normalizeRoomType = (roomType?: string | null, capacity?: number | string | null) => {
const trimmed = roomType?.trim();
if (trimmed) return trimmed;
const normalizedCapacity = Number(capacity);
return capacityRoomTypeText[normalizedCapacity] || (normalizedCapacity > 0 ? `${normalizedCapacity}人间` : '');
};
const roomTypeCapacity = (roomType?: string) => {
const text = roomType?.trim();
if (!text) return undefined;
const knownCapacity = Object.entries(capacityRoomTypeText).find(([, label]) => label === text);
if (knownCapacity) return Number(knownCapacity[0]);
const match = text.match(/^(\d+)人间$/);
return match ? Number(match[1]) : undefined;
};
@Injectable()
export class DepositsService {
@@ -18,6 +45,8 @@ export class DepositsService {
private installmentRepo: Repository<DepositInstallment>,
@InjectRepository(Student)
private studentRepo: Repository<Student>,
@InjectRepository(Occupancy)
private occupancyRepo?: Repository<Occupancy>,
) {}
async getStudentLookups() {
@@ -28,6 +57,78 @@ export class DepositsService {
});
}
async getEligibleStudents(roomType?: string) {
const trimmedRoomType = roomType?.trim();
const fallbackCapacity = roomTypeCapacity(trimmedRoomType);
const qb = this.occupancyRepo!
.createQueryBuilder('o')
.innerJoin('o.student', 'student')
.innerJoin('o.room', 'room')
.leftJoin(Deposit, 'deposit', 'deposit.student_id = student.id AND deposit.status != :archived', {
archived: 'archived',
})
.select('student.id', 'studentId')
.addSelect('student.name', 'studentName')
.addSelect('student.studentNo', 'studentNo')
.addSelect('room.id', 'roomId')
.addSelect('room.roomNumber', 'roomNumber')
.addSelect('room.building', 'building')
.addSelect('room.roomType', 'roomType')
.addSelect('room.capacity', 'capacity')
.addSelect('deposit.amount', 'depositAmount')
.where('o.status = :activeStatus', { activeStatus: 'active' })
.andWhere('o.checkOutDate IS NULL')
.andWhere('student.status = :studentStatus', { studentStatus: 'active' })
.orderBy('room.building', 'ASC')
.addOrderBy('room.roomNumber', 'ASC')
.addOrderBy('student.name', 'ASC');
if (trimmedRoomType) {
if (fallbackCapacity) {
qb.andWhere(
'(room.roomType = :roomType OR ((room.roomType IS NULL OR room.roomType = :emptyRoomType) AND room.capacity = :fallbackCapacity))',
{ roomType: trimmedRoomType, emptyRoomType: '', fallbackCapacity },
);
} else {
qb.andWhere('room.roomType = :roomType', { roomType: trimmedRoomType });
}
}
const rows = await qb.getRawMany();
return rows.map((row) => ({
studentId: Number(row.studentId),
studentName: row.studentName,
studentNo: row.studentNo ?? null,
roomId: Number(row.roomId),
roomNumber: row.roomNumber,
building: row.building ?? null,
roomType: normalizeRoomType(row.roomType, row.capacity),
capacity: Number(row.capacity),
depositAmount: money(row.depositAmount),
}));
}
async batchCreate(dto: BatchCreateDepositDto, userId?: number) {
const studentIds = [...new Set(dto.studentIds)];
if (studentIds.length === 0) throw new BadRequestException('请选择学生');
const amount = money(dto.amount);
if (!Number.isFinite(dto.amount) || Math.abs(dto.amount * 100 - Math.round(dto.amount * 100)) > 1e-8) {
throw new BadRequestException('收取金额最多保留两位小数');
}
if (amount <= 0) throw new BadRequestException('收取金额必须大于0');
const results: Deposit[] = [];
for (const studentId of studentIds) {
results.push(await this.create({
studentId,
amount,
paidDate: dto.paidDate,
notes: dto.notes,
}, userId));
}
return { count: results.length, amount, results };
}
async findAll(query?: { studentId?: number; status?: string }) {
const qb = this.repo
.createQueryBuilder('d')