From d69cf9afbd589d7a23f91d2f28823cccc9282f21 Mon Sep 17 00:00:00 2001 From: richarjiang Date: Fri, 15 Aug 2025 17:15:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=AD=E7=BB=83=E8=AE=A1?= =?UTF-8?q?=E5=88=92=E7=9B=B8=E5=85=B3=E6=9C=8D=E5=8A=A1=EF=BC=8C=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84completed=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=EF=BC=8C=E7=AE=80=E5=8C=96DTO=E5=92=8C=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E7=BB=93=E6=9E=84=E3=80=82=E5=90=8C=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E8=AE=AD=E7=BB=83=E4=BC=9A=E8=AF=9D=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=80=BB=E8=BE=91=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BA=8B?= =?UTF-8?q?=E5=8A=A1=E5=A4=84=E7=90=86=E5=92=8C=E9=94=99=E8=AF=AF=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=AE=B0=E5=BD=95=EF=BC=8C=E6=8F=90=E5=8D=87=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=B8=80=E8=87=B4=E6=80=A7=E5=92=8C=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/schedule-exercise.dto.ts | 5 -- .../models/schedule-exercise.model.ts | 3 - .../schedule-exercise.service.ts | 4 +- src/workouts/workouts.service.ts | 62 ++++++++++++------- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/training-plans/dto/schedule-exercise.dto.ts b/src/training-plans/dto/schedule-exercise.dto.ts index b9ff154..63085db 100644 --- a/src/training-plans/dto/schedule-exercise.dto.ts +++ b/src/training-plans/dto/schedule-exercise.dto.ts @@ -51,11 +51,6 @@ export class CreateScheduleExerciseDto { @IsEnum(['exercise', 'rest', 'note']) @IsOptional() itemType?: ScheduleItemType; - - @ApiProperty({ description: '是否已完成', default: false, required: false }) - @IsBoolean() - @IsOptional() - completed?: boolean; } export class UpdateScheduleExerciseDto extends PartialType(CreateScheduleExerciseDto) { } diff --git a/src/training-plans/models/schedule-exercise.model.ts b/src/training-plans/models/schedule-exercise.model.ts index 9563ff7..6ea24f9 100644 --- a/src/training-plans/models/schedule-exercise.model.ts +++ b/src/training-plans/models/schedule-exercise.model.ts @@ -60,9 +60,6 @@ export class ScheduleExercise extends Model { }) declare itemType: ScheduleItemType; - @Column({ type: DataType.BOOLEAN, defaultValue: false, comment: '是否已完成' }) - declare completed: boolean; - @Column({ type: DataType.INTEGER, allowNull: false, comment: '排序顺序' }) declare sortOrder: number; diff --git a/src/training-plans/schedule-exercise.service.ts b/src/training-plans/schedule-exercise.service.ts index 4cf3b00..86e0dcd 100644 --- a/src/training-plans/schedule-exercise.service.ts +++ b/src/training-plans/schedule-exercise.service.ts @@ -12,7 +12,7 @@ import { ActivityLogsService } from '../activity-logs/activity-logs.service'; import { ActivityActionType, ActivityEntityType } from '../activity-logs/models/activity-log.model'; import { Logger as WinstonLogger } from 'winston'; import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; -import { Op, Transaction } from 'sequelize'; +import { Op } from 'sequelize'; @Injectable() export class ScheduleExerciseService { @@ -79,7 +79,6 @@ export class ScheduleExerciseService { restSec: dto.restSec, note: dto.note || '', itemType: dto.itemType || 'exercise', - completed: dto.completed || false, sortOrder, }); @@ -182,7 +181,6 @@ export class ScheduleExerciseService { if (dto.restSec !== undefined) exercise.restSec = dto.restSec; if (dto.note !== undefined) exercise.note = dto.note || ''; if (dto.itemType !== undefined) exercise.itemType = dto.itemType; - if (dto.completed !== undefined) exercise.completed = dto.completed; await exercise.save(); diff --git a/src/workouts/workouts.service.ts b/src/workouts/workouts.service.ts index eab4e78..33d2be4 100644 --- a/src/workouts/workouts.service.ts +++ b/src/workouts/workouts.service.ts @@ -109,7 +109,11 @@ export class WorkoutsService { }); if (!activeTrainingPlan) { - throw new NotFoundException('请先激活一个训练计划'); + this.winstonLogger.info(`今日没有激活的训练计划`, { + context: 'WorkoutsService', + userId, + }); + return null; } // 创建今日训练会话 @@ -352,28 +356,44 @@ export class WorkoutsService { * 删除训练会话 */ async deleteWorkoutSession(userId: string, sessionId: string) { - const [count] = await this.workoutSessionModel.update( - { deleted: true }, - { where: { id: sessionId, userId, deleted: false } } - ); + const transaction = await this.workoutSessionModel.sequelize?.transaction(); + if (!transaction) throw new Error('Failed to start transaction'); - if (count === 0) { - throw new NotFoundException('训练会话不存在'); + try { + const [count] = await this.workoutSessionModel.update( + { deleted: true }, + { where: { id: sessionId, userId, deleted: false }, transaction } + ); + + if (count === 0) { + throw new NotFoundException('训练会话不存在'); + } + + // 同时删除关联的训练动作 + await this.workoutExerciseModel.update( + { deleted: true }, + { where: { workoutSessionId: sessionId, userId, deleted: false }, transaction } + ); + + await transaction.commit(); + + this.winstonLogger.info(`删除训练会话 ${sessionId}`, { + context: 'WorkoutsService', + userId, + sessionId, + }); + + return { success: true }; + } catch (error) { + await transaction.rollback(); + this.winstonLogger.error(`删除训练会话失败 ${sessionId}`, { + context: 'WorkoutsService', + userId, + sessionId, + error, + }); + throw error; } - - // 同时删除关联的训练动作 - await this.workoutExerciseModel.update( - { deleted: true }, - { where: { workoutSessionId: sessionId, userId, deleted: false } } - ); - - this.winstonLogger.info(`删除训练会话 ${sessionId}`, { - context: 'WorkoutsService', - userId, - sessionId, - }); - - return { success: true }; } // ==================== 训练动作管理 ====================