Order.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Controllers;
  3. use CodeIgniter\RESTful\ResourceController;
  4. class Order extends ResourceController
  5. {
  6. public function orderList($itemSeq = null)
  7. {
  8. $db = \Config\Database::connect();
  9. // ORDER_HEADER_LIST에서 전체 데이터 가져오기
  10. $headerBuilder = $db->table('ORDER_HEADER_LIST');
  11. $headerData = $headerBuilder->get()->getResultArray();
  12. // 헤더 데이터에 업로드 일자, 고유번호 필수 추가 (HEADER_21, 22 위치에)
  13. foreach ($headerData as &$header) {
  14. // 항상 HEADER_21, 22에 고정값 추가
  15. $header['HEADER_21'] = '업로드 일자';
  16. $header['HEADER_22'] = '고유번호';
  17. }
  18. // ORDER_LIST에서 전체 데이터 가져오기
  19. $orderBuilder = $db->table('ORDER_LIST')
  20. ->orderBy('CONTENT_REGDATE', 'DESC');
  21. $orderData = $orderBuilder->get()->getResultArray();
  22. // 주문 데이터는 그대로 반환 (CONTENT_REGDATE, COLUMN_CODE 포함)
  23. // 응답 데이터 구성
  24. $response = [
  25. 'headerList' => $headerData, // ORDER_HEADER_LIST 전체 데이터
  26. 'orderList' => $orderData // ORDER_LIST 전체 데이터
  27. ];
  28. return $this->respond($response, 200);
  29. }
  30. public function orderRegister()
  31. {
  32. $db = \Config\Database::connect();
  33. $request = $this->request->getJSON(true);
  34. $itemSeq = $request['itemSeq'] ?? null;
  35. $headers = $request['headers'] ?? [];
  36. $orderData = $request['orderData'] ?? [];
  37. // 디버깅 로그
  38. log_message('info', 'orderRegister 호출됨');
  39. log_message('info', 'itemSeq: ' . $itemSeq);
  40. log_message('info', 'headers: ' . json_encode($headers));
  41. log_message('info', 'orderData 개수: ' . count($orderData));
  42. if (!$itemSeq) {
  43. return $this->respond(['error' => 'ITEM_SEQ가 필요합니다.'], 400);
  44. }
  45. $db->transBegin();
  46. try {
  47. // 헤더와 주문 데이터가 모두 비어있으면 전체 삭제
  48. if (empty($headers) && empty($orderData)) {
  49. log_message('info', '전체 데이터 삭제 요청');
  50. // ORDER_HEADER_LIST에서 해당 ITEM_SEQ 데이터 삭제
  51. $db->table('ORDER_HEADER_LIST')->where('ITEM_SEQ', $itemSeq)->delete();
  52. // ORDER_LIST에서 해당 ITEM_SEQ 데이터 삭제
  53. $db->table('ORDER_LIST')->where('ITEM_SEQ', $itemSeq)->delete();
  54. $db->transCommit();
  55. return $this->respond([
  56. 'success' => true,
  57. 'message' => '모든 주문 데이터가 삭제되었습니다.',
  58. 'headerCount' => 0,
  59. 'orderCount' => 0
  60. ], 200);
  61. }
  62. // 1. ORDER_HEADER_LIST에 헤더 정보 저장/업데이트
  63. if (!empty($headers)) {
  64. log_message('info', '헤더 데이터 저장 시작');
  65. // 기존 헤더 데이터 삭제
  66. $deleteCount = $db->table('ORDER_HEADER_LIST')->where('ITEM_SEQ', $itemSeq)->countAllResults();
  67. log_message('info', '삭제할 기존 헤더 개수: ' . $deleteCount);
  68. $db->table('ORDER_HEADER_LIST')->where('ITEM_SEQ', $itemSeq)->delete();
  69. // headers 배열에는 주문일, 고유번호 추가하지 않음 (프론트엔드에서 처리)
  70. // 새로운 헤더 데이터 삽입
  71. $headerInsertData = ['ITEM_SEQ' => $itemSeq];
  72. // 순수 엑셀 헤더만 저장 (업로드 일자, 고유번호 제외)
  73. $pureHeaders = [];
  74. foreach ($headers as $header) {
  75. if ($header !== '업로드 일자' && $header !== '고유번호') {
  76. $pureHeaders[] = $header;
  77. }
  78. }
  79. foreach ($pureHeaders as $index => $header) {
  80. if ($index < 20 && !empty($header)) {
  81. $headerKey = 'HEADER_' . ($index + 1);
  82. $headerInsertData[$headerKey] = $header;
  83. log_message('info', "헤더 매핑: {$headerKey} = {$header}");
  84. }
  85. }
  86. // HEADER_21과 HEADER_22에 고정값 설정
  87. $headerInsertData['HEADER_21'] = '업로드 일자';
  88. $headerInsertData['HEADER_22'] = '고유번호';
  89. log_message('info', '헤더 삽입 데이터: ' . json_encode($headerInsertData));
  90. $result = $db->table('ORDER_HEADER_LIST')->insert($headerInsertData);
  91. log_message('info', '헤더 삽입 결과: ' . ($result ? 'success' : 'failed'));
  92. } else {
  93. log_message('info', '헤더 데이터가 비어있음');
  94. }
  95. // 2. ORDER_LIST에 주문 데이터 저장 (기존 데이터 모두 삭제 후 새로 저장)
  96. if (!empty($orderData)) {
  97. log_message('info', '주문 데이터 저장 시작');
  98. // 기존 주문 데이터 모두 삭제
  99. $deleteCount = $db->table('ORDER_LIST')->where('ITEM_SEQ', $itemSeq)->countAllResults();
  100. log_message('info', '삭제할 기존 주문 개수: ' . $deleteCount);
  101. $db->table('ORDER_LIST')->where('ITEM_SEQ', $itemSeq)->delete();
  102. // 새로운 주문 데이터 모두 삽입
  103. foreach ($orderData as $row) {
  104. $orderInsertData = ['ITEM_SEQ' => $itemSeq];
  105. // CONTENT_1 ~ CONTENT_20만 매핑 (21, 22는 제외)
  106. for ($i = 1; $i <= 20; $i++) {
  107. $contentKey = 'CONTENT_' . $i;
  108. if (isset($row[$contentKey])) {
  109. $orderInsertData[$contentKey] = $row[$contentKey];
  110. }
  111. }
  112. // CONTENT_REGDATE와 COLUMN_CODE 설정 (프론트엔드에서 전달받은 값 사용)
  113. if (isset($row['CONTENT_REGDATE']) && !empty($row['CONTENT_REGDATE'])) {
  114. $orderInsertData['CONTENT_REGDATE'] = $row['CONTENT_REGDATE']; // 업로드 일자
  115. }
  116. if (isset($row['COLUMN_CODE']) && !empty($row['COLUMN_CODE'])) {
  117. $orderInsertData['COLUMN_CODE'] = $row['COLUMN_CODE']; // 고유번호
  118. } else {
  119. // 고유번호가 없으면 새로 생성
  120. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  121. $hash = '';
  122. for ($j = 0; $j < 50; $j++) {
  123. $hash .= $chars[rand(0, strlen($chars) - 1)];
  124. }
  125. $orderInsertData['COLUMN_CODE'] = $hash;
  126. log_message('info', '고유번호 자동생성: ' . $hash);
  127. }
  128. $db->table('ORDER_LIST')->insert($orderInsertData);
  129. log_message('info', '새 데이터 인서트: ' . $orderInsertData['COLUMN_CODE']);
  130. }
  131. }
  132. $db->transCommit();
  133. $response = [
  134. 'success' => true,
  135. 'message' => '주문 데이터가 성공적으로 저장되었습니다.',
  136. 'headerCount' => count($headers),
  137. 'orderCount' => count($orderData)
  138. ];
  139. return $this->respond($response, 200);
  140. } catch (Exception $e) {
  141. $db->transRollback();
  142. return $this->respond([
  143. 'error' => '데이터 저장 중 오류가 발생했습니다.',
  144. 'details' => $e->getMessage()
  145. ], 500);
  146. }
  147. }
  148. }