package com.style24.batch.biz.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.style24.batch.biz.dao.TsbGoodsDao;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.biz.service.TscEnvsetService;
import com.style24.core.biz.thirdparty.SafetyKoreaApi;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Coupon;
import com.style24.persistence.domain.Goods;
import com.style24.persistence.domain.GoodsIfIncomelot;
import com.style24.persistence.domain.GoodsIfIncomelotitem;
import com.style24.persistence.domain.GoodsIfMeasurement;
import com.style24.persistence.domain.GoodsSafeNo;
import com.style24.persistence.domain.GoodsSummary;
import com.style24.persistence.domain.IfBrand;
import com.style24.persistence.domain.IfProduct;
import com.style24.persistence.domain.IfProvider;
import com.style24.persistence.domain.Option;
import com.style24.persistence.domain.ReinboundInform;
import com.style24.persistence.domain.WmsStock;

import io.netty.util.internal.StringUtil;

import lombok.extern.slf4j.Slf4j;

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

/**
 * 상품 Service
 *
 * @author eskim
 * @since 2020. 11. 27
 */
@Service
@Slf4j
public class TsbGoodsService {

	@Autowired
	private TsbGoodsDao goodsDao;

	@Autowired
	private TscEnvsetService envsetService;

	@Autowired
	private SafetyKoreaApi safetyKoreaApi;

	@Autowired
	private TsbWmsGoodsService wmsGoodsService;

	/**
	 * 상품 타이틀예약 작업
	 *
	 * @return
	 * @author eskim
	 * @since 2020. 11. 27
	 */
	@Transactional("shopTxnManager")
	public void saveGoodsTnmRsvt() {
		Goods goods = new Goods();

		// 1.예약기간 만료 상품 타이틀 작업
		// 1.1 이력쌓기
		goods.setProcJob("GTE"); // 상품타이틀예약 만료건 변경
		goods.setRegNo(TsbConstants.REG_NO);
		goods.setUpdNo(TsbConstants.REG_NO);
		goodsDao.createGoodsHst(goods);
		// 1.2 삼품타이틀 초기화
		goodsDao.updateGoodTnmInit();

		// 2.상품예약 종료처리
		goodsDao.updateGoodsTnmRes();

		// 3.예약도래 상품 타이틀 작업
		// 3.1 이력쌓기
		goods.setProcJob("GTS"); // 상품타이틀예약 예약 변경
		goodsDao.createGoodsHst(goods);
		// 3.2 상품타이틀 변경
		goodsDao.updateGoodsTnm();
	}

	/**
	 * 상품 가격예약 작업
	 *
	 * @return
	 * @author eskim
	 * @since 2020. 11. 27
	 */
	@Transactional("shopTxnManager")
	public void saveGoodsPriceRsvt() {
		Goods goods = new Goods();

		// 1.예약기간 만료 상품 가격 작업
		// 1.1 이력쌓기
		goods.setProcJob("GPE"); // 상품 가격예약 만료건 변경
		goods.setRegNo(TsbConstants.REG_NO);
		goods.setUpdNo(TsbConstants.REG_NO);
		goodsDao.createGoodsHst(goods);
		// 1.2 삼품 가격 원복
		goodsDao.updateGoodPriceInit();

		// 2.상품예약 종료처리
		goodsDao.updateGoodsPriceRes();

		// 3.예약도래 상품 가격 작업
		// 3.1 이력쌓기
		goods.setProcJob("GPS"); // 상품 예약 가격 적용
		goodsDao.createGoodsHst(goods);
		// 3.2 상품 가격 변경
		goodsDao.updateGoodsPrice();
	}

