package com.style24.admin.biz.service;

import java.util.Collection;

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

import com.gagaframework.web.parameter.GagaMap;
import com.gagaframework.web.rest.server.GagaResponseStatus;
import com.style24.admin.biz.dao.TsaMailTemplateDao;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.persistence.domain.Goods;
import com.style24.persistence.domain.MailTemplate;
import com.style24.persistence.domain.MktmailManagement;
import com.style24.persistence.domain.MktmailManagementGoods;
import com.style24.persistence.domain.MktmailManagementTc;

import lombok.extern.slf4j.Slf4j;

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

@Service
@Slf4j
public class TsaMailTemplateService {

	@Autowired
	private TsaMailTemplateDao mailtempletDao;

	/**
	 * 메일헤더푸터관리 목록
	 * @param mailtemplate - 메일관련 정보
	 * @return
	 * @author csh9191
	 * @since 2021. 02. 02
	 */
	public Collection<MailTemplate> getmailHFList(MailTemplate mailtemplate) {
		return mailtempletDao.getmailHFList(mailtemplate);
	}
	
	/**
	 * 메일헤더푸터관리 상세화면
	 * @param mailtemplate - 메일관련 정보
	 * @return
	 * @author csh9191
	 * @since 2021. 02. 02
	 */
	public MailTemplate getMailHFDetail(Integer mailtemplate) {
		return mailtempletDao.getMailHFDetail(mailtemplate);
	}
	

	/**
	 * 메일헤더푸터 상세 등록/수정
	 * @param mailtemplate - 메일관련 정보
	 * @author csh9191
	 * @since 2021. 02. 03
	 */
	@Transactional("shopTxnManager")
	public void saveMailHF(MailTemplate mailtemplate) {
		// 내용 유무 확인
		if (StringUtils.isNotBlank(mailtemplate.getMailhfContent())) {
			mailtemplate.setMailhfContent(mailtemplate.getMailhfContent().replaceAll("&lt;", "<").replaceAll("&gt;", ">"));
		}

		mailtempletDao.saveMailHF(mailtemplate);
	
	}
	
	/**
	 * 메일헤더푸터 리스트 삭제
	 * @param mailtemplate - 메일관련 정보
	 * @author csh9191
	 * @since 2021. 02. 03
	 */
	@Transactional("shopTxnManager")
	public void deleteMailHFList(Collection<MailTemplate> mailTemplateList) {
		int userId = TsaSession.getInfo().getUserNo();
		
		for(MailTemplate mailtemplate : mailTemplateList) {
			mailtemplate.setRegNo(userId);
			mailtemplate.setUpdNo(userId);
			mailtempletDao.deleteMailHFList(mailtemplate);
		}
	}
	
	/**
	 * MKT메일관리 상세화면
	 * @param mktmailManagement - 메일템플릿관련 정보
	 * @return
	 * @author csh9191
	 * @since 2021. 02. 05
	 */
	public MktmailManagement getMktmailManagementDetail(Integer mailtSq) {
		return mailtempletDao.getMktmailManagementDetail(mailtSq);
	}
	/**
	 * MKT메일관리 구분자 상세화면
	 * @param mktmailManagementTc
	 * @return
	 * @author csh9191
	 * @since 2021. 02. 16
	 */
	public Collection<MktmailManagementTc> getMktmailManagementTCList(Integer mailtSq) {
		MktmailManagementTc mktmailManagementTc = new MktmailManagementTc();
		mktmailManagementTc.setMailtSq(mailtSq);
		
		return mailtempletDao.getMktmailManagementTCList(mktmailManagementTc);
	}
	/**
	 * MKT메일관리 상품 상세화면
	 * @param mktmailManagementTc
	 * @return
	 * @author csh9191
	 * @since 2021. 02. 16
	 */
	public Collection<MktmailManagementGoods> getMktmailManagementGoodsList(Integer mailtcSq) {
		MktmailManagementTc mktmailManagementTc = new MktmailManagementTc();
		mktmailManagementTc.setMailtcSq(mailtcSq);
		
		log.info("mktmailManagementTc {}",mktmailManagementTc);
		
		return mailtempletDao.getMktmailManagementGoodsList(mktmailManagementTc);
	}
	
