package com.style24.front.biz.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import com.style24.front.biz.thirdparty.EigeneaiApi;
import com.style24.persistence.domain.eigene.Eigeneai;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.style24.core.biz.service.TscEnvsetService;
import com.style24.core.support.env.TscConstants;
import com.style24.front.biz.dao.TsfDisplayDao;
import com.style24.front.biz.dao.TsfGoodsDao;
import com.style24.front.support.env.TsfConstants;
import com.style24.front.support.security.session.TsfSession;
import com.style24.persistence.domain.BrandGroup;
import com.style24.persistence.domain.Cate1;
import com.style24.persistence.domain.Cate2;
import com.style24.persistence.domain.Cate3;
import com.style24.persistence.domain.Cate4;
import com.style24.persistence.domain.Cate4Srch;
import com.style24.persistence.domain.Cate5;
import com.style24.persistence.domain.Contents;
import com.style24.persistence.domain.GnbTab;
import com.style24.persistence.domain.Goods;
import com.style24.persistence.domain.GoodsSearch;
import com.style24.persistence.domain.Login;
import com.style24.persistence.domain.MainLayout;
import com.style24.persistence.domain.Popup;
import com.style24.persistence.domain.SearchKeyword;
import com.style24.persistence.domain.searchengine.Filter;
import com.style24.persistence.domain.searchengine.SearchEngine;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.parameter.GagaMap;
import com.gagaframework.web.util.GagaFileUtil;

/**
 * 전시 Service
 *
 * @author gagamel
 * @since 2020. 12. 29
 */
@Service
@Slf4j
public class TsfDisplayService {

	@Autowired
	private TsfDisplayDao displayDao;

	@Autowired
	private TscEnvsetService envsetService;

	@Autowired
	private TsfGoodsDao goodsDao;

	@Autowired
	private Environment env;

	@Autowired
	private EigeneaiApi eigeneaiApi;

	/**
	 * GNB 브랜드그룹 목록
	 * @param contents - 컨텐츠 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 3. 11
	 */
	@Cacheable(value = "commonGnb", key = "'brandGroup'")
	public Collection<BrandGroup> getGnbBrandGroupList(Contents contents) {
		return displayDao.getGnbBrandGroupList(contents);
	}

	/**
	 * 브랜드메인의 GNB브랜드그룹 정보 조회
	 * @param brandGroupNo - 브랜드그룹번호
	 * @return
	 * @author gagamel
	 * @since 2021. 3. 16
	 */
	public BrandGroup getGnbBrandGroup(Integer brandGroupNo) {
		return displayDao.getGnbBrandGroup(brandGroupNo);
	}

	/**
	 * GNB 탭 목록
	 * @param gnbTab - GNB탭 정보
	 * @return
	 * @author gagamel
	 * @date 2021. 3. 11
	 */
//	@Cacheable(value = "commonGnb", key = "'gnbTab-'.concat(#gnbTab.gtabGb)")
	public Collection<GnbTab> getGnbTabList(GnbTab gnbTab) {
		return displayDao.getGnbTabList(gnbTab);
	}

	/**
	 * 컨텐츠 목록
	 * @param contents - 컨텐츠 정보
	 * @return
	 * @author gagamel
	 * @date 2021. 3. 11
	 */
	public Collection<Contents> getContentsList(Contents contents) {
		return displayDao.getContentsList(contents);
	}

//	/**
//	 * FULL 카테고리1 정보
//	 * @param cateGb - 카테고리구분
//	 * @param cate1No - 카테고리1번호
//	 * @return
//	 * @author gagamel
//	 * @since 2021. 3. 11
//	 */
//	@Cacheable(value = "cate", key = "'cateList-'.concat(#cateGb).concat(#cate1No)")
//	public Cate1 getFullCategory1(String cateGb, Integer cate1No) {
//		Cate4Srch cate = new Cate4Srch();
//		cate.setSiteCd(TscConstants.Site.STYLE24.value()); // 사이트코드
//		cate.setCateGb(cateGb); // 카테고리구분
//		cate.setCateType(TsfConstants.CateType.GOODS.value()); // 상품분류카테고리
//		cate.setCate1No(cate1No);
//		return this.getFullCategory1(cate);
//	}
//
//	/**
//	 * FULL 카테고리1 정보
//	 * @param cate - 카테고리 정보
//	 * @return
//	 * @author gagamel
//	 * @since 2021. 3. 11
//	 */
//	public Cate1 getFullCategory1(Cate4Srch cate) {
//		// 카테고리1 정보
//		Cate1 cate1 = displayDao.getCategory1List(cate).get(0);
//
//		if (cate1 != null) {
//			if (cate1.getLeafYn().equals("N")) {
//				cate.setCate1No(cate1.getCate1No());
//
//				// 카테고리2 목록
//				Collection<Cate2> cate2List = displayDao.getCategory2List(cate);
//
//				if (cate2List != null && !cate2List.isEmpty()) {
//					for (Cate2 cate2 : cate2List) {
//						if (cate2.getLeafYn().equals("N")) {
//							cate.setCate2No(cate2.getCate2No());
//
//							// 카테고리3 목록
//							Collection<Cate3> cate3List = displayDao.getCategory3List(cate);
//
//							if (cate3List != null && !cate3List.isEmpty()) {
//								for (Cate3 cate3 : cate3List) {
//									if (cate3.getLeafYn().equals("N")) {
//										cate.setCate3No(cate3.getCate3No());
//
//										// 카테고리4 목록
//										Collection<Cate4> cate4List = displayDao.getCategory4List(cate);
//
//										if (cate4List != null && !cate4List.isEmpty()) {
//											for (Cate4 cate4 : cate4List) {
//												if (cate4.getLeafYn().equals("N")) {
//													cate.setCate4No(cate4.getCate4No());
//
//													// 카테고리5 목록 담기
//													cate4.setCate5List(displayDao.getCategory5List(cate));
//												}
//											}
//
//											log.info("cate4List: {}", cate4List);
//
//											// 카테고리4 목록 담기
//											cate3.setCate4List(cate4List);
//										}
//									}
//								}
//
//								// 카테고리3 목록 담기
//								cate2.setCate3List(cate3List);
//							}
//						}
//					}
//
//					// 카테고리2 목록 담기
//					cate1.setCate2List(cate2List);
//				}
//			}
//		}
//
//		return cate1;
//	}

