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.gagaframework.web.parameter.GagaMap;
import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbCjDeliveryService;
import com.style24.batch.biz.service.TsbDeliveryService;
import com.style24.persistence.domain.Delivery;

import lombok.extern.slf4j.Slf4j;

/**
 * CJ 회수상태 수신
 *
 * @author moon
 * @since 2021. 04. 12
 */
@Component
@Slf4j
public class TsbCjWithdrawInvoiceStatJob extends TsbAbstractJob<Collection<Delivery>, Collection<Delivery>, Collection<Delivery>> {

	@Autowired
	private TsbDeliveryService deliveryService;

	@Autowired
	private TsbCjDeliveryService cjDeliveryService;
	
	private int succCnt = 0;
	private int failCnt = 0;

	@Override
	public Collection<Delivery> read() throws Exception {
		// 1. cj  회수 상태 조회 
		Collection<Delivery> statList = cjDeliveryService.getCjWdStatList();
		return statList;
	}

	@Override
	public Collection<Delivery> process(Collection<Delivery> statList) throws Exception {
		succCnt = 0;
		failCnt = 1;
		/**
		 * 1. cj  회수 상태 조회 
		 * 2. mall tb_order_change 상태값 업뎃 , tb_order_change_detail 변경상태 업데이트
		 * 3. cj  완료 업데이트
		 * */

		 for(Delivery data : statList) {
			 
			 // 2. mall tb_order_change 상태값 업뎃 , tb_order_change_detail 변경상태 업데이트 (배송완료일경우)
			 deliveryService.updateTbOrderChangeStat(data);
			 
			 // 3. cj  완료 업데이트
			 cjDeliveryService.updateCjWdStat(data);
		 }
		
		succCnt = 1;
		failCnt = 0;
		
		return statList;
	}

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

		return statList;
	}

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

}