1ㅊㅏ완료
This commit is contained in:
112
backend/api/dashboard/stats.get.ts
Normal file
112
backend/api/dashboard/stats.get.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { query } from '../../utils/db'
|
||||
|
||||
/**
|
||||
* 대시보드 통계 API
|
||||
* GET /api/dashboard/stats
|
||||
*
|
||||
* - 인원별 이번 주 실적/차주 계획 시간
|
||||
* - 프로젝트별 투입 인원/시간
|
||||
* - 제출 현황
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const userId = getCookie(event, 'user_id')
|
||||
if (!userId) {
|
||||
throw createError({ statusCode: 401, message: '로그인이 필요합니다.' })
|
||||
}
|
||||
|
||||
const q = getQuery(event)
|
||||
const year = parseInt(q.year as string) || new Date().getFullYear()
|
||||
const week = parseInt(q.week as string) || getISOWeek(new Date())
|
||||
|
||||
// 1. 인원별 실적/계획 현황
|
||||
const employeeStats = await query<any>(`
|
||||
SELECT
|
||||
e.employee_id,
|
||||
e.employee_name,
|
||||
e.company,
|
||||
r.report_id,
|
||||
r.report_status,
|
||||
COALESCE(SUM(CASE WHEN t.task_type = 'WORK' THEN t.task_hours ELSE 0 END), 0) as work_hours,
|
||||
COALESCE(SUM(CASE WHEN t.task_type = 'PLAN' THEN t.task_hours ELSE 0 END), 0) as plan_hours,
|
||||
COUNT(DISTINCT CASE WHEN t.task_type = 'WORK' THEN t.project_id END) as work_project_count,
|
||||
COUNT(DISTINCT CASE WHEN t.task_type = 'PLAN' THEN t.project_id END) as plan_project_count
|
||||
FROM wr_employee_info e
|
||||
LEFT JOIN wr_weekly_report r ON e.employee_id = r.author_id
|
||||
AND r.report_year = $1 AND r.report_week = $2
|
||||
LEFT JOIN wr_weekly_report_task t ON r.report_id = t.report_id
|
||||
WHERE e.is_active = true
|
||||
GROUP BY e.employee_id, e.employee_name, e.company, r.report_id, r.report_status
|
||||
ORDER BY work_hours DESC, e.employee_name
|
||||
`, [year, week])
|
||||
|
||||
// 2. 프로젝트별 투입 현황
|
||||
const projectStats = await query<any>(`
|
||||
SELECT
|
||||
p.project_id,
|
||||
p.project_code,
|
||||
p.project_name,
|
||||
COUNT(DISTINCT r.author_id) as member_count,
|
||||
COALESCE(SUM(CASE WHEN t.task_type = 'WORK' THEN t.task_hours ELSE 0 END), 0) as work_hours,
|
||||
COALESCE(SUM(CASE WHEN t.task_type = 'PLAN' THEN t.task_hours ELSE 0 END), 0) as plan_hours,
|
||||
ARRAY_AGG(DISTINCT e.employee_name) as members
|
||||
FROM wr_project_info p
|
||||
JOIN wr_weekly_report_task t ON p.project_id = t.project_id
|
||||
JOIN wr_weekly_report r ON t.report_id = r.report_id
|
||||
AND r.report_year = $1 AND r.report_week = $2
|
||||
JOIN wr_employee_info e ON r.author_id = e.employee_id
|
||||
WHERE p.project_status = 'IN_PROGRESS'
|
||||
GROUP BY p.project_id, p.project_code, p.project_name
|
||||
ORDER BY work_hours DESC
|
||||
`, [year, week])
|
||||
|
||||
// 3. 전체 요약
|
||||
const activeEmployees = employeeStats.length
|
||||
const submittedCount = employeeStats.filter((e: any) => e.report_id).length
|
||||
const totalWorkHours = employeeStats.reduce((sum: number, e: any) => sum + parseFloat(e.work_hours || 0), 0)
|
||||
const totalPlanHours = employeeStats.reduce((sum: number, e: any) => sum + parseFloat(e.plan_hours || 0), 0)
|
||||
|
||||
return {
|
||||
year,
|
||||
week,
|
||||
summary: {
|
||||
activeEmployees,
|
||||
submittedCount,
|
||||
notSubmittedCount: activeEmployees - submittedCount,
|
||||
totalWorkHours,
|
||||
totalPlanHours,
|
||||
projectCount: projectStats.length
|
||||
},
|
||||
employees: employeeStats.map((e: any) => ({
|
||||
employeeId: e.employee_id,
|
||||
employeeName: e.employee_name,
|
||||
company: e.company,
|
||||
reportId: e.report_id,
|
||||
reportStatus: e.report_status,
|
||||
workHours: parseFloat(e.work_hours) || 0,
|
||||
planHours: parseFloat(e.plan_hours) || 0,
|
||||
workProjectCount: parseInt(e.work_project_count) || 0,
|
||||
planProjectCount: parseInt(e.plan_project_count) || 0,
|
||||
isSubmitted: !!e.report_id
|
||||
})),
|
||||
projects: projectStats.map((p: any) => ({
|
||||
projectId: p.project_id,
|
||||
projectCode: p.project_code,
|
||||
projectName: p.project_name,
|
||||
memberCount: parseInt(p.member_count) || 0,
|
||||
workHours: parseFloat(p.work_hours) || 0,
|
||||
planHours: parseFloat(p.plan_hours) || 0,
|
||||
members: p.members || []
|
||||
}))
|
||||
}
|
||||
})
|
||||
|
||||
function getISOWeek(date: Date): number {
|
||||
const target = new Date(date)
|
||||
target.setHours(0, 0, 0, 0)
|
||||
const thursday = new Date(target)
|
||||
thursday.setDate(target.getDate() - ((target.getDay() + 6) % 7) + 3)
|
||||
const year = thursday.getFullYear()
|
||||
const firstThursday = new Date(year, 0, 4)
|
||||
firstThursday.setDate(firstThursday.getDate() - ((firstThursday.getDay() + 6) % 7) + 3)
|
||||
return Math.ceil((thursday.getTime() - firstThursday.getTime()) / (7 * 24 * 60 * 60 * 1000)) + 1
|
||||
}
|
||||
Reference in New Issue
Block a user