	/**
	 * 전체 카테고리 목록
	 * @param cateGb - 카테고리구분
	 * @return
	 * @author gagamel
	 * @since 2021. 3. 11
	 */
	@Cacheable(value = "cate", key = "'allCate-'.concat(#cateGb)")
	public Collection<Cate1> getAllCategoryList(String cateGb) {
		Cate4Srch cate = new Cate4Srch();
		cate.setSiteCd(TscConstants.Site.STYLE24.value()); // 사이트코드
		cate.setCateGb(cateGb); // 카테고리구분
		cate.setCateType(TsfConstants.CateType.GOODS.value()); // 상품분류카테고리
		cate.setSoldoutGoodsDispYn(envsetService.getSoldoutGoodsDisplayYn(TscConstants.Site.STYLE24.value()));
		return this.getAllCategoryList(cate);
	}

	/**
	 * 전체 카테고리 목록
	 * @param cate - 카테고리 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 3. 11
	 */
	public Collection<Cate1> getAllCategoryList(Cate4Srch cate) {
//		// 카테고리1 목록
//		Collection<Cate1> cate1List = displayDao.getCategory1List(cate);
//
//		if (cate1List != null && !cate1List.isEmpty()) {
//			for (Cate1 cate1 : cate1List) {
//				if (cate1.getLeafYn().equals("N")) {
//					cate.setCate1No(cate1.getCate1No());
//
//					// 카테고리2 목록
//					Collection<Cate2> cate2List = displayDao.getCategory2List(cate);
//
//					if (cate2List != null && !cate2List.isEmpty()) {
//						for (Cate2 cate2 : cate2List) {
//							if (cate2.getLeafYn().equals("N")) {
//								cate.setCate2No(cate2.getCate2No());
//
//								// 카테고리3 목록
//								Collection<Cate3> cate3List = displayDao.getCategory3List(cate);
//
//								if (cate3List != null && !cate3List.isEmpty()) {
//									for (Cate3 cate3 : cate3List) {
//										if (cate3.getLeafYn().equals("N")) {
//											cate.setCate3No(cate3.getCate3No());
//
//											// 카테고리4 목록
//											Collection<Cate4> cate4List = displayDao.getCategory4List(cate);
//
//											if (cate4List != null && !cate4List.isEmpty()) {
//												for (Cate4 cate4 : cate4List) {
//													if (cate4.getLeafYn().equals("N")) {
//														cate.setCate4No(cate4.getCate4No());
//
//														// 카테고리5 목록 담기
//														cate4.setCate5List(displayDao.getCategory5List(cate));
//													}
//												}
//
//												log.info("cate4List: {}", cate4List);
//
//												// 카테고리4 목록 담기
//												cate3.setCate4List(cate4List);
//											}
//										}
//									}
//
//									// 카테고리3 목록 담기
//									cate2.setCate3List(cate3List);
//								}
//							}
//						}
//
//						// 카테고리2 목록 담기
//						cate1.setCate2List(cate2List);
//					}
//				}
//			}
//		}

		Collection<Cate1> cate1List = new ArrayList<Cate1>();

		// 전체 카테고리 목록
		Collection<Cate4Srch> cateList = displayDao.getAllCategoryList(cate);

		if (cateList != null && !cateList.isEmpty()) {
			Integer prevCate1No = 0;
			Integer prevCate2No = 0;
			Integer prevCate3No = 0;
			Integer prevCate4No = 0;
			Integer prevCate5No = 0;

			for (Cate4Srch cate4srch1 : cateList) {
				if (cate4srch1.getCate1No() != null && !cate4srch1.getCate1No().equals(prevCate1No)) {
					Cate1 cate1 = new Cate1();
					cate1.setSiteCd(cate4srch1.getSiteCd());
					cate1.setCateGb(cate4srch1.getCateGb());
					cate1.setCateType(cate4srch1.getCateType());
					cate1.setCate1No(cate4srch1.getCate1No());
					cate1.setCate1Nm(cate4srch1.getCate1Nm());
					cate1.setFormalGb(cate4srch1.getFormalGb());

					Collection<Cate2> cate2List = new ArrayList<Cate2>();
					for (Cate4Srch cate4srch2 : cateList) {
						if (cate4srch2.getCate1No() != null && cate4srch1.getCate1No().equals(cate4srch2.getCate1No())) {
							if (cate4srch2.getCate2No() != null && !cate4srch2.getCate2No().equals(prevCate2No)) {
								Cate2 cate2 = new Cate2();
								cate2.setSiteCd(cate4srch2.getSiteCd());
								cate2.setCateGb(cate4srch2.getCateGb());
								cate2.setCateType(cate4srch2.getCateType());
								cate2.setCate2No(cate4srch2.getCate2No());
								cate2.setCate2Nm(cate4srch2.getCate2Nm());
								cate2.setCate1No(cate4srch2.getCate1No());
								cate2.setFormalGb(cate4srch2.getFormalGb());

								Collection<Cate3> cate3List = new ArrayList<Cate3>();
								for (Cate4Srch cate4srch3 : cateList) {
									if (cate4srch3.getCate2No() != null && cate4srch2.getCate2No().equals(cate4srch3.getCate2No())) {
										if (cate4srch3.getCate3No() != null && !cate4srch3.getCate3No().equals(prevCate3No)) {
											Cate3 cate3 = new Cate3();
											cate3.setSiteCd(cate4srch3.getSiteCd());
											cate3.setCateGb(cate4srch3.getCateGb());
											cate3.setCateType(cate4srch3.getCateType());
											cate3.setCate3No(cate4srch3.getCate3No());
											cate3.setCate3Nm(cate4srch3.getCate3Nm());
											cate3.setCate1No(cate4srch3.getCate1No());
											cate3.setCate2No(cate4srch3.getCate2No());
											cate3.setFormalGb(cate4srch3.getFormalGb());

											Collection<Cate4> cate4List = new ArrayList<Cate4>();
											for (Cate4Srch cate4srch4 : cateList) {
												if (cate4srch4.getCate3No() != null && cate4srch3.getCate3No().equals(cate4srch4.getCate3No())) {
													if (cate4srch4.getCate4No() != null && !cate4srch4.getCate4No().equals(prevCate4No)) {
														Cate4 cate4 = new Cate4();
														cate4.setSiteCd(cate4srch4.getSiteCd());
														cate4.setCateGb(cate4srch4.getCateGb());
														cate4.setCateType(cate4srch4.getCateType());
														cate4.setCate4No(cate4srch4.getCate4No());
														cate4.setCate4Nm(cate4srch4.getCate4Nm());
														cate4.setCate1No(cate4srch4.getCate1No());
														cate4.setCate2No(cate4srch4.getCate2No());
														cate4.setCate3No(cate4srch4.getCate3No());
														cate4.setFormalGb(cate4srch4.getFormalGb());

														Collection<Cate5> cate5List = new ArrayList<Cate5>();
														for (Cate4Srch cate4srch5 : cateList) {
															if (cate4srch5.getCate4No() != null && cate4srch4.getCate4No().equals(cate4srch5.getCate4No())) {
																if (cate4srch5.getCate5No() != null && !cate4srch5.getCate5No().equals(prevCate5No)) {
																	Cate5 cate5 = new Cate5();
																	cate5.setSiteCd(cate4srch5.getSiteCd());
																	cate5.setCateGb(cate4srch5.getCateGb());
																	cate5.setCateType(cate4srch5.getCateType());
																	cate5.setCate5No(cate4srch5.getCate5No());
																	cate5.setCate5Nm(cate4srch5.getCate5Nm());
																	cate5.setCate1No(cate4srch5.getCate1No());
																	cate5.setCate2No(cate4srch5.getCate2No());
																	cate5.setCate3No(cate4srch5.getCate3No());
																	cate5.setCate4No(cate4srch5.getCate4No());
																	cate5.setFormalGb(cate4srch5.getFormalGb());
																	cate5.setLeafYn("Y");
																	cate5List.add(cate5);
																}
															}

															prevCate5No = cate4srch5.getCate5No();
														}

														cate4.setLeafYn(cate5List.size() > 0 ? "N" : "Y");
														cate4.setCate5List(cate5List);
														cate4List.add(cate4);
													}
												}

												prevCate4No = cate4srch4.getCate4No();
											}

											cate3.setLeafYn(cate4List.size() > 0 ? "N" : "Y");
											cate3.setCate4List(cate4List);
											cate3List.add(cate3);
										}
									}

									prevCate3No = cate4srch3.getCate3No();
								}

								cate2.setLeafYn(cate3List.size() > 0 ? "N" : "Y");
								cate2.setCate3List(cate3List);
								cate2List.add(cate2);
							}
						}

						prevCate2No = cate4srch2.getCate2No();
					}

					cate1.setLeafYn(cate2List.size() > 0 ? "N" : "Y");
					cate1.setCate2List(cate2List);
					cate1List.add(cate1);
				}

				prevCate1No = cate4srch1.getCate1No();
			}
		}

//				Map<Integer, List<Cate4Srch>> cate4srch1 = cateList.stream().collect(Collectors.groupingBy(Cate4Srch::getCate1No, LinkedHashMap::new, Collectors.toList()));
//				cate4srch1.forEach((key1, value1) -> {
//					log.info("{}:{}", key1, value1);
//					Cate1 cate1 = new Cate1();
//					cate1.setCate1No(key1);
//					Iterator<Cate4Srch> aaa = value1.iterator();
//					while (aaa.hasNext()) {
//						aaa.next();
//					}
//
//					Map<Integer, List<Cate4Srch>> cate4srch2 = value1.stream().collect(Collectors.groupingBy(Cate4Srch::getCate2No, LinkedHashMap::new, Collectors.toList()));
//					cate4srch2.forEach((key2, value2) -> {
//						log.info("{}:{}", key2, value2);
//					});
//				});

		return cate1List;
	}

