package com.style24.admin.biz.service;

import java.util.Collection;

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

import com.style24.admin.biz.dao.TsaSocialDao;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.persistence.domain.Social;

import lombok.extern.slf4j.Slf4j;

/**
 * 소셜관리 Service
 * 
 * @author sowon
 * @since 2021. 03. 03
 */
@Service
@Slf4j
public class TsaSocialService {
	
	@Autowired
	private TsaSocialDao socialDao;
	
	/**
	 * 소셜 저장
	 *
	 * @return
	 * @author sowon
	 * @since 2021. 3. 3
	 */
	@Transactional("shopTxnManager")
	public void saveSocial(Social param) {
		Social social = new Social();
		// 진행중인 소셜이 있는지 확인
		social.setSocialStdt(param.getSocialStdt());
		social.setSocialEddt(param.getSocialEddt());
		social.setSocialSq(param.getSocialSq());
		social.setUseYn(param.getUseYn());
		social.setApplyGb(param.getApplyGb());
		social.setFrontGb(param.getFrontGb());
		
		String applyGb = param.getApplyGb();
		String useYn = param.getUseYn();
		
		// 적용상태로 저장을 할 경우
		if (applyGb.equals("A") && useYn.equals("Y")) {
			if(socialDao.getSocialGoOffCount(social) > 0) {
				throw new IllegalStateException("진행중인 소셜(핫딜)이 있습니다.");
			}
		// 대기상태로 저장을 할 경우 
		}else if(applyGb.equals("P") && useYn.equals("Y")) {
			if(socialDao.getSocialGoOffCount(social) > 0) {
				throw new IllegalStateException("선택하신 날짜에 대기중이거나 진행중인 소셜(핫딜)이 있습니다.");
			}
		}
		Integer userNo = TsaSession.getInfo().getUserNo();
		param.setRegNo(userNo);
		param.setUpdNo(userNo);

		socialDao.saveSocial(param);
	}
	
	
	/**
	 * 쇼셜 목록
	 *
	 * @return
	 * @author sowon
	 * @since 2021. 3. 3
	 */
	public Collection<Social> getSocialList(Social params) {
		return socialDao.getSocialList(params);
	}
	
	/**
	 * 소셜 목록 카운트 조회
	 * @param  param
	 * @return int
	 * @author sowon
	 * @since 2021. 3. 3
	 */
	public int getSocialCount(Social param) {
		return socialDao.getSocialCount(param);
	}
	
	/**
	 * 소셜 상품 저장
	 *
	 * @return
	 * @author sowon
	 * @since 2021. 3. 4
	 */
	@Transactional("shopTxnManager")
	public void saveSocialGoods(Collection<Social> paramList) {
		Integer userNo = TsaSession.getInfo().getUserNo();

		for (Social social : paramList) {
			social.setRegNo(userNo);
			social.setUpdNo(userNo);
			social.setDelYn("N");

			socialDao.saveSocialGoods(social);
		}
	}
	
	
	/**
	 * 쇼셜 상품 목록
	 *
	 * @return
	 * @author sowon
	 * @since 2021. 3. 4
	 */
	public Collection<Social> getSocialGoodsList(Social params) {
		return socialDao.getSocialGoodsList(params);
	}

	/**
	 * 소셜 상품 삭제
	 *
	 * @return
	 * @author sowon
	 * @since 2021. 3. 4
	 */
	@Transactional("shopTxnManager")
	public void deleteSocialGoods(Collection<Social> paramList) {
		Integer userNo = TsaSession.getInfo().getUserNo();

		Social socialInfo = null;
		for (Social social : paramList) {
			socialInfo = new Social();
			socialInfo.setSocialSq(social.getSocialSq());
			socialInfo.setScgoodsSq(social.getScgoodsSq());
			socialInfo.setGoodsCd(social.getGoodsCd());
			socialInfo.setUpdNo(userNo);
			socialInfo.setDelYn(social.getDelYn());

			socialDao.deleteSocialGoods(socialInfo);
		}
	}
	
	
}
