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.persistence.domain.Delivery;

import lombok.extern.slf4j.Slf4j;

/**
 * 회수예외 결과 (mall -> wmsif) 
 *
 * @author moon
 * @since 2021. 03. 02
 */
@Component
@Slf4j
public class TsbWithdrawExcRsltJob extends TsbAbstractJob<Collection<Delivery>, Collection<Delivery>, Collection<Delivery>> {

	@Autowired
	private TsbDeliveryService deliveryService;

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

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

		// 1. 회수예외처리완료 조회 
		Collection<Delivery> list = deliveryService.getWithdrawExcRsltList();
		return list;
	}

	@Override
	public Collection<Delivery> process(Collection<Delivery> list) throws Exception {
		succCnt = 0;
		failCnt = 0;
		/**
		 * 1. mall: 회수예외처리완료 조회 
		 * 2. wms:  회수예외처리완료 update  
		 * 3. mall: wms 전송완료 update 
		 * */
		
		for(Delivery data : list) {
			try {
				// 2. 회수예외처리완료 update  
				wmsDeliveryService.updateTbIfRecallExceptionRslt(data);
				
				// 3. mall: wms 전송완료 update 
				deliveryService.updateTbOrderRecallException(data);
				
				succCnt ++;
			}catch(Exception e) {
				failCnt ++;
				log.info("Exception: "+e);
			}
		}
		return list;
	}

	@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);
	}

}