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.TsbCouponService;
import com.style24.core.biz.service.TscEnvsetService;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.CustCoupon;
import com.style24.persistence.domain.CustomerSearch;
import com.style24.persistence.domain.Envset;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 휴면/미접속자 방어 쿠폰
 *
 * @author jsshin
 * @since 2021.05.28
*/
@Component
@Slf4j
public class TsbCombackCouponNoticeJob extends TsbAbstractJob<String, String, String> {

	@Autowired
	private TscEnvsetService envsetService;

	@Autowired
	private TsbCouponService couponService;

	private static final int PAGE_SIZE = 10000;

	private int stDtG14;
	private int edDtG14;
	private int stDtG13;
	private int edDtG13;
	private int stDtG12;
	private int edDtG12;
	private int succCntG14;
	private int failCntG14;
	private int succCntG13;
	private int failCntG13;
	private int succCntG12;
	private int failCntG12;

	@Override
	public String read() throws Exception {
		return "OK";
	}

	@Override
	public String process(String str) throws Exception {

		// [G14] 휴면/미접속자 방어쿠폰 자동발송-3 : -365 ~ -271
		Envset envsetG14 = envsetService.getEnvset(TscConstants.Site.STYLE24.value(), "G14");
		if (envsetG14 == null) {
			return "OK";
		}

		if (StringUtils.isNotBlank(envsetG14.getStrSetVal1())
				&& StringUtils.isNotBlank(envsetG14.getStrSetVal2())
				&& StringUtils.isNotBlank(envsetG14.getStrSetVal3())) {

			stDtG14 = Integer.parseInt(envsetG14.getStrSetVal1());
			edDtG14 = Integer.parseInt(envsetG14.getStrSetVal2());

			Integer cpnId = Integer.valueOf(envsetG14.getStrSetVal3());
			CustCoupon custCoupon = couponService.getCombackCouponInfo(cpnId);
			if (custCoupon != null) {
				// 마이너스(-) 하기 때문에 StrSetVal2가 시작 임
				int stdt = Integer.parseInt(envsetG14.getStrSetVal2());
				int edDt = Integer.parseInt(envsetG14.getStrSetVal1());
				if (stdt > 0 && edDt > 0) {
					CustomerSearch customerSearch = new CustomerSearch();
					customerSearch.setStDay(stdt);
					customerSearch.setEdDay(edDt);
					customerSearch.setSiteCd(TscConstants.Site.STYLE24.value());

					GagaMap resultG14 = couponService.saveDivisonCombackCoupon(custCoupon, customerSearch, PAGE_SIZE);
					succCntG14 = resultG14.getInt("succCnt");
					failCntG14 = resultG14.getInt("failCnt");
				}
			}
		}


		// [G13] 휴면/미접속자 방어쿠폰 자동발송-2 : -270 ~ -181
		Envset envsetG13 = envsetService.getEnvset(TscConstants.Site.STYLE24.value(), "G13");
		if (envsetG13 == null) {
			return "OK";
		}

		if (StringUtils.isNotBlank(envsetG13.getStrSetVal1())
				&& StringUtils.isNotBlank(envsetG13.getStrSetVal2())
				&& StringUtils.isNotBlank(envsetG13.getStrSetVal3())) {

			stDtG13 = Integer.parseInt(envsetG13.getStrSetVal1());
			edDtG13 = Integer.parseInt(envsetG13.getStrSetVal2());

			Integer cpnId = Integer.valueOf(envsetG13.getStrSetVal3());
			CustCoupon custCoupon = couponService.getCombackCouponInfo(cpnId);

			if (custCoupon != null) {
				int stdt = Integer.parseInt(envsetG13.getStrSetVal2());
				int edDt = Integer.parseInt(envsetG13.getStrSetVal1());
				if (stdt > 0 && edDt > 0) {
					CustomerSearch customerSearch = new CustomerSearch();
					customerSearch.setStDay(stdt);
					customerSearch.setEdDay(edDt);
					customerSearch.setSiteCd(TscConstants.Site.STYLE24.value());

					GagaMap resultG13 = couponService.saveDivisonCombackCoupon(custCoupon, customerSearch, PAGE_SIZE);
					succCntG13 = resultG13.getInt("succCnt");
					failCntG13 = resultG13.getInt("failCnt");
				}

			}

		}


		// [G12] 휴면/미접속자 방어쿠폰 자동발송-1 : -180 ~ -91
		Envset envsetG12 = envsetService.getEnvset(TscConstants.Site.STYLE24.value(), "G12");
		if (envsetG12 == null) {
			return "OK";
		}
		if (StringUtils.isNotBlank(envsetG12.getStrSetVal1())
				&& StringUtils.isNotBlank(envsetG12.getStrSetVal2())
				&& StringUtils.isNotBlank(envsetG12.getStrSetVal3())) {

			stDtG12 = Integer.parseInt(envsetG12.getStrSetVal1());
			edDtG12 = Integer.parseInt(envsetG12.getStrSetVal2());

			Integer cpnId = Integer.valueOf(envsetG12.getStrSetVal3());
			CustCoupon custCoupon = couponService.getCombackCouponInfo(cpnId);

			if (custCoupon != null) {
				int stdt = Integer.parseInt(envsetG12.getStrSetVal2());
				int edDt = Integer.parseInt(envsetG12.getStrSetVal1());
				if (stdt > 0 && edDt > 0) {
					CustomerSearch customerSearch = new CustomerSearch();
					customerSearch.setStDay(stdt);
					customerSearch.setEdDay(edDt);
					customerSearch.setSiteCd(TscConstants.Site.STYLE24.value());

					GagaMap resultG12 = couponService.saveDivisonCombackCoupon(custCoupon, customerSearch, PAGE_SIZE);
					succCntG12 = resultG12.getInt("succCnt");
					failCntG12 = resultG12.getInt("failCnt");
				}
			}
		}

		return "OK";
	}

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

	@Override
	public void notify(String resultItem) throws Exception {
		log.info("--------------------------------------------------------------------------------");
		log.info("[G14]휴면/미접속자 방어 쿠폰: 시작 일 : {} ~ 종료 일 : {}, 성공 건 : {}, 실패 건 : {}", stDtG14, edDtG14, succCntG14, failCntG14);
		log.info("[G13]휴면/미접속자 방어 쿠폰: 시작 일 : {} ~ 종료 일 : {}, 성공 건 : {}, 실패 건 : {}", stDtG13, edDtG13, succCntG13, failCntG13);
		log.info("[G12]휴면/미접속자 방어 쿠폰: 시작 일 : {} ~ 종료 일 : {}, 성공 건 : {}, 실패 건 : {}", stDtG12, edDtG12, succCntG12, failCntG12);
		log.info("--------------------------------------------------------------------------------");
	}
}
