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

@@ -154,7 +154,7 @@ export class ExpensesController {
userId: req.user?.id,
username: req.user?.username,
module: '费用管理',
action: '删除费用',
action: '归档费用',
targetId: id,
targetType: 'room_expense',
ipAddress,
@@ -172,7 +172,7 @@ export class ExpensesController {
userId: req.user?.id,
username: req.user?.username,
module: '费用管理',
action: '批量删除宿舍费用',
action: '批量归档宿舍费用',
detail: `IDs: ${(body.ids || []).join(',')}`,
ipAddress,
userAgent,
@@ -235,7 +235,7 @@ export class ExpensesController {
userId: req.user?.id,
username: req.user?.username,
module: '费用管理',
action: '删除费用',
action: '归档费用',
targetId: id,
ipAddress,
userAgent,
@@ -252,7 +252,7 @@ export class ExpensesController {
userId: req.user?.id,
username: req.user?.username,
module: '费用管理',
action: '批量删除个人费用',
action: '批量归档个人费用',
detail: `IDs: ${(body.ids || []).join(',')}`,
ipAddress,
userAgent,

View File

@@ -76,6 +76,7 @@ export class ExpensesService {
const qb = this.roomExpRepo
.createQueryBuilder('e')
.leftJoinAndSelect('e.room', 'room')
.where('e.status = :status', { status: 'active' })
.orderBy('e.createdAt', 'DESC');
if (query?.roomId) qb.andWhere('e.roomId = :roomId', { roomId: query.roomId });
if (query?.periodStart) qb.andWhere('e.periodStart >= :ps', { ps: query.periodStart });
@@ -86,21 +87,23 @@ export class ExpensesService {
async deleteRoomExpense(id: number) {
const e = await this.roomExpRepo.findOne({ where: { id } });
if (!e) throw new NotFoundException('费用记录不存在');
await this.roomExpRepo.delete(id);
return { message: '删除成功' };
if (e.status === 'archived') throw new BadRequestException('费用记录已归档');
await this.roomExpRepo.update(id, { status: 'archived' });
return { message: '已归档' };
}
async batchDeleteRoomExpenses(ids: number[]) {
const uniqueIds = [...new Set(ids || [])];
if (uniqueIds.length === 0) throw new BadRequestException('请选择要删除的记录');
if (uniqueIds.length === 0) throw new BadRequestException('请选择要归档的记录');
const existing = await this.roomExpRepo.find({ where: { id: In(uniqueIds) }, select: ['id'] });
if (existing.length !== uniqueIds.length) throw new NotFoundException('部分费用记录不存在');
const result = await this.roomExpRepo
.createQueryBuilder()
.delete()
.update()
.set({ status: 'archived' })
.where('id IN (:...ids)', { ids: uniqueIds })
.execute();
return { message: '批量删除成功', deleted: result.affected || 0 };
return { message: `已批量归档 ${result.affected || 0}`, archived: result.affected || 0 };
}
async updateRoomExpense(id: number, dto: Partial<CreateRoomExpenseDto>) {
@@ -172,7 +175,7 @@ export class ExpensesService {
}
async findPersonalExpenses(query?: { studentId?: number }) {
const where: Record<string, unknown> = {};
const where: Record<string, unknown> = { status: 'active' };
if (query?.studentId) where.studentId = query.studentId;
return this.personalExpRepo.find({
where,
@@ -184,14 +187,15 @@ export class ExpensesService {
async deletePersonalExpense(id: number) {
const e = await this.personalExpRepo.findOne({ where: { id } });
if (!e) throw new NotFoundException('费用记录不存在');
if (e.billId) throw new BadRequestException('已计入账单的个人费用不能删除,请先取消账单');
await this.personalExpRepo.delete(id);
return { message: '删除成功' };
if (e.billId) throw new BadRequestException('已计入账单的个人费用不能归档,请先取消账单');
if (e.status === 'archived') throw new BadRequestException('费用记录已归档');
await this.personalExpRepo.update(id, { status: 'archived' });
return { message: '已归档' };
}
async batchDeletePersonalExpenses(ids: number[]) {
const uniqueIds = [...new Set(ids || [])];
if (uniqueIds.length === 0) throw new BadRequestException('请选择要删除的记录');
if (uniqueIds.length === 0) throw new BadRequestException('请选择要归档的记录');
const existing = await this.personalExpRepo.find({ where: { id: In(uniqueIds) } });
if (existing.length !== uniqueIds.length) throw new NotFoundException('部分费用记录不存在');
if (existing.some((expense) => expense.billId)) {
@@ -199,10 +203,11 @@ export class ExpensesService {
}
const result = await this.personalExpRepo
.createQueryBuilder()
.delete()
.update()
.set({ status: 'archived' })
.where('id IN (:...ids)', { ids: uniqueIds })
.execute();
return { message: '批量删除成功', deleted: result.affected || 0 };
return { message: `已批量归档 ${result.affected || 0}`, archived: result.affected || 0 };
}
async updatePersonalExpense(id: number, dto: Partial<CreatePersonalExpenseDto>) {