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 com.style24.core.support.env.TscConstants.OrderChangeGb;
import com.style24.core.support.env.TscConstants.OrderDetailStat;



import lombok.extern.slf4j.Slf4j;

/**
 * 출고 송장번호 수신 (wmsif to mall) 
 *
 * @author moon
 * @since 2021. 03. 26
 */
@Component
@Slf4j
public class TsbInvoiceNoRcvJob 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 {

		// WMS_IF 출고 송장번호 조회 
		Collection<Delivery> invoiceList = wmsDeliveryService.getWmsTbIfDeliveryInvoiceRslt();
		return invoiceList;
	}

	@Override
	public Collection<Delivery> process(Collection<Delivery> invoiceList) throws Exception {
		succCnt = 0;
		failCnt = 0;
		/**
		 * 1. wms 송장번호 조회
		 * 2. tb_order_detail_invoice 등록 
		 * 3. tb_order_detail 상태값 변경   -- G013_40	배송준비중
		 * 4. wms 수신완료 업데이트 
		 * 
		 * */
		

		for(Delivery data: invoiceList) {
			
			try {
				
				// 송장번호 등록 
				deliveryService.saveInvoice(data);
				
				// wms 수신완료 업데이트 
				wmsDeliveryService.updateWmsTbIfDeliveryInvoiceRslt(data);

				succCnt++;
			}catch(Exception e) {
				failCnt++;
			}
		}
		return invoiceList;
	}

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

		return invoiceList;
	}

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

}