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

import java.io.File;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.gagaframework.web.util.GagaDateUtil;
import com.gagaframework.web.util.GagaFileUtil;
import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.core.biz.service.TscShoplinkerService;
import com.style24.persistence.domain.ShoplinkerInvoice;

import lombok.extern.slf4j.Slf4j;


/**
 * 샵링커 송장전송
 *
 * @author jmh
 * @since 2021.06.22
*/
@Component
@Slf4j
public class TsbShoplinkerInvoiceJob extends TsbAbstractJob<Collection<ShoplinkerInvoice>, Collection<ShoplinkerInvoice>, String>{

	@Autowired
	private TscShoplinkerService shoplinkerService;

	@Autowired
	private Environment env;


	@Override
	public Collection<ShoplinkerInvoice> read() throws Exception {
		return shoplinkerService.getShoplinkerInvoiceOrdDtlSendList();
	}

	@Override
	public Collection<ShoplinkerInvoice> process(Collection<ShoplinkerInvoice> list) throws Exception {
		return list;
	}

	@Override
	public String write(Collection<ShoplinkerInvoice> list) throws Exception {

		String toDt = GagaDateUtil.getToday("yyyyMMdd"); //
		
		//하위폴더존재확인(상품등록)
		String slFolder = GagaFileUtil.getConcatenationPath(env.getProperty("shoplinker.xml.path")+"/invoice/"+toDt);
		File slPath = new File(slFolder);
		if (!slPath.exists()) {
			slPath.mkdir();
		}
		
		ShoplinkerInvoice shoplinkerInvoice = new ShoplinkerInvoice();
		shoplinkerInvoice.setXmlPath(env.getProperty("shoplinker.xml.path")+"/invoice/"+toDt);
		shoplinkerInvoice.setDomainUrl(env.getProperty("shoplinker.xml.view")+"/invoice/"+toDt);
		shoplinkerInvoice.setCustomerId(env.getProperty("shoplinker.customer_id"));

		shoplinkerService.createShoplinkerInvoiceXml(shoplinkerInvoice, list);
		return "OK";
	}

	@Override
	public void notify(String result) throws Exception {
		// Do nothing
	}
}