	/**
	 * 상품 검색어 작업
	 *
	 * @return
	 * @author eskim
	 * @since 2020. 11. 30
	 */
	@Transactional("shopTxnManager")
	public void updateGoodsSnm() {

		// 사용자 검색어 없는 상품 일괄 작업
		goodsDao.updateGoodsSnmByGoodsSnm1Null();
		// 사용자 검색어 있는 상품 작업
		Collection<Goods> goodsList = goodsDao.getGoodsByGooodsSnm1List();
		if (goodsList != null && !goodsList.isEmpty()) {
			for (Goods goods : goodsList) {

				// 사용자 검색어를 검색어에 적용
				String goodsSnm = goods.getGoodsSnm();
				String[] arrGoodsSnm = goods.getGoodsSnm1().split(";");
				StringBuilder tempGoodsSnm = new StringBuilder();
				for (String loopGoodsSnm : arrGoodsSnm) {
					if (goodsSnm.toUpperCase().indexOf(loopGoodsSnm.toUpperCase()) <= -1) {
						tempGoodsSnm.append(loopGoodsSnm).append(";");
					}
				}
				goods.setGoodsSnm(tempGoodsSnm.toString() + goodsSnm);
				goodsDao.updateGoodsSnm(goods);
			}
		}
	}

	/**
	 * 상품 즉시할인가 생성
	 *
	 * @param
	 * @return
	 * @author eskim
	 * @throws IOException
	 * @since 2020. 12. 02
	 */
	@Transactional("shopTxnManager")
	public void createGoodsBenefitPrice() throws IOException {

		// TB_GOODS_BENEFIT_PRICE_TEMP 삭제
		goodsDao.deleteGoodsBenefitPriceTemp();

		Coupon coupon = new Coupon();
		int floorUnit = envsetService.getPayUnit(TscConstants.Site.STYLE24.value()); // 가격 원단위 관리
		coupon.setPointUnit(floorUnit);
		/*
		 * 쿠폰적용순선 전체->일반->임직원
		 *
		 * 테이블(TB_GOODS_BENEFIT_PRICE)은 전체기준으로만 생성
		 */
		coupon.setUsableCustGb("G100_10"); // 전체 - 일반
		// TB_GOODS_BENEFIT_PRICE_TEMP 생성 - 상품쿠폰 전체
		goodsDao.crteateGoodsBenefitPriceTemp(coupon);

//		// TB_GOODS_BENEFIT_PRICE_TEMP 생성 - 상품쿠폰 일반
//		coupon.setUsableCustGb("G100_10"); // 일반
//		goodsDao.updateGoodsBenefitPriceTemp(coupon);

//		// TB_GOODS_BENEFIT_PRICE_TEMP 생성 - 상품쿠폰 임직원
//		coupon.setUsableCustGb("G100_20"); // 임직원
//		goodsDao.updateGoodsBenefitPriceTemp(coupon);

		// TB_GOODS_BENEFIT_PRICE 삭제
		goodsDao.deleteGoodsBenefitPrice();

		// TB_GOODS_BENEFIT_PRICE_TEMP => TB_GOODS_BENEFIT_PRICE
		goodsDao.createGoodsBenefitPrice();

	}

	/**
	 * 연관상품보기 스코어링 작업
	 *
	 * @return
	 * @author eskim
	 * @since 2020. 12. 04
	 */
	@Transactional("shopTxnManager")
	public void createGoodsRelateScore(String workDt, String work7agoDt) {

		// 작업일 데이터 삭제
		goodsDao.deleteGoodsRelate(workDt);
		// 작업일 데이터 생성
		goodsDao.createGoodsRelate(workDt);
		// 7일전 데이터 삭제
		goodsDao.deleteGoodsRelate(work7agoDt);

	}

