ShowroomController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace App\Controllers\Api;
  3. use CodeIgniter\HTTP\ResponseInterface;
  4. class ShowroomController extends BaseApiController
  5. {
  6. /**
  7. * Get showroom 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('showrooms s');
  17. // Join with branches table to get branch name
  18. $builder->select('s.*, b.name as branch_name');
  19. $builder->join('branches b', 's.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('s.name', $searchKeyword);
  27. } elseif ($searchType === 'address') {
  28. $builder->like('s.address', $searchKeyword);
  29. } elseif ($searchType === 'phone') {
  30. $builder->like('s.main_phone', $searchKeyword);
  31. }
  32. }
  33. // is_active 필터링
  34. if ($isActive !== null && $isActive !== '' && $isActive !== false) {
  35. $builder->where('s.is_active', strval($isActive));
  36. }
  37. $builder->orderBy('s.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 showroom
  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('showrooms s');
  59. $builder->select('s.*, b.name as branch_name');
  60. $builder->join('branches b', 's.branch_id = b.id', 'left');
  61. $builder->where('s.id', $id);
  62. $showroom = $builder->get()->getRow();
  63. if (!$showroom) {
  64. return $this->respondError('전시장을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  65. }
  66. // Decode links JSON
  67. if (!empty($showroom->links)) {
  68. $showroom->links = json_decode($showroom->links, true);
  69. } else {
  70. $showroom->links = [];
  71. }
  72. return $this->respondSuccess($showroom);
  73. }
  74. /**
  75. * Create showroom
  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. 'quote_link' => $json->quote_link ?? '',
  114. 'test_drive_link' => $json->test_drive_link ?? '',
  115. 'links' => $links,
  116. 'created_at' => date('Y-m-d H:i:s')
  117. ];
  118. $builder = $this->getDB()->table('showrooms');
  119. $result = $builder->insert($data);
  120. if (!$result) {
  121. return $this->respondError('전시장 등록에 실패했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  122. }
  123. return $this->respondSuccess(['id' => $this->getDB()->insertID()], '전시장이 등록되었습니다.');
  124. } catch (\Exception $e) {
  125. log_message('error', 'Showroom create error: ' . $e->getMessage());
  126. return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  127. }
  128. }
  129. /**
  130. * Update showroom
  131. */
  132. public function update($id = null)
  133. {
  134. $auth = $this->requireAuth();
  135. if ($auth instanceof ResponseInterface) {
  136. return $auth;
  137. }
  138. $json = $this->request->getJSON();
  139. // Encode links to JSON
  140. $links = null;
  141. if (!empty($json->links) && is_array($json->links)) {
  142. $links = json_encode($json->links);
  143. }
  144. // 데이터 준비
  145. $data = [
  146. 'name' => $json->name ?? '',
  147. 'branch_id' => $json->branch_id ?? null,
  148. 'main_phone' => $json->phone ?? '',
  149. 'address' => $json->address ?? '',
  150. 'detail_address' => $json->detail_address ?? '',
  151. 'latitude' => $json->latitude ?? null,
  152. 'longitude' => $json->longitude ?? null,
  153. 'business_hours' => $json->business_hours ?? '',
  154. 'quote_link' => $json->quote_link ?? '',
  155. 'test_drive_link' => $json->test_drive_link ?? '',
  156. 'links' => $links,
  157. 'updated_at' => date('Y-m-d H:i:s')
  158. ];
  159. $builder = $this->getDB()->table('showrooms');
  160. $builder->where('id', $id)->update($data);
  161. return $this->respondSuccess(null, '전시장이 수정되었습니다.');
  162. }
  163. /**
  164. * Delete showroom
  165. */
  166. public function delete($id = null)
  167. {
  168. $auth = $this->requireAuth();
  169. if ($auth instanceof ResponseInterface) {
  170. return $auth;
  171. }
  172. $builder = $this->getDB()->table('showrooms');
  173. $builder->where('id', $id)->delete();
  174. return $this->respondSuccess(null, '전시장이 삭제되었습니다.');
  175. }
  176. /**
  177. * Toggle showroom active status
  178. */
  179. public function toggleActive($id = null)
  180. {
  181. $auth = $this->requireAuth();
  182. if ($auth instanceof ResponseInterface) {
  183. return $auth;
  184. }
  185. $builder = $this->getDB()->table('showrooms');
  186. $showroom = $builder->where('id', $id)->get()->getRow();
  187. if (!$showroom) {
  188. return $this->respondError('전시장을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  189. }
  190. // 현재 상태의 반대로 변경
  191. $newStatus = $showroom->is_active == 1 ? 0 : 1;
  192. $builder->where('id', $id)->update([
  193. 'is_active' => $newStatus,
  194. 'updated_at' => date('Y-m-d H:i:s')
  195. ]);
  196. $statusText = $newStatus == 1 ? '사용' : '비사용';
  197. return $this->respondSuccess(['is_active' => $newStatus], "전시장이 {$statusText} 상태로 변경되었습니다.");
  198. }
  199. /**
  200. * Get public showroom list (No authentication required)
  201. * 공개 API - 인증 없이 활성 전시장 목록 조회
  202. */
  203. public function publicList()
  204. {
  205. try {
  206. $builder = $this->getDB()->table('showrooms s');
  207. $builder->select('s.*, b.name as branch_name');
  208. $builder->join('branches b', 's.branch_id = b.id', 'left');
  209. // 활성화된 전시장만 조회
  210. $builder->where('s.is_active', 1);
  211. $builder->orderBy('s.id', 'DESC');
  212. $showrooms = $builder->get()->getResult();
  213. // Decode links JSON for each item
  214. foreach ($showrooms as &$showroom) {
  215. if (!empty($showroom->links)) {
  216. $showroom->links = json_decode($showroom->links, true);
  217. } else {
  218. $showroom->links = [];
  219. }
  220. }
  221. return $this->respondSuccess($showrooms);
  222. } catch (\Exception $e) {
  223. log_message('error', 'Showroom public list error: ' . $e->getMessage());
  224. return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  225. }
  226. }
  227. /**
  228. * Get public single showroom (No authentication required)
  229. * 공개 API - 인증 없이 단일 전시장 정보 조회
  230. */
  231. public function publicShow($id = null)
  232. {
  233. try {
  234. $builder = $this->getDB()->table('showrooms s');
  235. $builder->select('s.*, b.name as branch_name');
  236. $builder->join('branches b', 's.branch_id = b.id', 'left');
  237. $builder->where('s.id', $id);
  238. $builder->where('s.is_active', 1);
  239. $showroom = $builder->get()->getRow();
  240. if (!$showroom) {
  241. return $this->respondError('전시장을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  242. }
  243. // Decode links JSON
  244. if (!empty($showroom->links)) {
  245. $showroom->links = json_decode($showroom->links, true);
  246. } else {
  247. $showroom->links = [];
  248. }
  249. return $this->respondSuccess($showroom);
  250. } catch (\Exception $e) {
  251. log_message('error', 'Showroom public show error: ' . $e->getMessage());
  252. return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  253. }
  254. }
  255. }