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.gagaframework.web.parameter.GagaMap;
import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbDeliveryService;
import com.style24.batch.biz.service.TsbWmsDeliveryService;
import com.style24.persistence.domain.Delivery;
import lombok.extern.slf4j.Slf4j;

/**
 * 스윗트래커 송장 조회 
 *
 * @author moon
 * @since 2021. 04. 13
 */
@Component
@Slf4j
public class TsbSweetTrackerJob extends TsbAbstractJob<Collection<Delivery>, Collection<Delivery>, Collection<Delivery>> {

	@Autowired
	private TsbDeliveryService deliveryService;

	
	
	private int succCnt = 0;
	private int failCnt = 0;

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

		Collection<Delivery> invcList = deliveryService.getDelvTrackingList();
		return invcList;
	}

	@Override
	public Collection<Delivery> process(Collection<Delivery> invcList) throws Exception {
		succCnt = 0;
		failCnt = 0;
		/**
		 * 배송추적 대상 조회   주문건 + 회수건 
		 * 스윗트래커 api 추적요청 
		 * 주문건: tb_order_detail_invoice 업데이트 , 회수건  tb_order_change 에 업데이트 
		 * 
		 * */
		
		for(Delivery data : invcList) {
			try {
				GagaMap rtn = deliveryService.sweetTrackerInvcSend(data);
				if("S".equals(rtn.getString("success"))) {
					deliveryService.updateOrdInvcSendYn(data);
				}
				succCnt++;
			} catch(Exception e) {
				log.info("Exception: "+e);
				failCnt++;
			}
		}
		
		//GagaMap rtn = deliveryService.sweetTrackerInvcSendTest();
//		log.info(""+rtn.getString("success"));
		
		 return invcList;
	}

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

		return delivery;
	}

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

}