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

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.TsbStatisticsService;
import com.style24.core.biz.thirdparty.HansaeErp;
import com.style24.persistence.domain.Erp;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.parameter.GagaMap;

/**
 * ERP 매출반영
 *
 * @author gagamel
 * @since 2021. 8. 27
 */
@Component
@Slf4j
public class TsbSalesUploadJob extends TsbAbstractJob<String, String, String> {

	@Autowired
	private TsbStatisticsService statisticsService;

	@Autowired
	private HansaeErp hansaeErp;

	private static final int PAGE_SIZE = 100;

	@Override
	public String read() throws Exception {
//		// 어제일자 매출반영목록 생성 (한세드림)
//		statisticsService.createYesterdaySalesUploadList(HansaeErp.ErpGb.HANSAE_DR.value());

		// 어제일자 매출반영목록 생성 (한세MK)
		statisticsService.createYesterdaySalesUploadList(HansaeErp.ErpGb.HANSAE_MK.value());

		return "OK";
	}

	@Override
	public String process(String result) throws Exception {
//		// 한세드림 매출반영
//		this.uploadSales(HansaeErp.ErpGb.HANSAE_DR.value());

		// 한세MK 매출반영
		this.uploadSales(HansaeErp.ErpGb.HANSAE_MK.value());

		return "OK";
	}

	@Override
	public String write(String result) throws Exception {
		return "OK";
	}

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

	/**
	 * 매출반영
	 * @param erpGb - ERP구분(hsmk:한세MK, hsdr:한세드림)
	 * @throws Exception
	 */
	private void uploadSales(String erpGb) throws Exception {
		// 매출반영 총건수
		int totCnt = statisticsService.getSalesUploadTotalCount(erpGb);
		log.info("{}'s totCnt: {}", erpGb, totCnt);

		if (totCnt < 1) {
			return;
		}

		int totPage = totCnt / PAGE_SIZE;
		log.info("{}'s totalPage: {}", erpGb, totPage);

		if (totCnt % PAGE_SIZE > 0) {
			totPage++;
		}

		for (int page = 0; page < totPage; page++) {
			int limitStartRow = (page * PAGE_SIZE + 1) - 1;
			Erp erp = new Erp();
			erp.setErpGb(erpGb);
			erp.setLimitStartRow(limitStartRow);
			erp.setPageSize(PAGE_SIZE);

			// 매출업로드 목록
			Collection<GagaMap> uploadList = statisticsService.getSalesUploadList(erp);

			if (uploadList == null || uploadList.isEmpty()) {
				log.error("{}'s {} Page 매출반영할 데이터가 없습니다.", erpGb, (page + 1));
				continue;
			}

			// 매출반영I/F번호 조회
			String noIf = statisticsService.getSalesUploadInterfaceNo(erpGb);
			log.info("매출반영I/F번호: {}", noIf);

			for (GagaMap dataMap : uploadList) {
				// 매출반영목록 I/F번호 Update
				dataMap.setString("NO_IF", noIf);
				statisticsService.updateSalesUploadListInterfaceNo(erpGb, dataMap);
			}

			// 매출업로드
			GagaMap salesMap = hansaeErp.uploadErpSales(erpGb, uploadList);

			if (salesMap == null || salesMap.isEmpty()) {
				log.error("{}'s {} Page 매출반영결과 데이터가 없습니다. 한세 ERP 시스템담당자에게 문의해 주세요.", erpGb, (page + 1));
				continue;
			}

			// 매출반영결과 처리
			statisticsService.updateSalesUploadResult(erpGb, salesMap);

			log.info("{}'s {} Page sales uploaded successfully!", erpGb, (page + 1));
		}
	}

}