	/**
	 * 전시상품 통계정보작업
	 *
	 * @return
	 * @author eskim
	 * @since 2020. 12. 04
	 */
	@Transactional("shopTxnManager")
	public void createGoodsSummary() {

		GoodsSummary goodsSummary = new GoodsSummary();

		goodsDao.deleteGoodsSummaryTemp();

		// 상품 판매수량-총판매수량
		goodsSummary.setColNm1("SELL_TOT_QTY");
		goodsSummary.setFromDay("");
		goodsDao.createGoodsSummaryBySale(goodsSummary);
		// 상품 판매수량-주간판매수량
		goodsSummary.setColNm1("SELL_WEEK_QTY");
		goodsSummary.setFromDay("7");
		goodsDao.createGoodsSummaryBySale(goodsSummary);
		// 상품 판매수량-월간판매수량
		goodsSummary.setColNm1("SELL_MONTH_QTY");
		goodsSummary.setFromDay("30");
		goodsDao.createGoodsSummaryBySale(goodsSummary);
		// 상품 판매수량-전일판매수량
		goodsSummary.setColNm1("SELL_YDAY_QTY");
		goodsSummary.setFromDay("1");
		goodsDao.createGoodsSummaryBySale(goodsSummary);
		// 상품 판매수량-15일간판매수량
		goodsSummary.setColNm1("SELL_DAY15_QTY");
		goodsSummary.setFromDay("15");
		goodsDao.createGoodsSummaryBySale(goodsSummary);
		// 상품 조회수-금주조회건수
		goodsSummary.setColNm1("READ_NWEEK_CNT");
		goodsSummary.setFromDay("1");
		goodsDao.createGoodsSummaryByView(goodsSummary);
		// 상품 조회수-금주조회건수
		goodsSummary.setColNm1("READ_PWEEK_CNT");
		goodsSummary.setFromDay("8");
		goodsDao.createGoodsSummaryByView(goodsSummary);
		// 상품평-상품평등록건수
		goodsSummary.setColNm1("REVIEW_REG_CNT");
		goodsSummary.setFromDay("");
		goodsSummary.setPhotoYn("");
		goodsDao.createGoodsSummaryByReviewCount(goodsSummary);
		// 상품평-텍스트상품평등록건수
		goodsSummary.setColNm1("RREVIEW_REG_CNT");
		goodsSummary.setFromDay("");
		goodsSummary.setPhotoYn("N");
		goodsDao.createGoodsSummaryByReviewCount(goodsSummary);
		// 상품평-포토상품평등록건수
		goodsSummary.setColNm1("PREVIEW_REG_CNT");
		goodsSummary.setFromDay("");
		goodsSummary.setPhotoYn("Y");
		goodsDao.createGoodsSummaryByReviewCount(goodsSummary);
		// 상품평-만족도점수
		goodsSummary.setColNm1("SCORE");
		goodsSummary.setReviewColNm("SCORE");
		//	goodsSummary.setPhotoYn("");
		goodsDao.createGoodsSummaryByReviewSum(goodsSummary);
		// 상품평-사이즈점수
		goodsSummary.setColNm1("SCORE_SIZE1");
		goodsSummary.setColNm2("SCORE_SIZE2");
		goodsSummary.setColNm3("SCORE_SIZE3");
		goodsSummary.setReviewColNm("SCORE_SIZE");
		goodsSummary.setPhotoYn("");
		goodsDao.createGoodsSummaryByReviewScore(goodsSummary);
		// 상품평-색상점수
		goodsSummary.setColNm1("SCORE_COLOR1");
		goodsSummary.setColNm2("SCORE_COLOR2");
		goodsSummary.setColNm3("SCORE_COLOR3");
		goodsSummary.setReviewColNm("SCORE_COLOR");
		goodsSummary.setPhotoYn("");
		goodsDao.createGoodsSummaryByReviewScore(goodsSummary);
		// 상품평-핏점수
		goodsSummary.setColNm1("SCORE_FIT1");
		goodsSummary.setColNm2("SCORE_FIT2");
		goodsSummary.setColNm3("SCORE_FIT3");
		goodsSummary.setReviewColNm("SCORE_FIT");
		goodsSummary.setPhotoYn("");
		goodsDao.createGoodsSummaryByReviewScore(goodsSummary);
		// 상품평-두께점수
		goodsSummary.setColNm1("SCORE_THICK1");
		goodsSummary.setColNm2("SCORE_THICK2");
		goodsSummary.setColNm3("SCORE_THICK3");
		goodsSummary.setReviewColNm("SCORE_THICK");
		goodsSummary.setPhotoYn("");
		goodsDao.createGoodsSummaryByReviewScore(goodsSummary);
		// 상품평-무게점수
		goodsSummary.setColNm1("SCORE_WEIGHT1");
		goodsSummary.setColNm2("SCORE_WEIGHT2");
		goodsSummary.setColNm3("SCORE_WEIGHT3");
		goodsSummary.setReviewColNm("SCORE_WEIGHT");
		goodsSummary.setPhotoYn("");
		goodsDao.createGoodsSummaryByReviewScore(goodsSummary);
		// 상품평-볼넓이점수
		goodsSummary.setColNm1("SCORE_BALL1");
		goodsSummary.setColNm2("SCORE_BALL2");
		goodsSummary.setColNm3("SCORE_BALL3");
		goodsSummary.setReviewColNm("SCORE_BALL");
		goodsSummary.setPhotoYn("");
		goodsDao.createGoodsSummaryByReviewScore(goodsSummary);
		// 상품평-상품문의
		goodsDao.createGoodsSummaryByCounsel(goodsSummary);

		// 딜전용 상품평등록건수
		goodsDao.createDealGoodsSummaryByReviewCount(goodsSummary);

		// 딜전용 상품건수
		goodsDao.createDealGoodsSummaryByCounsel(goodsSummary);

		// 전시상품 통계 저장
		goodsDao.saveGoodsSummary();

	}

