package com.style24.core.biz.service;

import java.util.ArrayList;
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.core.biz.dao.TscDeliveryDao;
import com.style24.core.biz.dao.TscWmsDao;
import com.style24.core.biz.thirdparty.HansaeErp;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Erp;
import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.WmsDelivery;

import lombok.extern.slf4j.Slf4j;

/**
 * Wms Service
 * 
 * @author moon
 * @since 2021. 03. 15
 */
@Service
@Slf4j
public class TscWmsService {

	@Autowired
	private TscWmsDao wmsDao;

	@Autowired
	private TscDeliveryDao deliveryDao;

//	@Autowired
//	private TscErpService erpService;

	@Autowired
	private HansaeErp hansaeErp;

	/**	
	 * 실시간 재고조회(WMS&ERP)
	 * @param  Delivery
	 * @return Collection<WmsDelivery> stockLoc:WH-wms 외 매장코드   locQty: 수량
	 * @author moon
	 * @since  2021. 03. 28
	 */
	public Collection<WmsDelivery> getRealStock(Integer productNo, String skuCode, String styleCd, String colorCd, String sizeCd) {

		////  임시 
		//productNo = 14475554;
		//skuCode   = "13096364-5"; // "13096364-5"
		//styleCd   = "COF0GABL02";   
		//colorCd   = "CE";         //"CE"
		//sizeCd    = "150";
		//////

		Collection<WmsDelivery> list = new ArrayList();
		String chk1 = "N";
		String chk2 = "N";

		WmsDelivery wmsRtn = getWmsRealStock(productNo, skuCode);

		Collection<Erp> erpRtnList = hansaeErp.getErpRealStockList(HansaeErp.ErpGb.HANSAE_DR.value(), styleCd, colorCd, sizeCd);

		if (wmsRtn != null) {
			list.add(wmsRtn);
		} else {
			chk1 = "Y";
		}

		if (erpRtnList != null) {
			for (Erp data : erpRtnList) {
				WmsDelivery tmp = new WmsDelivery();
				tmp.setStockLoc(data.getStockLoc());
				tmp.setLocQty(data.getLocQty());
				list.add(tmp);
			}
		} else {
			chk2 = "Y";
		}

		if ("Y".equals(chk1) && "Y".equals(chk2)) {
			list = null;
		}

		return list;
	}

	/**
	 * WMS 실재고 조회
	 * @param productNo,skuCode
	 * @return WmsDelivery  창고(StockLoc),재고수량(LocQty)
	 * @author moon
	 * @since 2021. 03. 15
	 */
	//@Transactional("wmsTxnManager")
	public WmsDelivery getWmsRealStock(Integer productNo, String skuCode) {

		WmsDelivery wmsRtn = null;
		WmsDelivery wmsDelv = new WmsDelivery();
		wmsDelv.setProductNo(productNo);
		wmsDelv.setSkuCode(skuCode);
		wmsDelv.setWhCode(TscConstants.WH_CODE);

		log.info("wmsDelv {}" + wmsDelv);
		wmsRtn = wmsDao.getRealStock(wmsDelv);

		return wmsRtn;
	}

	/**
	 * WMS 주문취소여부 
	 * @param ordNo, ordDtlNo
	 * @return String  Y:취소가능, N: 취소불가 
	 * @author moon
	 * @since 2021. 03. 15
	 */
	public String getCancelYn(Integer ordNo, Integer ordDtlNo) {

		WmsDelivery wmsData = new WmsDelivery();
		wmsData.setOrdNo(ordNo);
		wmsData.setOrdDtlNo(ordDtlNo);

		String result = wmsDao.getCancelYn(wmsData);

		return result;
	}

