package com.style24.batch.biz.service;

import java.util.Collection;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gagaframework.web.parameter.GagaMap;
import com.style24.batch.biz.dao.TsbWmsDeliveryDao;
import com.style24.batch.support.env.TsbConstants;
import com.style24.persistence.domain.Delivery;

import lombok.extern.slf4j.Slf4j;

/**
 * 배치 Service
 *
 * @author moon
 * @since  2020. 12. 08
 */
@Service
@Slf4j
public class TsbWmsDeliveryService {

	@Autowired
	private TsbWmsDeliveryDao wmsDeliveryDao;


	/**
	 * WMS_IF 총알배송 권역정보 조회 
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2020. 12. 08
	 */
	//@Transactional("wmsTxnManager")
	public Collection<Delivery> getWmsIfDailyDeliveryZoneList() {

		Collection<Delivery> list = null;
		
		list = wmsDeliveryDao.getWmsIfDailyDeliveryZoneList();
		
		return list ;
	}

	/**
	 * 배송(출고)지시대상 조회
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 02. 01
	 */
	//@Transactional("wmsTxnManager")
	public Collection<Delivery> getDeliveryOrderList() {

		Collection<Delivery> list = null;
		
		list = wmsDeliveryDao.getWmsIfDailyDeliveryZoneList();
		
		return list ;
	}
	
	/**
	 * WMS_IF 회수예외 목록  -H
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 03. 02
	 */
	//@Transactional("wmsTxnManager")
	public Collection<Delivery> getWmsIfWithdrawExcList() {

		Collection<Delivery> list = wmsDeliveryDao.getWmsIfWithdrawExcList();
		return list ;
	}	
	
	/**
	 * WMS_IF 회수예외 목록 -D
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 03. 02
	 */
	//@Transactional("wmsTxnManager")
	public Collection<Delivery> getWmsIfWithdrawExcItemList() {
		
		Collection<Delivery> list = wmsDeliveryDao.getWmsIfWithdrawExcItemList();
		return list ;

	}	
	
	/**
	 * WMS_IF 회수예외 목록  -M
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 03. 02
	 */
	//@Transactional("wmsTxnManager")
	public Collection<Delivery> getWmsIfWithdrawExcMemoList() {

		Collection<Delivery> list = wmsDeliveryDao.getWmsIfWithdrawExcMemoList();
		return list ;
	}		
	
	/**
	 * WMS_IF 회수예외  수신완료 수정
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 03. 02
	 */
	@Transactional("wmsTxnManager")
	public void updateTbIfRecallException(Collection<Delivery> excList, Collection<Delivery> itemList, Collection<Delivery> memoList) {

		for (Delivery excData : excList) {
			excData.setIfstat("1");
			wmsDeliveryDao.updateWmsIfWithdrawExcList(excData);
		}
		
		for (Delivery itemData : itemList) {
			itemData.setIfstat("1");
			wmsDeliveryDao.updateWmsIfWithdrawExcItemList(itemData);
		}
		
		for (Delivery memoData : memoList) {
			memoData.setIfstat("1");
			wmsDeliveryDao.updateWmsIfWithdrawExcMemoList(memoData);
		}
	}	
	
	/**
	 * WMS_IF 회수예외 처리결과 등록 
	 * @param 
	 * @return Delivery
	 * @author moon
	 * @since  2021. 03. 02
	 */
	@Transactional("wmsTxnManager")
	public void updateTbIfRecallExceptionRslt(Delivery excData) {

			excData.setIfstat("2");
			wmsDeliveryDao.updateWmsIfWithdrawExcList(excData);
			wmsDeliveryDao.updateWmsIfWithdrawExcItemList(excData);
			wmsDeliveryDao.updateWmsIfWithdrawExcMemoList(excData);
	}	
	
	/**
	 * WMS_IF 출고예외 목록  -H
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 03. 08
	 */
	//@Transactional("wmsTxnManager")
	public Collection<Delivery> getWmsIfDeliveryExcList() {

		Collection<Delivery> list = wmsDeliveryDao.getWmsIfDeliveryExcList();
		return list ;
	}	
	
