package com.style24.batch.biz.job.delivery;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbDeliveryService;
import com.style24.batch.biz.service.TsbWmsDeliveryService;
import com.style24.batch.support.env.TsbConstants;
import com.style24.persistence.domain.Delivery;
import com.style24.persistence.domain.Order;
import com.style24.core.biz.service.TscKakaotalkService;

import lombok.extern.slf4j.Slf4j;

/**
 * WMS 회시요청 송신 (mall to wms) 
 *
 * @author moon
 * @since 2021. 04. 08
 */
@Component
@Slf4j
public class TsbWithdrawRequestJob extends TsbAbstractJob<Collection<Delivery>, Collection<Delivery>, Collection<Delivery>> {

	@Autowired
	private TsbDeliveryService deliveryService;

	@Autowired
	private TsbWmsDeliveryService wmsDeliveryService;
	
	@Autowired
	private TscKakaotalkService kakaotalkService;
	
	private int succCnt = 0;
	private int failCnt = 0;

	@Override
	public Collection<Delivery> read() throws Exception {

		// 1.mall 회수대상 조회
		Collection<Delivery> reqList = deliveryService.getWithdrawRequestList();
		return reqList;
	}

	@Override
	public Collection<Delivery> process(Collection<Delivery> reqList) throws Exception {
		succCnt = 0;
		failCnt = 0;
		/**
		 * 1.mall 회수대상 조회  
		 * 2.wms  회수정보 등록 
		 * 3.mall 회수상태 업데이트   G685_30 -- 회수요청 , 
		 *  
		 * */
		
		Integer recallNo = 0;
		String ordChgSq  = "";
		for(Delivery wmsData : reqList) {
			// 2.wms  회수정보 등록 
			
			if(!ordChgSq.equals(wmsData.getOrderexceptionno()+"") ) {
				recallNo = wmsDeliveryService.insertWmsRecallDelivery(wmsData);
				ordChgSq = wmsData.getOrderexceptionno()+"";
			}
			
			wmsDeliveryService.insertWmsRecallDeliveryItem(wmsData, recallNo);
		}
		
		
		for(Delivery data : reqList) {
			// 3.mall 회수상태 업데이트   G685_30 -- 회수요청
			try {
				deliveryService.updateTbOrderChaneDetailStat(data);
				succCnt++;
			} catch(Exception e) {
				failCnt++;
			}
		}
		
		// 4. 알림톡  - 회수요청만 보냄  직접발송, 제휴몰자동수거 제외
		String ordChgSqChk  = "";
		for(Delivery wmsData : reqList) {
			
			if(!ordChgSqChk.equals(wmsData.getOrderexceptionno()+"") ) {
				
				if("Y".equals(wmsData.getRecallrequestyn())) { // 회수요청일경우 
					// 알림톡  문구 조회 
					try {
						Delivery info = deliveryService.getWithdrawStart(wmsData);
						// 알림톡 발송 
						Order order = new Order();
						order.setOrdNo(info.getOrdNo());
						order.setCustNo(info.getCustNo());
						order.setOrdNm(info.getOrdNm());
						order.setOrdPhnno(info.getOrdPhnno());
						order.setChgerBaseAddr(info.getChgerBaseAddr());
						order.setGoodsCnt(info.getGoodsCnt()+"");
						order.setGoodsNm(info.getGoodsNm());
						
						kakaotalkService.sendWithdrawStart(order,TsbConstants.REG_NO);
					}catch(Exception e) {
						log.debug("회수시작 알림톡 Exception: "+e.getStackTrace().toString());
					}
				}
				ordChgSqChk = wmsData.getOrderexceptionno()+"";
			}
		}
		
		return reqList;
	}

	@Override
	public Collection<Delivery> write(Collection<Delivery> reqList) throws Exception {

		return reqList;
	}

	@Override
	public void notify(Collection<Delivery> reqList) throws Exception {
		super.printResult(succCnt, failCnt);
	}

}