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

import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbOrderService;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.biz.service.TscKakaotalkService;
import com.style24.core.biz.service.TscOrderChangeService;
import com.style24.core.biz.service.TscOrderService;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.OrderChange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * 무통장입금 입금 기한 만료 주문 취소
 *
 * @author xodud1202
 * @since 2021. 05. 03
 */
@Component
@Slf4j
public class TsbOrderDepositWaitingNotifyJob extends TsbAbstractJob<Collection<Order>, Collection<Order>, OrderChange> {

	@Autowired
	private TscOrderChangeService coreOrderChangeService;

	@Autowired
	private TscOrderService coreOrderService;

	@Autowired
	private TsbOrderService orderService;

	@Autowired
	private TscKakaotalkService coreKakaotalkService;

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

	@Override
	public Collection<Order> read() throws Exception {
		return orderService.getOrderDepositWaitingCustomerList();
	}

	@Override
	public Collection<Order> process(Collection<Order> params) throws Exception {
		return params;
	}

	@Override
	public OrderChange write(Collection<Order> params) throws Exception {
		OrderChange result = new OrderChange();
		for(Order order : params) {
			try {
				// 무통장입금 입금 요청 알림톡 송부
				coreKakaotalkService.sendOrderDepositWaitingNotify(order, TsbConstants.REG_NO);
				succCnt++;
			} catch (Exception e) {
				e.printStackTrace();
				failCnt++;
			}
		}

		return result;
	}

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

}