package com.style24.scm.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.style24.core.biz.dao.TscOrderChangeDao;
import com.style24.core.biz.dao.TscOrderDao;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.core.support.env.TscConstants.OrderDetailStat;
import com.style24.core.support.env.TscConstants.OrderChangeStat;

import com.style24.scm.biz.dao.TssDeliveryDao;
import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.SweetTracker;
import com.style24.persistence.domain.OrderChange;


import lombok.extern.slf4j.Slf4j;

/**
 * 배송 Service
 *
 * @author moon
 * @since 2021. 04. 13
 */
@Service
@Slf4j
public class TssDeliveryService {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TscOrderChangeDao orderChangeDao;
	
	@Autowired
	private TssDeliveryDao deliveryDao;

	@Autowired
	private TscOrderDao orderDao;	
	
	/**
	 * 스윗트래커 배송상태 등록 및 주문상세상태 변경 
	 * @param  SweetTracker
	 * @return 
	 * @author moon
	 * @since  2021. 04. 13
	 */
	@Transactional("shopTxnManager")
	public void saveDelvStat(SweetTracker sweetTracker) {
        
		// 배송완료건 상태값 변경   
		if(6 == sweetTracker.getLevel()) {
			// 주문상세번호 조회 
			Collection<SweetTracker> list = deliveryDao.getOrdDtlNoList(sweetTracker);
			for(SweetTracker data : list) {
				// 주문상태값 변경
				data.setUpdNo(0);
				data.setRegNo(0);
				data.setOrdDtlStat(OrderDetailStat.DELIVERY_COMPLETE.value());  // 배송완료 G013_60
				deliveryDao.updateTbOrderDtlStat(data);
				// 주문상세이력
				Order order = new Order();
				order.setOrdDtlNo(data.getOrdDtlNo());
				order.setUpdNo(0);
				order.setRegNo(0);
				orderDao.createOrderDetailHst(order);				
			}
			
			// 주문변경번호 조회 
			Collection<SweetTracker> chgList = deliveryDao.getOrdChgSqList(sweetTracker);
			for(SweetTracker chgSq : chgList ) {
				
				// 주문상세변경 상태값 변경 
				chgSq.setUpdNo(0);
				chgSq.setRegNo(0);
				chgSq.setChgStat(OrderChangeStat.WITHDRAW_GOODS_CHECK.value());  // 상품검수중  G685_31
				deliveryDao.updateOrdChgSqStat(chgSq);
				
				// 주문상세변경 이력 
				OrderChange ordChg = new OrderChange();
				ordChg.setRegNo(0);
				ordChg.setUpdNo(0);
				ordChg.setOrdChgSq(chgSq.getOrdChgSq()); 
				ordChg.setOrdDtlNo(chgSq.getOrdDtlNo()); 
				orderChangeDao.createOrderChangeDetailHst(ordChg);
			}
		}
		
		// 	TB_SWEET_TRACKER 등록 
		sweetTracker.setDelvLevel(sweetTracker.getLevel()+"");
		sweetTracker.setDelvWhere(sweetTracker.getWhere());
		deliveryDao.insertTbSweetTracker(sweetTracker);
	}
	
	/**
	 * 스윗트래커 배송상태 등록 및 주문상세상태 변경 
	 * @param  SweetTracker
	 * @return 
	 * @author moon
	 * @since  2021. 04. 13
	 */
	@Transactional("shopTxnManager")
	public int saveTemp(String msg) {
		int rtn=0;
		SweetTracker tmp = new SweetTracker();
		tmp.setMan(msg);
		deliveryDao.saveTemp(tmp);
		return rtn;
	}

}
