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;

/**
 * 자동 품절쉬소
 *
 * @author moon
 * @since 2021. 04. 26
 */
@Component
@Slf4j
public class TsbSoldoutCancelJob extends TsbAbstractJob<Delivery, Delivery, Delivery> {

	@Autowired
	private TsbDeliveryService deliveryService;

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

	@Override
	public Delivery read() throws Exception {

		Delivery delivery = new Delivery();
		return delivery;
	}

	@Override
	public Delivery process(Delivery delivery) throws Exception {
		succCnt = 0;
		failCnt = 1;

		/**
		 * 1. (일반,예약) 품절취소대상 주문번호 목록 조회, 주문번호 단위  주문상세목록 조회 , 주문번호 단위 core 취소 서비스호출  
		 * 2. (출고예외 -- 재고부족)               품절 대상 조회     core 취소 서비스호출  처리완료 업데이트 
		 * 3. (회수예외 -- 확정전 품절, 확정전 불량 )   품절 대상 조회    core 취소 서비스호출  처리완료 업데이트 
		 * */
		
		// 1. (일반,예약) 품절취소대상 주문번호 목록 조회, 주문번호 단위  주문상세목록 조회 , 주문번호 단위 core 취소 서비스호출  
		
		Collection<Delivery> ordnoList = deliveryService.getSoldoutOrderNoList();
		try {
			for(Delivery data : ordnoList) {
				deliveryService.SoldoutCancel(data,"N");
				succCnt++;
			}
		}catch(Exception E) {
			failCnt++;
		}
		
		
		// 2. (출고예외 -- 재고부족)               품절 대상 조회     core 취소 서비스호출  처리완료 업데이트 
		
		Collection<Delivery> excOrdnoList = deliveryService.getSoldoutOrderExcNoList();
		try {
			for(Delivery data2 : excOrdnoList) {
				deliveryService.SoldoutCancel(data2,"E");
				succCnt++;
			}
		} catch(Exception E) {
			failCnt++;
		}
		
		
		// 3. (회수예외 -- 확정전 품절, 확정전 불량 )   품절 대상 조회    core 취소 서비스호출   처리완료 업데이트 
		Collection<Delivery> recallOrdnoList = deliveryService.getSoldoutOrderRecallNoList();
		try {
			for(Delivery data3 : recallOrdnoList) {
				deliveryService.SoldoutCancel(data3,"R");
				succCnt++;
			}
		} catch(Exception E) {
			failCnt++;
		}

		 return delivery;
	}

	@Override
	public Delivery write(Delivery delivery) throws Exception {

		return delivery;
	}

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

}