	/**
	 * MKT미리보기 상품정보
	 * @param mktmailManagementTc
	 * @return
	 * @author csh9191
	 * @since 2021. 02. 24
	 */
	public Collection<MktmailManagementGoods> getMktmailPreviewGoodsList(Integer mailtcSq) {
		MktmailManagementTc mktmailManagementTc = new MktmailManagementTc();
		mktmailManagementTc.setMailtcSq(mailtcSq);
		
		log.info("mktmailManagementTc {}",mktmailManagementTc);
		
		return mailtempletDao.getMktmailPreviewGoodsList(mktmailManagementTc);
	}
	
	/**
	 * MKT 상세 등록/수정
	 * @param mktmailManagement - 메일관련 정보
	 * @author csh9191
	 * @since 2021. 02. 13
	 */
	@Transactional("shopTxnManager")
	public GagaMap saveMktMailTemplate (MktmailManagement mktMailManagement) {
		GagaMap result = new GagaMap();
		
		int mailHsq = mktMailManagement.getMktHeader();
		int mailFsq = mktMailManagement.getMktFooter();
		String mailtNm = mktMailManagement.getMailtNm();
		String mailtGb = mktMailManagement.getMailtGb();
		String mailContent = mktMailManagement.getMailContent();
		log.info("mailtSq {}",mktMailManagement.getMailtSq());
		MktmailManagement mktmail = new MktmailManagement();

		mktmail.setMktHeader(mailHsq);
		mktmail.setMktFooter(mailFsq);
		mktmail.setMailtNm(mailtNm);
		mktmail.setMailtGb(mailtGb);
		mktmail.setMailContent(mailContent);
		mktmail.setRegNo(TsaSession.getInfo().getUserNo());
		mktmail.setUpdNo(TsaSession.getInfo().getUserNo());
		mktmail.setMailtSq(mktMailManagement.getMailtSq());
		
		log.info("mktmail : {}", mktMailManagement.getMailtSq());
		
		//메일 헤더 푸터 타이틀 내용 등록
		mailtempletDao.saveMktmailform(mktmail); 

		//구분자1 등록
		for(MktmailManagementTc mktmailManagementTc : mktMailManagement.getMktmailManagementTc()) {
			if(!StringUtils.isEmpty(mktmailManagementTc.getMailtcNm())) {
				
				String mailtcNm = mktmailManagementTc.getMailtcNm();
				String mailtcLink = mktmailManagementTc.getMailtcLink();
				int goodsCpr = mktmailManagementTc.getGoodsCpr();
				
				MktmailManagement mktmailtc = new MktmailManagement();
				
				mktmailtc.setRegNo(TsaSession.getInfo().getUserNo());
				mktmailtc.setUpdNo(TsaSession.getInfo().getUserNo());
				mktmailtc.setMailtcNm(mailtcNm);
				mktmailtc.setMailtcLink(mailtcLink);
				mktmailtc.setGoodsCpr(goodsCpr);
				mktmailtc.setMailtSq(mktmail.getMailtSq());
				mktmailtc.setMailtcSq(mktmailManagementTc.getMailtcSq());	
				
				log.info("구분자1.getMailtcSq() : {}", mktmailManagementTc.getMailtcSq());
				
				mailtempletDao.saveMailTc(mktmailtc);
				//grid1
				for (MktmailManagementGoods mktmailManagementGoods : mktMailManagement.getMktmailManagementGoods()) {
					
					String goodsCd = mktmailManagementGoods.getGoodsCd();
					int dispOrd = mktmailManagementGoods.getDispOrd();
	
					MktmailManagement mktmailGoods = new MktmailManagement();
					
					mktmailGoods.setRegNo(TsaSession.getInfo().getUserNo());
					mktmailGoods.setUpdNo(TsaSession.getInfo().getUserNo());
					mktmailGoods.setDispOrd(dispOrd);
					mktmailGoods.setGoodsCd(goodsCd);
					mktmailGoods.setMailtcSq(mktmailtc.getMailtcSq());
					
					log.info("그리드1.getMailtcSq() : {}", mktmailManagementTc.getMailtcSq());
					//grid 데이터 등록
					mailtempletDao.saveMailGoods(mktmailGoods);
				}
			}
		}
		

		//구분자2 등록
		for(MktmailManagementTc mktmailManagementTc : mktMailManagement.getMktmailManagementTc2()) {
			if(!StringUtils.isEmpty(mktmailManagementTc.getMailtcNm())) {
				String mailtcNm = mktmailManagementTc.getMailtcNm();
				String mailtcLink = mktmailManagementTc.getMailtcLink();
				int goodsCpr = mktmailManagementTc.getGoodsCpr();

				MktmailManagement mktmailtc = new MktmailManagement();
	
				mktmailtc.setRegNo(TsaSession.getInfo().getUserNo());
				mktmailtc.setUpdNo(TsaSession.getInfo().getUserNo());
				mktmailtc.setMailtcNm(mailtcNm);
				mktmailtc.setMailtcLink(mailtcLink);
				mktmailtc.setGoodsCpr(goodsCpr);
				mktmailtc.setMailtSq(mktmail.getMailtSq());
				mktmailtc.setMailtcSq(mktmailManagementTc.getMailtcSq());	
				
				log.info("구분자2.getMailtcSq() : {}", mktmailManagementTc.getMailtcSq());
				
				mailtempletDao.saveMailTc(mktmailtc);
				//grid2
				for (MktmailManagementGoods mktmailManagementGoods : mktMailManagement.getMktmailManagementGoods2()) {
	
					String goodsCd = mktmailManagementGoods.getGoodsCd();
					int dispOrd = mktmailManagementGoods.getDispOrd();
	
					MktmailManagement mktmailGoods = new MktmailManagement();
	
					mktmailGoods.setRegNo(TsaSession.getInfo().getUserNo());
					mktmailGoods.setUpdNo(TsaSession.getInfo().getUserNo());
					mktmailGoods.setDispOrd(dispOrd);
					mktmailGoods.setGoodsCd(goodsCd);
					mktmailGoods.setMailtcSq(mktmailtc.getMailtcSq());
					
					log.info("그리드2.getMailtcSq() : {}", mktmailtc.getMailtcSq());
					//grid 데이터 등록
					mailtempletDao.saveMailGoods(mktmailGoods);
				}
			}
		}
		
		//구분자3 등록
		for(MktmailManagementTc mktmailManagementTc : mktMailManagement.getMktmailManagementTc3()) {
			if(!StringUtils.isEmpty(mktmailManagementTc.getMailtcNm())) {
			String mailtcNm = mktmailManagementTc.getMailtcNm();
			String mailtcLink = mktmailManagementTc.getMailtcLink();
			int goodsCpr = mktmailManagementTc.getGoodsCpr();
			
			MktmailManagement mktmailtc = new MktmailManagement();

			mktmailtc.setRegNo(TsaSession.getInfo().getUserNo());
			mktmailtc.setUpdNo(TsaSession.getInfo().getUserNo());
			mktmailtc.setMailtcNm(mailtcNm);
			mktmailtc.setMailtcLink(mailtcLink);
			mktmailtc.setGoodsCpr(goodsCpr);
			mktmailtc.setMailtSq(mktmail.getMailtSq());
			mktmailtc.setMailtcSq(mktmailManagementTc.getMailtcSq());	
							
			log.info("mktmailManagementTc : {}",mktmailManagementTc.getMailtcSq());
			
			mailtempletDao.saveMailTc(mktmailtc);
			//grid3
			for (MktmailManagementGoods mktmailManagementGoods : mktMailManagement.getMktmailManagementGoods3()) {

				String goodsCd = mktmailManagementGoods.getGoodsCd();
				int dispOrd = mktmailManagementGoods.getDispOrd();

				MktmailManagement mktmailGoods = new MktmailManagement();

				mktmailGoods.setRegNo(TsaSession.getInfo().getUserNo());
				mktmailGoods.setUpdNo(TsaSession.getInfo().getUserNo());
				mktmailGoods.setDispOrd(dispOrd);
				mktmailGoods.setGoodsCd(goodsCd);
				mktmailGoods.setMailtcSq(mktmailtc.getMailtcSq());
				
				log.info("그리드3.getMailtcSq() : {}", mktmailManagementTc.getMailtcSq());//grid 데이터 등록
				mailtempletDao.saveMailGoods(mktmailGoods);
			}
			}
		}

		//구분자4 등록
		for(MktmailManagementTc mktmailManagementTc : mktMailManagement.getMktmailManagementTc4()) {
			if(!StringUtils.isEmpty(mktmailManagementTc.getMailtcNm())) {
			String mailtcNm = mktmailManagementTc.getMailtcNm();
			String mailtcLink = mktmailManagementTc.getMailtcLink();
			int goodsCpr = mktmailManagementTc.getGoodsCpr();
			
			MktmailManagement mktmailtc = new MktmailManagement();

			mktmailtc.setRegNo(TsaSession.getInfo().getUserNo());
			mktmailtc.setUpdNo(TsaSession.getInfo().getUserNo());
			mktmailtc.setMailtcNm(mailtcNm);
			mktmailtc.setMailtcLink(mailtcLink);
			mktmailtc.setGoodsCpr(goodsCpr);
			mktmailtc.setMailtSq(mktmail.getMailtSq());
			mktmailtc.setMailtcSq(mktmailManagementTc.getMailtcSq());	
							
			log.info("mktmailManagementTc : {}",mktmailManagementTc.getMailtcSq());
			
			
			mailtempletDao.saveMailTc(mktmailtc);
			//grid4
			for (MktmailManagementGoods mktmailManagementGoods : mktMailManagement.getMktmailManagementGoods4()) {
				
				String goodsCd = mktmailManagementGoods.getGoodsCd();
				int dispOrd = mktmailManagementGoods.getDispOrd();

				MktmailManagement mktmailGoods = new MktmailManagement();
				
				mktmailGoods.setRegNo(TsaSession.getInfo().getUserNo());
				mktmailGoods.setUpdNo(TsaSession.getInfo().getUserNo());
				mktmailGoods.setDispOrd(dispOrd);
				mktmailGoods.setGoodsCd(goodsCd);
				mktmailGoods.setMailtcSq(mktmailtc.getMailtcSq());
				
				log.info("그리드4.getMailtcSq() : {}", mktmailManagementTc.getMailtcSq());
				//grid 데이터 등록
				mailtempletDao.saveMailGoods(mktmailGoods);
			}
			}
		}
		
		//구분자5 등록
		for(MktmailManagementTc mktmailManagementTc : mktMailManagement.getMktmailManagementTc5()) {
			if(!StringUtils.isEmpty(mktmailManagementTc.getMailtcNm())) {
			String mailtcNm = mktmailManagementTc.getMailtcNm();
			String mailtcLink = mktmailManagementTc.getMailtcLink();
			int goodsCpr = mktmailManagementTc.getGoodsCpr();
			
			MktmailManagement mktmailtc = new MktmailManagement();

			mktmailtc.setRegNo(TsaSession.getInfo().getUserNo());
			mktmailtc.setUpdNo(TsaSession.getInfo().getUserNo());
			mktmailtc.setMailtcNm(mailtcNm);
			mktmailtc.setMailtcLink(mailtcLink);
			mktmailtc.setGoodsCpr(goodsCpr);
			mktmailtc.setMailtSq(mktmail.getMailtSq());
			mktmailtc.setMailtcSq(mktmailManagementTc.getMailtcSq());
							
			log.info("mktmailManagementTc : {}",mktmailManagementTc.getMailtcSq());
			
			
			mailtempletDao.saveMailTc(mktmailtc);
			
			//grid5
			for (MktmailManagementGoods mktmailManagementGoods : mktMailManagement.getMktmailManagementGoods5()) {
				
				String goodsCd = mktmailManagementGoods.getGoodsCd();
				int dispOrd = mktmailManagementGoods.getDispOrd();
				
				MktmailManagement mktmailGoods = new MktmailManagement();
				
				mktmailGoods.setRegNo(TsaSession.getInfo().getUserNo());
				mktmailGoods.setUpdNo(TsaSession.getInfo().getUserNo());
				mktmailGoods.setDispOrd(dispOrd);
				mktmailGoods.setGoodsCd(goodsCd);
				mktmailGoods.setMailtcSq(mktmailtc.getMailtcSq());
				
				log.info("그리드5.getMailtcSq() : {}", mktmailManagementTc.getMailtcSq());
				//grid 데이터 등록
				mailtempletDao.saveMailGoods(mktmailGoods);
			}
			}
		}


		result.set("status", GagaResponseStatus.SUCCESS.getCode());

		return result;
	}
	
