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

import com.gagaframework.web.parameter.GagaMap;
import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbCustomerService;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.CustCoupon;
import com.style24.persistence.domain.Customer;
import com.style24.persistence.domain.CustomerSearch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * 생일자 쿠폰 안내
 *
 * @author jsshin
 * @since 2021.05.10
 */
@Component
@Slf4j
public class TsbBirthdayCouponNoticeJob extends TsbAbstractJob<Collection<CustCoupon>, GagaMap, GagaMap> {

	@Autowired
	TsbCustomerService customerService;

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

	@Override
	public Collection<CustCoupon> read() throws Exception {
		CustomerSearch customerSearch = new CustomerSearch();
		customerSearch.setSiteCd(TscConstants.Site.STYLE24.value());
		return customerService.getBirthDayTargetList(customerSearch);
	}

	@Override
	public GagaMap process(Collection<CustCoupon> readItem) throws Exception {

		for (CustCoupon CustCoupon: readItem) {
			try {
				CustCoupon.setRegNo(TsbConstants.REG_NO);
				CustCoupon.setUpdNo(TsbConstants.REG_NO);
				customerService.sendBirthDayCouponNotice(CustCoupon);
				succCnt++;
			} catch (Exception e) {
				log.error(e.getMessage());
				failCnt++;
			}

		}
		return null;
	}

	@Override
	public GagaMap write(GagaMap convertedItem) throws Exception {
		return null;
	}

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