	/**
	 * WMS 주문취소
	 * @param Collection<Order>
	 * @author moon
	 * @since 2021. 03. 16
	 */
	@Transactional("wmsTxnManager")
	public void updateWmsCancel(Collection<Order> ordCancelList) {
		// 주문번호, 주문상세번호, 변경수량 
		// ordNo, ordDtlNo , chgQty
		// 세트상품 취소 
		// 오픈시점 이전 출고data 취소시 --주문상세번호 없음. productNo, skuCode 로 비교 
		// 사은품 취소 

		Integer vOrdNo = null;
		for (Order cData : ordCancelList) {

			vOrdNo = cData.getOrdNo();

			//1. 주문TB 조회  PRODUCT_NO, SKU_CODE 및  세트상품일경우 포함됨.
			WmsDelivery inData = new WmsDelivery();
			inData.setOrdNo(cData.getOrdNo());
			inData.setOrdDtlNo(cData.getOrdDtlNo());
			inData.setChgQty(cData.getChgQty());
			Collection<WmsDelivery> ordDtlList = deliveryDao.getOrderDetailItemList(inData);

			//1-1. WMS_IF (TB_IF_DELIVERYORDERITEM) 에 주문상세번호 있는지 체크   Y: 주문상세번호 null , N:주문상세번호 존재함.
			String chkYn = wmsDao.getWmsOrdDtlNoYn(inData);

			//2. WMS_IF (TB_IF_DELIVERYORDERITEM) 주문상세 취소 업데이트 
			for (WmsDelivery uData : ordDtlList) {
				if ("Y".equals(chkYn)) {
					uData.setOrdDtlNo(null);
				}
				wmsDao.updateWmsIfDeliveryOrderItemCancel(uData);
				wmsDao.updateWmsTbIfDelifixQty(uData);
			}
		}

		//3. WMS 남은 주문건수 조회  Y: 전체취소 N: 부분취소 
		WmsDelivery sData = new WmsDelivery();
		sData.setOrdNo(vOrdNo);
		String cancelChk = wmsDao.getWmsAllCancelYn(sData);

		//4. WMS 남은 주문건수 0일경우  WMS (TB_IF_DELIVERYORDER)주문TB 취소 업데이트  (TB_IF_DELIVERYORDERITEM) 사은품 취소 업데이트 
		if ("Y".equals(cancelChk)) {
			wmsDao.updateWmsIfDeliveryOrderCancel(sData);
			// 사은품 취소
			wmsDao.updateWmsIfDeliveryOrderGiftCancel(sData);

			wmsDao.deleteWmsTbIfDelifixQty(sData);
		}
	}
	
	/**
	 * WMS 회수지시 취소
	 * @param 주문번호, 주문변경번호 
	 * @author moon
	 * @since 2021. 06. 17
	 */
	@Transactional("wmsTxnManager")
	public void updateWmsRecallCancel(Integer ordNo, Integer ordChgSq) {
		WmsDelivery sData = new WmsDelivery();
		sData.setOrdNo(ordNo);
		sData.setOrdChgSq(ordChgSq);
		wmsDao.updateWmsRecallCancel(sData);
	}

	/**
	 * WMS 회수지시 여부 조회
	 *
	 * @param WmsDelivery
	 * @return Collection<WmsDelivery>
	 * @author card007
	 * @since 2021. 06. 16
	 */
	@Transactional("wmsTxnManager")
	public Collection<WmsDelivery> getWmsWithdrawRequestInfo(WmsDelivery wmsDelivery) {
		return wmsDao.getWmsWithdrawRequestInfo(wmsDelivery);
	}
	
	/**
	 * WMS 배송 주소변경
	 * @param Order
	 * @author moon
	 * @since 2021. 06. 30
	 */
	@Transactional("wmsTxnManager")
	public void updateWmsDeliveryChangeAddr(Order order) {

		wmsDao.updateWmsDeliveryChangeAddr(order);
	}
	
	/**
	 *WMS 회수 주소변경
	 * @param Order
	 * @author moon
	 * @since 2021. 06. 30
	 */
	@Transactional("wmsTxnManager")
	public void updateWmsRecallChangeAddr(Order order) {

		wmsDao.updateWmsRecallChangeAddr(order);
	}
	
}