	/**
	 * WMS_IF 출고예외 목록 -D
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 03. 08
	 */
	//@Transactional("wmsTxnManager")
	public Collection<Delivery> getWmsIfDeliveryExcItemList() {
		
		Collection<Delivery> list = wmsDeliveryDao.getWmsIfDeliveryExcItemList();
		return list ;

	}	
	
	/**
	 * WMS_IF 출고예외  수신완료 수정
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 03. 08
	 */
	@Transactional("wmsTxnManager")
	public void updateTbIfDeliveryException(Collection<Delivery> excList, Collection<Delivery> itemList) {

		for (Delivery excData : excList) {
			excData.setIfyn("Y");
			wmsDeliveryDao.updateWmsIfDeliveryExc(excData);
		}
		
		for (Delivery itemData : itemList) {
			itemData.setIfyn("Y");
			wmsDeliveryDao.updateWmsIfDeliveryExcItem(itemData);
			if("Y".equals(itemData.getGiftyn())) {
				wmsDeliveryDao.updateWmsGiftCancel(itemData);
			}
		}

	}		
	
	/**
	 * WMS_IF 출고결과 목록
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 03. 09
	 */
	//@Transactional("wmsTxnManager")
	public Collection<Delivery> getWmsIfDeliveryOrdRsltList() {

		Collection<Delivery> list = wmsDeliveryDao.getWmsIfDeliveryOrdRsltList();
		return list ;
	}	
	
	/**
	 * WMS_IF 출고결과 주문번호 목록
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 07. 15
	 */
	//@Transactional("wmsTxnManager")
	public Collection<Delivery> getWmsIfDeliveryOrderNoRsltList() {

		Collection<Delivery> list = wmsDeliveryDao.getWmsIfDeliveryOrderNoRsltList();
		return list ;
	}	
	
	
	/**
	 * WMS_IF 출고결과 처리결과 등록 
	 * @param 
	 * @return Delivery
	 * @author moon
	 * @since  2021. 03. 09
	 */
	@Transactional("wmsTxnManager")
	public void updateWmsIfDeliveryOrdRslt(Delivery data) {
		
			data.setIfyn("Y");
			wmsDeliveryDao.updateWmsIfDeliveryOrdRslt(data);
			wmsDeliveryDao.updateWmsIfDeliveryOrdRsltItem(data);
	}		
	
	
	/**
	 * WMS_IF 회수결과 주문변경번호 조회 
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 07. 12
	 */
	public Collection<Delivery> getWmsIfRecallDeliveryOrdChgNoList() {

		Collection<Delivery> list = wmsDeliveryDao.getWmsIfRecallDeliveryOrdChgNoList();
		return list ;
	}	
		
	
	
	
	/**
	 * WMS_IF 회수결과 처리결과 등록 
	 * 
	 * @param Delivery
	 * @author moon
	 * @since  2021. 03. 19
	 */
	@Transactional("wmsTxnManager")
	public void updateWmsIfRecallDeliveryRslt(Delivery delivery) {

		delivery.setIfstat("3"); // 수신완료 상태 
		wmsDeliveryDao.updateWmsIfRecallDeliveryRslt(delivery);
		wmsDeliveryDao.updateWmsIfRecallDeliveryItemRslt(delivery);
	}		
	
	/**
	 * WMS_IF 출고 송장번호 조회 
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 03. 26
	 */
	public Collection<Delivery> getWmsTbIfDeliveryInvoiceRslt() {

		Collection<Delivery> list = wmsDeliveryDao.getWmsTbIfDeliveryInvoiceRslt();
		return list ;
	}	
	
	
	/**
	 * WMS_IF 출고 수신완료
	 * 
	 * @param Delivery
	 * @author moon
	 * @since  2021. 03. 27
	 */
	@Transactional("wmsTxnManager")
	public void updateWmsTbIfDeliveryInvoiceRslt(Delivery delivery) {

		delivery.setIfyn("Y"); // 수신완료 상태 
		wmsDeliveryDao.updateWmsTbIfDeliveryInvoiceRslt(delivery);
	}	
	
	
	
