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

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbGoodsService;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.biz.service.TscKakaotalkService;
import com.style24.persistence.domain.ReinboundInform;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.parameter.GagaMap;

/**
 * 재입고 알림
 * 
 * @author eskim
 * @since 2021. 07. 01
 */
@Component
@Slf4j
public class TsbGoodsReinboundJob extends TsbAbstractJob<ReinboundInform, ReinboundInform, ReinboundInform> {

	@Autowired
	private TsbGoodsService goodsService;
	
	@Autowired
	private TscKakaotalkService coreKakaotalkService;

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

	@Override
	public ReinboundInform read() throws Exception {

		ReinboundInform reinboundInform = new ReinboundInform();
		return reinboundInform;
	}

	@Override
	public ReinboundInform process(ReinboundInform reinboundInform) throws Exception {
		return reinboundInform;
	}

	@Override
	public ReinboundInform write(ReinboundInform reinboundInform) throws Exception {

		Collection<ReinboundInform> reinboundInformList = goodsService.getReinboundInformList();
		if (reinboundInformList == null || reinboundInformList.isEmpty()) {
			return reinboundInform;	
		}
		
		for (ReinboundInform reinbound : reinboundInformList) {
			// 재입고 알림톡 발송
			
			GagaMap goods = new GagaMap();
			goods.set("custNo", reinbound.getCustNo());
			goods.set("custNm", reinbound.getCustNm());
			goods.set("goodsNm", reinbound.getGoodsNm());
			goods.set("optCd1", reinbound.getOptCd1Nm());
			goods.set("optCd2", reinbound.getOptCd2());
			goods.set("cellPhnno", reinbound.getCellPhnno());
			goods.set("goodsCd", reinbound.getGoodsCd());
			goods.set("colorCd", reinbound.getOptCd1());
			int result = coreKakaotalkService.goodsRestock(goods, TsbConstants.REG_NO);
			if (result > 0) {
				reinbound.setUpdNo(TsbConstants.REG_NO);
				goodsService.updateReinboundInform(reinbound);	
			}
		}
		
		return reinboundInform;	
	}

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

}
