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

import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbNaverPayService;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.biz.checkout.base.ChangedProductOrderInfo;
import com.style24.core.biz.checkout.base.ProductOrderChangeType;
import com.style24.core.biz.service.TscNaverPayService;
import com.style24.persistence.domain.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 네이버페이 결제완료 데이터 수신
 *
 * @author xodud1202
 * @since 2021. 05. 27
 */
@Component
@Slf4j
public class TsbNaverPayRecvOrderReturnReqJob extends TsbAbstractJob<ChangedProductOrderInfo[], ChangedProductOrderInfo[], Order> {
	@Autowired
	private TsbNaverPayService naverPayService;

	@Autowired
	private TscNaverPayService coreNaverPayService;

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

	@Override
	public ChangedProductOrderInfo[] read() throws Exception {
		log.info("<<<< 반품접수 수신 시작 >>>>");
		Order time = coreNaverPayService.setInitData();
		return coreNaverPayService.changedProductOrderList(ProductOrderChangeType.RETURN_REQUESTED, time.getFrom(), time.getTo(), TsbConstants.REG_NO); // 변경 상품주문 조회;
	}

	@Override
	public ChangedProductOrderInfo[] process(ChangedProductOrderInfo[] changedProductOrderList) throws Exception {
		if(changedProductOrderList != null) {
			log.info("<<<< 수신건수: [{}]건 >>>>", changedProductOrderList.length);
		}
		return changedProductOrderList;
	}

	@Override
	public Order write(ChangedProductOrderInfo[] changedProductOrderList) throws Exception {
		Order result = new Order();
		if (changedProductOrderList != null) {
			result = coreNaverPayService.recvReturnReqOrder(changedProductOrderList, TsbConstants.REG_NO);
		}

		log.info("<<<< 반품접수 수신 종료 >>>>");
		return result;
	}

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