package com.style24.admin.biz.service;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.style24.admin.biz.dao.TsaDisplayDao;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.core.biz.service.TscCommonService;
import com.style24.persistence.domain.Category;
import com.style24.persistence.domain.CategoryGoods;
import com.style24.persistence.domain.Contents;
import com.style24.persistence.domain.GnbTab;
import com.style24.persistence.domain.Goods;
import com.style24.persistence.domain.GoodsCategory;
import com.style24.persistence.domain.ItemkindCategory;
import com.style24.persistence.domain.MainDisplay;
import com.style24.persistence.domain.SearchKeyword;
import com.style24.persistence.domain.SplashImage;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.util.GagaDateUtil;
import com.gagaframework.web.util.GagaFileUtil;

/**
 * 전시 Service
 *
 * @author
 * @since 2020. 12. 16
 */
@Service
@Slf4j
public class TsaDisplayService {

	@Autowired
	private TsaDisplayDao displayDao;

	@Autowired
	private TscCommonService commonService;

	@Autowired
	private Environment env;

	/**
	 * 카테고리 목록
	 * @param category - 카테고리 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 4
	 */
	public Collection<Category> getCategoryList(Category category) {
		return displayDao.getCategoryList(category);
	}

	/**
	 * 카테고리 저장
	 * @param category - 카테고리 정보
	 * @author gagamel
	 * @since 2021. 1. 4
	 */
	@Transactional("shopTxnManager")
	public void saveCategory(Category category) {
		category.setRegNo(TsaSession.getInfo().getUserNo());
		category.setUpdNo(TsaSession.getInfo().getUserNo());

		Integer selLvl = category.getSelLvl();

		// 카테고리번호
		Integer cateNo = category.getCateNo();
		if (cateNo == null) {
			cateNo = commonService.getNextSequence("SEQ_CATE");
		}

		if (selLvl == 1) {
			category.setCate1No(cateNo);
			category.setCate1Nm(category.getCateNm());
			displayDao.saveCategory1(category);
		} else if (selLvl == 2) {
			category.setCate2No(cateNo);
			category.setCate2Nm(category.getCateNm());
			displayDao.saveCategory2(category);
		} else if (selLvl == 3) {
			category.setCate3No(cateNo);
			category.setCate3Nm(category.getCateNm());
			displayDao.saveCategory3(category);
		} else if (selLvl == 4) {
			category.setCate4No(cateNo);
			category.setCate4Nm(category.getCateNm());
			displayDao.saveCategory4(category);
		} else if (selLvl == 5) {
			category.setCate5No(cateNo);
			category.setCate5Nm(category.getCateNm());
			displayDao.saveCategory5(category);
		}

		if (selLvl >= 2) {
			displayDao.updateUpperCategoryLeafNo(category);
		}
	}

	/**
	 * 카테고리  1뎁스 저장(브랜드그룹관리에서 사용)
	 * @param category - 카테고리 정보
	 * @author eskim
	 * @since 2021. 4. 13
	 */
	@Transactional("shopTxnManager")
	public void saveCategory1(Category category) {
		category.setRegNo(TsaSession.getInfo().getUserNo());
		category.setUpdNo(TsaSession.getInfo().getUserNo());
		displayDao.saveCategory1(category);
	}

	/**
	 * 카테고리 갱신
	 * @author gagamel
	 * @since 2021. 1. 4
	 */
	@Transactional("shopTxnManager")
	@CacheEvict(value = "cate", allEntries = true)
	public void refreshCategory() {
		displayDao.truncateCategory4Srch();
		displayDao.createCategory4Srch();
	}

	/**
	 * 카테고리구분 목록
	 * @param cateGb - 카테고리구분
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 6
	 */
	public Collection<Category> getCategoryGbList(String cateGb) {
		return displayDao.getCategoryGbList(cateGb);
	}

	/**
	 * 품목카테고리매핑 저장
	 * @param itemkindCateList - 품목카테고리 목록
	 * @author gagamel
	 * @since 2021. 1. 6
	 */
	@Transactional("shopTxnManager")
	public void saveItemkindCategoryMappingList(Collection<ItemkindCategory> itemkindCateList) {
		for (ItemkindCategory itemkindCate : itemkindCateList) {
			itemkindCate.setRegNo(TsaSession.getInfo().getUserNo());

			// 품목카테고리매핑 생성
			displayDao.createItemkindCategoryMapping(itemkindCate);

			// 품목카테고리매핑에 의한 카테고리상품 생성
			displayDao.createCategoryGoodsByItemkindCategoryMapping(itemkindCate);
		}
	}

