ServiceCenterController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace App\Controllers\Api;
  3. use CodeIgniter\HTTP\ResponseInterface;
  4. class ServiceCenterController extends BaseApiController
  5. {
  6. /**
  7. * Get service center list
  8. */
  9. public function index()
  10. {
  11. $auth = $this->requireAuth();
  12. if ($auth instanceof ResponseInterface) {
  13. return $auth;
  14. }
  15. $params = $this->getPaginationParams();
  16. $builder = $this->getDB()->table('service_centers sc');
  17. // Join with branches table to get branch name
  18. $builder->select('sc.*, b.name as branch_name');
  19. $builder->join('branches b', 'sc.branch_id = b.id', 'left');
  20. // Search
  21. $searchType = $this->request->getGet('search_type');
  22. $searchKeyword = $this->request->getGet('search_keyword');
  23. $isActive = $this->request->getGet('is_active');
  24. if ($searchType && $searchKeyword) {
  25. if ($searchType === 'name') {
  26. $builder->like('sc.name', $searchKeyword);
  27. } elseif ($searchType === 'address') {
  28. $builder->like('sc.address', $searchKeyword);
  29. } elseif ($searchType === 'phone') {
  30. $builder->like('sc.main_phone', $searchKeyword);
  31. }
  32. }
  33. // is_active 필터링
  34. if ($isActive !== null && $isActive !== '' && $isActive !== false) {
  35. $builder->where('sc.is_active', strval($isActive));
  36. }
  37. $builder->orderBy('sc.id', 'DESC');
  38. $result = $this->paginatedResponse($builder, $params);
  39. // Decode links JSON for each item
  40. foreach ($result['items'] as &$item) {
  41. if (!empty($item->links)) {
  42. $item->links = json_decode($item->links, true);
  43. } else {
  44. $item->links = [];
  45. }
  46. }
  47. return $this->respondSuccess($result);
  48. }
  49. /**
  50. * Get single service center
  51. */
  52. public function show($id = null)
  53. {
  54. $auth = $this->requireAuth();
  55. if ($auth instanceof ResponseInterface) {
  56. return $auth;
  57. }
  58. $builder = $this->getDB()->table('service_centers sc');
  59. $builder->select('sc.*, b.name as branch_name');
  60. $builder->join('branches b', 'sc.branch_id = b.id', 'left');
  61. $builder->where('sc.id', $id);
  62. $serviceCenter = $builder->get()->getRow();
  63. if (!$serviceCenter) {
  64. return $this->respondError('서비스센터를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  65. }
  66. // Decode links JSON
  67. if (!empty($serviceCenter->links)) {
  68. $serviceCenter->links = json_decode($serviceCenter->links, true);
  69. } else {
  70. $serviceCenter->links = [];
  71. }
  72. return $this->respondSuccess($serviceCenter);
  73. }
  74. /**
  75. * Create service center
  76. */
  77. public function create()
  78. {
  79. $auth = $this->requireAuth();
  80. if ($auth instanceof ResponseInterface) {
  81. return $auth;
  82. }
  83. try {
  84. $json = $this->request->getJSON();
  85. // Validation
  86. if (empty($json->name)) {
  87. return $this->respondError('서비스센터명을 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
  88. }
  89. if (empty($json->branch_id)) {
  90. return $this->respondError('소속 지점을 선택하세요.', ResponseInterface::HTTP_BAD_REQUEST);
  91. }
  92. if (empty($json->phone)) {
  93. return $this->respondError('대표번호를 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
  94. }
  95. if (empty($json->address)) {
  96. return $this->respondError('주소를 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
  97. }
  98. // Encode links to JSON
  99. $links = null;
  100. if (!empty($json->links) && is_array($json->links)) {
  101. $links = json_encode($json->links);
  102. }
  103. // 데이터 준비
  104. $data = [
  105. 'name' => $json->name ?? '',
  106. 'branch_id' => $json->branch_id ?? null,
  107. 'main_phone' => $json->phone ?? '',
  108. 'address' => $json->address ?? '',
  109. 'detail_address' => $json->detail_address ?? '',
  110. 'latitude' => $json->latitude ?? null,
  111. 'longitude' => $json->longitude ?? null,
  112. 'business_hours' => $json->business_hours ?? '',
  113. 'service_reservation_link' => $json->service_reservation_link ?? '',
  114. 'links' => $links,
  115. 'created_at' => date('Y-m-d H:i:s')
  116. ];
  117. $builder = $this->getDB()->table('service_centers');
  118. $result = $builder->insert($data);
  119. if (!$result) {
  120. return $this->respondError('서비스센터 등록에 실패했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  121. }
  122. return $this->respondSuccess(['id' => $this->getDB()->insertID()], '서비스센터가 등록되었습니다.');
  123. } catch (\Exception $e) {
  124. log_message('error', 'ServiceCenter create error: ' . $e->getMessage());
  125. return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  126. }
  127. }
  128. /**
  129. * Update service center
  130. */
  131. public function update($id = null)
  132. {
  133. $auth = $this->requireAuth();
  134. if ($auth instanceof ResponseInterface) {
  135. return $auth;
  136. }
  137. $json = $this->request->getJSON();
  138. // Encode links to JSON
  139. $links = null;
  140. if (!empty($json->links) && is_array($json->links)) {
  141. $links = json_encode($json->links);
  142. }
  143. // 데이터 준비
  144. $data = [
  145. 'name' => $json->name ?? '',
  146. 'branch_id' => $json->branch_id ?? null,
  147. 'main_phone' => $json->phone ?? '',
  148. 'address' => $json->address ?? '',
  149. 'detail_address' => $json->detail_address ?? '',
  150. 'latitude' => $json->latitude ?? null,
  151. 'longitude' => $json->longitude ?? null,
  152. 'business_hours' => $json->business_hours ?? '',
  153. 'service_reservation_link' => $json->service_reservation_link ?? '',
  154. 'links' => $links,
  155. 'updated_at' => date('Y-m-d H:i:s')
  156. ];
  157. $builder = $this->getDB()->table('service_centers');
  158. $builder->where('id', $id)->update($data);
  159. return $this->respondSuccess(null, '서비스센터가 수정되었습니다.');
  160. }
  161. /**
  162. * Delete service center
  163. */
  164. public function delete($id = null)
  165. {
  166. $auth = $this->requireAuth();
  167. if ($auth instanceof ResponseInterface) {
  168. return $auth;
  169. }
  170. $builder = $this->getDB()->table('service_centers');
  171. $builder->where('id', $id)->delete();
  172. return $this->respondSuccess(null, '서비스센터가 삭제되었습니다.');
  173. }
  174. /**
  175. * Toggle service center active status
  176. */
  177. public function toggleActive($id = null)
  178. {
  179. $auth = $this->requireAuth();
  180. if ($auth instanceof ResponseInterface) {
  181. return $auth;
  182. }
  183. $builder = $this->getDB()->table('service_centers');
  184. $serviceCenter = $builder->where('id', $id)->get()->getRow();
  185. if (!$serviceCenter) {
  186. return $this->respondError('서비스센터를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  187. }
  188. // 현재 상태의 반대로 변경
  189. $newStatus = $serviceCenter->is_active == 1 ? 0 : 1;
  190. $builder->where('id', $id)->update([
  191. 'is_active' => $newStatus,
  192. 'updated_at' => date('Y-m-d H:i:s')
  193. ]);
  194. $statusText = $newStatus == 1 ? '사용' : '비사용';
  195. return $this->respondSuccess(['is_active' => $newStatus], "서비스센터가 {$statusText} 상태로 변경되었습니다.");
  196. }
  197. /**
  198. * Get public service center list (No authentication required)
  199. * 공개 API - 인증 없이 활성 서비스센터 목록 조회
  200. */
  201. public function publicList()
  202. {
  203. try {
  204. $builder = $this->getDB()->table('service_centers sc');
  205. $builder->select('sc.*, b.name as branch_name');
  206. $builder->join('branches b', 'sc.branch_id = b.id', 'left');
  207. // 활성화된 서비스센터만 조회
  208. $builder->where('sc.is_active', 1);
  209. $builder->orderBy('sc.id', 'DESC');
  210. $serviceCenters = $builder->get()->getResult();
  211. // Decode links JSON for each item
  212. foreach ($serviceCenters as &$serviceCenter) {
  213. if (!empty($serviceCenter->links)) {
  214. $serviceCenter->links = json_decode($serviceCenter->links, true);
  215. } else {
  216. $serviceCenter->links = [];
  217. }
  218. }
  219. return $this->respondSuccess($serviceCenters);
  220. } catch (\Exception $e) {
  221. log_message('error', 'ServiceCenter public list error: ' . $e->getMessage());
  222. return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  223. }
  224. }
  225. /**
  226. * Get public single service center (No authentication required)
  227. * 공개 API - 인증 없이 단일 서비스센터 정보 조회
  228. */
  229. public function publicShow($id = null)
  230. {
  231. try {
  232. $builder = $this->getDB()->table('service_centers sc');
  233. $builder->select('sc.*, b.name as branch_name');
  234. $builder->join('branches b', 'sc.branch_id = b.id', 'left');
  235. $builder->where('sc.id', $id);
  236. $builder->where('sc.is_active', 1);
  237. $serviceCenter = $builder->get()->getRow();
  238. if (!$serviceCenter) {
  239. return $this->respondError('서비스센터를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  240. }
  241. // Decode links JSON
  242. if (!empty($serviceCenter->links)) {
  243. $serviceCenter->links = json_decode($serviceCenter->links, true);
  244. } else {
  245. $serviceCenter->links = [];
  246. }
  247. return $this->respondSuccess($serviceCenter);
  248. } catch (\Exception $e) {
  249. log_message('error', 'ServiceCenter public show error: ' . $e->getMessage());
  250. return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  251. }
  252. }
  253. }