diff --git a/src/goals/goals.service.ts b/src/goals/goals.service.ts index 01a3809..21c4745 100644 --- a/src/goals/goals.service.ts +++ b/src/goals/goals.service.ts @@ -33,6 +33,7 @@ export class GoalsService { */ async createGoal(userId: string, createGoalDto: CreateGoalDto): Promise { try { + this.logger.log(`createGoal: ${JSON.stringify(createGoalDto, null, 2)}`); // 验证自定义重复规则 if (createGoalDto.repeatType === GoalRepeatType.CUSTOM && !createGoalDto.customRepeatRule) { throw new BadRequestException('自定义重复类型必须提供自定义重复规则'); diff --git a/src/goals/services/goal-task.service.ts b/src/goals/services/goal-task.service.ts index 06cc50e..44e8d42 100644 --- a/src/goals/services/goal-task.service.ts +++ b/src/goals/services/goal-task.service.ts @@ -153,38 +153,55 @@ export class GoalTaskService { const generateUntil = today.add(4, 'week'); // 提前生成4周的任务 const actualEndDate = endDate.isBefore(generateUntil) ? endDate : generateUntil; - let current = startDate.startOf('isoWeek'); + // 检查是否有自定义重复规则指定星期几 + const weekdays = goal.customRepeatRule?.weekdays; + + if (weekdays && weekdays.length > 0) { + // 如果有指定星期几,按指定星期几生成任务 + this.logger.log(`为目标 ${goal.title} 生成每周任务,指定星期几: ${weekdays}`); + + // 从今天开始生成,如果开始日期晚于今天则从开始日期开始 + let current = startDate.isBefore(today) ? today : startDate; + + let generatedCount = 0; - while (current.isSameOrBefore(actualEndDate)) { - const weekStart = current.startOf('isoWeek'); - const weekEnd = current.endOf('isoWeek'); + while (current.isSameOrBefore(actualEndDate)) { + const dayOfWeek = current.day(); // 0=周日, 6=周六 - // 检查是否已存在该周的任务 - const existingTask = existingTasks.find(task => { - const taskWeekStart = dayjs(task.startDate).startOf('isoWeek'); - return taskWeekStart.isSame(weekStart); - }); + if (weekdays.includes(dayOfWeek)) { + const taskDate = current.format('YYYY-MM-DD'); - if (!existingTask && weekStart.isSameOrAfter(startDate)) { - const taskTitle = `${goal.title} - 第${current.isoWeek()}周 (${weekStart.format('MM-DD')} 至 ${weekEnd.format('MM-DD')})`; + // 检查是否已存在该日期的任务 + const existingTask = existingTasks.find(task => + dayjs(task.startDate).format('YYYY-MM-DD') === taskDate + ); - await this.goalTaskModel.create({ - goalId: goal.id, - userId: goal.userId, - title: taskTitle, - description: `每周目标:完成${goal.frequency}次`, - startDate: weekStart.toDate(), - endDate: weekEnd.toDate(), - targetCount: goal.frequency, - currentCount: 0, - status: TaskStatus.PENDING, - }); + if (!existingTask) { + const weekDayNames = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']; + const taskTitle = `${goal.title} - ${current.format('YYYY年MM月DD日')} ${weekDayNames[dayOfWeek]}`; - this.logger.log(`为目标 ${goal.title} 生成每周任务: ${taskTitle}`); + await this.goalTaskModel.create({ + goalId: goal.id, + userId: goal.userId, + title: taskTitle, + description: `每周目标:完成${goal.frequency}次`, + startDate: current.toDate(), + endDate: current.toDate(), + targetCount: goal.frequency, + currentCount: 0, + status: TaskStatus.PENDING, + }); + + generatedCount++; + this.logger.log(`为目标 ${goal.title} 生成每周任务: ${taskTitle}`); + } + } + + current = current.add(1, 'day'); } - - current = current.add(1, 'week'); - } + + this.logger.log(`为目标 ${goal.title} 生成了 ${generatedCount} 个每周任务`); + } } /**