	/**
	 * 품목카테고리매핑 삭제
	 * @param itemkindCateList - 품목카테고리 목록
	 * @author gagamel
	 * @since 2021. 1. 6
	 */
	@Transactional("shopTxnManager")
	public void deleteItemkindCategoryMappingList(Collection<ItemkindCategory> itemkindCateList) {
		for (ItemkindCategory itemkindCate : itemkindCateList) {
			// 품목카테고리매핑 삭제
			displayDao.deleteItemkindCategoryMapping(itemkindCate);

			// 품목카테고리매핑에 의한 카테고리상품 삭제
			displayDao.deleteCategoryGoodsByItemkindCategoryMapping(itemkindCate);
		}
	}

	/**
	 * 품목카테고리매핑 목록
	 * @param itemkindCd - 품목코드
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 5
	 */
	public Collection<ItemkindCategory> getItemkindCategoryMappingList(String itemkindCd) {
		return displayDao.getItemkindCategoryMappingList(itemkindCd);
	}

	/**
	 * 상품별카테고리전시관리 목록 건수 조회
	 * @param goodsCategory
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 15
	 */
	public int getGoodsCategoryListCount(GoodsCategory goodsCategory) {
		return displayDao.getGoodsCategoryListCount(goodsCategory);
	}

	/**
	 * 상품별카테고리전시관리 목록 조회
	 * @param goodsCategory
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 15
	 */
	public Collection<Goods> getGoodsCategoryList(GoodsCategory goodsCategory) {
		return displayDao.getGoodsCategoryList(goodsCategory);
	}

	/**
	 * 상품별카테고리전시 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 17
	 */
	@Transactional("shopTxnManager")
	public void saveGoodsCategory(GoodsCategory goodsCategory) {
		goodsCategory.setRegNo(TsaSession.getInfo().getUserNo());
		goodsCategory.setUpdNo(TsaSession.getInfo().getUserNo());

		if (!StringUtils.isEmpty(goodsCategory.getGoodsCds())) {
			String[] goodsCdArr = goodsCategory.getGoodsCds().split(",");
			for (String goodsCd : goodsCdArr) {
				goodsCategory.setGoodsCd(goodsCd);
				int cnt = 0;
				for (String cateCd : goodsCategory.getCateCdArr()) {
					if (!StringUtils.isEmpty(cateCd)) {
						//goodsCategory.setCateCd(cateCd);
						//goodsCategory.setCateGb(goodsCategory.getCateGbArr()[cnt]);
						//goodsCategory.setCateType(goodsCategory.getCateTypeArr()[cnt]);
						//displayDao.saveGoodsCategory(goodsCategory);

						if (!"G031_20".equals(goodsCategory.getCateType())) {
							if (!StringUtils.isEmpty(goodsCategory.getCateCd1Arr()[cnt])) {
								String[] cate1Array = goodsCategory.getCateCd1Arr()[cnt].split(",");
								if (cate1Array[1].equals("Y")) {
									goodsCategory.setCateCd(cate1Array[0]);
									displayDao.saveGoodsCategory(goodsCategory);
								}
							}

							if (!StringUtils.isEmpty(goodsCategory.getCateCd2Arr()[cnt])) {
								String[] cate2Array = goodsCategory.getCateCd2Arr()[cnt].split(",");
								if (cate2Array[1].equals("Y")) {
									goodsCategory.setCateCd(cate2Array[0]);
									displayDao.saveGoodsCategory(goodsCategory);
								}
							}

							if (!StringUtils.isEmpty(goodsCategory.getCateCd3Arr()[cnt])) {
								String[] cate3Array = goodsCategory.getCateCd3Arr()[cnt].split(",");
								if (cate3Array[1].equals("Y")) {
									goodsCategory.setCateCd(cate3Array[0]);
									displayDao.saveGoodsCategory(goodsCategory);
								}
							}

							if (!StringUtils.isEmpty(goodsCategory.getCateCd4Arr()[cnt])) {
								String[] cate4Array = goodsCategory.getCateCd4Arr()[cnt].split(",");
								if (cate4Array[1].equals("Y")) {
									goodsCategory.setCateCd(cate4Array[0]);
									displayDao.saveGoodsCategory(goodsCategory);
								}
							}
						}
					}
					cnt++;
				}
			}
		}
	}

