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

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.persistence.domain.GoodsIfIncomelotitem;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.util.GagaDateUtil;

/**
 * WMS 입고 수신
 *
 * @author eskim
 * @since 2021. 01. 01
 */
@Component
@Slf4j
public class TsbGoodsWmsIncomelotJob extends TsbAbstractJob<GoodsIfIncomelotitem, GoodsIfIncomelotitem, GoodsIfIncomelotitem> {

	@Autowired
	private TsbGoodsService goodsService;

	@Autowired
	private TsbCommonService commonService;

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

	@Override
	public GoodsIfIncomelotitem 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;
		}
		GoodsIfIncomelotitem goodsIfIncomelotitem = new GoodsIfIncomelotitem();
		return goodsIfIncomelotitem;
	}

	@Override
	public GoodsIfIncomelotitem process(GoodsIfIncomelotitem goodsIfIncomelotitem) throws Exception {
		return goodsIfIncomelotitem;
	}

	@Override
	public GoodsIfIncomelotitem write(GoodsIfIncomelotitem goodsIfIncomelotitem) throws Exception {

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

		//wms 입고 정보 처리
		goodsService.saveGoodsWmsIncomelot(jobdate);

		//온라인 입고 상품 처리
		goodsService.saveWmsGoods(jobdate);

		//입고상품중 온라인 옵션이 없는 상품옵션 추가  -미사용(쿼리 수정해야함)
//		goodsService.createGoodsOptionList(jobdate);

		return goodsIfIncomelotitem;
	}

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

}