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

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.TsbOrderService;
import com.style24.core.biz.service.TscNaverPayService;
import com.style24.persistence.domain.Order;

import lombok.extern.slf4j.Slf4j;

/**
 * 네이버페이 주문형 배송지연 처리 
 *
 * @author card007
 * @since 2021. 10. 12
 */
@Component
@Slf4j
public class TsbNaverPayDeliveryDelayJob extends TsbAbstractJob<Collection<Order>, Collection<Order> , Collection<Order>> {

	@Autowired
	private TsbOrderService orderService;
	
	@Autowired
	private TscNaverPayService coreNaverPayService;

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

	@Override
	public Collection<Order> read() throws Exception {
		// 네이버페이 주문형 발송지연 대상 조회
		return coreNaverPayService.getNaverPayDelayTargetList();
	}

	@Override
	public Collection<Order> process(Collection<Order> dataList) throws Exception {
		succCnt = 0;
		failCnt = 0;

		for(Order order: dataList) {
			try {
				order.setRegNo(99999);
				order.setUpdNo(99999);
				int result = coreNaverPayService.nPayDelayProductOrder(order);
				
				if (result > 0) {
					succCnt++;
				} else {
					failCnt++;
				}
			}catch(Exception e) {
				e.printStackTrace();

				failCnt++;
			}
		}
		return dataList;
	}

	@Override
	public Collection<Order> write(Collection<Order> dataList) throws Exception {
		return dataList;
	}

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

}