	/**
	 * 상품별카테고리 리스트 조회
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 18
	 */
	public Collection<GoodsCategory> getGoodsCategoryViewList(String goodsCd) {
		return displayDao.getGoodsCategoryViewList(goodsCd);
	}

	/**
	 * 상품별카테고리 삭제
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 18
	 */
	@Transactional("shopTxnManager")
	public void deleteGoodsCategoryList(GoodsCategory goodsCategory) {
		for (int i = 0; i < goodsCategory.getGoodsList().size(); i++) {
			goodsCategory.setGoodsCd(goodsCategory.getGoodsList().get(i).getGoodsCd());
			goodsCategory.setCateNo(goodsCategory.getGoodsList().get(i).getCateCd());
			this.deleteGoodsCategory(goodsCategory);
		}
	}

	@Transactional("shopTxnManager")
	public void deleteGoodsCategory(GoodsCategory goodsCategory) {
		displayDao.deleteGoodsCategory(goodsCategory);
	}

	/**
	 * 카테고리별상품 건수 조회
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 27
	 */
	public int getCategoryGoodsListCount(CategoryGoods categoryGoods) {
		return displayDao.getCategoryGoodsListCount(categoryGoods);
	}

	/**
	 * 카테고리별상품 리스트 조회
	 * @return
	 * @author bin2107
	 * @since 2021. 1. 27
	 */
	public Collection<CategoryGoods> getCategoryGoodsList(CategoryGoods categoryGoods) {
		return displayDao.getCategoryGoodsList(categoryGoods);
	}

	/**
	 * 카테고리별상품전시 상품이동/복사 (브랜드/아울렛 정보 추가후 로직 재검토)
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 15
	 */
	public void moveCategoryGoods(CategoryGoods categoryGoods) {
		GoodsCategory goodsCategory = new GoodsCategory();
		goodsCategory.setRegNo(categoryGoods.getRegNo());
		for (int i = 0; i < categoryGoods.getGoodsList().size(); i++) {
			goodsCategory.setGoodsCd(categoryGoods.getGoodsList().get(i).getGoodsCd());
			goodsCategory.setCateCd(categoryGoods.getAddCateNo());
			displayDao.saveGoodsCategory(goodsCategory);
			if ("move".equals(categoryGoods.getMode())) {
				goodsCategory.setCateNo(categoryGoods.getCateNo());
				displayDao.deleteGoodsCategory(goodsCategory);
			}
		}
	}

	/**
	 * 카테고리별상품전시 상품삭제
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 15
	 */
	public void deleteCategoryGoods(CategoryGoods categoryGoods) {
		GoodsCategory goodsCategory = new GoodsCategory();
		for (int i = 0; i < categoryGoods.getGoodsList().size(); i++) {
			goodsCategory.setGoodsCd(categoryGoods.getGoodsList().get(i).getGoodsCd());
			goodsCategory.setCateNo(categoryGoods.getGoodsList().get(i).getCateNo());
			displayDao.deleteGoodsCategory(goodsCategory);
		}
	}

	/**
	 * 카테고리별상품전시 상품추가
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 15
	 */
	public void insertCategoryGoods(CategoryGoods categoryGoods) {
		GoodsCategory goodsCategory = new GoodsCategory();
		goodsCategory.setRegNo(categoryGoods.getRegNo());
		for (int i = 0; i < categoryGoods.getMultiGoods().length; i++) {
			goodsCategory.setGoodsCd(categoryGoods.getMultiGoods()[i]);
			goodsCategory.setCateCd((categoryGoods.getCateNo()).toString());
			goodsCategory.setBrandGroupNo(categoryGoods.getBrandGroupNo());
			displayDao.saveGoodsCategory(goodsCategory);
		}
	}

