package com.style24.admin.biz.service;

import java.util.Collection;

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

import com.style24.admin.biz.dao.TsaEnvsetDao;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.core.biz.dao.TscEnvsetDao;
import com.style24.persistence.domain.CustGradePolicy;
import com.style24.persistence.domain.Envset;

import lombok.extern.slf4j.Slf4j;

/**
 * 환경설정 Service
 *
 * @author gagamel
 * @since 2020. 10. 21
 */
@Service
@Slf4j
public class TsaEnvsetService {

	@Autowired
	private TscEnvsetDao cenvsetDao;

	@Autowired
	private TsaEnvsetDao envsetDao;

	/**
	 * 환경설정정보 저장
	 * 		정책은 중요한 정보로서 캐싱 문제 발생 시 심각해질 수 있으므로 캐싱처리 안 함
	 * @param envset - 환경설정 정보
	 * @author gagamel
	 * @since 2020. 10. 21
	 */
	@Transactional("shopTxnManager")
	public void createEnvset(Envset envset) {
		envset.setRegNo(TsaSession.getInfo().getUserNo());
		envsetDao.createEnvset(envset);
	}

	/**
	 * 환경설정정보 - 메타정보 저장. 수정 시 캐싱 삭제
	 * @param envset - 환경설정 정보
	 * @author gagamel
	 * @since 2020. 10. 21
	 */
	@Transactional("shopTxnManager")
	@CacheEvict(value = "metainfo", allEntries = true)
	public void createEnvsetMetaInfo(Envset envset) {
		envset.setRegNo(TsaSession.getInfo().getUserNo());
		envsetDao.createEnvset(envset);
	}

	/**
	 * 환경설정이력 목록
	 * @param siteCd - 사이트코드
	 * @param envsetType - 환경설정유형
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 21
	 */
	public Collection<Envset> getEnvsetHistoryList(String siteCd, String envsetType) {
		Envset envset = new Envset();
		envset.setSiteCd(siteCd);
		envset.setEnvsetType(envsetType);
		return envsetDao.getEnvsetList(envset);
	}

	/**
	 * 회원등급정책 저장
	 * @param policy - 회원등급정책 정보
	 * @author gagamel
	 * @since 2021. 1. 7
	 */
	public void createCustomerGradePolicy(CustGradePolicy policy) {
		policy.setRegNo(TsaSession.getInfo().getUserNo());
		policy.setUpdNo(TsaSession.getInfo().getUserNo());
		envsetDao.createCustomerGradePolicy(policy);
	}

	/**
	 * 회원등급정책 목록
	 * @param policy - 회원등급정책 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 7
	 */
	public Collection<CustGradePolicy> getCustomerGradePolicyList(CustGradePolicy policy) {
		return envsetDao.getCustomerGradePolicyList(policy);
	}

}