	/**
	 * 브랜드 카테고리 목록
	 * @param brandGroupNo - 브랜드그룹번호
	 * @return
	 * @author gagamel
	 * @since 2021. 4. 12
	 */
	@Cacheable(value = "cate", key = "'brandCate-'.concat(#brandGroupNo)")
	public Collection<Cate1> getBrandCategoryList(Integer brandGroupNo) {
		// 브랜드그룹의 기본카테고리여부 조회
		String defaultCateYn = displayDao.getBrandGroupDefaultCategoryYn(brandGroupNo);

		Cate4Srch cate = new Cate4Srch();
		cate.setSiteCd(TscConstants.Site.STYLE24.value()); // 사이트코드

		if (defaultCateYn.equals("Y")) { // 기본카테고리이면
			cate.setCateGb(TsfConstants.CateGb.BYITEM.value());
		} else { // 별도카테고리이면
			cate.setCateGb(TsfConstants.CateGb.BYBRAND.value());
		}

		cate.setCateType(TsfConstants.CateType.GOODS.value()); // 상품분류카테고리
		cate.setBrandGroupNo(brandGroupNo);
		cate.setSoldoutGoodsDispYn(envsetService.getSoldoutGoodsDisplayYn(TscConstants.Site.STYLE24.value()));
		cate.setDefaultCateYn(defaultCateYn);

		return this.getAllCategoryList(cate);
	}

