| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- namespace App\Controllers\Api;
- use CodeIgniter\HTTP\ResponseInterface;
- class ServiceCenterController extends BaseApiController
- {
- /**
- * Get service center list
- */
- public function index()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $params = $this->getPaginationParams();
- $builder = $this->getDB()->table('service_centers sc');
- // Join with branches table to get branch name
- $builder->select('sc.*, b.name as branch_name');
- $builder->join('branches b', 'sc.branch_id = b.id', 'left');
- // Search
- $searchType = $this->request->getGet('search_type');
- $searchKeyword = $this->request->getGet('search_keyword');
- $isActive = $this->request->getGet('is_active');
- if ($searchType && $searchKeyword) {
- if ($searchType === 'name') {
- $builder->like('sc.name', $searchKeyword);
- } elseif ($searchType === 'address') {
- $builder->like('sc.address', $searchKeyword);
- } elseif ($searchType === 'phone') {
- $builder->like('sc.main_phone', $searchKeyword);
- }
- }
- // is_active 필터링
- if ($isActive !== null && $isActive !== '' && $isActive !== false) {
- $builder->where('sc.is_active', strval($isActive));
- }
- $builder->orderBy('sc.id', 'DESC');
- $result = $this->paginatedResponse($builder, $params);
- // Decode links JSON for each item
- foreach ($result['items'] as &$item) {
- if (!empty($item->links)) {
- $item->links = json_decode($item->links, true);
- } else {
- $item->links = [];
- }
- }
- return $this->respondSuccess($result);
- }
- /**
- * Get single service center
- */
- public function show($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $builder = $this->getDB()->table('service_centers sc');
- $builder->select('sc.*, b.name as branch_name');
- $builder->join('branches b', 'sc.branch_id = b.id', 'left');
- $builder->where('sc.id', $id);
- $serviceCenter = $builder->get()->getRow();
- if (!$serviceCenter) {
- return $this->respondError('서비스센터를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- // Decode links JSON
- if (!empty($serviceCenter->links)) {
- $serviceCenter->links = json_decode($serviceCenter->links, true);
- } else {
- $serviceCenter->links = [];
- }
- return $this->respondSuccess($serviceCenter);
- }
- /**
- * Create service center
- */
- public function create()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- try {
- $json = $this->request->getJSON();
- // Validation
- if (empty($json->name)) {
- return $this->respondError('서비스센터명을 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- if (empty($json->branch_id)) {
- return $this->respondError('소속 지점을 선택하세요.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- if (empty($json->phone)) {
- return $this->respondError('대표번호를 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- if (empty($json->address)) {
- return $this->respondError('주소를 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- // Encode links to JSON
- $links = null;
- if (!empty($json->links) && is_array($json->links)) {
- $links = json_encode($json->links);
- }
- // 데이터 준비
- $data = [
- 'name' => $json->name ?? '',
- 'branch_id' => $json->branch_id ?? null,
- 'main_phone' => $json->phone ?? '',
- 'address' => $json->address ?? '',
- 'detail_address' => $json->detail_address ?? '',
- 'latitude' => $json->latitude ?? null,
- 'longitude' => $json->longitude ?? null,
- 'business_hours' => $json->business_hours ?? '',
- 'service_reservation_link' => $json->service_reservation_link ?? '',
- 'links' => $links,
- 'created_at' => date('Y-m-d H:i:s')
- ];
- $builder = $this->getDB()->table('service_centers');
- $result = $builder->insert($data);
- if (!$result) {
- return $this->respondError('서비스센터 등록에 실패했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- return $this->respondSuccess(['id' => $this->getDB()->insertID()], '서비스센터가 등록되었습니다.');
- } catch (\Exception $e) {
- log_message('error', 'ServiceCenter create error: ' . $e->getMessage());
- return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- /**
- * Update service center
- */
- public function update($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $json = $this->request->getJSON();
- // Encode links to JSON
- $links = null;
- if (!empty($json->links) && is_array($json->links)) {
- $links = json_encode($json->links);
- }
- // 데이터 준비
- $data = [
- 'name' => $json->name ?? '',
- 'branch_id' => $json->branch_id ?? null,
- 'main_phone' => $json->phone ?? '',
- 'address' => $json->address ?? '',
- 'detail_address' => $json->detail_address ?? '',
- 'latitude' => $json->latitude ?? null,
- 'longitude' => $json->longitude ?? null,
- 'business_hours' => $json->business_hours ?? '',
- 'service_reservation_link' => $json->service_reservation_link ?? '',
- 'links' => $links,
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- $builder = $this->getDB()->table('service_centers');
- $builder->where('id', $id)->update($data);
- return $this->respondSuccess(null, '서비스센터가 수정되었습니다.');
- }
- /**
- * Delete service center
- */
- public function delete($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $builder = $this->getDB()->table('service_centers');
- $builder->where('id', $id)->delete();
- return $this->respondSuccess(null, '서비스센터가 삭제되었습니다.');
- }
- /**
- * Toggle service center active status
- */
- public function toggleActive($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $builder = $this->getDB()->table('service_centers');
- $serviceCenter = $builder->where('id', $id)->get()->getRow();
- if (!$serviceCenter) {
- return $this->respondError('서비스센터를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- // 현재 상태의 반대로 변경
- $newStatus = $serviceCenter->is_active == 1 ? 0 : 1;
- $builder->where('id', $id)->update([
- 'is_active' => $newStatus,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- $statusText = $newStatus == 1 ? '사용' : '비사용';
- return $this->respondSuccess(['is_active' => $newStatus], "서비스센터가 {$statusText} 상태로 변경되었습니다.");
- }
- /**
- * Get public service center list (No authentication required)
- * 공개 API - 인증 없이 활성 서비스센터 목록 조회
- */
- public function publicList()
- {
- try {
- $builder = $this->getDB()->table('service_centers sc');
- $builder->select('sc.*, b.name as branch_name');
- $builder->join('branches b', 'sc.branch_id = b.id', 'left');
- // 활성화된 서비스센터만 조회
- $builder->where('sc.is_active', 1);
- $builder->orderBy('sc.id', 'DESC');
- $serviceCenters = $builder->get()->getResult();
- // Decode links JSON for each item
- foreach ($serviceCenters as &$serviceCenter) {
- if (!empty($serviceCenter->links)) {
- $serviceCenter->links = json_decode($serviceCenter->links, true);
- } else {
- $serviceCenter->links = [];
- }
- }
- return $this->respondSuccess($serviceCenters);
- } catch (\Exception $e) {
- log_message('error', 'ServiceCenter public list error: ' . $e->getMessage());
- return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- /**
- * Get public single service center (No authentication required)
- * 공개 API - 인증 없이 단일 서비스센터 정보 조회
- */
- public function publicShow($id = null)
- {
- try {
- $builder = $this->getDB()->table('service_centers sc');
- $builder->select('sc.*, b.name as branch_name');
- $builder->join('branches b', 'sc.branch_id = b.id', 'left');
- $builder->where('sc.id', $id);
- $builder->where('sc.is_active', 1);
- $serviceCenter = $builder->get()->getRow();
- if (!$serviceCenter) {
- return $this->respondError('서비스센터를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- // Decode links JSON
- if (!empty($serviceCenter->links)) {
- $serviceCenter->links = json_decode($serviceCenter->links, true);
- } else {
- $serviceCenter->links = [];
- }
- return $this->respondSuccess($serviceCenter);
- } catch (\Exception $e) {
- log_message('error', 'ServiceCenter public show error: ' . $e->getMessage());
- return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- }
|