	/**
	 * 전시 상품 순서변경 상품삭제
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 19
	 */
	@Transactional("shopTxnManager")
	public void deleteDispOrdChangeGoods(CategoryGoods categoryGoods) {
		GoodsCategory goodsCategory = new GoodsCategory();
		goodsCategory.setBrandGroupNo(categoryGoods.getBrandGroupNo());
		goodsCategory.setGoodsCd(categoryGoods.getGoodsCd());
		goodsCategory.setCateNo(categoryGoods.getCateNo());
		displayDao.deleteGoodsCategory(goodsCategory);
	}

	/**
	 * 전시 상품 순서변경 전시초기화
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 19
	 */
	@Transactional("shopTxnManager")
	public void updateCategoryGoodsDispOrdInit(CategoryGoods categoryGoods) {
		displayDao.updateCategoryGoodsDispOrdInit(categoryGoods);
	}

	/**
	 * 전시 상품 순서변경 최상단/하단 이동
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 19
	 */
	@Transactional("shopTxnManager")
	public void updateCategoryGoodsDispOrdUpdown(CategoryGoods categoryGoods) {
		if ("up".equals(categoryGoods.getChangeGb())) {
			categoryGoods.setChangeGb("min");
			displayDao.updateCategoryGoodsDispOrdUpdown(categoryGoods);
			categoryGoods.setChangeGb("up");
		} else if ("down".equals(categoryGoods.getChangeGb())) {
			categoryGoods.setChangeGb("max");
			displayDao.updateCategoryGoodsDispOrdUpdown(categoryGoods);
			categoryGoods.setChangeGb("down");
		}
		displayDao.updateCategoryGoodsDispOrdUpdown(categoryGoods);
	}

	/**
	 * 전시 상품 순서변경 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 19
	 */
	@Transactional("shopTxnManager")
	public void updateCategoryGoodsDispOrd(CategoryGoods categoryGoods) {
		int cnt = 0;
		for (String goodsCd : categoryGoods.getGoodsCdArr()) {
			categoryGoods.setGoodsCd(goodsCd);
			categoryGoods.setDispOrd(categoryGoods.getDispOrdArr()[cnt]);
			displayDao.updateCategoryGoodsDispOrd(categoryGoods);
			cnt++;
		}
	}

	/**
	 * 메인전시관리 카테고리 목록
	 * @param category - 카테고리 정보
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 22
	 */
	public Collection<Category> getMainCategoryList(Category category) {
		return displayDao.getMainCategoryList(category);
	}

	/**
	 * 메인전시 레이아웃 목록
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 22
	 */
	public Collection<MainDisplay> getMainLayoutList(MainDisplay mainDisplay) {
		return displayDao.getMainLayoutList(mainDisplay);
	}

	/**
	 * 메인전시 레이아웃 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 22
	 */
	@Transactional("shopTxnManager")
	public void saveMainLayout(Collection<MainDisplay> mainDisplayList) {
		for (MainDisplay mainDisplay : mainDisplayList) {
			log.info("mainDisplay.getContentsLoc()::::{}", mainDisplay.getContentsLoc());
			if (mainDisplay != null && mainDisplay.getContentsLoc() != null) {
				displayDao.deleteMainLayout(mainDisplay);
				break;
			}
		}

		for (MainDisplay mainDisplay : mainDisplayList) {
			log.info("mainDisplay.getCateNo()::::{}", mainDisplay.getCateNo());
			// 이미지 변경 시
			if (mainDisplay.getNewSysFileNm() != null && mainDisplay.getNewSysFileNm() != "") {
				String bannerUploadPath = env.getProperty("upload.default.target.path");
				bannerUploadPath = GagaFileUtil.getConcatenationPath(bannerUploadPath, "/mainLayout");
				String newFilename = "MAIN_LAYOUT_" + mainDisplay.getContentsLoc() + "_" + GagaDateUtil.getTodayDateTime() + "." + StringUtils.getFilenameExtension(mainDisplay.getNewSysFileNm());

				// 기존이미지 삭제
				try {
					GagaFileUtil.deleteFile(GagaFileUtil.getConcatenationPath(bannerUploadPath, mainDisplay.getOrgFileNm()));
				} catch (IOException e) {
					//  nothing
					log.info("[saveBanner MAIN_LAYOUT 기존 이미지 삭제중 error]");
					//e.printStackTrace();
				}

				File uniqueFile = GagaFileUtil.getUniqueFile(new File(GagaFileUtil.getConcatenationPath(bannerUploadPath, newFilename)));
				File file = new File(GagaFileUtil.getConcatenationPath(bannerUploadPath, mainDisplay.getNewSysFileNm()));

				// Rename a file
				file.renameTo(uniqueFile);

				mainDisplay.setOrgFileNm(newFilename);
				mainDisplay.setSysFileNm(newFilename);
			}
			mainDisplay.setRegNo(TsaSession.getInfo().getUserNo());
			mainDisplay.setUpdNo(TsaSession.getInfo().getUserNo());
			displayDao.saveMainLayout(mainDisplay);
		}
	}

