| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace App\Controllers\Api;
- use CodeIgniter\HTTP\ResponseInterface;
- class BranchManagerController extends BaseApiController
- {
- /**
- * Get branch manager list
- */
- public function index()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $params = $this->getPaginationParams();
- $builder = $this->getDB()->table('branch_managers bm');
- $builder->select('bm.*, b.name as branch_name');
- $builder->join('branches b', 'b.id = bm.branch_id', 'left');
- // Filter by showroom (branch_id)
- $showroom = $this->request->getGet('showroom');
- if ($showroom) {
- $builder->where('bm.branch_id', $showroom);
- }
- // Search
- $searchType = $this->request->getGet('search_type');
- $searchKeyword = $this->request->getGet('search_keyword');
- if ($searchType && $searchKeyword) {
- if ($searchType === 'branch_name') {
- $builder->like('b.name', $searchKeyword);
- } elseif ($searchType === 'name') {
- $builder->like('bm.name', $searchKeyword);
- } elseif ($searchType === 'username') {
- $builder->like('bm.username', $searchKeyword);
- } elseif ($searchType === 'email') {
- $builder->like('bm.email', $searchKeyword);
- }
- }
- $builder->orderBy('bm.id', 'DESC');
- $result = $this->paginatedResponse($builder, $params);
- return $this->respondSuccess($result);
- }
- /**
- * Get single branch manager
- */
- public function show($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $builder = $this->getDB()->table('branch_managers bm');
- $builder->select('bm.*, b.name as branch_name');
- $builder->join('branches b', 'b.id = bm.branch_id', 'left');
- $builder->where('bm.id', $id);
- $manager = $builder->get()->getRow();
- if (!$manager) {
- return $this->respondError('지점장을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- // Remove password from response
- unset($manager->password);
- return $this->respondSuccess($manager);
- }
- /**
- * Check if user_id is available
- */
- public function checkUserId()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $userId = $this->request->getGet('user_id');
- if (empty($userId)) {
- return $this->respondError('아이디를 입력하세요.');
- }
- $builder = $this->getDB()->table('branch_managers');
- $existing = $builder->where('username', $userId)->get()->getRow();
- return $this->respondSuccess([
- 'available' => !$existing
- ]);
- }
- /**
- * Create branch manager
- */
- public function create()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $json = $this->request->getJSON();
- // Check if username already exists
- $builder = $this->getDB()->table('branch_managers');
- $existing = $builder->where('username', $json->user_id)->get()->getRow();
- if ($existing) {
- return $this->respondError('이미 사용 중인 아이디입니다.');
- }
- $data = [
- 'branch_id' => $json->branch_id ?? null,
- 'username' => $json->user_id ?? '', // 프론트에서 user_id로 전송
- 'password' => password_hash($json->password ?? '', PASSWORD_DEFAULT),
- 'name' => $json->name ?? '',
- 'email' => $json->email ?? '',
- 'greeting' => $json->greeting ?? '',
- 'photo_url' => $json->photo_url ?? '',
- 'created_at' => date('Y-m-d H:i:s')
- ];
- $builder->insert($data);
- return $this->respondSuccess(['id' => $this->getDB()->insertID()], '지점장이 등록되었습니다.');
- }
- /**
- * Update branch manager
- */
- public function update($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $json = $this->request->getJSON();
- $data = [
- 'branch_id' => $json->branch_id ?? null,
- 'name' => $json->name ?? '',
- 'email' => $json->email ?? '',
- 'greeting' => $json->greeting ?? '',
- 'photo_url' => $json->photo_url ?? '',
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- // Update password only if provided
- if (!empty($json->password)) {
- $data['password'] = password_hash($json->password, PASSWORD_DEFAULT);
- }
- $builder = $this->getDB()->table('branch_managers');
- $builder->where('id', $id)->update($data);
- return $this->respondSuccess(null, '지점장이 수정되었습니다.');
- }
- /**
- * Delete branch manager
- */
- public function delete($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $builder = $this->getDB()->table('branch_managers');
- $builder->where('id', $id)->delete();
- return $this->respondSuccess(null, '지점장이 삭제되었습니다.');
- }
- /**
- * Get public branch manager list (No authentication required)
- * 공개 API - 인증 없이 지점장 정보 조회
- */
- public function publicList()
- {
- try {
- $builder = $this->getDB()->table('branch_managers bm');
- $builder->select('bm.*, b.name as branch_name');
- $builder->join('branches b', 'b.id = bm.branch_id', 'left');
- // Filter by showroom (branch_id)
- $showroom = $this->request->getGet('showroom');
- if ($showroom) {
- $builder->where('bm.branch_id', $showroom);
- }
- // Only active branches
- $builder->where('b.is_active', 1);
- $builder->orderBy('bm.id', 'DESC');
- $managers = $builder->get()->getResult();
- return $this->respondSuccess($managers);
- } catch (\Exception $e) {
- log_message('error', 'Branch manager public list error: ' . $e->getMessage());
- return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- }
|