	/**
	 * 영유아상품 인증번호 수신
	 *
	 * @return
	 * @author eskim
	 * @since 2020. 12. 04
	 */
	@Transactional("shopTxnManager")
	public void saveGoodsSafeNo() {

//		// 1. 대상 상품 조회
//		Collection<GoodsSafeNo> goodsSafeNoList = goodsDao.getGoodsSafeNoList();
//		// 2. 인증테이블 적용
//		for (GoodsSafeNo goodsSafeNo : goodsSafeNoList) {
//			try {
//				GagaMap result = safetyKoreaApi.getKoreaCertifyNo(goodsSafeNo.getGoodsNum()); // 품번으로 처리
//				if (result != null || !StringUtil.isNullOrEmpty(result.get("certNum").toString())) {
//					goodsSafeNo.setCertDt(result.get("certDt").toString());
//					goodsSafeNo.setCertNum(result.get("certNum").toString());
//					goodsSafeNo.setCertDiv(result.get("certDiv").toString());
//					goodsSafeNo.setCertState(result.get("certState").toString());
//					goodsSafeNo.setRegNo(TsbConstants.REG_NO);
//					goodsSafeNo.setUpdNo(TsbConstants.REG_NO);
//					goodsDao.saveGoodsSafeNo(goodsSafeNo);
//				}
//			} catch (Exception e) {
//				// do nothing
//			}
//		}

		// 1. 대상 상품 조회 - 인증번호가 있는 상품 조회(tb_goods_safe 변경)
		Collection<GoodsSafeNo> goodsSafeNoList = goodsDao.getGoodsSafeNoList();
		//log.info("[goodsSafeNoList] ={}", goodsSafeNoList.size());
		// 2. 인증테이블 적용
		for (GoodsSafeNo goodsSafeNo : goodsSafeNoList) {
			try {
				GagaMap result = safetyKoreaApi.getKoreaCertifyDetail(goodsSafeNo.getCertNum());
				if (result != null && !StringUtil.isNullOrEmpty(result.get("certNum").toString())) {
					goodsSafeNo.setCertDt(result.get("certDt").toString());
					goodsSafeNo.setCertNum(result.get("certNum").toString());
					goodsSafeNo.setCertDiv(result.get("certDiv").toString());
					goodsSafeNo.setCertState(result.get("certState").toString());
					goodsSafeNo.setCertOrganName(result.get("certOrganName").toString());
					goodsSafeNo.setRegNo(TsbConstants.REG_NO);
					goodsSafeNo.setUpdNo(TsbConstants.REG_NO);
					goodsDao.saveGoodsSafeNo(goodsSafeNo);
				} else {
					goodsSafeNo.setUpdNo(TsbConstants.REG_NO);
					goodsDao.updateGoodsSafeNo(goodsSafeNo);
				}
			} catch (Exception e) {
				// do nothing
				goodsSafeNo.setUpdNo(TsbConstants.REG_NO);
				goodsDao.updateGoodsSafeNo(goodsSafeNo);
			}
		}
	}