	/**
	 * 메인전시 컨텐츠 설명
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 22
	 */
	public String getContentsDesc(String contentsLoc) {
		return displayDao.getContentsDesc(contentsLoc);
	}

	/**
	 * 메인전시 컨텐츠 미리보기 리스트
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 23
	 */
	public Collection<Contents> getContentsPreviewList(Contents contents) {
		Collection<Contents> previewList = displayDao.getContentsPreviewList(contents);
		for (Contents tempGoods : previewList) {
			tempGoods.setContentsGoodsList(displayDao.getContentsForGoodsList(tempGoods));
		}

		return previewList;
	}

	/**
	 * 메인전시 컨텐츠 탭영역 미리보기 리스트
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 18
	 */
	public Collection<Contents> getContentsTabPreviewList(Contents contents) {
		return displayDao.getContentsTabPreviewList(contents);
	}

	/**
	 * 메인전시 컨텐츠 탭영역 카테고리 미리보기 리스트
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 19
	 */
	public Collection<GnbTab> getGnbtabPreviewList(GnbTab gnbTab) {
		return displayDao.getGnbtabPreviewList(gnbTab);
	}

	/**
	 * 메인전시 컨텐츠 탭영역 카테고리 미리보기 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 19
	 */
	@Transactional("shopTxnManager")
	public void saveGnbtabPreview(Collection<GnbTab> gnbTabList) {
		for (GnbTab gnbTab : gnbTabList) {
			if (gnbTab != null && gnbTab.getGtabGb() != null) {
				displayDao.deleteGnbTabPreview(gnbTab);
				break;
			}
		}

		for (GnbTab gnbTab : gnbTabList) {
			gnbTab.setRegNo(TsaSession.getInfo().getUserNo());
			gnbTab.setUpdNo(TsaSession.getInfo().getUserNo());
			displayDao.saveGnbTabPreview(gnbTab);
		}
	}

	/**
	 * 메인전시 컨텐츠 탭영역 카테고리  저장
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 19
	 */
	@Transactional("shopTxnManager")
	public void saveGnbtabContents(GnbTab gnbTab) {
		gnbTab.setRegNo(TsaSession.getInfo().getUserNo());
		gnbTab.setUpdNo(TsaSession.getInfo().getUserNo());
		displayDao.deleteGnbTabContents(gnbTab);
		displayDao.saveGnbTabContents(gnbTab);
	}

