package com.style24.admin.biz.service;

import java.util.Collection;

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

import com.style24.admin.biz.dao.TsaMailTemplateDao;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.persistence.domain.CommonCode;
import com.style24.persistence.domain.MailHeaderFooter;
import com.style24.persistence.domain.MailTemplate;
import com.style24.persistence.domain.MailTemplateCorner;
import com.style24.persistence.domain.MailTemplateGoods;

import lombok.extern.slf4j.Slf4j;

/**
 * 메일헤더푸터관리 Service
 *
 * @author csh9191
 * @since 2021. 02. 02
 */

@Service
@Slf4j
public class TsaMailTemplateService {

	@Value("${upload.goods.view}")
	private String uploadGoodsView;

	@Autowired
	private TsaMailTemplateDao mailTemplateDao;

	/**
	 * 메일헤더푸터 목록
	 * @param mailhf - 메일헤더푸터 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 5. 13
	 */
	public Collection<MailHeaderFooter> getMailHeaderFooterList(MailHeaderFooter mailhf) {
		return mailTemplateDao.getMailHeaderFooterList(mailhf);
	}

	/**
	 * 메일헤더푸터 조회
	 * @param mailhfSq - 메일헤더푸터일련번호
	 * @return
	 * @author gagamel
	 * @since 2021. 5. 13
	 */
	public MailHeaderFooter getMailHeaderFooter(Integer mailhfSq) {
		return mailTemplateDao.getMailHeaderFooter(mailhfSq);
	}

	/**
	 * 메일헤더푸터 저장 처리
	 * @param mailhf - 메일헤더푸터 정보
	 * @author gagamel
	 * @since 2021. 5. 13
	 */
	@Transactional("shopTxnManager")
	public void saveMailHeaderFooter(MailHeaderFooter mailhf) {
		mailhf.setRegNo(TsaSession.getInfo().getUserNo());
		mailhf.setUpdNo(TsaSession.getInfo().getUserNo());
		mailhf.setMailhfContent(mailhf.getMailhfContent().replaceAll("&lt;", "<").replaceAll("&gt;", ">"));
		mailTemplateDao.saveMailHeaderFooter(mailhf);
	}

	/**
	 * 메일헤더푸터 삭제
	 * @param mailhfSq - 메일헤더푸터일련번호
	 * @author csh9191
	 * @since 2021. 02. 03
	 */
	@Transactional("shopTxnManager")
	public void deleteMailHeaderFooter(Integer mailhfSq) {
		MailHeaderFooter mailhf = new MailHeaderFooter();
		mailhf.setMailhfSq(mailhfSq);
		mailhf.setUpdNo(TsaSession.getInfo().getUserNo());
		mailTemplateDao.deleteMailHeaderFooter(mailhf);
	}

	/**
	 * 메일헤더푸터 목록 삭제
	 * @param mailtemplate - 메일관련 정보
	 * @author csh9191
	 * @since 2021. 02. 03
	 */
	@Transactional("shopTxnManager")
	public void deleteMailHeaderFooterList(Collection<MailHeaderFooter> mailhfList) {
		for (MailHeaderFooter mailhf : mailhfList) {
			mailhf.setUpdNo(TsaSession.getInfo().getUserNo());
			mailTemplateDao.deleteMailHeaderFooter(mailhf);
		}
	}