	/**
	 * WMS 실측사이즈 연용
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 01
	 */
	@Transactional("shopTxnManager")
	public void saveGoodsWmsMeasurement(String jobdate) {

		// wms 대상건 조회 (등록, 수정건 조회)
		Collection<GoodsIfMeasurement> goodsMeasurementList = wmsGoodsService.getWmsMeasurementList(jobdate);

		for (GoodsIfMeasurement goodsMeasurement : goodsMeasurementList) {
			goodsDao.saveGoodsIfMeasurement(goodsMeasurement);
		}
	}

	/**
	 * WMS 입고정보 연동
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 01
	 */
	@Transactional("shopTxnManager")
	public void saveGoodsWmsIncomelot(String jobdate) {

		// wms 입고목록 조회 (등록, 수정건 조회)
		Collection<GoodsIfIncomelot> goodsIfIncomelotList = wmsGoodsService.getWmsIncomelotList(jobdate);

		for (GoodsIfIncomelot goodsIfIncomelot : goodsIfIncomelotList) {
			goodsDao.saveGoodsIfIncomelot(goodsIfIncomelot);
		}

		// wms 입고상품목록 조회 (등록, 수정건 조회)
		Collection<GoodsIfIncomelotitem> goodsIfIncomelotitemList = wmsGoodsService.getWmsIncomelotitemList(jobdate);

		for (GoodsIfIncomelotitem goodsIfIncomelotitem : goodsIfIncomelotitemList) {
			goodsDao.saveGoodsIfIncomelotitem(goodsIfIncomelotitem);
		}
	}

	/**
	 * 온라인 입고 상품 처리
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 01
	 */
	@Transactional("shopTxnManager")
	public void saveWmsGoods(String jobdate) {
		GoodsIfIncomelot goodsIfIncomelot = new GoodsIfIncomelot();
		goodsIfIncomelot.setRegNo(TsbConstants.REG_NO);
		goodsIfIncomelot.setUpdNo(TsbConstants.REG_NO);
		goodsIfIncomelot.setJobdate(jobdate);
		goodsDao.saveWmsGoods(goodsIfIncomelot);
	}

	/**
	 * 업체정보 조회
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 01
	 */
	public Collection<IfProvider> getSupplyCompanyList() {
		return goodsDao.getSupplyCompanyList();
	}

	/**
	 * 브랜드정보 조회
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 01
	 */
	public Collection<IfBrand> getBrandList() {
		return goodsDao.getBrandList();
	}

	/**
	 * 브랜드 업체 정보 조회
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 26
	 */
	public Collection<IfBrand> getBrandProviderXrefList() {
		return goodsDao.getBrandProviderXrefList();
	}

	/**
	 * 상품 정보 조회
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 06
	 */
	public Collection<IfProduct> getGoodsList(String jobdate) {
		return goodsDao.getGoodsList(jobdate);
	}

	/**
	 * wms상품재고이력 생성
	 *
	 * @param jobdate
	 * @return
	 * @author eskim
	 * @since 2021. 01. 07
	 */
//	@Transactional("shopTxnManager")
//	public void createWmsProductSkuHst(String jobdate) {
//
//		// TB_IF_PRODUCTSKU  작업
//		goodsDao.deleteIfProductSku();
//
//		// TB_IF_PRODUCTSKU  생성
//		Collection<IfProductSku> wmsIfProductSukList = wmsGoodsService.getWmsIfProductSukList();
//		String byrow = "Y";
//		if ("Y".equals(byrow)) {
//			for (IfProductSku ifProductSku : wmsIfProductSukList) {
//				goodsDao.createIfProductSkuByRow(ifProductSku);
//			}
//		} else {
//
//			List<IfProductSku> productSukList = new ArrayList<IfProductSku>();
//
//			GagaMap dataMap = new GagaMap();
//			int index = 0;
//			for (IfProductSku ifProductSku : wmsIfProductSukList) {
//
//				productSukList.add(ifProductSku);
//				index++;
//				if (index % 200 == 0 || wmsIfProductSukList.size() == index) {
//					dataMap.set("productSukList", productSukList);
//					goodsDao.createIfProductSku(dataMap);
//					productSukList.clear();
//				}
//			}
//
//		}
//
//		// TB_IF_PRODUCTSKU_HST 2달 전 데이터 삭제
//		String delYyyymmdd = GagaDateUtil.getOffsetMonth(-2, "yyyyMMdd") + "000000";  //년월일시분초
//		goodsDao.deleteWmsProductSkuHst(delYyyymmdd);
//		// TB_IF_PRODUCTSKU_HST 생성
//		goodsDao.createWmsProductSkuHst(jobdate);
//
//	}