	/**
	 * 아울렛 카테고리 목록
	 * @param brandGroupNo - 브랜드그룹번호
	 * @return
	 * @author gagamel
	 * @since 2021. 4. 12
	 */
	@Cacheable(value = "cate", key = "'outletCate-'.concat(#formalGb)")
	public Collection<Cate1> getOutletCategoryList(String formalGb) {
		Cate4Srch cate = new Cate4Srch();
		cate.setSiteCd(TscConstants.Site.STYLE24.value()); // 사이트코드

		cate.setFormalGb(formalGb);
		cate.setCateGb(TsfConstants.CateGb.BYITEM.value());
		cate.setCateType(TsfConstants.CateType.GOODS.value()); // 상품분류카테고리
		cate.setSoldoutGoodsDispYn(envsetService.getSoldoutGoodsDisplayYn(TscConstants.Site.STYLE24.value()));

		return this.getAllCategoryList(cate);
	}

	/**
	 * 카테고리 목록
	 * @param brandGroupNo - 브랜드그룹번호
	 * @return
	 * @author gagamel
	 * @since 2021. 4. 12
	 */
	@Cacheable(value = "cate", key = "'reloadCate-'.concat(#cateGb)")
	public Collection<Cate1> getCategoryReloadList(String cateGb) {
		Cate4Srch cate = new Cate4Srch();
		cate.setSiteCd(TscConstants.Site.STYLE24.value()); // 사이트코드

		cate.setCateGb(TsfConstants.CateGb.BYITEM.value());
		cate.setCateType(TsfConstants.CateType.GOODS.value()); // 상품분류카테고리
		cate.setSoldoutGoodsDispYn(envsetService.getSoldoutGoodsDisplayYn(TscConstants.Site.STYLE24.value()));

		return this.getAllCategoryList(cate);
	}

	/**
	 * 메인 레이아웃 목록
	 * @param cateNo - 카테고리No
	 * @return
	 * @author bin2107
	 * @date 2021. 3. 16
	 */
	public Collection<MainLayout> getMainLayout(MainLayout mainLayout) {
		return displayDao.getMainLayout(mainLayout);
	}

	/**
	 * 카테고리 4srch 조회
	 * @param
	 * @return
	 * @author bin2107
	 * @date 2021. 3. 16
	 */
	public Cate4Srch getCate4srch(Cate4Srch cate4Srch) {
		return displayDao.getCate4srch(cate4Srch);
	}

	/**
	 * 팝업 목록
	 *
	 * @param popup
	 * @return
	 * @author eskim
	 * @date 2021. 3. 30
	 */
	public GagaMap getPopupList(Popup popup) {
		GagaMap result = new GagaMap();
		Collection<Popup> popupList = new ArrayList<Popup>();
		popup.setSiteCd(TscConstants.Site.STYLE24.value());

		if ("P".equals(popup.getFrontGb())) {
			result.set("dataList", displayDao.getPopupList(popup));
		} else {
			//우선순위 full 팝
			popup.setPopupGb("F");
			popupList = displayDao.getPopupList(popup);
			result.set("popupGb", "F");
			result.set("dataList", popupList);
			if (popupList != null && popupList.size() > 0) {
				return result;
			}
			popup.setPopupGb("H");
			popupList = displayDao.getPopupList(popup);
			result.set("popupGb", "H");
			result.set("dataList", popupList);
		}

		// Collection<Popup>

		return result;
	}

	/**
	 * 몰메인 MD PICK 목록(메인용)
	 * @param
	 * @return
	 * @author bin2107
	 * @date 2021. 3. 30
	 */
	public Collection<Contents> getContentsForGoodsMain(Contents contents) {
		Collection<Contents> contentsList = new ArrayList<>();
		if(contents.getContentsLoc().equals("SMM007")){
			contentsList = displayDao.getBrandpickTitleBrandList(contents);
		}else{
			contentsList = displayDao.getContentsList(contents);
		}

		Collection<Goods> goodsList = new ArrayList<>();
		Cate4Srch tempContents = new Cate4Srch();
		for (Contents data : contentsList) {
			tempContents.setContentsLoc(data.getContentsLoc());
			tempContents.setDispOrd(data.getDispOrd());
			tempContents.setMaxRow(20);
			tempContents.setCustNo(TsfSession.isLogin() ? TsfSession.getInfo().getCustNo() : 0);
			
			// 2021.08.31 FN_GET_BENEFIT_PRICE() 쿼리 null 처리
			tempContents.setFrontGb(TsfSession.getFrontGb());
			tempContents.setCustGb(TsfSession.getCustGb());
			
			if ("SMM007".equals(data.getContentsLoc())) {
				if(!"".equals(data.getBrandGroupNo()) && !"null".equals(data.getBrandGroupNo()) && !data.getBrandGroupNo().equals(null)){
					tempContents.setBrandGroupNo(Integer.parseInt(data.getBrandGroupNo()));
				}
				data.setFrontGb(TsfSession.getFrontGb());
				data.setBannerList(displayDao.getContentsBannerList(data));
			}
			goodsList = (goodsDao.getContentsCategoryGoodsListMain(tempContents));
			if ("SMM007".equals(data.getContentsLoc())) {
				int maxrow = 3;
				if (goodsList == null || goodsList.size() < 3) {
					Cate4Srch cate = new Cate4Srch();
					cate.setSiteCd(TscConstants.Site.STYLE24.value());
					cate.setCateGb(TsfConstants.CateGb.BYITEM.value());
					cate.setFrontGb(data.getFrontGb());
					cate.setBrandGroupNo(Integer.parseInt(data.getBrandGroupNo()));
					cate.setMaxRow(maxrow - goodsList.size());
					
					// 2021.09.29 신상품조회시 frontGb, custGb 누락 추가
					cate.setCustGb(TsfSession.getCustGb());
					
					goodsList.addAll(goodsDao.getContentsCategoryNewGoodsList(cate));
				}
			}
			data.setGoodsList(goodsList);
		}

		log.info("getContentsForGoods contentsList.size()::{}", contentsList.size());

		return contentsList;
	}
	
