package com.style24.front.biz.service;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.style24.core.support.env.TscConstants;
import com.style24.front.biz.dao.TsfCommonDao;
import com.style24.front.support.security.session.TsfSession;
import com.style24.persistence.domain.InflowHst;
import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.SupplyCompany;

import lombok.extern.slf4j.Slf4j;

/**
 * 공통 Service
 *
 * @author eskim
 * @since 2021. 02. 09
 */
@Service
@Slf4j
public class TsfCommonService {

	@Autowired
	private TsfCommonDao commonDao;

	@Autowired
	private Environment env;

//	/**
//	 * 사이트명 조회
//	 *
//	 * @param siteCd - 사이트코드
//	 * @return
//	 * @author gagamel
//	 * @since 2020. 5. 26
//	 */
//	public String getSiteName(String siteCd) {
//		return commonDao.getSiteName(siteCd);
//	}

	/**
	 * 유효한 웹제휴링크코드 여부 조회
	 * @param afLinkCd - 제휴링크코드
	 * @return Y/N
	 * @author gagamel
	 * @date 2021. 08. 31
	 */
	public String getAvailableAfLinkYn(String afLinkCd) {
		return commonDao.getAvailableAfLinkYn(afLinkCd);
	}

	/**
	 * 유입경로이력(웹제휴채널) 생성
	 * @param afLinkCd - 제휴링크코드
	 * @author eskim
	 * @date 2021. 02. 09
	 */
	@Transactional("shopTxnManager")
	public void createInflowHistory(InflowHst inflow) {
		inflow.setSiteCd(TscConstants.Site.STYLE24.value());
		inflow.setInflowRefer(TsfSession.getHttpServletRequest().getHeader("referer"));
		inflow.setIpAddr(TsfSession.getIpAddress());
		inflow.setJsessionId(TsfSession.getSessionId());
		inflow.setFrontGb(TsfSession.getFrontGb());
		inflow.setAppYn("true".equals(TsfSession.getAttribute("isApp")) ? "Y" : "N");
		inflow.setOsType(TsfSession.getAttribute("osType"));
		if (!StringUtils.isEmpty(inflow.getInflowRefer())) {
			byte[] referBytes = inflow.getInflowRefer().getBytes();
			if (referBytes.length > 500) {
				inflow.setInflowRefer(new String(referBytes, 0, 500));
			}
		}
		if (!StringUtils.isEmpty(inflow.getPageUrl())) {
			byte[] referBytes = inflow.getPageUrl().getBytes();
			if (referBytes.length > 1000) {
				inflow.setPageUrl(new String(referBytes, 0, 1000));
			}
		}

		commonDao.createInflowHistory(inflow);
	}

//	/**
//	 * 앱 기기 등록
//	 *
//	 * @param
//	 * @return
//	 * @author sasa004
//	 * @since 2020. 08. 10
//	 */
//	public void saveMobileDevice(TsfMobileDevice mobileDevice) {
//		commonDao.saveMobileDevice(mobileDevice);
//	}

	/**
	 * 주문배송지정보수정
	 *
	 * @param
	 * @return
	 * @author jsh77b
	 * @since 2021. 04. 02
	 */
	@Transactional("shopTxnManager")
	public int updateDeliverAddr(Order order) {
		return commonDao.updateDeliverAddr(order);
	}

	/**
	 * 주문배송지정보수정
	 *
	 * @param
	 * @return
	 * @author jsh77b
	 * @since 2021. 04. 02
	 */
	@Transactional("shopTxnManager")
	public int updateDeliverAddrDelvMemo(Order order) {
		return commonDao.updateDeliverAddrDelvMemo(order);
	}

	/**
	 * 검색엔진 연동 여부
	 * @return
	 * @author gagamel
	 * @since 2021. 6. 3
	 */
	public String getSearchEngineSyncYn() {
		return commonDao.getSearchEngineSyncYn();
	}

	/**
	 * 공급업체 총 건수
	 *
	 * @return int - 건수
	 * @author jsshin
	 * @since 2021. 06. 10
	 */
	public int getSupplyCompanyCount() {
		return commonDao.getSupplyCompanyCount();
	}

	/**
	 * 공급업체 목록
	 *
	 * @param supplyCompany - 페이징 정보
	 * @return Collection<SupplyCompany> - 목록
	 * @author jsshin
	 * @since 2021. 06. 10
	 */
	public Collection<SupplyCompany> getSupplyCompanyList(SupplyCompany supplyCompany) {
		return commonDao.getSupplyCompanyList(supplyCompany);
	}
}
