'성수서비스센터', 'CB' => '수원서비스센터', 'CC' => '대전서비스센터', 'CD' => '광주서비스센터' ]; return $centers[$code] ?? $code; } /** * Get service center codes from search keyword */ private function getServiceCenterCodes($keyword) { $centers = [ 'CA' => '성수서비스센터', 'CB' => '수원서비스센터', 'CC' => '대전서비스센터', 'CD' => '광주서비스센터' ]; $matchedCodes = []; foreach ($centers as $code => $name) { // 코드 또는 이름에 키워드가 포함되어 있으면 추가 if (stripos($code, $keyword) !== false || stripos($name, $keyword) !== false) { $matchedCodes[] = $code; } } return $matchedCodes; } /** * Get advisor list */ public function index() { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $params = $this->getPaginationParams(); $builder = $this->getDB()->table('advisors'); // Search $searchType = $this->request->getGet('search_type'); $searchKeyword = $this->request->getGet('search_keyword'); if ($searchKeyword) { if ($searchType === 'name') { $builder->like('name', $searchKeyword); } elseif ($searchType === 'service_center') { // 서비스센터 검색: 코드나 이름으로 검색 $matchedCodes = $this->getServiceCenterCodes($searchKeyword); if (!empty($matchedCodes)) { $builder->whereIn('service_center', $matchedCodes); } else { // 매칭되는 것이 없으면 결과 없음 $builder->where('1=0'); } } elseif ($searchType === 'all' || !$searchType) { // 전체 검색: 이름, 서비스센터(코드+이름), 대표번호에서 검색 $matchedCodes = $this->getServiceCenterCodes($searchKeyword); $builder->groupStart() ->like('name', $searchKeyword) ->orLike('main_phone', $searchKeyword); // 서비스센터 코드 매칭이 있으면 추가 if (!empty($matchedCodes)) { $builder->orWhereIn('service_center', $matchedCodes); } $builder->groupEnd(); } } $builder->orderBy('id', 'DESC'); $result = $this->paginatedResponse($builder, $params); // Add service_center_name to each item if (isset($result['items'])) { foreach ($result['items'] as &$item) { $item->service_center_name = $this->getServiceCenterName($item->service_center); } } return $this->respondSuccess($result); } /** * Get single advisor */ public function show($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('advisors'); $advisor = $builder->where('id', $id)->get()->getRow(); if (!$advisor) { return $this->respondError('어드바이저를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // Add service_center_name $advisor->service_center_name = $this->getServiceCenterName($advisor->service_center); return $this->respondSuccess($advisor); } /** * Create advisor */ public function create() { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); $data = [ 'service_center' => $json->service_center ?? '', 'name' => $json->name ?? '', 'main_phone' => $json->main_phone ?? '', 'direct_phone' => $json->direct_phone ?? '', 'photo_url' => $json->photo_url ?? '', 'created_at' => date('Y-m-d H:i:s') ]; $builder = $this->getDB()->table('advisors'); $builder->insert($data); return $this->respondSuccess(['id' => $this->getDB()->insertID()], '어드바이저가 등록되었습니다.'); } /** * Update advisor */ public function update($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); $data = [ 'service_center' => $json->service_center ?? '', 'name' => $json->name ?? '', 'main_phone' => $json->main_phone ?? '', 'direct_phone' => $json->direct_phone ?? '', 'photo_url' => $json->photo_url ?? '', 'updated_at' => date('Y-m-d H:i:s') ]; $builder = $this->getDB()->table('advisors'); $builder->where('id', $id)->update($data); return $this->respondSuccess(null, '어드바이저가 수정되었습니다.'); } /** * Delete advisor */ public function delete($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('advisors'); $builder->where('id', $id)->delete(); return $this->respondSuccess(null, '어드바이저가 삭제되었습니다.'); } }