requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $params = $this->getPaginationParams(); $builder = $this->getDB()->table('sales_staff'); // Search filters $showroom = $this->request->getGet('showroom'); $team = $this->request->getGet('team'); $position = $this->request->getGet('position'); $keyword = $this->request->getGet('keyword'); $name = $this->request->getGet('name'); $isAct = $this->request->getGet('is_act'); if ($showroom) { $builder->where('showroom', $showroom); } if ($team) { $builder->where('team', $team); } if ($position) { $builder->where('position', $position); } // is_act 필터링 (파라미터가 있을 때만) // 문자열 비교를 위해 형변환 없이 처리 if ($isAct !== null && $isAct !== '' && $isAct !== false) { $builder->where('is_act', strval($isAct)); } // keyword가 있으면 전체 검색 (이름, 직책, 대표번호, 이메일, 전시장, 팀) if ($keyword) { $builder->groupStart() ->like('name', $keyword) ->orLike('position', $keyword) ->orLike('main_phone', $keyword) ->orLike('email', $keyword) ->orLike('showroom', $keyword) ->orLike('team', $keyword) ->groupEnd(); } else if ($name) { // name 파라미터는 이름만 검색 (하위 호환성) $builder->like('name', $name); } // 최신 등록순으로 정렬 (id 내림차순) $builder->orderBy('id', 'DESC'); $result = $this->paginatedResponse($builder, $params); return $this->respondSuccess($result); } /** * Get single sales staff */ public function show($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('sales_staff'); $staff = $builder->where('id', $id)->get()->getRow(); if (!$staff) { return $this->respondError('영업사원을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } return $this->respondSuccess($staff); } /** * Create sales staff */ public function create() { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); $data = [ 'showroom' => $json->showroom ?? '', 'team' => $json->team ?? '', 'name' => $json->name ?? '', 'position' => (string)($json->position ?? ''), 'main_phone' => $json->main_phone ?? '', 'direct_phone' => $json->direct_phone ?? '', 'email' => $json->email ?? '', 'photo_url' => $json->photo_url ?? '', 'is_sact' => $json->is_sact ?? 0, 'is_goldplt' => $json->is_goldplt ?? 0, 'is_top30' => $json->is_top30 ?? 0, 'is_best_advisor' => $json->is_best_advisor ?? 0, 'is_best_advisor_year' => $json->is_best_advisor_year ?? 0, 'display_order' => $json->display_order ?? 0, 'created_at' => date('Y-m-d H:i:s') ]; $builder = $this->getDB()->table('sales_staff'); $builder->insert($data); return $this->respondSuccess(['id' => $this->getDB()->insertID()], '영업사원이 등록되었습니다.'); } /** * Update sales staff */ public function update($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); $data = [ 'showroom' => $json->showroom ?? '', 'team' => $json->team ?? '', 'name' => $json->name ?? '', 'position' => (string)($json->position ?? ''), 'main_phone' => $json->main_phone ?? '', 'direct_phone' => $json->direct_phone ?? '', 'email' => $json->email ?? '', 'photo_url' => $json->photo_url ?? '', 'is_sact' => $json->is_sact ?? 0, 'is_goldplt' => $json->is_goldplt ?? 0, 'is_top30' => $json->is_top30 ?? 0, 'is_best_advisor' => $json->is_best_advisor ?? 0, 'is_best_advisor_year' => $json->is_best_advisor_year ?? 0, 'display_order' => $json->display_order ?? 0, 'updated_at' => date('Y-m-d H:i:s') ]; $builder = $this->getDB()->table('sales_staff'); $builder->where('id', $id)->update($data); return $this->respondSuccess(null, '영업사원이 수정되었습니다.'); } /** * Delete sales staff */ public function delete($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('sales_staff'); $builder->where('id', $id)->delete(); return $this->respondSuccess(null, '영업사원이 삭제되었습니다.'); } /** * Print single */ public function printSingle($id) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } // TODO: Implement print logic return $this->respondSuccess(null, 'Print endpoint'); } /** * Toggle is_act status (노출/비노출 토글) */ public function toggleActive($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('sales_staff'); $staff = $builder->where('id', $id)->get()->getRow(); if (!$staff) { return $this->respondError('영업사원을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // Toggle is_act: 1 -> 0, 0 -> 1 $newStatus = $staff->is_act == 1 ? 0 : 1; $builder->where('id', $id)->update([ 'is_act' => $newStatus, 'updated_at' => date('Y-m-d H:i:s') ]); $statusText = $newStatus == 1 ? '노출' : '비노출'; return $this->respondSuccess( ['is_act' => $newStatus], "영업사원이 {$statusText} 상태로 변경되었습니다." ); } /** * Toggle is_best_advisor status (Best Sales Advisor 토글) */ public function toggleBestAdvisor($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('sales_staff'); $staff = $builder->where('id', $id)->get()->getRow(); if (!$staff) { return $this->respondError('영업사원을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // Toggle is_best_advisor: 1 -> 0, 0 -> 1 $newStatus = $staff->is_best_advisor == 1 ? 0 : 1; $builder->where('id', $id)->update([ 'is_best_advisor' => $newStatus, 'updated_at' => date('Y-m-d H:i:s') ]); $statusText = $newStatus == 1 ? 'Best Sales Advisor로 지정' : 'Best Sales Advisor 해제'; return $this->respondSuccess( ['is_best_advisor' => $newStatus], "{$statusText}되었습니다." ); } /** * Toggle is_best_advisor_year status (Best Sales Advisor Year 토글) */ public function toggleBestAdvisorYear($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('sales_staff'); $staff = $builder->where('id', $id)->get()->getRow(); if (!$staff) { return $this->respondError('영업사원을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // Toggle is_best_advisor_year: 1 -> 0, 0 -> 1 $newStatus = $staff->is_best_advisor_year == 1 ? 0 : 1; $builder->where('id', $id)->update([ 'is_best_advisor_year' => $newStatus, 'updated_at' => date('Y-m-d H:i:s') ]); $statusText = $newStatus == 1 ? 'Best Sales Advisor (Year)로 지정' : 'Best Sales Advisor (Year) 해제'; return $this->respondSuccess( ['is_best_advisor_year' => $newStatus], "{$statusText}되었습니다." ); } /** * Get public sales staff list (No authentication required) * 공개 API - 인증 없이 영업사원 목록 조회 */ public function publicList() { try { $builder = $this->getDB()->table('sales_staff'); // Filter by showroom $showroom = $this->request->getGet('showroom'); if ($showroom) { $builder->where('showroom', $showroom); } // Only show active sales staff (is_act = 1) $builder->where('is_act', 1); $builder->orderBy('id', 'DESC'); $salesStaff = $builder->get()->getResult(); return $this->respondSuccess($salesStaff); } catch (\Exception $e) { log_message('error', 'Sales staff public list error: ' . $e->getMessage()); log_message('error', 'Stack trace: ' . $e->getTraceAsString()); return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR); } } /** * Get best advisors list (No authentication required) * 공개 API - Best Sales Advisor 목록 조회 */ public function bestAdvisors() { try { $builder = $this->getDB()->table('sales_staff'); // Filter by showroom (optional) $showroom = $this->request->getGet('showroom'); if ($showroom) { $builder->where('showroom', $showroom); } // Only show active and best advisors $builder->where('is_act', 1); $builder->where('is_best_advisor', 1); // Order by display_order, then by id DESC $builder->orderBy('display_order', 'ASC'); $builder->orderBy('id', 'DESC'); $bestAdvisors = $builder->get()->getResult(); return $this->respondSuccess($bestAdvisors); } catch (\Exception $e) { log_message('error', 'Best advisors list error: ' . $e->getMessage()); log_message('error', 'Stack trace: ' . $e->getTraceAsString()); return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR); } } /** * Get best advisors year list (No authentication required) * 공개 API - Best Sales Advisor (Year) 목록 조회 */ public function bestAdvisorsYear() { try { $builder = $this->getDB()->table('sales_staff'); // Filter by showroom (optional) $showroom = $this->request->getGet('showroom'); if ($showroom) { $builder->where('showroom', $showroom); } // Only show active and best advisors (year) $builder->where('is_act', 1); $builder->where('is_best_advisor_year', 1); // Order by display_order, then by id DESC $builder->orderBy('display_order', 'ASC'); $builder->orderBy('id', 'DESC'); $bestAdvisorsYear = $builder->get()->getResult(); return $this->respondSuccess($bestAdvisorsYear); } catch (\Exception $e) { log_message('error', 'Best advisors year list error: ' . $e->getMessage()); log_message('error', 'Stack trace: ' . $e->getTraceAsString()); return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR); } } }