fix: 归档删除改为软删除

This commit is contained in:
2026-07-17 14:13:17 +08:00
parent 3131d2e141
commit f7328d670d
49 changed files with 520 additions and 354 deletions

View File

@@ -166,7 +166,7 @@ export class OccupanciesController {
userId: req.user?.id,
username: req.user?.username,
module: '入住管理',
action: '删除入住记录',
action: '归档入住记录',
targetId: +id,
targetType: 'occupancy',
ipAddress,
@@ -184,7 +184,7 @@ export class OccupanciesController {
userId: req.user?.id,
username: req.user?.username,
module: '入住管理',
action: '批量删除入住记录',
action: '批量归档入住记录',
detail: `IDs: ${(body.ids || []).join(',')}`,
ipAddress,
userAgent,

View File

@@ -39,6 +39,7 @@ export class OccupanciesService {
.leftJoinAndSelect('o.room', 'room')
.leftJoinAndSelect('o.bed', 'bed')
.leftJoinAndSelect('o.locker', 'locker')
.where('o.status = :status', { status: 'active' })
.orderBy('o.checkInDate', 'DESC');
if (query?.roomId) qb.andWhere('o.roomId = :roomId', { roomId: query.roomId });
if (query?.studentId) qb.andWhere('o.studentId = :studentId', { studentId: query.studentId });
@@ -287,13 +288,14 @@ export class OccupanciesService {
async remove(id: number) {
const occ = await this.repo.findOne({ where: { id } });
if (!occ) throw new NotFoundException('入住记录不存在');
if (!occ.checkOutDate) throw new BadRequestException('在住记录不能删除,请先办理退宿');
await this.repo.delete(id);
return { message: '删除成功' };
if (!occ.checkOutDate) throw new BadRequestException('在住记录不能归档,请先办理退宿');
if (occ.status === 'archived') throw new BadRequestException('入住记录已归档');
await this.repo.update(id, { status: 'archived' });
return { message: '已归档' };
}
async batchRemove(ids: number[]) {
if (!ids || ids.length === 0) throw new BadRequestException('请选择要删除的记录');
if (!ids || ids.length === 0) throw new BadRequestException('请选择要归档的记录');
const records = await this.repo.find({ where: { id: In(ids) }, relations: ['student'] });
const skipped: string[] = [];
const deletableIds: number[] = [];
@@ -304,20 +306,21 @@ export class OccupanciesService {
deletableIds.push(occ.id);
}
}
let deleted = 0;
let archived = 0;
if (deletableIds.length > 0) {
const result = await this.repo
.createQueryBuilder()
.delete()
.update()
.set({ status: 'archived' })
.where('id IN (:...ids)', { ids: deletableIds })
.execute();
deleted = result.affected || 0;
archived = result.affected || 0;
}
const message =
skipped.length > 0
? `成功删除 ${deleted} 条;${skipped.length} 条在住记录被跳过(${skipped.slice(0, 5).join('、')}${skipped.length > 5 ? '…' : ''}),请先办理退宿`
: `批量删除成功,共 ${deleted}`;
return { message, deleted, skipped: skipped.length };
? `成功归档 ${archived} 条;${skipped.length} 条在住记录被跳过(${skipped.slice(0, 5).join('、')}${skipped.length > 5 ? '…' : ''}),请先办理退宿`
: `批量归档成功,共 ${archived}`;
return { message, archived, skipped: skipped.length };
}
async batchCheckOut(dto: {