	/**
	 * wms상품재고이력 생성
	 *
	 * @param jobdate
	 * @return
	 * @author eskim
	 * @since 2021. 01. 07
	 */
	@Transactional("shopTxnManager")
	public void createWmsStockHst(String jobdate) {

		// TB_WMS_STOCK_HST 2달 전 데이터 삭제
		String delYyyymmdd = GagaDateUtil.getOffsetMonth(-2, "yyyyMMdd") + "000000";  //년월일시분초
		goodsDao.deleteWmsStockHst(delYyyymmdd);
		// TB_WMS_STOCK_HST 생성
		goodsDao.createWmsStockHst(jobdate);
	}

	/**
	 * shop 상품재고 생성
	 *
	 * @param jobdate
	 * @return
	 * @author eskim
	 * @since 2021. 05. 27
	 */
	@Transactional("shopTxnManager")
	public void createShopStock(String jobdate) {

		// TB_SHOP_STOCK 작업
		goodsDao.deleteShopStock();
		int cnt = goodsDao.createShopStock();

		// TB_SHOP_STOCK_HST 2달 전 데이터 삭제
		String delYyyymmdd = GagaDateUtil.getOffsetMonth(-2, "yyyyMMdd") + "000000";  //년월일시분초
		goodsDao.deleteShopStockHst(delYyyymmdd);
		// TB_SHOP_STOCK_HST 생성
		if (cnt > 0) {
			goodsDao.createShopStockHst(jobdate);
		}
	}

	/**
	 * WMS 임시 테이블에서 실테이블로 이관
	 *
	 * @throws Exception
	 * @author eskim
	 * @since 2021. 01. 07
	 */
	@Transactional("shopTxnManager")
	public void saveGoodsStock() throws Exception {

		// wms 재고 임시 테이블(TB_OPTION_SYNC_TMP) 삭제
		goodsDao.deleteTbOptionSyncTemp();
		// wms 재고 임시 테이블(TB_OPTION_SYNC_TMP) 적용 - 출고처별 판매비율 적용
		goodsDao.createTbOptionSyncTemp();
//		// 매장재고 - 출고처별 판매비율 적용
		goodsDao.saveTbOptionSyncTemp();

		// 입점 상품 재고 동기화 
		updateOptionByOfSellQty();

		// TB_OPTION = > TB_OPTION_SYNC 적용
		goodsDao.deleteGoodsOptionSync();
		goodsDao.createGoodsOptionSync();  // TB_OPTION_SYNC.CURR_STOCK_QTY, STORE_STOCK_QTY, STORE_MAX_QTY 초기화

		// TB_OPTION_SYNC_TMP => TB_OPTION_SYNC 변경적용
		goodsDao.saveGoodsOptionSyncSync();

		// WMS, 매장별 최대 주문수량 적용  == > 작업합시다.
		//TB_SHOP_STOCK과 TB_OPTION_SYNC.CURR_STOCK_QTY 비교하여 STORE_MAX_QTY에 적용
		goodsDao.updateGoodsOptionSyncSync();

		// TB_OPTION_SYNC => TB_OPTION 적용
		goodsDao.saveGoodsOptionNew();

		// 자사상품중 주문상세상태가 입금대기, 결제완료, 교환대기 건은 TB_SELL_QTY 생성
		createSellQtySelfGoods();

	}