	/**
	 * 몰메인 MD PICK 목록
	 * @param
	 * @return
	 * @author jmh
	 * @date 2021. 8. 13
	 */
	public Collection<Contents> getContentsForGoods(Contents contents) {
		Collection<Contents> contentsList = new ArrayList<>();
		if(contents.getContentsLoc().equals("SMM007")){
			contentsList = displayDao.getBrandpickTitleBrandList(contents);
		}else{
			contentsList = displayDao.getContentsList(contents);
		}

		Collection<Goods> goodsList = new ArrayList<>();
		Cate4Srch tempContents = new Cate4Srch();
		for (Contents data : contentsList) {
			tempContents.setContentsLoc(data.getContentsLoc());
			tempContents.setDispOrd(data.getDispOrd());
			tempContents.setMaxRow(20);
			tempContents.setCustNo(TsfSession.isLogin() ? TsfSession.getInfo().getCustNo() : 0);
			if ("SMM007".equals(data.getContentsLoc())) {
				if(!"".equals(data.getBrandGroupNo()) && !"null".equals(data.getBrandGroupNo()) && !data.getBrandGroupNo().equals(null)){
					tempContents.setBrandGroupNo(Integer.parseInt(data.getBrandGroupNo()));
				}
				data.setFrontGb(TsfSession.getFrontGb());
				data.setBannerList(displayDao.getContentsBannerList(data));
			}
			goodsList = (goodsDao.getContentsCategoryGoodsList(tempContents));
			if ("SMM007".equals(data.getContentsLoc())) {
				int maxrow = 3;
				if (goodsList == null || goodsList.size() < 3) {
					Cate4Srch cate = new Cate4Srch();
					cate.setSiteCd(TscConstants.Site.STYLE24.value());
					cate.setCateGb(TsfConstants.CateGb.BYITEM.value());
					cate.setFrontGb(data.getFrontGb());
					cate.setBrandGroupNo(Integer.parseInt(data.getBrandGroupNo()));
					cate.setMaxRow(maxrow - goodsList.size());
					
					// 2021.09.29 신상품조회시 frontGb, custGb 누락 추가
					cate.setFrontGb(TsfSession.getFrontGb());
					cate.setCustGb(TsfSession.getCustGb());
					goodsList.addAll(goodsDao.getContentsCategoryNewGoodsList(cate));
				}
			}
			data.setGoodsList(goodsList);
		}

		log.info("getContentsForGoods contentsList.size()::{}", contentsList.size());

		return contentsList;
	}

	/**
	 * 몰메인 BESTITEM 목록(메인용)
	 * @param
	 * @return
	 * @author bin2107
	 * @date 2021. 4. 9
	 */
	public Collection<Contents> getBestItemForGoodsMain(Contents contents) {
		contents.setContentsLoc("SCM003");
		contents.setCateNo(null);
		Collection<Contents> contentsList = displayDao.getBestItemCategoryList(contents);
		Cate4Srch tempCate = new Cate4Srch();

		for (Contents data : contentsList) {
			tempCate.setContentsLoc(data.getContentsLoc());
			tempCate.setMaxRow(10);
			tempCate.setCustNo(TsfSession.isLogin() ? TsfSession.getInfo().getCustNo() : 0);
			tempCate.setCateNo(data.getCateNo());
			tempCate.setSiteCd(data.getSiteCd());
			tempCate.setCateGb(data.getCateGb());
			
			// 2021.08.31 FN_GET_BENEFIT_PRICE() 쿼리 null 처리
			tempCate.setFrontGb(TsfSession.getFrontGb());
			tempCate.setCustGb(TsfSession.getCustGb());
			
			Collection<Goods> cateGoodsList = goodsDao.getContentsCategoryGoodsListMain(tempCate);
			if(10 > cateGoodsList.size()){
				//TODO : 추천솔루션
				int cnt = 0;
				cnt = 10 - cateGoodsList.size();

				Collection<Eigeneai.Result> itemList = eigeneaiApi.getCategoryWeeklySellBestGoodsList(cnt, data.getCateNo());
				for(Eigeneai.Result tempData : itemList){
					Goods tempBest = new Goods();
					tempBest.setBrandGroupNm(tempData.getProduct().getBrandName());
					tempBest.setGoodsCd(tempData.getProduct().getItemId());
					tempBest.setGoodsFullNm(tempData.getProduct().getItemName());
					tempBest.setListPrice((int) tempData.getProduct().getOriginalPrice());
					tempBest.setCurrPrice((int) tempData.getProduct().getSalePrice());
					tempBest.setSysImgNm(tempData.getProduct().getItemImage());
					tempBest.setItemUrl(tempData.getProduct().getItemUrl());
					cateGoodsList.add(tempBest);
				}
			}

			data.setGoodsList(cateGoodsList);
		}

		return contentsList;
	}
	
