53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
// Get material statistics (total count and monthly breakdown)
|
|
export async function GET() {
|
|
try {
|
|
// Get total count
|
|
const total = await prisma.material.count()
|
|
|
|
// Get all materials with their creation dates
|
|
const materials = await prisma.material.findMany({
|
|
select: {
|
|
createdAt: true
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc'
|
|
}
|
|
})
|
|
|
|
// Group by month
|
|
const monthMap = new Map<string, number>()
|
|
materials.forEach(material => {
|
|
const date = new Date(material.createdAt)
|
|
const monthKey = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`
|
|
monthMap.set(monthKey, (monthMap.get(monthKey) || 0) + 1)
|
|
})
|
|
|
|
// Convert to array and format
|
|
const months = Array.from(monthMap.entries())
|
|
.map(([value, count]) => {
|
|
const [year, month] = value.split('-')
|
|
return {
|
|
value,
|
|
label: `${year}年${parseInt(month)}月`,
|
|
count
|
|
}
|
|
})
|
|
.sort((a, b) => b.value.localeCompare(a.value)) // Sort by date descending
|
|
|
|
return NextResponse.json({
|
|
total,
|
|
months
|
|
})
|
|
} catch (error) {
|
|
console.error('Error fetching material stats:', error)
|
|
return NextResponse.json(
|
|
{ error: '获取统计数据失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|