	/**
	 * 입점상품 SELL_QTY 만큼 재고 차감
	 *
	 * @param
	 * @return
	 * @author eskim
	 * @since 2021. 01. 08
	 */
	private void updateOptionByOfSellQty() {
		Collection<Option> goodsSelfNoStockList = goodsDao.getGoodsSelfNoStockList();
		if (goodsSelfNoStockList != null && !goodsSelfNoStockList.isEmpty()) {

			for (Option option : goodsSelfNoStockList) {

				option.setRegNo(TsbConstants.REG_NO);
				option.setUpdNo(TsbConstants.REG_NO);
				// 이력생성
				goodsDao.createStockHst(option);

				// 재고 변경
				goodsDao.updateStock(option);
			}
		}
	}

	/**
	 * 자사상품, TB_SELL_QTY 생성
	 *
	 * @param
	 * @return
	 * @author eskim
	 * @since 2021. 01. 08
	 */
	private void createSellQtySelfGoods() {

		// TB_SELL_QTY_TEMP 삭제
		goodsDao.deleteTtbSellQtyTemp();

		// TB_SELL_QTY_TEMP 생성
		goodsDao.createTtbSellQtyTemp();

		// 초기화
		goodsDao.deleteSellQty();

		// TB_SELL_QTY_TEMP => TB_SELL_QTY 생성
		goodsDao.createTtbSellQty();

		// TB_ORDER_DETAIL => TB_SELL_QTY 생성
		goodsDao.createTtbSellQtyByOrderDetail();

	}

	/**
	 * 상품 전시재고 생성
	 *
	 * @author eskim
	 * @since 2020. 01. 06
	 */
	@Transactional("shopTxnManager")
	public void saveGoodsDispStock() {
		goodsDao.deleteGoodsDispStock();
		goodsDao.createGoodsDispStock();
	}

//	/**
//	 * TB_IF_PRODUCTSKU  삭제
//	 *
//	 * @author eskim
//	 * @since 2020. 01. 11
//	 */
//	@Transactional("shopTxnManager")
//	public void deleteIfProductSku() {
//		goodsDao.deleteIfProductSku();
//	}
//
//	/**
//	 * TB_IF_PRODUCTSKU 생성(by row )
//	 *
//	 * @author eskim
//	 * @since 2020. 01. 11
//	 */
//	@Transactional("shopTxnManager")
//	public void createIfProductSkuByRow(IfProductSku ifProductSku) {
//		goodsDao.createIfProductSkuByRow(ifProductSku);
//	}
//
//	/**
//	 * TB_IF_PRODUCTSKU 생성
//	 *
//	 * @author eskim
//	 * @since 2020. 01. 11
//	 */
//	@Transactional("shopTxnManager")
//	public void createIfProductSku(GagaMap dataMap) {
//		goodsDao.createIfProductSku(dataMap);
//	}
//
//	/**
//	 * TB_IF_PRODUCTSKU_HST 저장
//	 *
//	 * @author eskim
//	 * @since 2021. 01. 11
//	 */
//	@Transactional("shopTxnManager")
//	public void createIfProductSku(String jobdate) {
//		goodsDao.createWmsProductSkuHst(jobdate);
//	}

	/**
	 * TB_WMS_STOCK  삭제
	 *
	 * @author eskim
	 * @since 2020. 01. 11
	 */
	@Transactional("shopTxnManager")
	public void deleteWmsStock() {
		goodsDao.deleteWmsStock();
	}

	/**
	 * TB_WMS_STOCK 생성(by row )
	 *
	 * @author eskim
	 * @since 2020. 01. 11
	 */
	@Transactional("shopTxnManager")
	public void createWmsStockByRow(WmsStock wmsStock) {
		goodsDao.createWmsStockByRow(wmsStock);
	}