	/**
	 * 몰메인 BESTITEM 목록
	 * @param
	 * @return
	 * @author jmh
	 * @date 2021. 8. 13
	 */
	public Collection<Contents> getBestItemForGoods(Contents contents) {
		contents.setContentsLoc("SCM003");
		contents.setCateNo(null);
		Collection<Contents> contentsList = displayDao.getBestItemCategoryList(contents);
		Cate4Srch tempCate = new Cate4Srch();

		for (Contents data : contentsList) {
			tempCate.setContentsLoc(data.getContentsLoc());
			tempCate.setMaxRow(10);
			tempCate.setCustNo(TsfSession.isLogin() ? TsfSession.getInfo().getCustNo() : 0);
			tempCate.setCateNo(data.getCateNo());
			tempCate.setSiteCd(data.getSiteCd());
			tempCate.setCateGb(data.getCateGb());
			Collection<Goods> cateGoodsList = goodsDao.getContentsCategoryGoodsList(tempCate);
			if(10 > cateGoodsList.size()){
				//TODO : 추천솔루션
				int cnt = 0;
				cnt = 10 - cateGoodsList.size();

				Collection<Eigeneai.Result> itemList = eigeneaiApi.getCategoryWeeklySellBestGoodsList(cnt, data.getCateNo());
				for(Eigeneai.Result tempData : itemList){
					Goods tempBest = new Goods();
					tempBest.setBrandGroupNm(tempData.getProduct().getBrandName());
					tempBest.setGoodsCd(tempData.getProduct().getItemId());
					tempBest.setGoodsFullNm(tempData.getProduct().getItemName());
					tempBest.setListPrice((int) tempData.getProduct().getOriginalPrice());
					tempBest.setCurrPrice((int) tempData.getProduct().getSalePrice());
					tempBest.setSysImgNm(tempData.getProduct().getItemImage());
					tempBest.setItemUrl(tempData.getProduct().getItemUrl());
					cateGoodsList.add(tempBest);
				}
			}

			data.setGoodsList(cateGoodsList);
		}

		return contentsList;
	}

	/**
	 * BEST ITEM 카테고리 목록
	 * @param
	 * @return
	 * @author bin2107
	 * @date 2021. 4. 13
	 */
	public Collection<Contents> getBestItemCategoryList(Contents contents) {
		return displayDao.getBestItemCategoryList(contents);
	}

	/**
	 * BEST ITEM 카테고리별 상품 리스트
	 * @param
	 * @return
	 * @author bin2107
	 * @date 2021. 4. 13
	 */
	public Collection<Goods> getBestItemCategoryGoodsList(Contents contents) {
		Cate4Srch cate4Srch = new Cate4Srch();
		// 로그인 유무 확인 (로그인이 되어 있지 않으면 regNo 를 0으로 장바구니에 저장한다.)
		Login login = new Login();
		if (TsfSession.isLogin()) {
			login = TsfSession.getInfo();
		} else {
			login.setCustNo(0);
		}

		String cateNo = String.valueOf(contents.getCateNo());
		Collection<Goods> goodsList = new ArrayList<>();
		if(!"".equals(cateNo) && !"null".equals(cateNo) && !cateNo.equals(null)){
			cate4Srch.setCustNo(login.getCustNo());
			cate4Srch.setPageGb(contents.getPageGb());
			cate4Srch.setContentsLoc(contents.getContentsLoc());
			cate4Srch.setPageable(contents.getPageable());
			cate4Srch.setPageNo(contents.getPageNo());
			cate4Srch.setPageSize(contents.getPageSize());
			cate4Srch.setPageUnit(contents.getPageUnit());
			cate4Srch.setCustNo(TsfSession.isLogin() ? TsfSession.getInfo().getCustNo() : 0);
			cate4Srch.setCate1No(contents.getCateNo());
			cate4Srch.setSiteCd(TscConstants.Site.STYLE24.value());
			cate4Srch.setCateGb("G032_101");

			log.info("getBestItemCategoryGoodsList cate4Srch:::::::::{}", cate4Srch);

			goodsList = goodsDao.getContentsCategoryGoodsList(cate4Srch);
			log.info("goodsList 1::::::{}",goodsList);
		}
/*
		if (goodsList.size() < 100) {
			// TODO 추천솔루션
			log.info("goodsList.size()::::::{}",goodsList.size());
			int cnt = 100 - goodsList.size();
			log.info("itemList cnt::::::{}",cnt);
			Collection<Eigeneai.Result> itemList = new ArrayList<>();
			if(!"".equals(cateNo) && !"null".equals(cateNo) && !cateNo.equals(null)){
				itemList = eigeneaiApi.getCategoryWeeklySellBestGoodsList(cnt, contents.getCateNo());
			}else{
				itemList = eigeneaiApi.getWeeklySellBestGoodsList(cnt);
			}

			log.info("itemList::::::{}",itemList);
			for(Eigeneai.Result tempData: itemList){
				Goods tempBest = new Goods();
				tempBest.setBrandGroupNm(tempData.getProduct().getBrandName());
				tempBest.setGoodsCd(tempData.getProduct().getItemId());
				tempBest.setMainColorCd("");
				tempBest.setGoodsNm(tempData.getProduct().getItemName());
				tempBest.setGoodsFullNm(tempData.getProduct().getItemName());
				tempBest.setListPrice((int) tempData.getProduct().getOriginalPrice());
				tempBest.setCurrPrice((int) tempData.getProduct().getSalePrice());
				
				// 2021.08.30 검색엔진데이타 https: 붙여서 넘어옴 처리 
				if (tempData.getProduct().getItemImage().indexOf("https:") > -1) {
					tempBest.setSysImgNm(tempData.getProduct().getItemImage().replaceAll("https://image.istyle24.com/Upload/ProductImage/",""));
				} else {
					tempBest.setSysImgNm(tempData.getProduct().getItemImage().replaceAll("//image.istyle24.com/Upload/ProductImage/",""));
				}
				
				tempBest.setItemUrl(tempData.getProduct().getItemUrl());
				goodsList.add(tempBest);
			}
			log.info("goodsList 2::::::{}",goodsList);
		}
*/
		return goodsList;
	}

