package com.style24.batch.biz.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.style24.batch.biz.dao.TsbStatisticsDao;
import com.style24.persistence.domain.Erp;
import com.style24.persistence.domain.SalesSms;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.parameter.GagaMap;

/**
 * 통계 Service
 *
 * @author eskim
 * @since 2020. 12. 03
 */
@Service
@Slf4j
public class TsbStatisticsService {

	@Autowired
	private TsbStatisticsDao statisticsDao;

	/**
	 * 사이트 제휴코드 유입집계
	 *
	 * @author daehyoung
	 * @since 2020. 08. 18
	 */
	@Transactional("shopTxnManager")
	public int createInflowDaily(String inflowDt) {
		// 사이트 제휴코드 유입집계 전일데이터 생성
		return statisticsDao.createInflowDaily(inflowDt);
	}

	@Transactional("shopTxnManager")
	public int createOrderDaily(String currDay) {
		return statisticsDao.createOrderDaily(currDay);
	}

	@Transactional("shopTxnManager")
	public int createOrderDateTime(String currDay) {
		return statisticsDao.createOrderDateTime(currDay);
	}

	@Transactional("shopTxnManager")
	public int createStatPay(String currDay) {
		return statisticsDao.createStatPay(currDay);
	}

	@Transactional("shopTxnManager")
	public int createStatSubPay(String currDay) {
		return statisticsDao.createStatSubPay(currDay);
	}

	/**
	 * 당일 매출문자 조회
	 * @return
	 * @author gagamel
	 * @since 2021. 8. 14
	 */
	public SalesSms getCurrentDaySalesSms() {
		return statisticsDao.getCurrentDaySalesSms();
	}

	/**
	 * 어제일자 매출반영목록 생성
	 * @param erpGb - ERP구분(hsmk:한세MK, hsdr:한세드림)
	 * @return
	 * @author gagamel
	 * @since 2021. 8. 27
	 */
	@Transactional("shopTxnManager")
	public int createYesterdaySalesUploadList(String erpGb) {
		return statisticsDao.createYesterdaySalesUploadList(erpGb);
	}

	/**
	 * 매출반영 총건수
	 * @param erpGb - ERP구분(hsmk:한세MK, hsdr:한세드림)
	 * @return
	 * @author gagamel
	 * @since 2021. 8. 27
	 */
	public int getSalesUploadTotalCount(String erpGb) {
		return statisticsDao.getSalesUploadTotalCount(erpGb);
	}

	/**
	 * 매출반영 목록
	 * @param erp - ERP 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 8. 27
	 */
	public Collection<GagaMap> getSalesUploadList(Erp erp) {
		return statisticsDao.getSalesUploadList(erp);
	}

	/**
	 * 매출반영I/F번호 조회
	 * @param erpGb - ERP구분(hsmk:한세MK, hsdr:한세드림)
	 * @return
	 * @author gagamel
	 * @since 2021. 8. 27
	 */
	public String getSalesUploadInterfaceNo(String erpGb) {
		return statisticsDao.getSalesUploadInterfaceNo(erpGb);
	}

	/**
	 * 매출반영목록 I/F번호 Update
	 * @param erpGb - ERP구분(hsmk:한세MK, hsdr:한세드림)
	 * @param paramMap - ERP 정보
	 * @author gagamel
	 * @since 2021. 8. 30
	 */
	@Transactional("shopTxnManager")
	public void updateSalesUploadListInterfaceNo(String erpGb, GagaMap paramMap) {
		GagaMap dataMap = new GagaMap();
		dataMap.setString("ERP_GB", erpGb);
		dataMap.putAll(paramMap);
		statisticsDao.updateSalesUploadListInterfaceNo(dataMap);
	}

	/**
	 * 매출반영결과 처리
	 * @param erpGb - ERP구분(hsmk:한세MK, hsdr:한세드림)
	 * @param paramMap - 결과 정보
	 * @author gagamel
	 * @since 2021. 8. 27
	 */
	@SuppressWarnings("unchecked")
	@Transactional("shopTxnManager")
	public void updateSalesUploadResult(String erpGb, GagaMap salesMap) {
		if (salesMap.get("succList") != null) {
			// 성공건 처리
			List<Map<String, Object>> succList = (ArrayList<Map<String, Object>>)salesMap.get("succList");
			if (succList != null && !succList.isEmpty()) {
				for (Map<String, Object> dataMap : succList) {
					dataMap.put("ERP_GB", erpGb);
					statisticsDao.updateSalesUploadResult(dataMap);
				}
			}
		}

		if (salesMap.get("failList") != null) {
			// 실패건 처리
			List<Map<String, Object>> failList = (ArrayList<Map<String, Object>>)salesMap.get("failList");
			if (failList != null && !failList.isEmpty()) {
				for (Map<String, Object> dataMap : failList) {
					dataMap.put("ERP_GB", erpGb);
					statisticsDao.updateSalesUploadResult(dataMap);
				}
			}
		}
	}

}