	/**
	 * 출고처지정 지연재고 등록 
	 * 
	 * @param Delivery
	 * @author moon
	 * @since  2021. 04. 05
	 */
	@Transactional("wmsTxnManager")
	public void insertWmsTbIfDelifixQty(Delivery delivery) {

		wmsDeliveryDao.insertWmsTbIfDelifixQty(delivery);
	}	
		
	/**
	 * 출고처지정 지연재고 등록 
	 * 
	 * @param Delivery
	 * @author moon
	 * @since  2021. 04. 05
	 */
	@Transactional("wmsTxnManager")
	public void deleteWmsTbIfDelifixQty(Delivery delivery) {

		wmsDeliveryDao.deleteWmsTbIfDelifixQty(delivery);
	}	
	
	/**
	 * WMS_IF 출고요청 등록
	 * @param Collection<Delivery>
	 * @return 
	 * @author moon
	 * @since  2021. 04. 08
	 */
	@Transactional("wmsTxnManager")
	public void insertWmsDelivery(Collection<Delivery> list, Collection<Delivery>giftList) {

		// TB_IF_DeliveryOrder
		String uuid = UUID.randomUUID().toString();
		Delivery dataM = list.iterator().next();	

		dataM.setOrderguid(uuid);

		
		wmsDeliveryDao.insertWmsTbIfDeliveryOrder(dataM);
		Integer ordPk = dataM.getDeliveryorderno();
		String exchGb = dataM.getShippingtypecd(); // 10:일반, 20:  교환
		log.info("ordPk: "+ordPk);
		for (Delivery data : list) {
			// TB_IF_DeliveryOrderItem
			data.setDeliveryorderno(ordPk);
			data.setIfyn("N");
			wmsDeliveryDao.insertWmsTbIfDeliveryOrderItem(data);
		}
		if("10".equals(exchGb)) {
			for(Delivery gift : giftList) {
				gift.setDeliveryorderno(ordPk);
				gift.setIfyn("N");
				wmsDeliveryDao.insertWmsTbIfDeliveryOrderItem(gift);
			}
		}
		
	}
	
	
	/**
	 * WMS_IF 회수요청 등록
	 * @param Delivery
	 * @return 
	 * @author moon
	 * @since  2021. 04. 08
	 */
	@Transactional("wmsTxnManager")
	public Integer insertWmsRecallDelivery(Delivery data) {
		
		Integer recallNo = 0;
		wmsDeliveryDao.insertWmsRecallDelivery(data);
		recallNo = data.getRecallno(); 
	
		return recallNo;
	}
	
	/**
	 * WMS_IF 회수요청 등록
	 * @param Delivery
	 * @return 
	 * @author moon
	 * @since  2021. 04. 08
	 */
	@Transactional("wmsTxnManager")
	public void insertWmsRecallDeliveryItem(Delivery data, Integer recallNo) {
		
		data.setRecallno(recallNo);
		wmsDeliveryDao.insertWmsRecallDeliveryItem(data);

	}
	
	
	/**
	 * 회수 송장번호 등록 
	 * @param Delivery
	 * @return 
	 * @author moon
	 * @since  2021. 04. 09
	 */
	@Transactional("wmsTxnManager")
	public void updateWmsRecallInvoice(Delivery delivery) {
		wmsDeliveryDao.updateWmsRecallInvoice(delivery);
	}
	
	/**
	 * 입고결과 주문상세번호 조회 
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 07. 12
	 */
	public Collection<Delivery> getWmsOrdDtlNoList(Delivery data) {

		Collection<Delivery> list = wmsDeliveryDao.getWmsOrdDtlNoList(data);
		return list ;
	}	
	
	/**
	 * 입고결과 아이템 조회 
	 * @param 
	 * @return Collection<Delivery>
	 * @author moon
	 * @since  2021. 07. 12
	 */
	public Collection<Delivery> getWmsOrdDtlItemList(Delivery data) {

		Collection<Delivery> list = wmsDeliveryDao.getWmsOrdDtlItemList(data);
		return list ;
	}	
	
	
	
}
