78 lines
2.1 KiB
TypeScript
78 lines
2.1 KiB
TypeScript
import { query } from '../../utils/db'
|
|
|
|
/**
|
|
* 사업 목록 조회
|
|
* GET /api/business/list
|
|
*
|
|
* Query params:
|
|
* - status: 상태 필터 (active, completed, suspended)
|
|
* - businessName: 사업명 검색
|
|
* - businessCode: 사업코드 검색
|
|
* - clientName: 발주처 검색
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const params = getQuery(event)
|
|
|
|
const status = params.status as string | null
|
|
const businessName = params.businessName as string | null
|
|
const businessCode = params.businessCode as string | null
|
|
const clientName = params.clientName as string | null
|
|
|
|
const conditions: string[] = []
|
|
const values: any[] = []
|
|
let paramIndex = 1
|
|
|
|
if (status) {
|
|
conditions.push(`b.business_status = $${paramIndex++}`)
|
|
values.push(status)
|
|
}
|
|
|
|
if (businessName) {
|
|
conditions.push(`b.business_name ILIKE $${paramIndex++}`)
|
|
values.push(`%${businessName}%`)
|
|
}
|
|
|
|
if (businessCode) {
|
|
conditions.push(`b.business_code ILIKE $${paramIndex++}`)
|
|
values.push(`%${businessCode}%`)
|
|
}
|
|
|
|
if (clientName) {
|
|
conditions.push(`b.client_name ILIKE $${paramIndex++}`)
|
|
values.push(`%${clientName}%`)
|
|
}
|
|
|
|
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''
|
|
|
|
const sql = `
|
|
SELECT
|
|
b.*,
|
|
e.employee_name as created_by_name,
|
|
(SELECT COUNT(*) FROM wr_project_info WHERE business_id = b.business_id) as project_count
|
|
FROM wr_business b
|
|
LEFT JOIN wr_employee_info e ON b.created_by = e.employee_id
|
|
${whereClause}
|
|
ORDER BY b.created_at DESC
|
|
`
|
|
|
|
const businesses = await query(sql, values)
|
|
|
|
return {
|
|
businesses: businesses.map((b: any) => ({
|
|
businessId: b.business_id,
|
|
businessName: b.business_name,
|
|
businessCode: b.business_code,
|
|
clientName: b.client_name,
|
|
contractStartDate: b.contract_start_date,
|
|
contractEndDate: b.contract_end_date,
|
|
businessStatus: b.business_status,
|
|
description: b.description,
|
|
projectCount: Number(b.project_count),
|
|
createdBy: b.created_by,
|
|
createdByName: b.created_by_name,
|
|
createdAt: b.created_at,
|
|
updatedAt: b.updated_at
|
|
}))
|
|
}
|
|
})
|