refactor: 移除使用次数扣除逻辑,调整活动类型及记录逻辑
This commit is contained in:
@@ -3,12 +3,14 @@ import { InjectModel } from '@nestjs/sequelize';
|
||||
import { Op, WhereOptions } from 'sequelize';
|
||||
import { Goal, GoalRepeatType, GoalStatus } from '../models/goal.model';
|
||||
import { GoalTask, TaskStatus } from '../models/goal-task.model';
|
||||
import { CreateGoalTaskDto, UpdateGoalTaskDto, GoalTaskQueryDto, CompleteGoalTaskDto } from '../dto/goal-task.dto';
|
||||
import { UpdateGoalTaskDto, GoalTaskQueryDto, CompleteGoalTaskDto } from '../dto/goal-task.dto';
|
||||
import * as dayjs from 'dayjs';
|
||||
import * as weekOfYear from 'dayjs/plugin/weekOfYear';
|
||||
import * as isoWeek from 'dayjs/plugin/isoWeek';
|
||||
import * as isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
|
||||
import * as isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
|
||||
import { ActivityLevel, ActivityType } from 'src/users/models/user-activity.model';
|
||||
import { UserActivityService } from 'src/users/services/user-activity.service';
|
||||
|
||||
dayjs.extend(weekOfYear);
|
||||
dayjs.extend(isoWeek);
|
||||
@@ -24,6 +26,7 @@ export class GoalTaskService {
|
||||
private readonly goalModel: typeof Goal,
|
||||
@InjectModel(GoalTask)
|
||||
private readonly goalTaskModel: typeof GoalTask,
|
||||
private readonly userActivityService: UserActivityService,
|
||||
) { }
|
||||
|
||||
/**
|
||||
@@ -42,7 +45,7 @@ export class GoalTaskService {
|
||||
}
|
||||
|
||||
const goals = await this.goalModel.findAll({ where });
|
||||
|
||||
|
||||
this.logger.log(`为用户 ${userId} 找到 ${goals.length} 个活跃目标`);
|
||||
|
||||
for (const goal of goals) {
|
||||
@@ -155,14 +158,14 @@ export class GoalTaskService {
|
||||
|
||||
// 检查是否有自定义重复规则指定星期几
|
||||
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)) {
|
||||
@@ -199,9 +202,9 @@ export class GoalTaskService {
|
||||
|
||||
current = current.add(1, 'day');
|
||||
}
|
||||
|
||||
|
||||
this.logger.log(`为目标 ${goal.title} 生成了 ${generatedCount} 个每周任务`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -233,11 +236,11 @@ export class GoalTaskService {
|
||||
while (current.isSameOrBefore(actualEndDate)) {
|
||||
// 计算该月的目标日期
|
||||
const targetDate = current.date(targetDayOfMonth);
|
||||
|
||||
|
||||
// 如果目标日期超出了该月的天数,则使用该月的最后一天
|
||||
const daysInMonth = current.daysInMonth();
|
||||
const actualTargetDate = targetDayOfMonth > daysInMonth ? current.date(daysInMonth) : targetDate;
|
||||
|
||||
|
||||
// 检查是否已经过了该月的目标日期
|
||||
if (actualTargetDate.isBefore(today)) {
|
||||
this.logger.log(`跳过 ${current.format('YYYY年MM月')},目标日期 ${actualTargetDate.format('MM-DD')} 已过期`);
|
||||
@@ -272,7 +275,7 @@ export class GoalTaskService {
|
||||
|
||||
current = current.add(1, 'month');
|
||||
}
|
||||
|
||||
|
||||
this.logger.log(`为目标 ${goal.title} 生成了 ${generatedCount} 个每月任务`);
|
||||
}
|
||||
|
||||
@@ -291,7 +294,7 @@ export class GoalTaskService {
|
||||
}
|
||||
|
||||
const { weekdays } = goal.customRepeatRule;
|
||||
|
||||
|
||||
this.logger.log(`为目标 ${goal.title} 生成自定义任务,重复规则: ${JSON.stringify(goal.customRepeatRule)}`);
|
||||
|
||||
if (weekdays && weekdays.length > 0) {
|
||||
@@ -302,7 +305,7 @@ export class GoalTaskService {
|
||||
|
||||
// 从今天开始生成,如果开始日期晚于今天则从开始日期开始
|
||||
let current = startDate.isBefore(today) ? today : startDate;
|
||||
|
||||
|
||||
let generatedCount = 0;
|
||||
|
||||
this.logger.log(`开始生成自定义任务,日期范围: ${current.format('YYYY-MM-DD')} 到 ${actualEndDate.format('YYYY-MM-DD')}`);
|
||||
@@ -340,7 +343,7 @@ export class GoalTaskService {
|
||||
|
||||
current = current.add(1, 'day');
|
||||
}
|
||||
|
||||
|
||||
this.logger.log(`为目标 ${goal.title} 生成了 ${generatedCount} 个自定义任务`);
|
||||
} else {
|
||||
this.logger.warn(`目标 ${goal.title} 的自定义重复规则中没有指定星期几`);
|
||||
@@ -452,6 +455,21 @@ export class GoalTaskService {
|
||||
|
||||
await task.save();
|
||||
|
||||
try {
|
||||
const today = dayjs().format('YYYY-MM-DD');
|
||||
|
||||
await this.userActivityService.recordActivity(userId, {
|
||||
activityType: ActivityType.GOAL,
|
||||
activityDate: today,
|
||||
level: ActivityLevel.MEDIUM,
|
||||
remark: `完成目标任务: ${task.title}`,
|
||||
});
|
||||
this.logger.log(`记录用户活跃 - 用户: ${userId} 完成目标任务: ${task.title}`);
|
||||
} catch (activityError) {
|
||||
// 记录活跃失败不影响主要业务流程
|
||||
this.logger.error(`记录用户活跃失败: ${activityError.message}`);
|
||||
}
|
||||
|
||||
this.logger.log(`用户 ${userId} 完成任务: ${task.title}, 当前进度: ${task.currentCount}/${task.targetCount}`);
|
||||
|
||||
return this.formatTaskResponse(task);
|
||||
|
||||
Reference in New Issue
Block a user