	/**
	 * 메인전시 컨텐츠 미리보기 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 23
	 */
	@Transactional("shopTxnManager")
	public void saveContentsPreview(Collection<Contents> contentsList) {
		for (Contents contents : contentsList) {
			if (contents != null && contents.getContentsLoc() != null) {
				displayDao.deleteContentsPreview(contents);
				break;
			}
		}

		int ind = 1;
		for (Contents contents : contentsList) {
//			contents.setContentsSq(null);
			if (contents != null && contents.getDispStdt() != null && !contents.getDispStdt().equals("")) {
				// 브랜드 이미지 변경이 일어났을 경우
				String brandGroupImg = contents.getBrandGroupImg();
				if (brandGroupImg != null && !brandGroupImg.equals("")) {
					String imgInd = "01";
					String bannerUploadPath = env.getProperty("upload.default.target.path");
					String yearMonth = GagaDateUtil.getToday().substring(0, 6);
					bannerUploadPath = GagaFileUtil.getConcatenationPath(bannerUploadPath, "/contents/brand");
					File newFile = new File(GagaFileUtil.getConcatenationPath(bannerUploadPath, brandGroupImg));
					bannerUploadPath = GagaFileUtil.getConcatenationPath(bannerUploadPath, yearMonth);
					String newFilename = "BRAND_" + GagaDateUtil.getTodayDateTime() + "_" + imgInd + "." + StringUtils.getFilenameExtension(brandGroupImg);
					File uniqueFile = GagaFileUtil.getUniqueFile(new File(GagaFileUtil.getConcatenationPath(bannerUploadPath, newFilename)));
					// Rename a file
					File path = new File(bannerUploadPath);
					if (!path.exists()) {
						path.mkdir();
					}
					newFile.renameTo(uniqueFile);
					contents.setBrandGroupImg("/contents/brand/" + yearMonth + "/" + newFilename);
				}

				int cnt = 1;
				for (String newImgFile : contents.getNewImgFileArr()) {
					//이미지 변경이 일어났을 경우
					if (newImgFile != null && !newImgFile.equals("")) {
						String imgInd = String.valueOf(ind);
						if (ind < 10) {
							imgInd = "0" + imgInd;
						}
						String bannerUploadPath = env.getProperty("upload.default.target.path");
						String yearMonth = GagaDateUtil.getToday().substring(0, 6);
						if ("Y".equals(contents.getMultiContentsYn())) {
							bannerUploadPath = GagaFileUtil.getConcatenationPath(bannerUploadPath, "/contents/planning");
						} else {
							bannerUploadPath = GagaFileUtil.getConcatenationPath(bannerUploadPath, "/contents");
						}
						File newFile = new File(GagaFileUtil.getConcatenationPath(bannerUploadPath, newImgFile));
						bannerUploadPath = GagaFileUtil.getConcatenationPath(bannerUploadPath, yearMonth);
						String newFilename = "";
						if ("Y".equals(contents.getMultiContentsYn())) {
							newFilename = "PLANNING_" + GagaDateUtil.getTodayDateTime() + "_" + imgInd + "." + StringUtils.getFilenameExtension(newImgFile);
						} else {
							newFilename = "CONTENS_" + GagaDateUtil.getTodayDateTime() + "_" + imgInd + "." + StringUtils.getFilenameExtension(newImgFile);
						}
						File uniqueFile = GagaFileUtil.getUniqueFile(new File(GagaFileUtil.getConcatenationPath(bannerUploadPath, newFilename)));
						// Rename a file
						File path = new File(bannerUploadPath);
						if (!path.exists()) {
							path.mkdir();
						}
						newFile.renameTo(uniqueFile);

						String contentsPath = "";
						if ("Y".equals(contents.getMultiContentsYn())) {
							contentsPath = "/contents/planning/";
						} else {
							contentsPath = "/contents/";
						}
						if (cnt == 1) {
							contents.setImgPath1(contentsPath + yearMonth + "/" + newFilename);
						} else if (cnt == 2) {
							contents.setImgPath2(contentsPath + yearMonth + "/" + newFilename);
						} else if (cnt == 3) {
							contents.setImgPath3(contentsPath + yearMonth + "/" + newFilename);
						} else if (cnt == 4) {
							contents.setImgPath4(contentsPath + yearMonth + "/" + newFilename);
						} else if (cnt == 5) {
							contents.setImgPath5(contentsPath + yearMonth + "/" + newFilename);
						} else if (cnt == 6) {
							contents.setImgPath6(contentsPath + yearMonth + "/" + newFilename);
						} else if (cnt == 7) {
							contents.setImgPath7(contentsPath + yearMonth + "/" + newFilename);
						} else if (cnt == 8) {
							contents.setImgPath8(contentsPath + yearMonth + "/" + newFilename);
						}
						ind++;

					}
					cnt++;
				}

				contents.setRegNo(TsaSession.getInfo().getUserNo());
				contents.setUpdNo(TsaSession.getInfo().getUserNo());
				displayDao.saveContentsPreview(contents);
			}
		}

		for (Contents contents1 : contentsList) {
			if (contents1 != null && contents1.getGoodsCdArr() != null) {
				if (contents1.getContentsLoc().equals("SMM012")) {
					contents1.setCateNo(1700);
				}
				contents1.setContentsSq(contents1.getDispOrd());
				contents1.setRegNo(TsaSession.getInfo().getUserNo());
				displayDao.deleteContentsGoodsTemp(contents1);

				if (contents1.getGoodsCdArr().length > 0) {
					for (int i = 0; i < contents1.getGoodsCdArr().length; i++) {
						contents1.setGoodsCd(contents1.getGoodsCdArr()[i]);
//						contents1.setGoodsNm(contents1.getGoodsNmArr()[i]);
//						contents1.setImgPath1(contents1.getGoodsImgArr()[i]);
						contents1.setDispOrd(Integer.parseInt(contents1.getGoodsDispOrdArr()[i]));
						displayDao.saveContentsGoodsTemp(contents1);
					}
				}
			}
		}
	}

