feat(users): 新增围度分析报表接口
- 添加 dayjs 依赖用于日期处理 - 新增 GetBodyMeasurementAnalysisDto 和 GetBodyMeasurementAnalysisResponseDto - 支持按周、月、年三种时间范围统计围度变化趋势 - 实现最近数据点匹配算法,返回各围度类型最接近时间点的测量值
This commit is contained in:
@@ -19,6 +19,9 @@ import { UpdateUserDto, UpdateUserResponseDto } from './dto/update-user.dto';
|
||||
import { UserPurchase, PurchaseType, PurchaseStatus, PurchasePlatform } from './models/user-purchase.model';
|
||||
import { ApplePurchaseService } from './services/apple-purchase.service';
|
||||
import * as dayjs from 'dayjs';
|
||||
import * as isoWeek from 'dayjs/plugin/isoWeek';
|
||||
|
||||
dayjs.extend(isoWeek);
|
||||
import { AccessTokenPayload, AppleAuthService, AppleTokenPayload } from './services/apple-auth.service';
|
||||
import { AppleLoginDto, AppleLoginResponseDto, RefreshTokenDto, RefreshTokenResponseDto } from './dto/apple-login.dto';
|
||||
import { DeleteAccountDto, DeleteAccountResponseDto } from './dto/delete-account.dto';
|
||||
@@ -2386,4 +2389,143 @@ export class UsersService {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户围度分析报表
|
||||
*/
|
||||
async getBodyMeasurementAnalysis(userId: string, period: 'week' | 'month' | 'year'): Promise<any> {
|
||||
try {
|
||||
const now = dayjs();
|
||||
let startDate: Date;
|
||||
let dataPoints: Date[] = [];
|
||||
|
||||
// 根据时间范围计算起始日期和数据点
|
||||
switch (period) {
|
||||
case 'week':
|
||||
// 获取本周7天,按中国习惯从周一开始
|
||||
const startOfWeek = now.startOf('isoWeek'); // ISO周从周一开始
|
||||
for (let i = 0; i < 7; i++) {
|
||||
const date = startOfWeek.add(i, 'day');
|
||||
dataPoints.push(date.toDate());
|
||||
}
|
||||
startDate = startOfWeek.toDate();
|
||||
break;
|
||||
case 'month':
|
||||
// 获取本月的4周,以周日结束
|
||||
const startOfMonth = now.startOf('month');
|
||||
|
||||
// 本月1日作为起始日期
|
||||
startDate = startOfMonth.toDate();
|
||||
|
||||
// 生成4个数据点,每个代表一周,以该周的周日为准
|
||||
for (let i = 0; i < 4; i++) {
|
||||
// 每周从本月1日开始算第i周,取该周的周日
|
||||
const weekStart = startOfMonth.add(i * 7, 'day');
|
||||
const weekEnd = weekStart.endOf('week'); // dayjs默认周日结束
|
||||
dataPoints.push(weekEnd.toDate());
|
||||
}
|
||||
break;
|
||||
case 'year':
|
||||
// 获取今年1月到12月,每月取最后一天
|
||||
const startOfYear = now.startOf('year'); // 今年1月1日
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const date = startOfYear.add(i, 'month').endOf('month');
|
||||
dataPoints.push(date.toDate());
|
||||
}
|
||||
startDate = startOfYear.toDate();
|
||||
break;
|
||||
}
|
||||
|
||||
// 获取时间范围内的所有围度数据
|
||||
const measurements = await this.userBodyMeasurementHistoryModel.findAll({
|
||||
where: {
|
||||
userId,
|
||||
createdAt: {
|
||||
[Op.gte]: startDate,
|
||||
[Op.lte]: now,
|
||||
},
|
||||
},
|
||||
order: [['createdAt', 'ASC']],
|
||||
});
|
||||
|
||||
// 初始化结果数组
|
||||
const analysisData = dataPoints.map(date => {
|
||||
const label = this.formatDateLabel(date, period);
|
||||
return {
|
||||
label,
|
||||
chestCircumference: null,
|
||||
waistCircumference: null,
|
||||
upperHipCircumference: null,
|
||||
armCircumference: null,
|
||||
thighCircumference: null,
|
||||
calfCircumference: null,
|
||||
};
|
||||
});
|
||||
|
||||
// 为每个数据点找到最接近的围度数据
|
||||
analysisData.forEach((point, index) => {
|
||||
const targetDate = dataPoints[index];
|
||||
|
||||
// 为每种围度类型找到最接近目标日期的值
|
||||
Object.values(BodyMeasurementType).forEach(measurementType => {
|
||||
const relevantMeasurements = measurements.filter(m =>
|
||||
m.measurementType === measurementType &&
|
||||
new Date(m.createdAt) <= targetDate
|
||||
);
|
||||
|
||||
if (relevantMeasurements.length > 0) {
|
||||
// 取最接近目标日期的最新记录
|
||||
const closestMeasurement = relevantMeasurements
|
||||
.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())[0];
|
||||
|
||||
// 将数据库字段映射到响应字段
|
||||
const fieldMapping = {
|
||||
[BodyMeasurementType.ChestCircumference]: 'chestCircumference',
|
||||
[BodyMeasurementType.WaistCircumference]: 'waistCircumference',
|
||||
[BodyMeasurementType.UpperHipCircumference]: 'upperHipCircumference',
|
||||
[BodyMeasurementType.ArmCircumference]: 'armCircumference',
|
||||
[BodyMeasurementType.ThighCircumference]: 'thighCircumference',
|
||||
[BodyMeasurementType.CalfCircumference]: 'calfCircumference',
|
||||
};
|
||||
|
||||
const fieldName = fieldMapping[measurementType];
|
||||
if (fieldName) {
|
||||
point[fieldName] = closestMeasurement.value;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
code: ResponseCode.SUCCESS,
|
||||
message: 'success',
|
||||
data: analysisData,
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error(`获取围度分析报表失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
return {
|
||||
code: ResponseCode.ERROR,
|
||||
message: `获取围度分析报表失败: ${error instanceof Error ? error.message : '未知错误'}`,
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期标签
|
||||
*/
|
||||
private formatDateLabel(date: Date, period: 'week' | 'month' | 'year'): string {
|
||||
const dayjsDate = dayjs(date);
|
||||
|
||||
switch (period) {
|
||||
case 'week':
|
||||
return dayjsDate.format('YYYY-MM-DD');
|
||||
case 'month':
|
||||
return dayjsDate.format('YYYY-MM-DD');
|
||||
case 'year':
|
||||
return dayjsDate.format('YYYY-MM');
|
||||
default:
|
||||
return dayjsDate.format('YYYY-MM-DD');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user