SalesStaffController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace App\Controllers\Api;
  3. use CodeIgniter\HTTP\ResponseInterface;
  4. class SalesStaffController extends BaseApiController
  5. {
  6. /**
  7. * Get sales staff 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('sales_staff');
  17. // Search filters
  18. $showroom = $this->request->getGet('showroom');
  19. $team = $this->request->getGet('team');
  20. $position = $this->request->getGet('position');
  21. $keyword = $this->request->getGet('keyword');
  22. $name = $this->request->getGet('name');
  23. $isAct = $this->request->getGet('is_act');
  24. if ($showroom) {
  25. $builder->where('showroom', $showroom);
  26. }
  27. if ($team) {
  28. $builder->where('team', $team);
  29. }
  30. if ($position) {
  31. $builder->where('position', $position);
  32. }
  33. // is_act 필터링 (파라미터가 있을 때만)
  34. // 문자열 비교를 위해 형변환 없이 처리
  35. if ($isAct !== null && $isAct !== '' && $isAct !== false) {
  36. $builder->where('is_act', strval($isAct));
  37. }
  38. // keyword가 있으면 전체 검색 (이름, 직책, 대표번호, 이메일, 전시장, 팀)
  39. if ($keyword) {
  40. $builder->groupStart()
  41. ->like('name', $keyword)
  42. ->orLike('position', $keyword)
  43. ->orLike('main_phone', $keyword)
  44. ->orLike('email', $keyword)
  45. ->orLike('showroom', $keyword)
  46. ->orLike('team', $keyword)
  47. ->groupEnd();
  48. } else if ($name) {
  49. // name 파라미터는 이름만 검색 (하위 호환성)
  50. $builder->like('name', $name);
  51. }
  52. // 최신 등록순으로 정렬 (id 내림차순)
  53. $builder->orderBy('id', 'DESC');
  54. $result = $this->paginatedResponse($builder, $params);
  55. return $this->respondSuccess($result);
  56. }
  57. /**
  58. * Get single sales staff
  59. */
  60. public function show($id = null)
  61. {
  62. $auth = $this->requireAuth();
  63. if ($auth instanceof ResponseInterface) {
  64. return $auth;
  65. }
  66. $builder = $this->getDB()->table('sales_staff');
  67. $staff = $builder->where('id', $id)->get()->getRow();
  68. if (!$staff) {
  69. return $this->respondError('영업사원을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  70. }
  71. return $this->respondSuccess($staff);
  72. }
  73. /**
  74. * Create sales staff
  75. */
  76. public function create()
  77. {
  78. $auth = $this->requireAuth();
  79. if ($auth instanceof ResponseInterface) {
  80. return $auth;
  81. }
  82. $json = $this->request->getJSON();
  83. $data = [
  84. 'showroom' => $json->showroom ?? '',
  85. 'team' => $json->team ?? '',
  86. 'name' => $json->name ?? '',
  87. 'position' => (string)($json->position ?? ''),
  88. 'main_phone' => $json->main_phone ?? '',
  89. 'direct_phone' => $json->direct_phone ?? '',
  90. 'email' => $json->email ?? '',
  91. 'photo_url' => $json->photo_url ?? '',
  92. 'is_sact' => $json->is_sact ?? 0,
  93. 'is_goldplt' => $json->is_goldplt ?? 0,
  94. 'is_top30' => $json->is_top30 ?? 0,
  95. 'is_best_advisor' => $json->is_best_advisor ?? 0,
  96. 'is_best_advisor_year' => $json->is_best_advisor_year ?? 0,
  97. 'display_order' => $json->display_order ?? 0,
  98. 'created_at' => date('Y-m-d H:i:s')
  99. ];
  100. $builder = $this->getDB()->table('sales_staff');
  101. $builder->insert($data);
  102. return $this->respondSuccess(['id' => $this->getDB()->insertID()], '영업사원이 등록되었습니다.');
  103. }
  104. /**
  105. * Update sales staff
  106. */
  107. public function update($id = null)
  108. {
  109. $auth = $this->requireAuth();
  110. if ($auth instanceof ResponseInterface) {
  111. return $auth;
  112. }
  113. $json = $this->request->getJSON();
  114. $data = [
  115. 'showroom' => $json->showroom ?? '',
  116. 'team' => $json->team ?? '',
  117. 'name' => $json->name ?? '',
  118. 'position' => (string)($json->position ?? ''),
  119. 'main_phone' => $json->main_phone ?? '',
  120. 'direct_phone' => $json->direct_phone ?? '',
  121. 'email' => $json->email ?? '',
  122. 'photo_url' => $json->photo_url ?? '',
  123. 'is_sact' => $json->is_sact ?? 0,
  124. 'is_goldplt' => $json->is_goldplt ?? 0,
  125. 'is_top30' => $json->is_top30 ?? 0,
  126. 'is_best_advisor' => $json->is_best_advisor ?? 0,
  127. 'is_best_advisor_year' => $json->is_best_advisor_year ?? 0,
  128. 'display_order' => $json->display_order ?? 0,
  129. 'updated_at' => date('Y-m-d H:i:s')
  130. ];
  131. $builder = $this->getDB()->table('sales_staff');
  132. $builder->where('id', $id)->update($data);
  133. return $this->respondSuccess(null, '영업사원이 수정되었습니다.');
  134. }
  135. /**
  136. * Delete sales staff
  137. */
  138. public function delete($id = null)
  139. {
  140. $auth = $this->requireAuth();
  141. if ($auth instanceof ResponseInterface) {
  142. return $auth;
  143. }
  144. $builder = $this->getDB()->table('sales_staff');
  145. $builder->where('id', $id)->delete();
  146. return $this->respondSuccess(null, '영업사원이 삭제되었습니다.');
  147. }
  148. /**
  149. * Print single
  150. */
  151. public function printSingle($id)
  152. {
  153. $auth = $this->requireAuth();
  154. if ($auth instanceof ResponseInterface) {
  155. return $auth;
  156. }
  157. // TODO: Implement print logic
  158. return $this->respondSuccess(null, 'Print endpoint');
  159. }
  160. /**
  161. * Toggle is_act status (노출/비노출 토글)
  162. */
  163. public function toggleActive($id = null)
  164. {
  165. $auth = $this->requireAuth();
  166. if ($auth instanceof ResponseInterface) {
  167. return $auth;
  168. }
  169. $builder = $this->getDB()->table('sales_staff');
  170. $staff = $builder->where('id', $id)->get()->getRow();
  171. if (!$staff) {
  172. return $this->respondError('영업사원을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  173. }
  174. // Toggle is_act: 1 -> 0, 0 -> 1
  175. $newStatus = $staff->is_act == 1 ? 0 : 1;
  176. $builder->where('id', $id)->update([
  177. 'is_act' => $newStatus,
  178. 'updated_at' => date('Y-m-d H:i:s')
  179. ]);
  180. $statusText = $newStatus == 1 ? '노출' : '비노출';
  181. return $this->respondSuccess(
  182. ['is_act' => $newStatus],
  183. "영업사원이 {$statusText} 상태로 변경되었습니다."
  184. );
  185. }
  186. /**
  187. * Toggle is_best_advisor status (Best Sales Advisor 토글)
  188. */
  189. public function toggleBestAdvisor($id = null)
  190. {
  191. $auth = $this->requireAuth();
  192. if ($auth instanceof ResponseInterface) {
  193. return $auth;
  194. }
  195. $builder = $this->getDB()->table('sales_staff');
  196. $staff = $builder->where('id', $id)->get()->getRow();
  197. if (!$staff) {
  198. return $this->respondError('영업사원을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  199. }
  200. // Toggle is_best_advisor: 1 -> 0, 0 -> 1
  201. $newStatus = $staff->is_best_advisor == 1 ? 0 : 1;
  202. $builder->where('id', $id)->update([
  203. 'is_best_advisor' => $newStatus,
  204. 'updated_at' => date('Y-m-d H:i:s')
  205. ]);
  206. $statusText = $newStatus == 1 ? 'Best Sales Advisor로 지정' : 'Best Sales Advisor 해제';
  207. return $this->respondSuccess(
  208. ['is_best_advisor' => $newStatus],
  209. "{$statusText}되었습니다."
  210. );
  211. }
  212. /**
  213. * Toggle is_best_advisor_year status (Best Sales Advisor Year 토글)
  214. */
  215. public function toggleBestAdvisorYear($id = null)
  216. {
  217. $auth = $this->requireAuth();
  218. if ($auth instanceof ResponseInterface) {
  219. return $auth;
  220. }
  221. $builder = $this->getDB()->table('sales_staff');
  222. $staff = $builder->where('id', $id)->get()->getRow();
  223. if (!$staff) {
  224. return $this->respondError('영업사원을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
  225. }
  226. // Toggle is_best_advisor_year: 1 -> 0, 0 -> 1
  227. $newStatus = $staff->is_best_advisor_year == 1 ? 0 : 1;
  228. $builder->where('id', $id)->update([
  229. 'is_best_advisor_year' => $newStatus,
  230. 'updated_at' => date('Y-m-d H:i:s')
  231. ]);
  232. $statusText = $newStatus == 1 ? 'Best Sales Advisor (Year)로 지정' : 'Best Sales Advisor (Year) 해제';
  233. return $this->respondSuccess(
  234. ['is_best_advisor_year' => $newStatus],
  235. "{$statusText}되었습니다."
  236. );
  237. }
  238. /**
  239. * Get public sales staff list (No authentication required)
  240. * 공개 API - 인증 없이 영업사원 목록 조회
  241. */
  242. public function publicList()
  243. {
  244. try {
  245. $builder = $this->getDB()->table('sales_staff');
  246. // Filter by showroom
  247. $showroom = $this->request->getGet('showroom');
  248. if ($showroom) {
  249. $builder->where('showroom', $showroom);
  250. }
  251. // Only show active sales staff (is_act = 1)
  252. $builder->where('is_act', 1);
  253. $builder->orderBy('id', 'DESC');
  254. $salesStaff = $builder->get()->getResult();
  255. return $this->respondSuccess($salesStaff);
  256. } catch (\Exception $e) {
  257. log_message('error', 'Sales staff public list error: ' . $e->getMessage());
  258. log_message('error', 'Stack trace: ' . $e->getTraceAsString());
  259. return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  260. }
  261. }
  262. /**
  263. * Get best advisors list (No authentication required)
  264. * 공개 API - Best Sales Advisor 목록 조회
  265. */
  266. public function bestAdvisors()
  267. {
  268. try {
  269. $builder = $this->getDB()->table('sales_staff');
  270. // Filter by showroom (optional)
  271. $showroom = $this->request->getGet('showroom');
  272. if ($showroom) {
  273. $builder->where('showroom', $showroom);
  274. }
  275. // Only show active and best advisors
  276. $builder->where('is_act', 1);
  277. $builder->where('is_best_advisor', 1);
  278. // Order by display_order, then by id DESC
  279. $builder->orderBy('display_order', 'ASC');
  280. $builder->orderBy('id', 'DESC');
  281. $bestAdvisors = $builder->get()->getResult();
  282. return $this->respondSuccess($bestAdvisors);
  283. } catch (\Exception $e) {
  284. log_message('error', 'Best advisors list error: ' . $e->getMessage());
  285. log_message('error', 'Stack trace: ' . $e->getTraceAsString());
  286. return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  287. }
  288. }
  289. /**
  290. * Get best advisors year list (No authentication required)
  291. * 공개 API - Best Sales Advisor (Year) 목록 조회
  292. */
  293. public function bestAdvisorsYear()
  294. {
  295. try {
  296. $builder = $this->getDB()->table('sales_staff');
  297. // Filter by showroom (optional)
  298. $showroom = $this->request->getGet('showroom');
  299. if ($showroom) {
  300. $builder->where('showroom', $showroom);
  301. }
  302. // Only show active and best advisors (year)
  303. $builder->where('is_act', 1);
  304. $builder->where('is_best_advisor_year', 1);
  305. // Order by display_order, then by id DESC
  306. $builder->orderBy('display_order', 'ASC');
  307. $builder->orderBy('id', 'DESC');
  308. $bestAdvisorsYear = $builder->get()->getResult();
  309. return $this->respondSuccess($bestAdvisorsYear);
  310. } catch (\Exception $e) {
  311. log_message('error', 'Best advisors year list error: ' . $e->getMessage());
  312. log_message('error', 'Stack trace: ' . $e->getTraceAsString());
  313. return $this->respondError('서버 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
  314. }
  315. }
  316. }