BranchManagerController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace App\Controllers\Api;
  3. use CodeIgniter\HTTP\ResponseInterface;
  4. class BranchManagerController extends BaseApiController
  5. {
  6. /**
  7. * Get branch manager 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('branch_managers bm');
  17. $builder->select('bm.*, b.name as branch_name');
  18. $builder->join('branches b', 'b.id = bm.branch_id', 'left');
  19. // Filter by showroom (branch_id)
  20. $showroom = $this->request->getGet('showroom');
  21. if ($showroom) {
  22. $builder->where('bm.branch_id', $showroom);
  23. }
  24. // Search
  25. $searchType = $this->request->getGet('search_type');
  26. $searchKeyword = $this->request->getGet('search_keyword');
  27. if ($searchType && $searchKeyword) {
  28. if ($searchType === 'branch_name') {
  29. $builder->like('b.name', $searchKeyword);
  30. } elseif ($searchType === 'name') {
  31. $builder->like('bm.name', $searchKeyword);
  32. } elseif ($searchType === 'username') {
  33. $builder->like('bm.username', $searchKeyword);
  34. } elseif ($searchType === 'email') {
  35. $builder->like('bm.email', $searchKeyword);
  36. }
  37. }
  38. $builder->orderBy('bm.id', 'DESC');
  39. $result = $this->paginatedResponse($builder, $params);
  40. return $this->respondSuccess($result);
  41. }
  42. /**
  43. * Get single branch manager
  44. */
  45. public function show($id = null)
  46. {
  47. $auth = $this->requireAuth();
  48. if ($auth instanceof ResponseInterface) {
  49. return $auth;
  50. }
  51. $builder = $this->getDB()->table('branch_managers bm');
  52. $builder->select('bm.*, b.name as branch_name');
  53. $builder->join('branches b', 'b.id = bm.branch_id', 'left');
  54. $builder->where('bm.id', $id);
  55. $manager = $builder->get()->getRow();
  56. if (!$manager) {
  57. return $this->respondError('지점장을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  58. }
  59. // Remove password from response
  60. unset($manager->password);
  61. return $this->respondSuccess($manager);
  62. }
  63. /**
  64. * Check if user_id is available
  65. */
  66. public function checkUserId()
  67. {
  68. $auth = $this->requireAuth();
  69. if ($auth instanceof ResponseInterface) {
  70. return $auth;
  71. }
  72. $userId = $this->request->getGet('user_id');
  73. if (empty($userId)) {
  74. return $this->respondError('아이디를 입력하세요.');
  75. }
  76. $builder = $this->getDB()->table('branch_managers');
  77. $existing = $builder->where('username', $userId)->get()->getRow();
  78. return $this->respondSuccess([
  79. 'available' => !$existing
  80. ]);
  81. }
  82. /**
  83. * Create branch manager
  84. */
  85. public function create()
  86. {
  87. $auth = $this->requireAuth();
  88. if ($auth instanceof ResponseInterface) {
  89. return $auth;
  90. }
  91. $json = $this->request->getJSON();
  92. // Check if username already exists
  93. $builder = $this->getDB()->table('branch_managers');
  94. $existing = $builder->where('username', $json->user_id)->get()->getRow();
  95. if ($existing) {
  96. return $this->respondError('이미 사용 중인 아이디입니다.');
  97. }
  98. $data = [
  99. 'branch_id' => $json->branch_id ?? null,
  100. 'username' => $json->user_id ?? '', // 프론트에서 user_id로 전송
  101. 'password' => password_hash($json->password ?? '', PASSWORD_DEFAULT),
  102. 'name' => $json->name ?? '',
  103. 'email' => $json->email ?? '',
  104. 'greeting' => $json->greeting ?? '',
  105. 'photo_url' => $json->photo_url ?? '',
  106. 'created_at' => date('Y-m-d H:i:s')
  107. ];
  108. $builder->insert($data);
  109. return $this->respondSuccess(['id' => $this->getDB()->insertID()], '지점장이 등록되었습니다.');
  110. }
  111. /**
  112. * Update branch manager
  113. */
  114. public function update($id = null)
  115. {
  116. $auth = $this->requireAuth();
  117. if ($auth instanceof ResponseInterface) {
  118. return $auth;
  119. }
  120. $json = $this->request->getJSON();
  121. $data = [
  122. 'branch_id' => $json->branch_id ?? null,
  123. 'name' => $json->name ?? '',
  124. 'email' => $json->email ?? '',
  125. 'greeting' => $json->greeting ?? '',
  126. 'photo_url' => $json->photo_url ?? '',
  127. 'updated_at' => date('Y-m-d H:i:s')
  128. ];
  129. // Update password only if provided
  130. if (!empty($json->password)) {
  131. $data['password'] = password_hash($json->password, PASSWORD_DEFAULT);
  132. }
  133. $builder = $this->getDB()->table('branch_managers');
  134. $builder->where('id', $id)->update($data);
  135. return $this->respondSuccess(null, '지점장이 수정되었습니다.');
  136. }
  137. /**
  138. * Delete branch manager
  139. */
  140. public function delete($id = null)
  141. {
  142. $auth = $this->requireAuth();
  143. if ($auth instanceof ResponseInterface) {
  144. return $auth;
  145. }
  146. $builder = $this->getDB()->table('branch_managers');
  147. $builder->where('id', $id)->delete();
  148. return $this->respondSuccess(null, '지점장이 삭제되었습니다.');
  149. }
  150. /**
  151. * Get public branch manager list (No authentication required)
  152. * 공개 API - 인증 없이 지점장 정보 조회
  153. */
  154. public function publicList()
  155. {
  156. try {
  157. $builder = $this->getDB()->table('branch_managers bm');
  158. $builder->select('bm.*, b.name as branch_name');
  159. $builder->join('branches b', 'b.id = bm.branch_id', 'left');
  160. // Filter by showroom (branch_id)
  161. $showroom = $this->request->getGet('showroom');
  162. if ($showroom) {
  163. $builder->where('bm.branch_id', $showroom);
  164. }
  165. // Only active branches
  166. $builder->where('b.is_active', 1);
  167. $builder->orderBy('bm.id', 'DESC');
  168. $managers = $builder->get()->getResult();
  169. return $this->respondSuccess($managers);
  170. } catch (\Exception $e) {
  171. log_message('error', 'Branch manager public list error: ' . $e->getMessage());
  172. return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  173. }
  174. }
  175. }