package com.style24.batch.biz.service;

import java.util.Collection;

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

import com.gagaframework.web.util.GagaDateUtil;
import com.style24.batch.biz.dao.TsbWmsGoodsDao;
import com.style24.batch.support.env.TsbConstants;
import com.style24.persistence.domain.GoodsIfIncomelot;
import com.style24.persistence.domain.GoodsIfIncomelotitem;
import com.style24.persistence.domain.GoodsIfMeasurement;
import com.style24.persistence.domain.IfBrand;
import com.style24.persistence.domain.IfProduct;
import com.style24.persistence.domain.IfProvider;
import com.style24.persistence.domain.IfPurchaseorder;
import com.style24.persistence.domain.IfPurchaseorderbrandXref;
import com.style24.persistence.domain.WmsStock;

import lombok.extern.slf4j.Slf4j;

/**
 * WMS 상품 연동  Service
 *
 * @author eskim
 * @since 2021. 01. 01
 */
@Service
@Slf4j
public class TsbWmsGoodsService {

	@Autowired
	private TsbWmsGoodsDao wmsGoodsDao;

	@Autowired
	private TsbGoodsService goodsService;

	/**
	 * WMS 실측사이즈 연용
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 01
	 */
	public Collection<GoodsIfMeasurement> getWmsMeasurementList(String jobdate) {
		return wmsGoodsDao.getWmsMeasurementList(jobdate);
	}

	/**
	 * WMS 입고 목록
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 01
	 */
	public Collection<GoodsIfIncomelot> getWmsIncomelotList(String jobdate) {
		return wmsGoodsDao.getWmsIncomelotList(jobdate);
	}

	/**
	 * WMS 입고 상품목록
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 01
	 */
	public Collection<GoodsIfIncomelotitem> getWmsIncomelotitemList(String jobdate) {
		return wmsGoodsDao.getWmsIncomelotitemList(jobdate);
	}

//	/**
//	 * WMS 업체정보 송신
//	 *
//	 * @return
//	 * @author eskim
//	 * @since 2021. 01. 05
//	 */
//	@Transactional("wmsTxnManager")
//	public void saveWmsBrand(IfBrand ifBrand) {
//		//브랜드 정보 송신
//		wmsGoodsDao.saveWmsBrand(ifBrand);
//		//WMS 브랜드업체관계정보 송신
//		wmsGoodsDao.saveWmsBrandProviderXref(ifBrand);
//	}


	/**
	 * WMS 브랜드/업체정보 송신
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 01
	 */
	@Transactional("wmsTxnManager")
	public void saveWmsBrandProvider() {

		//업체정보 송신(수정일 7일)
		/*
		 * 매핑 확인건
		G065_10	자사-제조
		G065_11	자사-사입
		G065_12	자사-위탁
		 */

		Collection<IfProvider> ifProviderList = goodsService.getSupplyCompanyList();

		for (IfProvider ifProvider : ifProviderList) {
			ifProvider.setRegNo(TsbConstants.REG_NO);
			ifProvider.setUpdNo(TsbConstants.REG_NO);
			wmsGoodsDao.saveWmsProvider(ifProvider);
			
			//발주정보(업체별)
			IfPurchaseorder paramifPurchaseorder = new IfPurchaseorder();
			paramifPurchaseorder.setProviderno(ifProvider.getProviderno());
			IfPurchaseorder ifPurchaseorderMap = wmsGoodsDao.getWmsIfPurchaseorder(paramifPurchaseorder);
			if ( ifPurchaseorderMap != null) {
				ifPurchaseorderMap.setDistributioncd(ifProvider.getDistributioncd());
				ifPurchaseorderMap.setStatuscd("입고대기");
				wmsGoodsDao.updateWmsIfPurchaseorder(ifPurchaseorderMap);

			}else {
				IfPurchaseorder ifPurchaseorder = new IfPurchaseorder();
				ifPurchaseorder.setPurchasetitle(ifProvider.getDistributioncd()+"_"+ ifProvider.getProvidername());
				ifPurchaseorder.setProviderno(ifProvider.getProviderno());
				ifPurchaseorder.setProvidername(ifProvider.getProvidername());
				ifPurchaseorder.setDistributioncd(ifProvider.getDistributioncd());
				ifPurchaseorder.setTotalpayment(0);
				ifPurchaseorder.setIspayment("1");
				ifPurchaseorder.setTotalproductqty(0);
				ifPurchaseorder.setTotalskuqty(0);
				ifPurchaseorder.setStatuscd("입고대기");
				wmsGoodsDao.createWmsIfPurchaseorder(ifPurchaseorder);
			} 
			
		}

		//브랜드 정보 송신
		Collection<IfBrand> ifBrandList = goodsService.getBrandList();

		for (IfBrand ifBrand : ifBrandList) {
			ifBrand.setRegNo(TsbConstants.REG_NO);
			ifBrand.setUpdNo(TsbConstants.REG_NO);
			wmsGoodsDao.saveWmsBrand(ifBrand);
			
		}

		// 브랜드/업체 관계정보 송신
		Collection<IfBrand> ifBrandProviderXrefList = goodsService.getBrandProviderXrefList();

		for (IfBrand ifBrandProviderXref : ifBrandProviderXrefList) {
			ifBrandProviderXref.setRegNo(TsbConstants.REG_NO);
			ifBrandProviderXref.setUpdNo(TsbConstants.REG_NO);
			wmsGoodsDao.saveWmsBrandProviderXref(ifBrandProviderXref);
			
			// 발주정보(업체/브랜드별)
			IfPurchaseorder paramifPurchaseorder = new IfPurchaseorder();
			paramifPurchaseorder.setProviderno(ifBrandProviderXref.getProviderno());
			IfPurchaseorder ifPurchaseorder = wmsGoodsDao.getWmsIfPurchaseorder(paramifPurchaseorder);
			if (ifPurchaseorder != null) {
				IfPurchaseorderbrandXref ifPurchaseorderbrandXref = new IfPurchaseorderbrandXref();
				ifPurchaseorderbrandXref.setBrandno(ifBrandProviderXref.getBrandno());
				ifPurchaseorderbrandXref.setBrandname(ifBrandProviderXref.getBrandname());
				ifPurchaseorderbrandXref.setMgmtbrandname(ifBrandProviderXref.getBrandname());
				ifPurchaseorderbrandXref.setBrandstatuscd(ifBrandProviderXref.getStatuscd());
				ifPurchaseorderbrandXref.setPurchaseno(ifPurchaseorder.getPurchaseno());
				wmsGoodsDao.saveWmsIfPurchaseorderbrandXref(ifPurchaseorderbrandXref);
			} 
		}
		
	}

	/**
	 * WMS 상품정보 송신
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 01. 06
	 */
	@Transactional("wmsTxnManager")
	public void saveGoodsWmsProduct(IfProduct ifProduct) {
		//상품 정보 송신
		wmsGoodsDao.saveWmsProduct(ifProduct);
	}

	/**
	 * WMS 재고 테이블 조회
	 *
	 * @return
	 * @author eskim
	 * @since 2021. 05. 26
	 */
	public Collection<WmsStock> getWmsStockList() {
		return wmsGoodsDao.getWmsStockList();
	}

}