	/* MKT 메일 상품전시 */
	public Collection<Goods> getMailGoodsInfo(Goods goods) {
		return mailtempletDao.getMailGoodsInfo(goods);
	}
	
	/**
	 * MKT메일관리 목록
	 * @param mktmailManagement - 메일관련 정보
	 * @return
	 * @author csh9191
	 * @since 2021. 02. 13
	 */
	public Collection<MktmailManagement> getMktMailList(MktmailManagement mktmailManagement) {
		return mailtempletDao.getMktMailList(mktmailManagement);
	}
	
	/**
	 * MKT메일 목록 삭제
	 * @param mktmailManagement - 메일관련 정보
	 * @author csh9191
	 * @since 2021. 02. 15
	 */
	@Transactional("shopTxnManager")
	public void deleteMktmailList(Collection<MktmailManagement> mktmailManagementList) {
		int userId = TsaSession.getInfo().getUserNo();
		
		for(MktmailManagement mktmailManagement : mktmailManagementList) {
			mktmailManagement.setRegNo(userId);
			mktmailManagement.setUpdNo(userId);
			mailtempletDao.deleteMktmailList(mktmailManagement);
		}
	}
	/**
	 * MKT메일 구분자 삭제
	 * @param mktmailManagementGoods 메일전시상품
	 * @author csh9191
	 * @since 2021. 02. 22
	 */
	@Transactional("shopTxnManager")
	public void deleteMktCorner(Collection<MktmailManagementTc> mktmailManagementTcList) {
		for(MktmailManagementTc mktmailManagementTc : mktmailManagementTcList) {
			mailtempletDao.deleteMktCorner(mktmailManagementTc);
		}
	}
	
	/**
	 * MKT메일 상품 삭제
	 * @param mktmailManagementGoods 메일전시상품
	 * @author csh9191
	 * @since 2021. 02. 22
	 */
	@Transactional("shopTxnManager")
	public void deleteMktGoodsList(Collection<MktmailManagementGoods> mktmailManagementGoodsList) {
		for(MktmailManagementGoods mktmailManagementGoods : mktmailManagementGoodsList) {
			mailtempletDao.deleteMktGoodsList(mktmailManagementGoods);
		}
	}
	
	/**
	 * MKT메일 미리보기
	 * @param mktmailManagement - 메일관련 정보
	 * @return
	 * @author csh9191
	 * @since 2021. 02. 19
	 */
	public MktmailManagement getMktmailPreviewHeader(Integer mktMailManagement) {
		return mailtempletDao.getMktmailPreviewHeader(mktMailManagement);
	}
	public MktmailManagement getMktmailPreviewFooter(Integer mktMailManagement) {
		return mailtempletDao.getMktmailPreviewFooter(mktMailManagement);
	}
	public MktmailManagement getMktmailPreviewContent(Integer mktMailManagement) {
		return mailtempletDao.getMktmailPreviewContent(mktMailManagement);
	}
	
}
