BranchController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace App\Controllers\Api;
  3. use CodeIgniter\HTTP\ResponseInterface;
  4. class BranchController extends BaseApiController
  5. {
  6. /**
  7. * Get branch 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('branches');
  17. // Search
  18. $searchType = $this->request->getGet('search_type');
  19. $searchKeyword = $this->request->getGet('search_keyword');
  20. $isActive = $this->request->getGet('is_active');
  21. if ($searchType && $searchKeyword) {
  22. if ($searchType === 'name') {
  23. $builder->like('name', $searchKeyword);
  24. }
  25. }
  26. // is_active 필터링 (파라미터가 있을 때만)
  27. if ($isActive !== null && $isActive !== '' && $isActive !== false) {
  28. $builder->where('is_active', strval($isActive));
  29. }
  30. $builder->orderBy('id', 'DESC');
  31. $result = $this->paginatedResponse($builder, $params);
  32. return $this->respondSuccess($result);
  33. }
  34. /**
  35. * Get single branch
  36. */
  37. public function show($id = null)
  38. {
  39. $auth = $this->requireAuth();
  40. if ($auth instanceof ResponseInterface) {
  41. return $auth;
  42. }
  43. $builder = $this->getDB()->table('branches');
  44. $branch = $builder->where('id', $id)->get()->getRow();
  45. if (!$branch) {
  46. return $this->respondError('지점을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  47. }
  48. return $this->respondSuccess($branch);
  49. }
  50. /**
  51. * Create branch
  52. */
  53. public function create()
  54. {
  55. $auth = $this->requireAuth();
  56. if ($auth instanceof ResponseInterface) {
  57. return $auth;
  58. }
  59. try {
  60. $json = $this->request->getJSON();
  61. // Validation
  62. if (empty($json->name)) {
  63. return $this->respondError('딜러사 명을 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
  64. }
  65. // if (empty($json->phone)) {
  66. // return $this->respondError('대표번호를 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
  67. // }
  68. // if (empty($json->address)) {
  69. // return $this->respondError('주소를 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
  70. // }
  71. // 데이터 준비
  72. $data = [
  73. 'name' => $json->name ?? '',
  74. // 'main_phone' => $json->phone ?? '', // DB 컬럼명은 main_phone
  75. // 'address' => $json->address ?? '',
  76. // 'detail_address' => $json->detail_address ?? '',
  77. // 'latitude' => $json->latitude ?? null,
  78. // 'longitude' => $json->longitude ?? null,
  79. // 'business_hours' => $json->business_hours ?? '',
  80. 'created_at' => date('Y-m-d H:i:s')
  81. ];
  82. $builder = $this->getDB()->table('branches');
  83. $result = $builder->insert($data);
  84. if (!$result) {
  85. return $this->respondError('지점 등록에 실패했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  86. }
  87. return $this->respondSuccess(['id' => $this->getDB()->insertID()], '지점이 등록되었습니다.');
  88. } catch (\Exception $e) {
  89. log_message('error', 'Branch create error: ' . $e->getMessage());
  90. return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  91. }
  92. }
  93. /**
  94. * Update branch
  95. */
  96. public function update($id = null)
  97. {
  98. $auth = $this->requireAuth();
  99. if ($auth instanceof ResponseInterface) {
  100. return $auth;
  101. }
  102. $json = $this->request->getJSON();
  103. // 데이터 준비
  104. $data = [
  105. 'name' => $json->name ?? '',
  106. // 'main_phone' => $json->phone ?? '', // DB 컬럼명은 main_phone
  107. // 'address' => $json->address ?? '',
  108. // 'detail_address' => $json->detail_address ?? '',
  109. // 'latitude' => $json->latitude ?? null,
  110. // 'longitude' => $json->longitude ?? null,
  111. // 'business_hours' => $json->business_hours ?? '',
  112. 'updated_at' => date('Y-m-d H:i:s')
  113. ];
  114. $builder = $this->getDB()->table('branches');
  115. $builder->where('id', $id)->update($data);
  116. return $this->respondSuccess(null, '지점이 수정되었습니다.');
  117. }
  118. /**
  119. * Delete branch
  120. */
  121. public function delete($id = null)
  122. {
  123. $auth = $this->requireAuth();
  124. if ($auth instanceof ResponseInterface) {
  125. return $auth;
  126. }
  127. $builder = $this->getDB()->table('branches');
  128. $builder->where('id', $id)->delete();
  129. return $this->respondSuccess(null, '지점이 삭제되었습니다.');
  130. }
  131. /**
  132. * Toggle branch active status
  133. */
  134. public function toggleActive($id = null)
  135. {
  136. $auth = $this->requireAuth();
  137. if ($auth instanceof ResponseInterface) {
  138. return $auth;
  139. }
  140. $builder = $this->getDB()->table('branches');
  141. $branch = $builder->where('id', $id)->get()->getRow();
  142. if (!$branch) {
  143. return $this->respondError('지점을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  144. }
  145. // 현재 상태의 반대로 변경
  146. $newStatus = $branch->is_active == 1 ? 0 : 1;
  147. $builder->where('id', $id)->update([
  148. 'is_active' => $newStatus,
  149. 'updated_at' => date('Y-m-d H:i:s')
  150. ]);
  151. $statusText = $newStatus == 1 ? '사용' : '비사용';
  152. return $this->respondSuccess(['is_active' => $newStatus], "지점이 {$statusText} 상태로 변경되었습니다.");
  153. }
  154. /**
  155. * Get public branch list (No authentication required)
  156. * 공개 API - 인증 없이 활성 지점 목록 조회
  157. */
  158. public function publicList()
  159. {
  160. try {
  161. $builder = $this->getDB()->table('branches');
  162. // 활성화된 지점만 조회
  163. $builder->where('is_active', 1);
  164. $builder->orderBy('id', 'DESC');
  165. $branches = $builder->get()->getResult();
  166. return $this->respondSuccess($branches);
  167. } catch (\Exception $e) {
  168. log_message('error', 'Branch public list error: ' . $e->getMessage());
  169. return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  170. }
  171. }
  172. /**
  173. * Get public single branch (No authentication required)
  174. * 공개 API - 인증 없이 단일 지점 정보 조회
  175. */
  176. public function publicShow($id = null)
  177. {
  178. try {
  179. $builder = $this->getDB()->table('branches');
  180. $builder->where('id', $id);
  181. $builder->where('is_active', 1);
  182. $branch = $builder->get()->getRow();
  183. if (!$branch) {
  184. return $this->respondError('지점을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  185. }
  186. return $this->respondSuccess($branch);
  187. } catch (\Exception $e) {
  188. log_message('error', 'Branch public show error: ' . $e->getMessage());
  189. return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  190. }
  191. }
  192. }