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.TsbPointService;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.biz.service.TscEnvsetService;
import com.style24.core.biz.service.TscPointService;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Point;
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.04.23
 */
@Component
@Slf4j
public class TsbAutoBuyConfirmJob extends TsbAbstractJob<Collection<Point>, GagaMap, GagaMap> {

	@Autowired
	private TsbPointService pointService;

	@Autowired
	private TscEnvsetService envsetService;

	@Autowired
	private TscPointService corePointService;

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

	@Override
	public Collection<Point> read() throws Exception {
		int expireDays = envsetService.getBuyPointExpireDays(TscConstants.Site.STYLE24.value());
		int confirmDays = envsetService.getAutoBuyConfirmDays(TscConstants.Site.STYLE24.value());
		Point point = new Point();
		point.setBuyPointExpireDays(expireDays);
		point.setAutoBuyConfirmDays(confirmDays);
		return pointService.getAutoBuyConfirmList(point);
	}

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

		for (Point point: readItem) {
			try {
				point.setRegNo(TsbConstants.REG_NO);
				point.setUpdNo(TsbConstants.REG_NO);
				corePointService.saveAutoBuyConfirm(point);
				succCnt++;
			} catch (Exception e) {
				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);
	}
}
