package com.style24.front.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.env.TscConstants.OrderChangeStat;
import com.style24.core.support.env.TscConstants.OrderDetailStat;
import com.style24.core.support.env.TscConstants.MallGb;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.core.biz.service.TscKakaotalkService;
import com.style24.front.biz.dao.TsfDeliveryDao;
import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.OrderChange;
import com.style24.persistence.domain.SweetTracker;

import lombok.extern.slf4j.Slf4j;

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

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TscOrderChangeDao orderChangeDao;
	
	@Autowired
	private TsfDeliveryDao deliveryDao;

	@Autowired
	private TscOrderDao orderDao;	
	
	@Autowired
	private TscKakaotalkService kakaotalkService;	
	
	
	
	/**
	 * 스윗트래커 배송상태 등록 및 주문상세상태 변경 
	 * @param  SweetTracker
	 * @return 
	 * @author moon
	 * @since  2021. 04. 13
	 */
	@Transactional("shopTxnManager")
	public void saveDelvStat(SweetTracker sweetTracker) {
		Integer userNo = 99999;
		// 배송완료건 상태값 변경   
		log.info("step1");
		
		log.info("sweetTracker.getLevel() : "+sweetTracker.getLevel());
		if(6 == sweetTracker.getLevel()) {
			// 주문상세번호 조회 
			Collection<SweetTracker> list = deliveryDao.getOrdDtlNoList(sweetTracker);
			int cnt = 0;
			for(SweetTracker data : list) {
				// 주문상태값 변경
				data.setUpdNo(userNo);
				data.setRegNo(userNo);
				data.setOrdDtlStat(OrderDetailStat.DELIVERY_COMPLETE.value());  // 배송완료 G013_60
				deliveryDao.updateTbOrderDtlStat(data);
				// 주문상세이력
				Order order = new Order();
				order.setOrdDtlNo(data.getOrdDtlNo());
				order.setUpdNo(userNo);
				order.setRegNo(userNo);
				orderDao.createOrderDetailHst(order);
				cnt++;
			}
			/*  2021-09-08 박중재팀장님 요청  배송완료(상품평등록안내)알림톡  구매확정후 발송으로 변경
			 *
			if(cnt > 0) {
				try {
					// 배송완료(상품평등록안내)알림톡
					SweetTracker info = deliveryDao.getOrdNoInfo(sweetTracker);
					if(MallGb.SELFMALL.value().equals(info.getMallGb()) && "O".equals(info.getOrdExchGb())) {
						Order order = new Order();
						order.setOrdNo(info.getOrdNo());
						order.setCustNo(info.getCustNo());
						order.setOrdNm(info.getOrdNm());
						order.setOrdPhnno(info.getOrdPhnno());
						
						kakaotalkService.sendDeliveryComplete(order, userNo);
					}
				}catch(Exception e) {
					log.debug("배송완료 알림톡 Exception invoice: "+sweetTracker.getInvoice_no());
				}
			}
			 */

			// 주문변경번호 조회 
			
			Collection<SweetTracker> chgList = deliveryDao.getOrdChgSqList(sweetTracker);
			for(SweetTracker chgSq : chgList ) {
				
				// 주문상세변경 상태값 변경 
				chgSq.setUpdNo(userNo);
				chgSq.setRegNo(userNo);
				chgSq.setChgStat(OrderChangeStat.WITHDRAW_GOODS_CHECK.value());  // 상품검수중  G685_31
				
				int rtn = deliveryDao.updateOrdChgSqStat(chgSq);
				
				if(rtn > 0 ) {
					// 주문상세변경 이력 
					OrderChange ordChg = new OrderChange();
					ordChg.setRegNo(userNo);
					ordChg.setUpdNo(userNo);
					ordChg.setChgStat(chgSq.getChgStat());
					ordChg.setOrdChgSq(chgSq.getOrdChgSq()); 
					ordChg.setOrdDtlNo(chgSq.getOrdDtlNo()); 
					orderChangeDao.createOrderChangeDetailHst(ordChg);
				}
			}
		}
		log.info("step2");
		// 	TB_SWEET_TRACKER 등록 
		sweetTracker.setDelvLevel(sweetTracker.getLevel()+"");
		sweetTracker.setDelvWhere(sweetTracker.getWhere());
		deliveryDao.insertTbSweetTracker(sweetTracker);
		log.info("step end");
	}

	/**
	 * 스윗트래커 정보 조회
	 *
	 * @param SweetTracker
	 * @return Collection<SweetTracker>
	 * @author card007
	 * @since 2021. 05. 04
	 */
	public Collection<SweetTracker> getSweetTrackerDeliveryInfo(SweetTracker sweetTracker) {
		return deliveryDao.getSweetTrackerDeliveryInfo(sweetTracker);
	}
}