	/**
	 * 상품리스트 카테고리별 필터 목록
	 * @param params - 검색엔진 정보
	 * @return
	 * @author gagamel
	 * @date 2021. 5. 3
	 */
	public Collection<Filter> getGoodsListCategoryFilterList(SearchEngine params) {
		params.setSiteCd(TscConstants.Site.STYLE24.value());
		if (StringUtils.isBlank(params.getFormalGb())) {
			params.setFormalGb("G009_10");
		}
		return displayDao.getGoodsListCategoryFilterList(params);
	}

	/**
	 * 검색키워드를 통한 검색상품리스트 카테고리별 필터 목록
	 * @param params - 검색엔진 정보
	 * @return
	 * @author gagamel
	 * @date 2021. 5. 3
	 */
	public Collection<Filter> getSearchGoodsListCategoryFilterList(SearchEngine params) {
		params.setSiteCd(TscConstants.Site.STYLE24.value());
		params.setCateGb(TsfConstants.CateGb.BYITEM.value());
		params.setFormalGb("G009_10");
		return displayDao.getSearchGoodsListCategoryFilterList(params);
	}

	/**
	 * 카테고리별 필터 목록
	 * @param filterList - 필터 목록
	 * @param filterGb - 필터 구분
	 * @return
	 * @author gagamel
	 * @date 2021. 5. 3
	 */
	public Collection<Filter> getCategoryFilterList(Collection<Filter> filterList, String filterGb) {
		Collection<Filter> resultList = new ArrayList<Filter>();
		int idx = 0;
		String maxPrice = "0";
		String minPrice = "0";
		String lastStr = "";
		ArrayList<String> seasonSortArr = new ArrayList<>();
		Collection<Filter> seasonArr = new ArrayList<Filter>();
		for (Filter filter : filterList) {
			if (!"".equals(filter.getFilterCd()) && !filter.getFilterCd().equals(null)) {
				if (filter.getFilterGb().equals("PRICE")) {
					if (idx == 0) {
						minPrice = filter.getFilterCd();
					} else {
						maxPrice = filter.getFilterCd();
					}
					idx++;
				} else {
					if (filter.getFilterGb().equals(filterGb)) {
						if ("BENEFIT".equals(filterGb)) {
							if (!filter.getFilterCd().equals("40")) {
								resultList.add(filter);
							}
						}else if ("SIZE".equals(filterGb)) {
							// 자사 사이즈만 표시 
							if(null != filter.getFilterCd()) {
								lastStr = filter.getFilterCd().substring(filter.getFilterCd().length()-1, filter.getFilterCd().length()); 
								if("Y".equals(lastStr)) {
									resultList.add(filter);
								}
							}
						}else if ("SEASON".equals(filterGb)) {
							// 시즌정보 정렬 
							seasonSortArr.add(filter.getFilterCd());
							seasonArr.add(filter);
						} else {
							resultList.add(filter);
						}
					}
				}
			}
		}

		if (filterGb.equals("PRICE")) {
			int sliceVal = (Integer.parseInt(maxPrice) - Integer.parseInt(minPrice)) / 5;
			int tempPrice = 0;
			Filter temp = new Filter();
			Collection<Filter> priceList = new ArrayList<Filter>();
			for (int i = 0; i < 6; i++) {
				tempPrice = 0;
				temp = new Filter();
				temp.setFilterGb("PRICE");
				if (i == 0) {
//					tempPrice = (int)Math.floor((Integer.parseInt(minPrice)) / 1000) * 1000 ;
					tempPrice = (int)Math.floor((Integer.parseInt(minPrice)));
					temp.setFilterCd(String.valueOf(tempPrice));
					temp.setFilterNm(String.valueOf(tempPrice));
				} else if (i == 5) {
					tempPrice = (int)Math.floor((Integer.parseInt(maxPrice)));
					temp.setFilterCd(String.valueOf(tempPrice));
					temp.setFilterNm(String.valueOf(tempPrice));
				} else {
					tempPrice = (int)Math.floor((Integer.parseInt(minPrice) + sliceVal * i) / 1000) * 1000;
					temp.setFilterCd(String.valueOf(tempPrice));
					temp.setFilterNm(String.valueOf(tempPrice));
				}
				priceList.add(temp);
			}
			resultList.addAll(priceList);
		}
		
		// 시즌정렬
		if (filterGb.equals("SEASON")) {
			Collections.sort(seasonSortArr);
			for (String filterCd : seasonSortArr) {
				for (Filter item : seasonArr) {
					if( filterCd.equals(item.getFilterCd())){
						resultList.add(item);
						break;
					}
				}
			}
		}
		
		return resultList;
	}

	/**
	 * 상품카테고리 필터 목록
	 * @param
	 * @return
	 * @author bin2107
	 * @date 2021. 4. 5
	 */
	public Collection<GoodsSearch> getCategoryFilter(Cate4Srch cate4Srch, String filterGb) {
		cate4Srch.setFilterGb(filterGb);
		return displayDao.getCategoryFilter(cate4Srch);
	}

