feat: 更新目标管理模块,优化数据库表结构和API逻辑

- 修改目标表和目标完成记录表的字段类型,增强数据一致性和查询性能。
- 移除不必要的搜索字段,简化目标查询DTO,提升查询效率。
- 引入目标状态枚举,增强代码可读性和维护性。
- 添加复合索引以优化查询性能,提升系统响应速度。
- 更新目标管理控制器和服务逻辑,确保与新数据库结构的兼容性。
This commit is contained in:
richarjiang
2025-08-22 11:22:27 +08:00
parent ffc0cd1d13
commit acf8d0c48c
8 changed files with 132 additions and 134 deletions

View File

@@ -32,6 +32,8 @@ export class GoalsService {
...createGoalDto,
startDate: new Date(createGoalDto.startDate),
endDate: createGoalDto.endDate ? new Date(createGoalDto.endDate) : null,
startTime: createGoalDto.startTime,
endTime: createGoalDto.endTime,
});
this.logger.log(`用户 ${userId} 创建了目标: ${goal.title}`);
@@ -47,7 +49,7 @@ export class GoalsService {
*/
async getGoals(userId: string, query: GoalQueryDto) {
try {
const { page = 1, pageSize = 20, status, repeatType, category, search, startDate, endDate, sortBy = 'createdAt', sortOrder = 'desc' } = query;
const { page = 1, pageSize = 20, status, repeatType, category, startDate, endDate, sortBy = 'createdAt', sortOrder = 'desc' } = query;
const offset = (page - 1) * pageSize;
// 构建查询条件
@@ -68,13 +70,6 @@ export class GoalsService {
where.category = category;
}
if (search) {
where[Op.or] = [
{ title: { [Op.like]: `%${search}%` } },
{ description: { [Op.like]: `%${search}%` } },
];
}
if (startDate || endDate) {
where.startDate = {};
if (startDate) {
@@ -85,6 +80,8 @@ export class GoalsService {
}
}
this.logger.log(`查询条件: ${JSON.stringify(where)}`);
// 构建排序条件
const order: Order = [[sortBy, sortOrder.toUpperCase()]];
@@ -107,7 +104,7 @@ export class GoalsService {
page,
pageSize,
total: count,
items: goals.map(goal => this.formatGoalResponse(goal)),
list: goals.map(goal => this.formatGoalResponse(goal)),
};
} catch (error) {
this.logger.error(`获取目标列表失败: ${error.message}`);
@@ -390,7 +387,7 @@ export class GoalsService {
*/
private formatGoalResponse(goal: Goal) {
const goalData = goal.toJSON();
// 计算进度百分比
if (goalData.targetCount) {
goalData.progressPercentage = Math.min(100, Math.round((goalData.completedCount / goalData.targetCount) * 100));