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

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


/**
 * 휴면회원 전환
 *
 * @author jsshin
 * @since 2021.03.08
*/
@Component
@Slf4j
public class TsbDormantTransformJob extends TsbAbstractJob<CustomerSearch, GagaMap, GagaMap> {

	@Autowired
	private TsbCustomerService customerService;

	@Autowired
	private TscEnvsetService coreEnvsetService;

	private int createCustCnt = 0;
	private int updateCustCnt = 0;
	private int createOrdCnt = 0;
	private int updateOrdCnt = 0;


	@Override
	public CustomerSearch read() throws Exception {
		int conversionDay = coreEnvsetService.getDormantMemberConvertDays(TscConstants.Site.STYLE24.value());
		CustomerSearch customerSearch = new CustomerSearch();
		customerSearch.setDormantConversionDay(conversionDay);
		customerSearch.setSiteCd(TscConstants.Site.STYLE24.value());
		customerSearch.setRegNo(TsbConstants.REG_NO);
		customerSearch.setUpdNo(TsbConstants.REG_NO);
		return customerSearch;
	}

	@Override
	public GagaMap process(CustomerSearch customerSearch) throws Exception {
		log.info("customerSearch {}", customerSearch.toString());
		return customerService.saveDormantCustomer(customerSearch);
	}

	@Override
	public GagaMap write(GagaMap result) throws Exception {
		createCustCnt = result.getInt("createCustCnt");
		updateCustCnt = result.getInt("updateCustCnt");
		createOrdCnt = result.getInt("createOrdCnt");
		updateOrdCnt = result.getInt("updateOrdCnt");
		return result;
	}

	@Override
	public void notify(GagaMap result) throws Exception {
		log.info("--------------------------------------------------------------------------------");
		log.info("휴면회원정보생성: {}건, 회원휴면처리: {}건", createCustCnt, updateCustCnt);
		log.info("휴면주문정보생성: {}건, 주문휴면처리: {}건", createOrdCnt, updateOrdCnt);
		log.info("--------------------------------------------------------------------------------");
	}
}
