| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace App\Controllers;
- use CodeIgniter\RESTful\ResourceController;
- class Order extends ResourceController
- {
- public function orderList($itemSeq = null)
- {
- $db = \Config\Database::connect();
- // ORDER_HEADER_LIST에서 전체 데이터 가져오기
- $headerBuilder = $db->table('ORDER_HEADER_LIST');
- $headerData = $headerBuilder->get()->getResultArray();
-
- // 헤더 데이터에 업로드 일자, 고유번호 필수 추가 (HEADER_21, 22 위치에)
- foreach ($headerData as &$header) {
- // 항상 HEADER_21, 22에 고정값 추가
- $header['HEADER_21'] = '업로드 일자';
- $header['HEADER_22'] = '고유번호';
- }
- // ORDER_LIST에서 전체 데이터 가져오기
- $orderBuilder = $db->table('ORDER_LIST')
- ->orderBy('CONTENT_REGDATE', 'DESC');
- $orderData = $orderBuilder->get()->getResultArray();
-
- // 주문 데이터는 그대로 반환 (CONTENT_REGDATE, COLUMN_CODE 포함)
- // 응답 데이터 구성
- $response = [
- 'headerList' => $headerData, // ORDER_HEADER_LIST 전체 데이터
- 'orderList' => $orderData // ORDER_LIST 전체 데이터
- ];
- return $this->respond($response, 200);
- }
- public function orderRegister()
- {
- $db = \Config\Database::connect();
- $request = $this->request->getJSON(true);
- $itemSeq = $request['itemSeq'] ?? null;
- $headers = $request['headers'] ?? [];
- $orderData = $request['orderData'] ?? [];
- // 디버깅 로그
- log_message('info', 'orderRegister 호출됨');
- log_message('info', 'itemSeq: ' . $itemSeq);
- log_message('info', 'headers: ' . json_encode($headers));
- log_message('info', 'orderData 개수: ' . count($orderData));
- if (!$itemSeq) {
- return $this->respond(['error' => 'ITEM_SEQ가 필요합니다.'], 400);
- }
- $db->transBegin();
- try {
- // 헤더와 주문 데이터가 모두 비어있으면 전체 삭제
- if (empty($headers) && empty($orderData)) {
- log_message('info', '전체 데이터 삭제 요청');
-
- // ORDER_HEADER_LIST에서 해당 ITEM_SEQ 데이터 삭제
- $db->table('ORDER_HEADER_LIST')->where('ITEM_SEQ', $itemSeq)->delete();
-
- // ORDER_LIST에서 해당 ITEM_SEQ 데이터 삭제
- $db->table('ORDER_LIST')->where('ITEM_SEQ', $itemSeq)->delete();
-
- $db->transCommit();
-
- return $this->respond([
- 'success' => true,
- 'message' => '모든 주문 데이터가 삭제되었습니다.',
- 'headerCount' => 0,
- 'orderCount' => 0
- ], 200);
- }
-
- // 1. ORDER_HEADER_LIST에 헤더 정보 저장/업데이트
- if (!empty($headers)) {
- log_message('info', '헤더 데이터 저장 시작');
- // 기존 헤더 데이터 삭제
- $deleteCount = $db->table('ORDER_HEADER_LIST')->where('ITEM_SEQ', $itemSeq)->countAllResults();
- log_message('info', '삭제할 기존 헤더 개수: ' . $deleteCount);
- $db->table('ORDER_HEADER_LIST')->where('ITEM_SEQ', $itemSeq)->delete();
- // headers 배열에는 주문일, 고유번호 추가하지 않음 (프론트엔드에서 처리)
- // 새로운 헤더 데이터 삽입
- $headerInsertData = ['ITEM_SEQ' => $itemSeq];
- // 순수 엑셀 헤더만 저장 (업로드 일자, 고유번호 제외)
- $pureHeaders = [];
- foreach ($headers as $header) {
- if ($header !== '업로드 일자' && $header !== '고유번호') {
- $pureHeaders[] = $header;
- }
- }
-
- foreach ($pureHeaders as $index => $header) {
- if ($index < 20 && !empty($header)) {
- $headerKey = 'HEADER_' . ($index + 1);
- $headerInsertData[$headerKey] = $header;
- log_message('info', "헤더 매핑: {$headerKey} = {$header}");
- }
- }
-
- // HEADER_21과 HEADER_22에 고정값 설정
- $headerInsertData['HEADER_21'] = '업로드 일자';
- $headerInsertData['HEADER_22'] = '고유번호';
- log_message('info', '헤더 삽입 데이터: ' . json_encode($headerInsertData));
- $result = $db->table('ORDER_HEADER_LIST')->insert($headerInsertData);
- log_message('info', '헤더 삽입 결과: ' . ($result ? 'success' : 'failed'));
- } else {
- log_message('info', '헤더 데이터가 비어있음');
- }
- // 2. ORDER_LIST에 주문 데이터 저장 (기존 데이터 모두 삭제 후 새로 저장)
- if (!empty($orderData)) {
- log_message('info', '주문 데이터 저장 시작');
-
- // 기존 주문 데이터 모두 삭제
- $deleteCount = $db->table('ORDER_LIST')->where('ITEM_SEQ', $itemSeq)->countAllResults();
- log_message('info', '삭제할 기존 주문 개수: ' . $deleteCount);
-
- $db->table('ORDER_LIST')->where('ITEM_SEQ', $itemSeq)->delete();
- // 새로운 주문 데이터 모두 삽입
- foreach ($orderData as $row) {
- $orderInsertData = ['ITEM_SEQ' => $itemSeq];
- // CONTENT_1 ~ CONTENT_20만 매핑 (21, 22는 제외)
- for ($i = 1; $i <= 20; $i++) {
- $contentKey = 'CONTENT_' . $i;
- if (isset($row[$contentKey])) {
- $orderInsertData[$contentKey] = $row[$contentKey];
- }
- }
- // CONTENT_REGDATE와 COLUMN_CODE 설정 (프론트엔드에서 전달받은 값 사용)
- if (isset($row['CONTENT_REGDATE']) && !empty($row['CONTENT_REGDATE'])) {
- $orderInsertData['CONTENT_REGDATE'] = $row['CONTENT_REGDATE']; // 업로드 일자
- }
-
- if (isset($row['COLUMN_CODE']) && !empty($row['COLUMN_CODE'])) {
- $orderInsertData['COLUMN_CODE'] = $row['COLUMN_CODE']; // 고유번호
- } else {
- // 고유번호가 없으면 새로 생성
- $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- $hash = '';
- for ($j = 0; $j < 50; $j++) {
- $hash .= $chars[rand(0, strlen($chars) - 1)];
- }
- $orderInsertData['COLUMN_CODE'] = $hash;
- log_message('info', '고유번호 자동생성: ' . $hash);
- }
-
- $db->table('ORDER_LIST')->insert($orderInsertData);
- log_message('info', '새 데이터 인서트: ' . $orderInsertData['COLUMN_CODE']);
- }
- }
- $db->transCommit();
- $response = [
- 'success' => true,
- 'message' => '주문 데이터가 성공적으로 저장되었습니다.',
- 'headerCount' => count($headers),
- 'orderCount' => count($orderData)
- ];
- return $this->respond($response, 200);
- } catch (Exception $e) {
- $db->transRollback();
- return $this->respond([
- 'error' => '데이터 저장 중 오류가 발생했습니다.',
- 'details' => $e->getMessage()
- ], 500);
- }
- }
- }
|