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

import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbCartService;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.biz.dao.TscEnvsetDao;
import com.style24.core.biz.service.TscEnvsetService;
import com.style24.core.biz.service.TscFingerPushService;
import com.style24.core.biz.thirdparty.FingerPushSender;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Customer;
import com.style24.persistence.domain.Envset;
import com.style24.persistence.domain.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * 장바구니 미구매 상품 존재 알림(앱푸시) 대상 리스트트(장바구니 삭제 10일전)
 *
 * @author xodud1202
 * @since 2021. 07. 08
 */
@Component
@Slf4j
public class TsbCartUnpurchasePushJob extends TsbAbstractJob<Collection<Order>, Collection<Order>, Order> {

	@Autowired
	private TsbCartService cartService;

	@Autowired
	private TscEnvsetDao envsetDao;

	@Autowired
	private TscEnvsetService envsetService;

	@Autowired
	private TscFingerPushService coreFingerPushService;

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

	@Override
	public Collection<Order> read() throws Exception {
		Order param = new Order();

		// 장바구니 보관 기간 조회
		int limitDays = envsetService.getCartStoreDays(TscConstants.Site.STYLE24.value());

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

		param.setSaveLimitDay(limitDays);

		return cartService.getUnpurchaseCartCustList(param);
	}

	@Override
	public Collection<Order> process(Collection<Order> params) throws Exception {
		for(Order order : params) {
			// 앱 푸시 데이터 insert
			int ansSq = FingerPushSender.FingerPushSq.GOODS_UNPURCHASED.value();
			Customer customer = new Customer();
			customer.setCustNo(order.getCustNo());
			customer.setCellPhnno(order.getCellPhnno());
			customer.setRegNo(TsbConstants.REG_NO);
			customer.setUpdNo(TsbConstants.REG_NO);
			customer.setContactType(TscConstants.ContactType.GOODS_UNPURCHASED.value());
			customer.setContactContents("미구매 상품 확인안내");

			coreFingerPushService.send(customer, ansSq);
		}

		return params;
	}

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

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

}