	/**
	 * TB_WMS_STOCK 생성
	 *
	 * @author eskim
	 * @since 2020. 01. 11
	 */
	@Transactional("shopTxnManager")
	public void createWmsStock(GagaMap dataMap) {
		goodsDao.createWmsStock(dataMap);
	}

	/**
	 * TB_WMS_STOCK_HST 삭제
	 *
	 * @author eskim
	 * @since 2021. 01. 11
	 */
	@Transactional("shopTxnManager")
	public void deleteWmsStockHst(String jobdate) {
		goodsDao.deleteWmsStockHst(jobdate);
	}

	/**
	 * 입고상품중 온라인 옵션이 없는 상품옵션 추가
	 *
	 * @author eskim
	 * @since 2021. 01. 18
	 */
	@Transactional("shopTxnManager")
	public void createGoodsOptionList(String jobdate) {
		goodsDao.createGoodsOptionList(jobdate);
	}

	/**
	 * 상품혜택임시 생성
	 * @return 생성건수
	 * @author gagamel
	 * @since 2021. 4. 7
	 */
	@Transactional("shopTxnManager")
	public int createTemporaryGoodsBenefit() {
		// 상품혜택임시 테이블 truncate
		goodsDao.truncateTemporaryGoodsBenefit();

		int totCnt = 0;
		int cnt = 0;
		// 쿼리 튜닝중
		cnt = goodsDao.createTemporaryGoodsBenefitAllCouponDiscount();
		totCnt += cnt;

		cnt = goodsDao.createTemporaryGoodsBenefitScopeCouponDiscount();
		totCnt += cnt;

		log.info("1.1.상품혜택임시-쿠폰할인 생성: {}", cnt);

		cnt = goodsDao.createTemporaryGoodsBenefitFreeDelivery();
		totCnt += cnt;
		log.info("1.2.상품혜택임시-무료배송 생성: {}", cnt);

		cnt = goodsDao.createTemporaryGoodsBenefitFreegift();
		totCnt += cnt;
		log.info("1.3.상품혜택임시-사은품 생성: {}", cnt);

		cnt = goodsDao.createTemporaryGoodsBenefitNew();
		totCnt += cnt;
		log.info("1.4.상품혜택임시-신상(=정상상품) 생성: {}", cnt);

		cnt = goodsDao.createTemporaryGoodsShotDelivery();
		totCnt += cnt;
		log.info("1.5.상품혜택임시-총알배송 생성: {}", cnt);

		return totCnt;
	}

	/**
	 * 상품혜택 생성
	 * @return 생성건수
	 * @author gagamel
	 * @since 2021. 4. 7
	 */
	@Transactional("shopTxnManager")
	public int createGoodsBenefit() {
		goodsDao.deleteGoodsBenefit();
		return goodsDao.createGoodsBenefit();
	}

	/**
	 * 재입고알림 목록
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 07. 01
	 */
	public Collection<ReinboundInform> getReinboundInformList() {
		return goodsDao.getReinboundInformList();
	}

	/** 재입고 알림톡 발송처리
	 * 
	 * @author eskim
	 * @since 2021. 7. 01
	 */
	@Transactional("shopTxnManager")
	public int updateReinboundInform(ReinboundInform reinboundInform) {
		return goodsDao.updateReinboundInform(reinboundInform);
	}

	/**
	 * VW_STOCK임시 테이블 Truncate
	 * @author gagamel
	 * @since 2021. 8. 30
	 */
	public void truncateVwStockTemporary() {
		goodsDao.truncateVwStockTemporary();
	}

	/**
	 * VW_STOCK임시 테이블 생성
	 * @return
	 * @author gagamel
	 * @since 2021. 8. 30
	 */
	public int createVwStockTemporary() {
		return goodsDao.createVwStockTemporary();
	}

	/**
	 * VW_STOCK 생성
	 * @return
	 * @author gagamel
	 * @since 2021. 8. 30
	 */
	@Transactional("shopTxnManager")
	public int createVwStock() {
		// VW_STOCK 테이블 삭제
		goodsDao.deleteVwStock();

		// VW_STOCK 테이블 생성
		return goodsDao.createVwStock();
	}

}