	/**
	 * 상품카테고리 필터 혜택 목록
	 * @param
	 * @return
	 * @author bin2107
	 * @date 2021. 4. 7
	 */
	public Collection<GoodsSearch> getCategoryFilterBenefit(Cate4Srch cate4Srch) {
		return displayDao.getCategoryFilterBenefit(cate4Srch);
	}

	/**
	 * 전체 브랜드 이미지 리스트
	 * @param
	 * @return
	 * @author bin2107
	 * @since 2021. 4. 28
	 */
	public Collection<BrandGroup> getBrandImgList(BrandGroup brandGroup) {
		return displayDao.getBrandImgList(brandGroup);
	}

	/**
	 * 전체 브랜드
	 * @param
	 * @return
	 * @author bin2107
	 * @since 2021. 4. 12
	 */
	public Collection<BrandGroup> getAllBrandList(BrandGroup brandGroup) {
		return displayDao.getAllBrandList(brandGroup);
	}

	/**
	 * GNB TAB 기획전배너 리스트
	 * @param Contents
	 * @return
	 * @author bin2107
	 * @since 2021. 4. 14
	 */
	public Collection<Contents> getGnbTabBannerlist(Contents contenst) {
		Collection<Contents> contentsList = displayDao.getGnbTabBannerList(contenst);
		return contentsList;
	}

	/**
	 * 키워드를 이용한 전체 카테고리 목록
	 * @param keyword - 상품검색키워드
	 * @return
	 * @author gagamel
	 * @since 2021. 5. 6
	 */
	public Collection<Cate1> getKeywordCategoryList(String keyword) {
		Cate4Srch cate = new Cate4Srch();
		cate.setSiteCd(TscConstants.Site.STYLE24.value()); // 사이트코드
		cate.setCateGb(TsfConstants.CateGb.BYITEM.value()); // 카테고리구분
		cate.setCateType(TsfConstants.CateType.GOODS.value()); // 상품분류카테고리
		cate.setSoldoutGoodsDispYn(envsetService.getSoldoutGoodsDisplayYn(TscConstants.Site.STYLE24.value()));
		cate.setKeyword(keyword);
		return this.getAllCategoryList(cate);
	}

	public int getContentsCategoryGoodsCount(Contents contents) {
		Cate4Srch cate4Srch = new Cate4Srch();
		// 로그인 유무 확인 (로그인이 되어 있지 않으면 regNo 를 0으로 장바구니에 저장한다.)
		Login login = new Login();
		if (TsfSession.isLogin()) {
			login = TsfSession.getInfo();
		} else {
			login.setCustNo(0);
		}

		cate4Srch.setCustNo(login.getCustNo());
		cate4Srch.setPageGb(contents.getPageGb());
		cate4Srch.setContentsLoc(contents.getContentsLoc());
		cate4Srch.setMaxRow(100);
		cate4Srch.setCustNo(TsfSession.isLogin() ? TsfSession.getInfo().getCustNo() : 0);
		cate4Srch.setCate1No(contents.getCateNo());
		cate4Srch.setSiteCd(TscConstants.Site.STYLE24.value());
		cate4Srch.setCateGb("G032_101");
		return goodsDao.getContentsCategoryGoodsCount(cate4Srch);
	}

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

	/**
	 * 스플래시 이미지 가져오기
	 * @param siteCd - 사이트코드
	 * @return
	 * @author gagamel
	 * @since 2021. 7. 5
	 */
	@Cacheable(value = "splashimg")
	public String getSplashImageUrl() {
		String splashImg = displayDao.getSplashImage();
		if (StringUtils.isBlank(splashImg)) {
			return "";
		}
		return "http:" + GagaFileUtil.getConcatenationPath(env.getProperty("upload.image.view"), "splashimg", splashImg);
	}

	/**
	 * 몰메인 신상풍 목록
	 * @param
	 * @return
	 * @author bin2107
	 * @date 2021. 7. 13
	 */
	public Collection<Contents> getMainNewGoodsList(Contents contents) {
		Collection<Contents> contentsList = displayDao.getBestItemCategoryList(contents);
		Cate4Srch tempCate = new Cate4Srch();

		for (Contents data : contentsList) {
			tempCate.setContentsLoc(data.getContentsLoc());
			tempCate.setMaxRow(20);
			tempCate.setCustNo(TsfSession.isLogin() ? TsfSession.getInfo().getCustNo() : 0);
			tempCate.setSiteCd(data.getSiteCd());
			tempCate.setCateGb(data.getCateGb());
			
			// 2021.09.29 신상품조회시 frontGb, custGb 누락 추가
			tempCate.setFrontGb(TsfSession.getFrontGb());
			tempCate.setCustGb(TsfSession.getCustGb());
			
			data.setGoodsList(goodsDao.getContentsCategoryNewGoodsList(tempCate));
		}

		return contentsList;
	}

	/**
	 * 몰메인 신상품 목록
	 * @param
	 * @return
	 * @author bin2107
	 * @date 2021. 7. 13
	 */
	public Collection<Goods> getContentsCategoryNewGoodsList(Cate4Srch cate4Srch){
		return goodsDao.getContentsCategoryNewGoodsList(cate4Srch);
	}

	/**
	 * 몰메인 브랜드픽 타이틀 브랜드 목록
	 * @param contents - 컨텐츠 정보
	 * @return
	 * @author bin2107
	 * @date 2021. 7. 23
	 */
	public Collection<Contents> getBrandpickTitleBrandList(Contents contents) {
		return displayDao.getBrandpickTitleBrandList(contents);
	}
	
}