	/**
	 * 메인전시 컨텐츠 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 23
	 */
	@Transactional("shopTxnManager")
	public void saveContents(Contents contents) {
		contents.setRegNo(TsaSession.getInfo().getUserNo());
		contents.setUpdNo(TsaSession.getInfo().getUserNo());
		for (String contentsLoc : contents.getContentsLocArr()) {
			contents.setContentsLoc(contentsLoc);
			displayDao.deleteContents(contents);
			displayDao.saveContents(contents);
			if ("SMM007".equals(contentsLoc) || "SMM009".equals(contentsLoc)) {
				displayDao.deleteContentsGoods(contents);
				displayDao.saveContentsGoods(contents);
			}
			if ("STAB002".equals(contentsLoc) || "SBM002".equals(contentsLoc)) {
				GnbTab gnbTab = new GnbTab();
				if ("STAB002".equals(contentsLoc)) {
					gnbTab.setGtabGb("C");
				} else {
					gnbTab.setGtabGb("B");
					gnbTab.setBrandGroupNo(Integer.parseInt(contents.getBrandGroupNo()));
				}
				gnbTab.setRegNo(TsaSession.getInfo().getUserNo());
				gnbTab.setUpdNo(TsaSession.getInfo().getUserNo());
				displayDao.deleteGnbTabContents(gnbTab);
				displayDao.saveGnbTabContents(gnbTab);
			}
		}
	}

	/**
	 * CATEGORY4SRCH 조회
	 * @return
	 * @author bin2107
	 * @since 2021. 2. 24
	 */
	public Category getCategory4srch(Category category) {
		return displayDao.getCategory4srch(category);
	}

	/**
	 * 검색 키워드 저장
	 * @param searchKeyword
	 * @return
	 * @author bin2107
	 * @since 2021. 7. 1
	 */
	@Transactional("shopTxnManager")
	public void saveSearchKeyword(SearchKeyword searchKeyword) {
		searchKeyword.setRegNo(TsaSession.getInfo().getUserNo());
		searchKeyword.setUpdNo(TsaSession.getInfo().getUserNo());

//		if(searchKeyword.getKeywordSq() == null || searchKeyword.getKeywordSq() == 0){
//			displayDao.createSearchKeyword(searchKeyword);
//		}else{
//			displayDao.updateSearchKeyword(searchKeyword);
//		}
		displayDao.createSearchKeyword(searchKeyword);
	}

	/**
	 * 검색 키워드 목록
	 *
	 * @param SearchKeyword
	 * @return
	 * @author bin2107
	 * @since 2021. 7. 2
	 */
	public Collection<SearchKeyword> getSearchKeywordList(SearchKeyword searchKeyword) {
		return displayDao.getSearchKeywordList(searchKeyword);
	}

	/**
	 * 스플래시이미지 목록
	 * @return
	 * @author gagamel
	 * @since 2021. 7. 2
	 */
	public Collection<SplashImage> getSplashImageList() {
		return displayDao.getSplashImageList();
	}

	/**
	 * 스플래시이미지 등록/수정
	 * @param splashImg - 스플래시이미지 정보
	 * @author gagamel
	 * @since 2021. 7. 2
	 */
	@Transactional("shopTxnManager")
	@CacheEvict(value = "splashimg", allEntries = true)
	public void saveSplashImage(SplashImage splashImg) {
		if (splashImg.getMode().equals("N")) {
			displayDao.createSplashImage(splashImg);
		} else {
			displayDao.updateSplashImage(splashImg);
		}
	}

}
