55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
/**
|
|
* RFM 分组统计 API
|
|
* 对接 MongoDB 真实数据
|
|
*/
|
|
|
|
import { NextResponse } from "next/server"
|
|
import { getMongoRFMGroupSummary } from "@/services/rfm-mongodb-service"
|
|
import { getGroupSummary } from "@/services/rfm-engine"
|
|
|
|
export async function GET() {
|
|
try {
|
|
// 尝试从 MongoDB 获取真实数据
|
|
const mongoData = await getMongoRFMGroupSummary()
|
|
|
|
if (mongoData.totalUsers > 0) {
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: {
|
|
gradeCount: mongoData.gradeCount,
|
|
valueCount: mongoData.valueCount,
|
|
lifecycleCount: {}, // MongoDB 暂无此字段
|
|
totalUsers: mongoData.totalUsers,
|
|
avgScore: mongoData.avgScore
|
|
},
|
|
source: 'mongodb'
|
|
})
|
|
}
|
|
|
|
// 回退到内存数据
|
|
const memData = getGroupSummary()
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: memData,
|
|
source: 'memory'
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error('RFM group summary error:', error)
|
|
|
|
// 返回默认数据
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: {
|
|
gradeCount: { S: 0, A: 0, B: 0, C: 0, D: 0 },
|
|
valueCount: { '高': 0, '中': 0, '低': 0 },
|
|
lifecycleCount: {},
|
|
totalUsers: 0,
|
|
avgScore: 0
|
|
},
|
|
source: 'fallback',
|
|
error: error instanceof Error ? error.message : 'Unknown error'
|
|
})
|
|
}
|
|
}
|