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;

/**
 * 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
	
	;
	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;
		for(Delivery wmsData : reqList) {
			// 2.wms  회수정보 등록 
			String ordChgSq  = "";
			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++;
			}
		}

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

}