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

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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.TscKakaotalkService;
import com.style24.core.biz.service.TscMailService;
import com.style24.persistence.domain.Point;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class TsbExpectPointJob  extends TsbAbstractJob<Collection<Point>, GagaMap, GagaMap>{
	
	@Autowired
	TsbPointService pointService;
	
	@Autowired
	TscKakaotalkService kakaoService;
	
	@Autowired
	TscMailService mailService;
	
	private int succCnt = 0;
	private int failCnt = 0;

	
	@Override
	public Collection<Point> read() throws Exception {
		Point point = new Point(); 
		return pointService.getSendPointExpectList(point);
	}

	@Override
	public GagaMap process(Collection<Point> readItem) throws Exception {
		Integer senderNo = TsbConstants.REG_NO;
		
		for (Point point : readItem) {
			try {
				// 이메일 발송
				if (StringUtils.isNotBlank(point.getEmail())) {
					mailService.sendPointExpectNotify(point, senderNo);
				}
				// 카카오톡 알림톡
				if (StringUtils.isNotBlank(point.getCellPhnno())) {
					kakaoService.sendPointExpectNotify(point, senderNo);
				}
				
				succCnt++;
			} catch (Exception e) {
				failCnt++;
				log.error(e.getMessage());
			}
		}
		return null;
	}

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

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

}
