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.TsbCommonService;
import com.style24.batch.biz.service.TsbGoodsService;
import com.style24.batch.biz.service.TsbWmsGoodsService;
import com.style24.batch.support.env.TsbConstants;
import com.style24.persistence.domain.IfProduct;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.util.GagaDateUtil;

/**
 * WMS 상품정보 송신
 *
 * @author eskim
 * @since 2021. 01. 01
 */
@Component
@Slf4j
public class TsbGoodsWmsGoodsJob extends TsbAbstractJob<IfProduct, IfProduct, IfProduct> {

	@Autowired
	private TsbWmsGoodsService wmsGoodsService;

	@Autowired
	private TsbCommonService commonService;
	
	@Autowired
	private TsbGoodsService goodsService;
	

	private int succCnt = 0;
	private int failCnt = 0;
	private String jobdate = "";

	@Override
	public IfProduct read() throws Exception {

		// WMS 동기화 옵션 여부 확인
		String wmsSyncYn = commonService.getWmsSyncYn();
		if ("N".equals(wmsSyncYn)) {
			log.info("WMS 정보 동기화 미실행 {}, 연동여부 : {}", GagaDateUtil.getToday("yyyy-MM-dd HH:mm:ss"), wmsSyncYn);
			return null;
		}
		IfProduct ifProduct = new IfProduct();
		return ifProduct;
	}

	@Override
	public IfProduct process(IfProduct ifProduct) throws Exception {
		return ifProduct;
	}

	@Override
	public IfProduct write(IfProduct ifProduct) throws Exception {

		jobdate = GagaDateUtil.getOffsetDate(-1);	//전일자

		String toDt = GagaDateUtil.getToday("yyyyMMdd"); // 20210901만 최초 전체 데이터 가져오기 
		Collection<IfProduct> ifGoodsList = goodsService.getGoodsList(toDt);
		
		//온라인 상품정보 조회 처리
		//wmsGoodsService.saveGoodsWmsProduct(jobdate);
		
		//상품 정보 송신
		for (IfProduct ifProduct2 : ifGoodsList) {
			ifProduct.setRegNo(TsbConstants.REG_NO);
			ifProduct.setUpdNo(TsbConstants.REG_NO);

			try {
				wmsGoodsService.saveGoodsWmsProduct(ifProduct2);
			}catch (Exception e) {
				log.error("TsbGoodsWmsGoodsJob error : "+e.getMessage());
			}
		}

		return ifProduct;
	}

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

}