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

import com.style24.core.biz.service.TscEnvsetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbCartService;
import com.style24.core.biz.dao.TscEnvsetDao;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Envset;
import com.style24.persistence.domain.Order;

import lombok.extern.slf4j.Slf4j;

/**
 * 장바구니 만료 정보 삭제
 *
 * @author xodud1202
 * @since 2021. 05. 03
 */
@Component
@Slf4j
public class TsbCartExpirationJob extends TsbAbstractJob<Order, Order, Order> {

	@Autowired
	private TsbCartService cartService;

	@Autowired
	private TscEnvsetDao envsetDao;

	@Autowired
	private TscEnvsetService envsetService;

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

	@Override
	public Order read() throws Exception {
		Order order = new Order();
		return order;
	}

	@Override
	public Order process(Order param) throws Exception {
		// 장바구니 보관 기간 조회
		int limitDays = envsetService.getCartStoreDays(TscConstants.Site.STYLE24.value());

		// 값이 없을 경우 종료
		if (limitDays < 1) {
			throw new IllegalArgumentException("장바구니 보관 기간이 존재하지 않습니다.");
		}

		param.setSaveLimitDay(limitDays);

		return param;
	}

	@Override
	public Order write(Order param) throws Exception {
		succCnt = cartService.deleteCartExpriration(param);		// 장바구니 마스터 정보 삭제
		cartService.deleteCartDetail(param);					// 장바구니 상세 정보 삭제
		return param;
	}

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

}