	/**
	 * 메일템플릿 저장 처리
	 * @param mailTemplate - 메일템플릿 정보
	 * @author gagamel
	 * @since 2021. 5. 11
	 */
	@Transactional("shopTxnManager")
	public void saveMailTemplate(MailTemplate mailTemplate) {
		mailTemplate.setRegNo(TsaSession.getInfo().getUserNo());
		mailTemplate.setUpdNo(TsaSession.getInfo().getUserNo());

		// 1.메일템플릿 등록 처리
		mailTemplateDao.saveMailTemplate(mailTemplate);

		// 등록된 메일템플릿번호 값 가져오기
		Integer mailtSq = mailTemplate.getMailtSq();
		log.info("mailtSq: {}", mailtSq);
		mailTemplate.setMailtSq(mailtSq);

		// 2.메일템플릿코너 저장 처리
		Collection<MailTemplateCorner> mtcList = mailTemplate.getMailtcList();
		if (mtcList.size() > 0) {
			for (MailTemplateCorner mtc : mtcList) {
				mtc.setMailtSq(mailTemplate.getMailtSq());
				mtc.setRegNo(TsaSession.getInfo().getUserNo());
				mtc.setUpdNo(TsaSession.getInfo().getUserNo());
				mailTemplateDao.saveMailTemplateCorner(mtc);

				// 등록된 메일템플릿코너번호 값 가져오기
				Integer mailtcSq = mtc.getMailtcSq();
				log.info("mailtcSq: {}", mailtcSq);
				mtc.setMailtcSq(mailtcSq);

				// 3.메일템플릿코너상품 저장 처리
				Collection<MailTemplateGoods> mtcgList = mtc.getMailtcGoodsList();
				if (mtcgList.size() > 0) {
					for (MailTemplateGoods mtcg : mtcgList) {
						mtcg.setMailtcSq(mtc.getMailtcSq());
						mtcg.setRegNo(TsaSession.getInfo().getUserNo());
						mtcg.setUpdNo(TsaSession.getInfo().getUserNo());
						mailTemplateDao.saveMailTemplateCornerGoods(mtcg);
					}
				}
			}
		}
	}

	/**
	 * 메일템플릿 목록
	 * @param mailTemplate - 메일템플릿 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 5. 11
	 */
	public Collection<MailTemplate> getMailTemplateList(MailTemplate mailTemplate) {
		return mailTemplateDao.getMailTemplateList(mailTemplate);
	}

	/**
	 * 메일헤더푸터콤보박스 목록
	 * @param mailhfGb - 메일헤더푸터구분
	 * @return
	 * @author gagamel
	 * @since 2021. 5. 11
	 */
	public Collection<CommonCode> getMailHeaderFooterComboList(String mailhfGb) {
		return mailTemplateDao.getMailHeaderFooterComboList(mailhfGb);
	}

	/**
	 * 메일템플릿 목록 삭제
	 * @param mailTemplateList - 메일템플릿 목록
	 * @author gagamel
	 * @since 2021. 5. 11
	 */
	@Transactional("shopTxnManager")
	public void deleteMailTemplateList(Collection<MailTemplate> mailTemplateList) {
		for (MailTemplate mailTemplate : mailTemplateList) {
			mailTemplate.setRegNo(TsaSession.getInfo().getUserNo());
			mailTemplate.setUpdNo(TsaSession.getInfo().getUserNo());
			mailTemplateDao.deleteMailTemplate(mailTemplate);
		}
	}

	/**
	 * 메일템플릿코너 삭제
	 * @param mailtcSq - 메일템플릿코너일련번호
	 * @author gagamel
	 * @since 2021. 5. 12
	 */
	@Transactional("shopTxnManager")
	public void deleteMailTemplateCorner(Integer mailtcSq) {
		MailTemplateCorner mtc = new MailTemplateCorner();
		mtc.setMailtcSq(mailtcSq);
		mtc.setUpdNo(TsaSession.getInfo().getUserNo());
		mailTemplateDao.deleteMailTemplateCorner(mtc);
	}

	/**
	 * 메일템플릿코너상품 목록 삭제
	 * @param mtcgList - 메일템플릿코너상품 목록
	 * @author gagamel
	 * @since 2021. 5. 12
	 */
	@Transactional("shopTxnManager")
	public void deleteMailTemplateCornerGoodsList(Collection<MailTemplateGoods> mtcgList) {
		for (MailTemplateGoods mtcg : mtcgList) {
			mailTemplateDao.deleteMailTemplateCornerGoods(mtcg);
		}
	}

}
