detail.vue 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. <template>
  2. <div>
  3. <div class="inner--headers">
  4. <h2>{{ pageId }}</h2>
  5. <div class="bread--crumbs--wrap">
  6. <span>홈</span>
  7. <span>{{ pageId }}</span>
  8. </div>
  9. </div>
  10. <div class="data--list--wrap">
  11. <div class="btn--actions--wrap">
  12. <div class="left--sections">
  13. <v-btn class="custom-btn btn-pink bdrs--10"><i class="ico"></i>개별 배송</v-btn>
  14. <v-btn class="custom-btn bdrs--10 btn-white" @click="deliLocated()"
  15. ><i class="ico"></i>공동구매 배송</v-btn
  16. >
  17. </div>
  18. <div class="right--sections"></div>
  19. </div>
  20. <div class="item--section">
  21. <div v-if="imgTemp" class="item--thumb">
  22. <img :src="imgTemp" alt="" />
  23. </div>
  24. <div v-else class="item--thumb min--240">NO IMAGE</div>
  25. <div class="item--info">
  26. <h2>{{ form.formValue1 }}</h2>
  27. <p>공급가: {{ Number(form.formValue2).toLocaleString() }}원</p>
  28. <p>판매가: {{ Number(form.formValue3).toLocaleString() }}원</p>
  29. </div>
  30. </div>
  31. <div class="btn--actions--wrap">
  32. <div class="left--sections"></div>
  33. <div class="right--sections">
  34. <div class="caption--wrap">
  35. <i class="ico">!</i>
  36. <div class="caption--box">
  37. - 주문일은 YYYY.MM.DD 혹은 YYYY-MM-DD 형태로 입력해 주세요.<br />
  38. - 구매자 정보 입력 후 저장 버튼을 꼭 클릭해 주세요.<br />
  39. - 엑셀 파일은 최대 10MB까지 업로드 가능합니다.<br />
  40. - 엑셀 파일의 헤더는 다음과 같아야 합니다: 구매자명, 주소, 연락처, 이메일,
  41. 구매수량, 총구매금액, 배송업체, 송장번호, 주문일
  42. </div>
  43. </div>
  44. <v-btn class="custom-btn btn-white mini" @click="addEmptyRow"
  45. ><i class="ico"></i>항목 추가</v-btn
  46. >
  47. <v-btn class="custom-btn btn-white mini" @click="deleteSelectedRows"
  48. ><i class="ico"></i>항목 삭제</v-btn
  49. >
  50. <input
  51. ref="excelFileInput"
  52. type="file"
  53. accept=".xlsx,.xls"
  54. @change="handleExcelUpload"
  55. style="display: none"
  56. />
  57. <v-btn class="custom-btn btn-excel" @click="$refs.excelFileInput.click()"
  58. ><i class="ico"></i>엑셀 업로드</v-btn
  59. >
  60. <v-btn class="custom-btn btn-excel" @click="downloadExcel"
  61. ><i class="ico"></i>엑셀 다운로드</v-btn
  62. >
  63. </div>
  64. </div>
  65. <div class="tbl-wrapper">
  66. <div class="tbl-wrap">
  67. <!-- ag grid -->
  68. <ag-grid-vue
  69. style="width: 100%; height: calc(10 * 2.94rem)"
  70. class="ag-theme-quartz"
  71. :gridOptions="gridOptions"
  72. rowSelection="multiple"
  73. :rowData="tblItems"
  74. :paginationPageSize="pageObj.pageSize"
  75. :suppressPaginationPanel="true"
  76. @grid-ready="onGridReady"
  77. >
  78. </ag-grid-vue>
  79. <!-- 페이징 -->
  80. <div class="ag-grid-custom-pagenations">
  81. <pagination @chg_page="chgPage" :pageObj="pageObj"></pagination>
  82. </div>
  83. </div>
  84. </div>
  85. <div class="view-btm-btn">
  86. <div class="btn-l">
  87. <v-btn class="custom-btn btn-list" @click="listLocated"
  88. ><i class="ico"></i>목록</v-btn
  89. >
  90. </div>
  91. <div class="btn-r">
  92. <v-btn class="custom-btn btn-blue2" @click="fnRegEvt"
  93. ><i class="ico"></i>저장</v-btn
  94. >
  95. </div>
  96. </div>
  97. </div>
  98. <!-- 로딩 오버레이 -->
  99. <LoadingOverlay :is-loading="isLoading" :loading-message="loadingMessage" />
  100. </div>
  101. </template>
  102. <script setup>
  103. import "@vuepic/vue-datepicker/dist/main.css";
  104. import { AgGridVue } from "ag-grid-vue3";
  105. import * as XLSX from "xlsx";
  106. import pagination from "../components/common/pagination.vue";
  107. /************************************************************************
  108. | 레이아웃
  109. ************************************************************************/
  110. definePageMeta({
  111. layout: "default",
  112. });
  113. /************************************************************************
  114. | PROPS
  115. ************************************************************************/
  116. const props = defineProps({
  117. propsData: {
  118. type: Object,
  119. default: () => {},
  120. },
  121. });
  122. /************************************************************************
  123. | 스토어
  124. ************************************************************************/
  125. const useDtStore = useDetailStore();
  126. const useAtStore = useAuthStore();
  127. /************************************************************************
  128. | 전역
  129. ************************************************************************/
  130. const memberType = useAtStore.auth.memberType;
  131. const memberSeq = useAtStore.auth.seq;
  132. const { $toast, $log, $dayjs, $eventBus } = useNuxtApp();
  133. const router = useRouter();
  134. const pageId = ref("배송 관리");
  135. const { isLoading, loadingMessage, withLoading } = useLoading();
  136. let pageObj = ref({
  137. page: 1, // 현재 페이지
  138. pageMaxNumSize: 10, // 페이지 숫자 최대 표현 개수
  139. pageSize: 10, // 테이블 조회 데이터 개수
  140. totalCnt: 0, // 전체 페이지
  141. });
  142. const imgTemp = ref("");
  143. const tblItems = ref([]); // stat 데이터
  144. const form = ref({
  145. formValue1: "",
  146. formValue2: "",
  147. formValue3: "",
  148. formValue4: "",
  149. formValue5: null,
  150. formValue6: "",
  151. formValue7: "",
  152. formValue8: "0",
  153. formValue8Arr: [
  154. { title: "판매중", value: "0" },
  155. { title: "품절", value: "1" },
  156. ],
  157. formValue9: "Y",
  158. formValue9Arr: [
  159. { title: "노출", value: "Y" },
  160. { title: "비노출", value: "N" },
  161. ],
  162. formValue10: "",
  163. });
  164. /* eslint-disable */
  165. /* prettier-ignore */
  166. pageObj.value.totalCnt = tblItems.value.length;
  167. const remToPx = () => parseFloat(getComputedStyle(document.documentElement).fontSize);
  168. const rowHeightRem = 2.65; // 원하는 rem 값
  169. const rowHeightPx = rowHeightRem * remToPx();
  170. const gridApi = shallowRef();
  171. // gridOption
  172. const gridOptions = {
  173. columnDefs: [
  174. { checkboxSelection: true, headerCheckboxSelection: true, width: 50 },
  175. {
  176. headerName: "No",
  177. valueGetter: (params) => params.api.getDisplayedRowCount() - params.node.rowIndex,
  178. sortable: false,
  179. width: 70,
  180. },
  181. {
  182. headerName: "인플루언서",
  183. field: "NICK_NAME",
  184. width: 150,
  185. hide: memberType == "INFLUENCER",
  186. },
  187. {
  188. headerName: "구매자명",
  189. field: "BUYER_NAME",
  190. width: 120,
  191. editable: true,
  192. },
  193. {
  194. headerName: "주소",
  195. field: "ADDRESS",
  196. editable: true,
  197. },
  198. {
  199. headerName: "연락처",
  200. field: "PHONE",
  201. width: 140,
  202. editable: true,
  203. },
  204. {
  205. headerName: "이메일",
  206. field: "EMAIL",
  207. editable: true,
  208. },
  209. {
  210. headerName: "구매수량",
  211. field: "QTY",
  212. width: 120,
  213. editable: true,
  214. cellRenderer: (params) => {
  215. return Number(params.value).toLocaleString();
  216. },
  217. },
  218. {
  219. headerName: "총구매금액",
  220. field: "TOTAL",
  221. editable: true,
  222. width: 120,
  223. cellRenderer: (params) => {
  224. return Number(params.value).toLocaleString();
  225. },
  226. },
  227. {
  228. headerName: "배송업체",
  229. field: "DELI_COMP",
  230. width: 100,
  231. editable: true,
  232. },
  233. {
  234. headerName: "송장번호",
  235. field: "DELI_NUMB",
  236. editable: true,
  237. },
  238. {
  239. headerName: "주문일",
  240. field: "ORDER_DATE",
  241. editable: true,
  242. },
  243. ],
  244. rowData: tblItems.value, // 테이블 데이터
  245. autoSizeStrategy: {
  246. type: "fitGridWidth", // width맞춤
  247. },
  248. suppressMovableColumns: true,
  249. headerHeight: rowHeightPx,
  250. rowHeight: rowHeightPx,
  251. pagination: true,
  252. suppressPaginationPanel: true, // 하단 default 페이징 컨트롤 숨김
  253. rowMultiSelectWithClick: true,
  254. rowSelection: {
  255. checkboxes: true,
  256. headerCheckbox: true,
  257. enableClickSelection: false,
  258. mode: "multiRow",
  259. },
  260. };
  261. /************************************************************************
  262. | 함수(METHODS)
  263. ************************************************************************/
  264. const listLocated = () => {
  265. router.push({
  266. path: "/view/common/deli/",
  267. });
  268. };
  269. const onGridReady = (__PARAMS) => {
  270. gridApi.value = __PARAMS.api;
  271. };
  272. const chgPage = (__PAGE) => {
  273. pageObj.value.page = __PAGE;
  274. gridApi.value.paginationGoToPage(__PAGE - 1);
  275. };
  276. const fnRegEvt = () => {
  277. let param = {
  278. id: pageId,
  279. title: pageId,
  280. content: "등록하시겠습니까?",
  281. yes: {
  282. text: "등록",
  283. isProc: true,
  284. event: "FN_INSERT",
  285. param: "",
  286. },
  287. no: {
  288. text: "취소",
  289. isProc: false,
  290. },
  291. };
  292. $eventBus.emit("OPEN_CONFIRM_POP_UP", param);
  293. };
  294. // 엑셀 컬럼명 매핑 테이블
  295. const excelColumnMapping = {
  296. 구매자명: "BUYER_NAME",
  297. 주소: "ADDRESS",
  298. 연락처: "PHONE",
  299. 이메일: "EMAIL",
  300. 구매수량: "QTY",
  301. 총구매금액: "TOTAL",
  302. 배송업체: "DELI_COMP",
  303. 송장번호: "DELI_NUMB",
  304. 주문일: "ORDER_DATE",
  305. };
  306. const addEmptyRow = () => {
  307. const newRow = {
  308. BUYER_NAME: "",
  309. ADDRESS: "",
  310. PHONE: "",
  311. EMAIL: "",
  312. QTY: "",
  313. TOTAL: "",
  314. DELI_COMP: "",
  315. DELI_NUMB: "",
  316. ORDER_DATE: "",
  317. };
  318. // 맨 앞에 추가 (unshift 사용)
  319. tblItems.value.unshift(newRow);
  320. pageObj.value.totalCnt = tblItems.value.length;
  321. // ag-grid 데이터 갱신
  322. if (gridApi.value) {
  323. gridApi.value.setGridOption("rowData", tblItems.value);
  324. }
  325. $toast.success("새 항목이 추가되었습니다.");
  326. };
  327. const deleteSelectedRows = () => {
  328. if (!gridApi.value) return;
  329. const selectedRows = gridApi.value.getSelectedRows();
  330. if (selectedRows.length === 0) {
  331. $toast.warning("삭제할 항목을 선택해주세요.");
  332. return;
  333. }
  334. let param = {
  335. id: pageId,
  336. title: pageId,
  337. content: `선택된 ${selectedRows.length}개 항목을 삭제하시겠습니까?`,
  338. yes: {
  339. text: "삭제",
  340. isProc: true,
  341. event: "FN_DELETE_SELECTED",
  342. param: selectedRows,
  343. },
  344. no: {
  345. text: "취소",
  346. isProc: false,
  347. },
  348. };
  349. $eventBus.emit("OPEN_CONFIRM_POP_UP", param);
  350. };
  351. const fnDeleteSelected = (selectedRows) => {
  352. // 선택된 행들을 tblItems에서 제거
  353. selectedRows.forEach((selectedRow) => {
  354. const index = tblItems.value.findIndex(
  355. (item) =>
  356. item.BUYER_NAME === selectedRow.BUYER_NAME &&
  357. item.ADDRESS === selectedRow.ADDRESS &&
  358. item.PHONE === selectedRow.PHONE &&
  359. item.EMAIL === selectedRow.EMAIL
  360. );
  361. if (index > -1) {
  362. tblItems.value.splice(index, 1);
  363. }
  364. });
  365. pageObj.value.totalCnt = tblItems.value.length;
  366. // ag-grid 데이터 갱신
  367. if (gridApi.value) {
  368. gridApi.value.setGridOption("rowData", tblItems.value);
  369. }
  370. $toast.success(`${selectedRows.length}개 항목이 삭제되었습니다.`);
  371. };
  372. const handleExcelUpload = async (event) => {
  373. const file = event.target.files[0];
  374. if (!file) return;
  375. // 사용자 타입에 따라 다른 업로드 로직 호출
  376. if (memberType === 'VENDOR') {
  377. await handleVendorExcelUpload(file);
  378. } else {
  379. await handleInfluencerExcelUpload(file);
  380. }
  381. // 파일 입력 초기화
  382. event.target.value = "";
  383. };
  384. const handleInfluencerExcelUpload = async (file) => {
  385. const errorHandler = useErrorHandler();
  386. // 파일 크기 검증 (10MB)
  387. const maxSize = 10 * 1024 * 1024;
  388. if (file.size > maxSize) {
  389. const sizeError = new Error("파일 크기 초과");
  390. sizeError.name = "FileSizeError";
  391. errorHandler.handleFileError(sizeError, file.name);
  392. event.target.value = "";
  393. return;
  394. }
  395. // 파일 형식 검증
  396. const allowedTypes = [
  397. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  398. "application/vnd.ms-excel",
  399. ];
  400. if (!allowedTypes.includes(file.type)) {
  401. const typeError = new Error("지원하지 않는 파일 형식");
  402. typeError.name = "FileTypeError";
  403. errorHandler.handleFileError(typeError, file.name);
  404. event.target.value = "";
  405. return;
  406. }
  407. const reader = new FileReader();
  408. reader.onload = async (e) => {
  409. try {
  410. const data = new Uint8Array(e.target.result);
  411. const workbook = XLSX.read(data, { type: "array" });
  412. const sheetName = workbook.SheetNames[0];
  413. const worksheet = workbook.Sheets[sheetName];
  414. const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
  415. if (jsonData.length < 2) {
  416. $toast.error(
  417. "엑셀 파일에 데이터가 없습니다. 헤더와 최소 1개 이상의 데이터 행이 필요합니다."
  418. );
  419. return;
  420. }
  421. const headers = jsonData[0];
  422. const rows = jsonData.slice(1);
  423. // 필수 헤더 검증
  424. const requiredHeaders = ["구매자명", "주소", "연락처"];
  425. const missingHeaders = requiredHeaders.filter(
  426. (header) => !headers.includes(header)
  427. );
  428. if (missingHeaders.length > 0) {
  429. $toast.error(`필수 헤더가 누락되었습니다: ${missingHeaders.join(", ")}`);
  430. return;
  431. }
  432. // 컬럼명 매핑 및 데이터 변환
  433. const mappedData = rows
  434. .map((row, rowIndex) => {
  435. const mappedRow = {};
  436. let hasValidData = false;
  437. headers.forEach((header, index) => {
  438. const fieldName = excelColumnMapping[header];
  439. if (fieldName && row[index] !== undefined && row[index] !== "") {
  440. mappedRow[fieldName] = row[index];
  441. hasValidData = true;
  442. }
  443. });
  444. // 필수 필드 검증
  445. if (hasValidData) {
  446. const missingRequiredFields = [];
  447. if (!mappedRow.BUYER_NAME) missingRequiredFields.push("구매자명");
  448. if (!mappedRow.ADDRESS) missingRequiredFields.push("주소");
  449. if (!mappedRow.PHONE) missingRequiredFields.push("연락처");
  450. if (missingRequiredFields.length > 0) {
  451. console.warn(
  452. `${rowIndex + 2}행: 필수 필드 누락 - ${missingRequiredFields.join(
  453. ", "
  454. )}`
  455. );
  456. }
  457. }
  458. return hasValidData ? mappedRow : null;
  459. })
  460. .filter((row) => row !== null);
  461. if (mappedData.length === 0) {
  462. $toast.error(
  463. "매핑 가능한 데이터가 없습니다. 엑셀 헤더명과 데이터를 확인해주세요."
  464. );
  465. return;
  466. }
  467. // 기존 주문 데이터와 매칭 검증
  468. const matchResults = await validateAndMatchOrders(mappedData);
  469. const { matchedData, unmatchedData, errors } = matchResults;
  470. // 매칭된 데이터만 그리드에 추가
  471. tblItems.value = [...matchedData];
  472. pageObj.value.totalCnt = tblItems.value.length;
  473. // ag-grid 데이터 갱신
  474. if (gridApi.value) {
  475. gridApi.value.setGridOption("rowData", tblItems.value);
  476. }
  477. // 결과에 따른 사용자 피드백
  478. if (matchedData.length > 0 && unmatchedData.length === 0) {
  479. $toast.success(
  480. `${matchedData.length}건의 데이터가 성공적으로 업로드되었습니다.`
  481. );
  482. } else if (matchedData.length > 0 && unmatchedData.length > 0) {
  483. $toast.warning(
  484. `${matchedData.length}건 업로드 완료, ${unmatchedData.length}건 매칭 실패`
  485. );
  486. showUnmatchedDataModal(unmatchedData, errors);
  487. } else {
  488. $toast.error(
  489. "매칭된 주문 데이터가 없습니다. 구매자명과 연락처를 확인해주세요."
  490. );
  491. showUnmatchedDataModal(unmatchedData, errors);
  492. }
  493. } catch (error) {
  494. console.error("엑셀 파일 처리 중 오류:", error);
  495. $toast.error(
  496. "엑셀 파일을 읽는 중 오류가 발생했습니다. 파일 형식을 확인해주세요."
  497. );
  498. }
  499. };
  500. reader.onerror = () => {
  501. $toast.error("파일을 읽는 중 오류가 발생했습니다.");
  502. };
  503. reader.readAsArrayBuffer(file);
  504. };
  505. const handleVendorExcelUpload = async (file) => {
  506. const errorHandler = useErrorHandler();
  507. // 파일 크기 검증 (10MB)
  508. const maxSize = 10 * 1024 * 1024;
  509. if (file.size > maxSize) {
  510. const sizeError = new Error("파일 크기 초과");
  511. sizeError.name = "FileSizeError";
  512. errorHandler.handleFileError(sizeError, file.name);
  513. return;
  514. }
  515. // 파일 형식 검증
  516. const allowedTypes = [
  517. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  518. "application/vnd.ms-excel",
  519. ];
  520. if (!allowedTypes.includes(file.type)) {
  521. const typeError = new Error("지원하지 않는 파일 형식");
  522. typeError.name = "FileTypeError";
  523. errorHandler.handleFileError(typeError, file.name);
  524. return;
  525. }
  526. const reader = new FileReader();
  527. reader.onload = async (e) => {
  528. try {
  529. const data = new Uint8Array(e.target.result);
  530. const workbook = XLSX.read(data, { type: "array" });
  531. const sheetName = workbook.SheetNames[0];
  532. const worksheet = workbook.Sheets[sheetName];
  533. const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
  534. if (jsonData.length < 2) {
  535. $toast.error("엑셀 파일에 데이터가 없습니다. 헤더와 최소 1개 이상의 데이터 행이 필요합니다.");
  536. return;
  537. }
  538. const headers = jsonData[0];
  539. const rows = jsonData.slice(1);
  540. // 벤더사용 필수 헤더 검증
  541. const requiredHeaders = ["구매자명", "연락처", "배송업체", "송장번호"];
  542. const missingHeaders = requiredHeaders.filter(
  543. (header) => !headers.includes(header)
  544. );
  545. if (missingHeaders.length > 0) {
  546. $toast.error(`필수 헤더가 누락되었습니다: ${missingHeaders.join(", ")}`);
  547. return;
  548. }
  549. // 벤더사용 컬럼 매핑
  550. const vendorColumnMapping = {
  551. "구매자명": "BUYER_NAME",
  552. "연락처": "PHONE",
  553. "배송업체": "DELI_COMP",
  554. "송장번호": "DELI_NUMB"
  555. };
  556. // 업로드 데이터 변환
  557. const uploadData = rows
  558. .map((row, rowIndex) => {
  559. const mappedRow = {};
  560. let hasValidData = false;
  561. headers.forEach((header, index) => {
  562. const fieldName = vendorColumnMapping[header];
  563. if (fieldName && row[index] !== undefined && row[index] !== "") {
  564. mappedRow[fieldName] = String(row[index]).trim();
  565. hasValidData = true;
  566. }
  567. });
  568. // 필수 필드 검증
  569. if (hasValidData) {
  570. const missingRequiredFields = [];
  571. if (!mappedRow.BUYER_NAME) missingRequiredFields.push("구매자명");
  572. if (!mappedRow.PHONE) missingRequiredFields.push("연락처");
  573. if (!mappedRow.DELI_COMP) missingRequiredFields.push("배송업체");
  574. if (!mappedRow.DELI_NUMB) missingRequiredFields.push("송장번호");
  575. if (missingRequiredFields.length > 0) {
  576. console.warn(`${rowIndex + 2}행: 필수 필드 누락 - ${missingRequiredFields.join(", ")}`);
  577. return null;
  578. }
  579. }
  580. return hasValidData ? mappedRow : null;
  581. })
  582. .filter((row) => row !== null);
  583. if (uploadData.length === 0) {
  584. $toast.error("유효한 데이터가 없습니다. 엑셀 헤더명과 데이터를 확인해주세요.");
  585. return;
  586. }
  587. // 기존 데이터와 매칭하여 업데이트
  588. await updateDeliveryInfo(uploadData);
  589. } catch (error) {
  590. console.error("엑셀 파일 처리 중 오류:", error);
  591. errorHandler.handleApiError(error, "엑셀 파일 처리");
  592. }
  593. };
  594. reader.onerror = () => {
  595. $toast.error("파일을 읽는 중 오류가 발생했습니다.");
  596. };
  597. reader.readAsArrayBuffer(file);
  598. };
  599. const updateDeliveryInfo = async (uploadData) => {
  600. let matchedCount = 0;
  601. let unmatchedCount = 0;
  602. const unmatchedItems = [];
  603. // 기존 데이터와 매칭
  604. const updatedItems = tblItems.value.map(existingItem => {
  605. // 구매자명과 연락처로 매칭 (공백 제거 후 비교)
  606. const matchedUpload = uploadData.find(upload =>
  607. upload.BUYER_NAME.replace(/\s/g, '') === existingItem.BUYER_NAME.replace(/\s/g, '') &&
  608. upload.PHONE.replace(/\s/g, '').replace(/-/g, '') === existingItem.PHONE.replace(/\s/g, '').replace(/-/g, '')
  609. );
  610. if (matchedUpload) {
  611. matchedCount++;
  612. // 배송업체와 송장번호만 업데이트
  613. return {
  614. ...existingItem,
  615. DELI_COMP: matchedUpload.DELI_COMP,
  616. DELI_NUMB: matchedUpload.DELI_NUMB
  617. };
  618. }
  619. return existingItem;
  620. });
  621. // 매칭되지 않은 업로드 데이터 확인
  622. uploadData.forEach(upload => {
  623. const matched = tblItems.value.find(existing =>
  624. existing.BUYER_NAME.replace(/\s/g, '') === upload.BUYER_NAME.replace(/\s/g, '') &&
  625. existing.PHONE.replace(/\s/g, '').replace(/-/g, '') === upload.PHONE.replace(/\s/g, '').replace(/-/g, '')
  626. );
  627. if (!matched) {
  628. unmatchedCount++;
  629. unmatchedItems.push({
  630. buyerName: upload.BUYER_NAME,
  631. phone: upload.PHONE
  632. });
  633. }
  634. });
  635. // 업데이트된 데이터로 테이블 갱신
  636. tblItems.value = updatedItems;
  637. // ag-grid 데이터 갱신
  638. if (gridApi.value) {
  639. gridApi.value.setGridOption("rowData", tblItems.value);
  640. }
  641. // 결과 메시지 표시
  642. if (matchedCount > 0 && unmatchedCount === 0) {
  643. $toast.success(`${matchedCount}건의 배송정보가 성공적으로 업데이트되었습니다.`);
  644. } else if (matchedCount > 0 && unmatchedCount > 0) {
  645. $toast.warning(
  646. `${matchedCount}건 업데이트 완료, ${unmatchedCount}건 매칭 실패\n매칭 실패 항목: ${unmatchedItems.map(item => `${item.buyerName}(${item.phone})`).join(", ")}`
  647. );
  648. } else {
  649. $toast.error("매칭되는 주문 정보가 없습니다. 구매자명과 연락처를 확인해주세요.");
  650. }
  651. };
  652. const validateAndMatchOrders = async (uploadData) => {
  653. try {
  654. const response = await useAxios().post("/deli/validateOrders", {
  655. item_seq: useDtStore.boardInfo.seq,
  656. uploadData: uploadData,
  657. });
  658. return {
  659. matchedData: response.data.matched || [],
  660. unmatchedData: response.data.unmatched || [],
  661. errors: response.data.errors || [],
  662. };
  663. } catch (error) {
  664. console.error("주문 매칭 검증 중 오류:", error);
  665. // API 오류 시 클라이언트에서 기본 매칭 수행
  666. return performClientSideMatching(uploadData);
  667. }
  668. };
  669. const performClientSideMatching = (uploadData) => {
  670. // 클라이언트 사이드 매칭 로직 (백업용)
  671. const matchedData = [];
  672. const unmatchedData = [];
  673. const errors = [];
  674. uploadData.forEach((item, index) => {
  675. // 기본 검증: 구매자명과 연락처가 있는지 확인
  676. if (!item.BUYER_NAME || !item.PHONE) {
  677. unmatchedData.push(item);
  678. errors.push(`${index + 1}행: 구매자명 또는 연락처 누락`);
  679. } else {
  680. matchedData.push(item);
  681. }
  682. });
  683. return { matchedData, unmatchedData, errors };
  684. };
  685. const showUnmatchedDataModal = (unmatchedData, errors) => {
  686. const errorDetails =
  687. errors.length > 0 ? errors.join("\n") : "매칭 실패 데이터가 있습니다.";
  688. let param = {
  689. id: "unmatchedData",
  690. title: "업로드 실패 항목",
  691. content: `다음 ${unmatchedData.length}건의 데이터를 처리할 수 없습니다:\n\n${errorDetails}\n\n데이터를 수정 후 다시 업로드해주세요.`,
  692. yes: {
  693. text: "확인",
  694. isProc: false,
  695. },
  696. };
  697. $eventBus.emit("OPEN_CONFIRM_POP_UP", param);
  698. };
  699. const downloadExcel = () => {
  700. if (!tblItems.value || tblItems.value.length === 0) {
  701. $toast.warning("다운로드할 데이터가 없습니다.");
  702. return;
  703. }
  704. // 한글 헤더명 배열
  705. const headers = [
  706. "구매자명",
  707. "주소",
  708. "연락처",
  709. "이메일",
  710. "구매수량",
  711. "총구매금액",
  712. "배송업체",
  713. "송장번호",
  714. "주문일",
  715. ];
  716. // 데이터를 엑셀 형식으로 변환
  717. const excelData = tblItems.value.map((item) => [
  718. item.BUYER_NAME || "",
  719. item.ADDRESS || "",
  720. item.PHONE || "",
  721. item.EMAIL || "",
  722. item.QTY || "",
  723. item.TOTAL || "",
  724. item.DELI_COMP || "",
  725. item.DELI_NUMB || "",
  726. item.ORDER_DATE || "",
  727. ]);
  728. // 헤더를 첫 번째 행에 추가
  729. const worksheetData = [headers, ...excelData];
  730. // 워크시트 생성
  731. const worksheet = XLSX.utils.aoa_to_sheet(worksheetData);
  732. // 워크북 생성
  733. const workbook = XLSX.utils.book_new();
  734. XLSX.utils.book_append_sheet(workbook, worksheet, "배송관리");
  735. // 파일명 생성 (현재 날짜 포함)
  736. const today = new Date();
  737. const dateString =
  738. today.getFullYear() +
  739. String(today.getMonth() + 1).padStart(2, "0") +
  740. String(today.getDate()).padStart(2, "0");
  741. const fileName = `배송관리_${dateString}.xlsx`;
  742. // 엑셀 파일 다운로드
  743. XLSX.writeFile(workbook, fileName);
  744. $toast.success("엑셀 파일이 다운로드되었습니다.");
  745. };
  746. const fnDetail = () => {
  747. let req = {
  748. seq: useDtStore.boardInfo.seq,
  749. };
  750. let req2 = {
  751. item_seq: useDtStore.boardInfo.seq,
  752. };
  753. // 인플루언서일 경우 본인의 inf_seq값 추가
  754. if (memberType === 'INFLUENCER') {
  755. req2.inf_seq = memberSeq;
  756. }
  757. useAxios()
  758. .get(`/item/detail/${req.seq}`)
  759. .then((res) => {
  760. form.value.formValue1 = res.data.NAME;
  761. form.value.formValue2 = res.data.PRICE1;
  762. form.value.formValue3 = res.data.PRICE2;
  763. form.value.formValue4 = res.data.DELI_FEE;
  764. form.value.formValue5 = res.data.THUMB_FILE;
  765. form.value.formValue6 = res.data.SUB_TITLE;
  766. form.value.formValue8 = res.data.STATUS;
  767. form.value.formValue9 = res.data.SHOW_YN;
  768. form.value.formValue10 = res.data.ADD_INFO;
  769. //썸네일 파일이 있으면 넣어줌
  770. if (form.value.formValue5) {
  771. imgTemp.value = `https://shopdeli.mycafe24.com/writable/uploads/item/thumb/${form.value.formValue5}`;
  772. }
  773. })
  774. .catch((error) => {
  775. $toast.error("제품 정보를 불러오는 중 오류가 발생했습니다.");
  776. })
  777. .finally(() => {});
  778. // 기 저장된 구매자명 리스트
  779. // 제품 seq, 인플루언서 seq가 일치하는 리스트만
  780. useAxios()
  781. .post(`/deli/list`, req2)
  782. .then((res) => {
  783. console.log(res.data);
  784. tblItems.value = res.data;
  785. pageObj.value.totalCnt = tblItems.value.length;
  786. })
  787. .catch((error) => {
  788. $toast.error("제품 정보를 불러오는 중 오류가 발생했습니다.");
  789. })
  790. .finally(() => {});
  791. };
  792. const fnInsert = async () => {
  793. // 벤더사인 경우 배송정보 업데이트 API 사용
  794. if (memberType === 'VENDOR') {
  795. await fnVendorUpdate();
  796. return;
  797. }
  798. // 인플루언서인 경우 기존 로직 사용
  799. const deliveryData = {
  800. item_seq: useDtStore.boardInfo.seq,
  801. inf_seq: memberSeq,
  802. deliveryList: tblItems.value.map((item) => ({
  803. buyerName: item.BUYER_NAME,
  804. address: item.ADDRESS,
  805. phone: item.PHONE,
  806. email: item.EMAIL,
  807. qty: item.QTY,
  808. total: item.TOTAL,
  809. deliComp: item.DELI_COMP || '',
  810. deliNumb: item.DELI_NUMB ? String(item.DELI_NUMB) : '',
  811. orderDate: item.ORDER_DATE.replaceAll(".", "-"),
  812. })),
  813. };
  814. await withLoading(async () => {
  815. return useAxios().post("/deli/reg", deliveryData);
  816. }, "배송 데이터를 저장하는 중...")
  817. .then((res) => {
  818. // 송장번호가 등록된 경우 상태를 COMPLETE로 업데이트
  819. const hasTrackingNumbers = deliveryData.deliveryList.some(
  820. (item) => item.deliNumb && typeof item.deliNumb === 'string' && item.deliNumb.trim() !== ""
  821. );
  822. if (hasTrackingNumbers) {
  823. // 상태 업데이트 API 호출
  824. const statusUpdateData = {
  825. item_seq: useDtStore.boardInfo.seq,
  826. status: "COMPLETE",
  827. };
  828. useAxios()
  829. .post("/deli/updateStatus", statusUpdateData)
  830. .then(() => {
  831. $toast.success(
  832. "배송 데이터가 성공적으로 저장되었습니다. 상태가 완료로 변경되었습니다."
  833. );
  834. // WebSocket을 통해 상태 변경 이벤트 발행
  835. const { $socket } = useNuxtApp();
  836. if ($socket) {
  837. $socket.emit("deliveryStatusUpdate", {
  838. itemId: useDtStore.boardInfo.seq,
  839. itemName: form.value.formValue1,
  840. status: "COMPLETE",
  841. updatedBy: memberSeq,
  842. updatedAt: new Date().toISOString(),
  843. });
  844. }
  845. })
  846. .catch(() => {
  847. $toast.success("배송 데이터가 저장되었으나 상태 업데이트에 실패했습니다.");
  848. });
  849. } else {
  850. $toast.success("배송 데이터가 성공적으로 저장되었습니다.");
  851. }
  852. // 저장 완료 후 배송 관리 리스트로 이동하며 업로드 성공 상태 전달
  853. router.push({
  854. path: "/view/common/deli",
  855. query: {
  856. uploadStatus: "success",
  857. itemId: useDtStore.boardInfo.seq,
  858. recordCount: tblItems.value.length,
  859. statusUpdated: hasTrackingNumbers ? "true" : "false",
  860. },
  861. });
  862. })
  863. .catch((error) => {
  864. const errorHandler = useErrorHandler();
  865. errorHandler.handleApiError(error, "배송 데이터 저장");
  866. })
  867. .finally(() => {});
  868. };
  869. const fnVendorUpdate = async () => {
  870. // 배송정보가 있는 항목만 추출 (구매자명과 연락처는 필수)
  871. const deliveryUpdates = tblItems.value
  872. .filter(item => item.BUYER_NAME && item.PHONE)
  873. .map(item => ({
  874. buyerName: item.BUYER_NAME,
  875. phone: item.PHONE,
  876. deliComp: item.DELI_COMP || '',
  877. deliNumb: item.DELI_NUMB ? String(item.DELI_NUMB) : ''
  878. }));
  879. if (deliveryUpdates.length === 0) {
  880. $toast.error("업데이트할 배송정보가 없습니다.");
  881. return;
  882. }
  883. const updateData = {
  884. item_seq: useDtStore.boardInfo.seq,
  885. deliveryUpdates: deliveryUpdates
  886. };
  887. await withLoading(async () => {
  888. return useAxios().post("/deli/updateDeliveryInfo", updateData);
  889. }, "배송정보를 업데이트하는 중...")
  890. .then((res) => {
  891. // 송장번호가 등록된 경우 상태를 COMPLETE로 업데이트
  892. const hasTrackingNumbers = deliveryUpdates.some(
  893. (item) => item.deliNumb && typeof item.deliNumb === 'string' && item.deliNumb.trim() !== ""
  894. );
  895. if (hasTrackingNumbers) {
  896. // 상태 업데이트 API 호출
  897. const statusUpdateData = {
  898. item_seq: useDtStore.boardInfo.seq,
  899. status: "COMPLETE",
  900. };
  901. useAxios()
  902. .post("/deli/updateStatus", statusUpdateData)
  903. .then(() => {
  904. $toast.success(
  905. "배송정보가 업데이트되고 상태가 완료로 변경되었습니다."
  906. );
  907. })
  908. .catch((error) => {
  909. console.error("상태 업데이트 실패:", error);
  910. $toast.warning("배송정보는 업데이트되었으나 상태 변경에 실패했습니다.");
  911. });
  912. } else {
  913. $toast.success(`${res.data.updated_count}건의 배송정보가 업데이트되었습니다.`);
  914. }
  915. // 배송 관리 목록으로 이동
  916. router.push({
  917. path: "/view/common/deli",
  918. query: {
  919. uploadStatus: "success",
  920. itemId: useDtStore.boardInfo.seq,
  921. recordCount: res.data.updated_count,
  922. statusUpdated: hasTrackingNumbers ? "true" : "false",
  923. },
  924. });
  925. })
  926. .catch((error) => {
  927. const errorHandler = useErrorHandler();
  928. errorHandler.handleApiError(error, "배송정보 업데이트");
  929. });
  930. };
  931. /************************************************************************
  932. | 팝업 이벤트버스 정의
  933. ************************************************************************/
  934. $eventBus.off("FN_INSERT");
  935. $eventBus.on("FN_INSERT", () => {
  936. fnInsert();
  937. });
  938. $eventBus.off("FN_DELETE_SELECTED");
  939. $eventBus.on("FN_DELETE_SELECTED", (selectedRows) => {
  940. fnDeleteSelected(selectedRows);
  941. });
  942. /************************************************************************
  943. | WATCH
  944. ************************************************************************/
  945. watch(
  946. () => props,
  947. () => {
  948. searchObj.value = props.propsData;
  949. fnGetStat();
  950. },
  951. { deep: true }
  952. );
  953. onMounted(() => {
  954. fnDetail();
  955. });
  956. </script>