package com.style24.core.biz.service;

import java.util.Collection;

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

import com.style24.core.biz.dao.TscMailTemplateDao;
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 gagamel
 * @since 2021. 5. 28
 */

@Service
@Slf4j
public class TscMailTemplateService {

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

	@Autowired
	private TscMailTemplateDao mailTemplateDao;

	/**
	 * 메일템플릿 정보
	 * @param mailtSq - 메일템플릿일련번호
	 * @return
	 * @author gagamel
	 * @since 2021. 5. 11
	 */
	public MailTemplate getMailTemplate(Integer mailtSq) {
		return mailTemplateDao.getMailTemplate(mailtSq);
	}

	/**
	 * 메일템플릿코너 목록
	 * @param mailtSq - 메일템플릿일련번호
	 * @return
	 * @author gagamel
	 * @since 2021. 5. 11
	 */
	public Collection<MailTemplateCorner> getMailTemplateCornerList(Integer mailtSq) {
		// 메일템플릿코너 목록
		Collection<MailTemplateCorner> mtcList = mailTemplateDao.getMailTemplateCornerList(mailtSq);

		if (mtcList != null && !mtcList.isEmpty()) {
			for (MailTemplateCorner mtc : mtcList) {
				// 메일템플릿코너상품 목록
				mtc.setMailtcGoodsList(mailTemplateDao.getMailTemplateCornerGoodsList(mtc.getMailtcSq()));
			}
		}

		return mtcList;
	}

	/**
	 * 메일템플릿 미리보기 조회
	 * @param mailtSq - 메일템플릿일련번호
	 * @return
	 * @author gagamel
	 * @since 2021. 5. 11
	 */
	public MailTemplate getMailTemplatePreview(Integer mailtSq) {
		MailTemplate mailTemplate = mailTemplateDao.getMailTemplatePreview(mailtSq);

		// 마케팅메일이면
		if (mailTemplate.getMailtGb().equals("M")) {
			// 메일템플릿코너 목록
			Collection<MailTemplateCorner> conerList = mailTemplateDao.getMailTemplateCornerList(mailtSq);

			if (conerList != null && !conerList.isEmpty()) {
				StringBuilder goodsContent = new StringBuilder();
				goodsContent.append("<!-- 컨텐츠 시작 -->\n");
				goodsContent.append("<div style=\"WHITE-SPACE: normal; TEXT-TRANSFORM: none; WORD-SPACING: 0px; FLOAT: none; COLOR: rgb(0,0,0); FONT: 12px/18px Dotum; ORPHANS: 2; WIDOWS: 2; DISPLAY: inline !important; LETTER-SPACING: normal; TEXT-INDENT: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;\">\n");
				goodsContent.append("	<table style=\"width:100%;background-color:#f5f5f5;letter-spacing:-1px;\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" align=\"center\">\n");
				goodsContent.append("		<tbody>\n");
				goodsContent.append("			<tr>\n");
				goodsContent.append("				<td align=\"center\">\n");
				goodsContent.append("					<div style=\"max-width:700px; margin:0 auto\">\n");
				goodsContent.append("						<table class=\"tabletabl\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"border:0;border-collapse:separate;border-spacing:0;margin:0;min-width:700px;outline:0;padding:0;font-family: '나눔고딕',NanumGothic,'맑은고딕',Malgun Gothic,'돋움',Dotum,Helvetica,'Apple SD Gothic Neo',Sans-serif;letter-spacing: -0.025em;\">\n");
				goodsContent.append("							<tbody>\n");
				goodsContent.append("								<!-- 마케팅메일전용상단배너: 컨텐츠 기본 영역을 벗어나는 타입 -->\n");
				goodsContent.append("								<tr>\n");
				goodsContent.append("									<td width=\"700px;\" height=\"206px;\" colspan=\"3\" style=\"margin:0;padding:0\" align=\"center\">\n");
				goodsContent.append("										<a href=\"#none\" target=\"_blank\" style=\"margin:0;padding:0\">\n");
				goodsContent.append(mailTemplate.getMailContent()).append("\n");
				goodsContent.append("										</a>\n");
				goodsContent.append("									</td>\n");
				goodsContent.append("								</tr>\n");
				goodsContent.append("								<tr>\n");
				goodsContent.append("									<td width=\"100%\" height=\"60px;\" colspan=\"3\" bgcolor=\"#ffffff\" style=\"background: #ffffff;\"></td>\n");
				goodsContent.append("								</tr>\n");
				goodsContent.append("									<!--// 마케팅메일전용상단배너: 컨텐츠 기본 영역을 벗어나는 타입 -->\n");
				goodsContent.append("								<tr>\n");
				goodsContent.append("									<td width=\"60px;\" bgcolor=\"#ffffff\" style=\"background: #ffffff;\"></td>\n");
				goodsContent.append("									<!-- 메일시작 -->\n");
				goodsContent.append("									<td width=\"580px;\" bgcolor=\"#ffffff\" style=\"background: #ffffff;\">\n");
				goodsContent.append("										<!-- 메일내용 시작 -->\n");

				for (MailTemplateCorner coner : conerList) {
					// 메일템플릿코너상품 목록
					Collection<MailTemplateGoods> conerGoodsList = mailTemplateDao.getMailTemplateCornerGoodsList(coner.getMailtcSq());

					// 메일템플릿코너별 상품HTML 설정
					goodsContent.append(this.getGoodsHtml(coner, conerGoodsList));
				}

				goodsContent.append("										<!-- 메일내용 종료 -->\n");
				goodsContent.append("									</td>\n");
				goodsContent.append("									<td width=\"60px;\" bgcolor=\"#ffffff\" style=\"background: #ffffff;\"></td>\n");
				goodsContent.append("								</tr>\n");
				goodsContent.append("								<tr>\n");
				goodsContent.append("									<td width=\"100%\" height=\"60px;\" colspan=\"3\" bgcolor=\"#ffffff\" style=\"background: #ffffff;\"></td>\n");
				goodsContent.append("								</tr>\n");
				goodsContent.append("							</tbody>\n");
				goodsContent.append("						</table>\n");
				goodsContent.append("					</div>\n");
				goodsContent.append("				</td>\n");
				goodsContent.append("			</tr>\n");
				goodsContent.append("		</tbody>\n");
				goodsContent.append("	</table>\n");
				goodsContent.append("</div>\n");
				goodsContent.append("<!-- // 컨텐츠 종료 -->\n");

				mailTemplate.setGoodsMailContent(goodsContent.toString());
			}
		}

		return mailTemplate;
	}

	/**
	 * 마케팅메일용 상품HTML 조회
	 * @param coner - 메일템플릿코너 정보
	 * @param goodsList - 메일템플릿코너상품 목록
	 * @return
	 * @author gagamel
	 * @since 2021. 5. 27
	 */
	private String getGoodsHtml(MailTemplateCorner coner, Collection<MailTemplateGoods> goodsList) {
		// 메일템플릿코너 미리보기 내용 설정
		StringBuilder sb = new StringBuilder();

		// 템플릿코너명
		sb.append("<table style=\"width:100%;margin:0;padding:0;\" cellspacing=\"0\" cellpadding=\"0\">\n");
		sb.append("	<thead>\n");
		sb.append("		<tr>\n");
		sb.append("			<th colspan=\"2\" style=\"text-align:center;line-height: 1;font-size: 29px;font-weight: bold;letter-spacing: -0.067em;color:#222222;font-family: 'Apple SD Gothic Neo','나눔고딕',NanumGothic,'맑은고딕',Malgun Gothic, Helvetica, sans-serif;\">").append(coner.getMailtcNm()).append("</th>\n");
		sb.append("		</tr>\n");
		sb.append("	</thead>\n");
		sb.append("	<tbody<tr><td colspan=\"2\" height=\"28px;\"></td></tr></tbody>\n");
		sb.append("</table>\n");

		//상품노출갯수
		if (coner.getGoodsCpr() == 1) {
			// 상품 목록
			if (goodsList != null && !goodsList.isEmpty()) {
				for (MailTemplateGoods goods : goodsList) {
					sb.append("<!-- 상품 1-Horizontal 진열 테이블 -->\n");
					sb.append("<table style=\"width:100%;height:100%;margin:0;padding:0;font-size: 14px;\" cellspacing=\"0\" cellpadding=\"0\">\n");
					sb.append("	<tbody>\n");
					sb.append("		<tr>\n");
					sb.append("			<td width=\"240px;\" height=\"360px;\" rowspan=\"6\" valign=\"top\" style=\"margin:0;padding:0;border-spacing:0;\">\n");
					sb.append("				<a href=\"#none\" target=\"_blank\" style=\"margin:0;padding:0\">\n");
					sb.append("					<span style=\"background: #f5f5f5;width: 240px;height: 360px;margin:0;padding:0\">\n");
					sb.append("						<img src=\"http:").append(uploadGoodsView).append("/").append(goods.getSysImgNm()).append("\" alt=\"이미지\" style=\"width: 240px;height: auto;display:block;margin:0;padding:0\"/>\n");
					sb.append("					</span>\n");
					sb.append("				</a>\n");
					sb.append("			</td>\n");
					sb.append("			<td width=\"40px;\" style=\"height: 40px;\"></td>\n");
					sb.append("			<td valign=\"top\" height=\"0px;\" style=\"height:0px; padding: 0; margin: 0;border-spacing:0;\"></td>\n");
					sb.append("		</tr>\n");
					sb.append("		<tr>\n");
					sb.append("			<td width=\"40px;\" style=\"height: 20px;\"></td>\n");
					sb.append("			<td valign=\"top\" height=\"20px;\" style=\"padding: 0; margin: 0;border-spacing:0;\">\n");
					sb.append("				<span style=\"color: #888888; line-height: 40px; font-size: 11px;font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;line-height:20px;letter-spacing: -0.025em; text-align: left; margin:0;\">").append(goods.getBrandNm()).append("</span>\n");
					sb.append("			</td>\n");
					sb.append("		</tr>\n");
					sb.append("		<tr>\n");
					sb.append("			<td width=\"40px;\" style=\"height: 20px;\"></td>\n");
					sb.append("			<td valign=\"top\" style=\"padding: 0; margin: 0;border-spacing:0;\">\n");
					sb.append("				<a href=\"#none\" target=\"_blank\" style=\"display: block; color: #222222; font-size: 14px; font-weight: normal; letter-spacing: -0.025em; text-decoration: none;\">\n");
					sb.append("					<span style=\"height:40px;color: #222222; font-size: 14px;font-weight: normal;line-height:20px;letter-spacing: -0.025em; text-align: left; margin:0; overflow: hidden; text-overflow: ellipsis; -webkit-line-clamp: 2; -webkit-box-orient: vertical; display: block; display: -webkit-box;\">").append(goods.getGoodsNm()).append("</span>\n");
					sb.append("				</a>\n");
					sb.append("			</td>\n");
					sb.append("		</tr>\n");
					sb.append("		<tr>\n");
					sb.append("			<td width=\"40px;\" style=\"height: 20px;\"></td>\n");
					sb.append("			<td valign=\"top\" style=\"padding: 0; margin: 0;border-spacing:0;\">\n");
					sb.append("				<span style=\"color: #222222; font-size: 16px; font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;line-height:20px;letter-spacing: -0.025em; display: inline-block; width:auto; padding-top:0; padding-bottom:0; padding-left:0; padding-right: 6px; text-align: left;\">").append(String.format("%,d", goods.getCurrPrice())).append("</span>\n");
					if(goods.getDcRate() > 0) {
						sb.append("				<span style=\"color: #888888; font-size: 14px; text-decoration: line-through;font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;line-height:20px;letter-spacing: -0.025em; display: inline-block; width:auto; text-align: left;\">").append(String.format("%,d", goods.getListPrice())).append("</span>\n");
						sb.append("				<span style=\"color: #fd4802; font-size: 16px; font-weight: bold;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;line-height:20px;letter-spacing: -0.05em; display: inline-block; width:50px; text-align: right;\">").append(goods.getDcRate()).append("%</span>\n");
					}
					sb.append("			</td>\n");
					sb.append("		</tr>\n");

					// 상품혜택정보
					if (StringUtils.isNotBlank(goods.getBenefits())) {
						sb.append("		<tr>\n");
						sb.append("			<td width=\"40px;\" style=\"height: 20px;\"></td>\n");
						sb.append("			<td valign=\"top\" style=\"padding: 0; margin: 0;border-spacing:0;\">\n");

						String[] arrBenefit = goods.getBenefits().split(",");
						for (String benefit : arrBenefit) {
							sb.append("			<span style=\"width: auto; height: 20px;text-align: center;line-height:18px; display: inline-block; padding-top:0;padding-bottom:0;padding-left:5px;padding-right:5px; font-size:9px; color: #888888; background: #f5f5f5; border-width: 1px; border-style: solid; border-color: #f5f5f5; box-sizing: border-box;\">").append(benefit).append("</span>\n");
						}

						sb.append("			</td>\n");
						sb.append("		</tr>\n");
					}

					// 상품타이틀명(세일링문구)
					if (StringUtils.isNotBlank(goods.getGoodsTnm())) {
						sb.append("		<tr>\n");
						sb.append("			<td width=\"40px;\" style=\"height: 60px;\"></td>\n");
						sb.append("			<td valign=\"top\" style=\"padding: 0; margin: 0;border-spacing:0;\">\n");
						sb.append("				<span style=\"color: #fd4802;font-size: 14px; line-height: 18px; font-weight: bold;letter-spacing: -0.05em;\">").append(goods.getGoodsTnm()).append("</span>\n");
						sb.append("			</td>\n");
						sb.append("		</tr>\n");
					}

					sb.append("		<tr>\n");
					sb.append("			<td height=\"30px;\" colspan=\"3\" style=\"height: 30px;border-spacing:0;\"></td>\n");
					sb.append("		</tr>\n");
					sb.append("	</tbody>\n");
					sb.append("</table>\n");
				}
			}
		} else if (coner.getGoodsCpr() == 2) { //상품노출갯수
			sb.append("<!-- 상품 2-Column 진열 테이블 -->\n");
			sb.append("<table style=\"width:100%;height:100%;margin:0;padding:0;font-size: 14px;\" cellspacing=\"0\" cellpadding=\"0\">\n");
			sb.append("	<tbody>\n");

			// 상품 목록
			if (goodsList != null && !goodsList.isEmpty()) {
				int idx = 1;
				for (MailTemplateGoods goods : goodsList) {
					if (idx % 2 == 1) {
						sb.append("		<tr>\n");
					} else {
						sb.append("			<td width=\"20px;\" height=\"420px;\" style=\"width: 20px;display: block;margin:0;padding:0\"></td>\n");
					}

					sb.append("			<td width=\"280px;\" height=\"660px;\" colspan=\"2\" style=\"margin:0;padding:0\">\n");
					sb.append("				<a href=\"#none\" target=\"_blank\" style=\"margin:0;padding:0;text-decoration: none;\">\n");
					sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:0; padding-right: 0px;\">\n");
					sb.append("						<span style=\"background: #ffffff;width: 280px;height: 420px;display: block;margin:0;padding:0\">\n");
					sb.append("							<img src=\"http:").append(uploadGoodsView).append("/").append(goods.getSysImgNm()).append("\" alt=\"이미지\" style=\"width: 280px;height: auto; margin:0;padding:0\"/>\n");
					sb.append("						</span>\n");
					sb.append("					</span>\n");
					sb.append("					<span style=\"display: block; width: 100%; height: 40px; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
					sb.append("						<span style=\"color: #888888; height:40px; line-height: 40px; font-size: 11px;font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.025em; text-align: left; margin:0;padding-left:0px; padding-right: 0px;\">").append(goods.getBrandNm()).append("</span>\n");
					sb.append("					</span>\n");
					sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
					sb.append("						<span style=\"height:40px; color: #222222; font-size: 14px;font-weight: normal;line-height:20px;letter-spacing: -0.025em; text-align: left; margin:0; overflow: hidden; text-overflow: ellipsis; -webkit-line-clamp: 2; -webkit-box-orient: vertical; display: block; display: -webkit-box;\">").append(goods.getGoodsNm()).append("</span>\n");
					sb.append("					</span>\n");
					sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
					sb.append("						<span style=\"height:40px; line-height:40px; color: #222222; font-size: 16px; font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.025em; display: inline-block; width:auto; padding-top:0; padding-bottom:0; padding-left:0px; padding-right: 10px; text-align: left;\">").append(String.format("%,d", goods.getCurrPrice())).append("</span>\n");
					if(goods.getDcRate() > 0) {
						sb.append("						<span style=\"height:40px; line-height:40px; color: #888888; font-size: 14px; text-decoration: line-through;font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.025em; display: inline-block; width:auto; text-align: left;\">").append(String.format("%,d", goods.getListPrice())).append("</span>\n");
						sb.append("						<span style=\"height:40px; line-height:40px; color: #fd4802; font-size: 16px; font-weight: bold;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.05em; display: table-cell; width:40%; float: right; text-align: right;\">").append(goods.getDcRate()).append("%</span>\n");
					}
					sb.append("					</span>\n");

					// 상품혜택정보
					if (StringUtils.isNotBlank(goods.getBenefits())) {
						sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");

						String[] arrBenefit = goods.getBenefits().split(",");
						for (String benefit : arrBenefit) {
							sb.append("						<span style=\"width: auto; height: 20px;text-align: center;line-height:18px; display: inline-block; padding-top:0;padding-bottom:0;padding-left:5px;padding-right:5px; font-size:9px; color: #888888; background: #f5f5f5; border-width: 1px; border-style: solid; border-color: #f5f5f5; box-sizing: border-box;\">").append(benefit).append("</span>\n");
						}

						sb.append("					</span>\n");
					}

					// 상품타이틀명(세일링문구)
					sb.append("						<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
					sb.append("							<span style=\"color: #fd4802;font-size: 14px; line-height: 40px; height: 40px; font-weight: bold;letter-spacing: -0.05em;\">").append(StringUtils.isNotBlank(goods.getGoodsTnm()) ? goods.getGoodsTnm() : "&nbsp;").append("</span>\n");
					sb.append("						</span>\n");

					sb.append("					</a>\n");
					sb.append("				</td>\n");

					if (idx % 2 == 0) {
						sb.append("		</tr>\n");
						sb.append("		<tr>\n");
						sb.append("			<td height=\"20px;\" style=\"height: 20px;\"></td>\n");
						sb.append("		</tr>\n");
					}

					idx++;
				}
			}

			sb.append("	</tbody>\n");
			sb.append("</table>\n");
		} else if (coner.getGoodsCpr() == 3) { //상품노출갯수
			sb.append("<!-- 상품 3-Column 진열 테이블 -->\n");
			sb.append("<table style=\"width:100%;height:100%;margin:0;padding:0;font-size: 14px;\" cellspacing=\"0\" cellpadding=\"0\">\n");
			sb.append("	<tbody>\n");

			// 상품 목록
			if (goodsList != null && !goodsList.isEmpty()) {
				int idx = 1;
				for (MailTemplateGoods goods : goodsList) {
					if (idx % 3 == 1) {
						sb.append("		<tr>\n");
					} else {
						sb.append("			<td width=\"20px;\" height=\"420px;\" style=\"width: 20px;display: block;margin:0;padding:0\"></td>\n");
					}

					sb.append("			<td width=\"180px;\" height=\"430px;\" colspan=\"2\" style=\"margin:0;padding:0\">\n");
					sb.append("				<a href=\"#none\" target=\"_blank\" style=\"margin:0;padding:0;text-decoration: none;\">\n");
					sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:0; padding-right: 0px;\">\n");
					sb.append("						<span style=\"background: #ffffff;width: 180px;height: 270px;display: block;margin:0;padding:0\">\n");
					sb.append("							<img src=\"http:").append(uploadGoodsView).append("/").append(goods.getSysImgNm()).append("\" alt=\"이미지\" style=\"width: 180px;height: auto; margin:0;padding:0\"/>\n");
					sb.append("						</span>\n");
					sb.append("					</span>\n");
					sb.append("					<span style=\"display: block; width: 100%; height: 40px; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
					sb.append("						<span style=\"color: #888888; height:40px; line-height: 40px; font-size: 11px;font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.025em; text-align: left; margin:0;padding-left:0px; padding-right: 0px;\">").append(goods.getBrandNm()).append("</span>\n");
					sb.append("					</span>\n");
					sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
					sb.append("						<span style=\"height:40px; color: #222222; font-size: 14px;font-weight: normal;line-height:20px;letter-spacing: -0.025em; text-align: left; margin:0; overflow: hidden; text-overflow: ellipsis; -webkit-line-clamp: 2; -webkit-box-orient: vertical; display: block; display: -webkit-box;\">").append(goods.getGoodsNm()).append("</span>\n");
					sb.append("					</span>\n");
					sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
					sb.append("						<span style=\"height:40px; line-height:40px; color: #222222; font-size: 16px; font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.025em; display: inline-block; width:auto; padding-top:0; padding-bottom:0; padding-left:0px; padding-right: 10px; text-align: left;\">").append(String.format("%,d", goods.getCurrPrice())).append("</span>\n");
					if(goods.getDcRate() > 0) {
						sb.append("						<span style=\"height:40px; line-height:40px; color: #888888; font-size: 14px; text-decoration: line-through;font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.025em; display: inline-block; width:auto; text-align: left;\">").append(String.format("%,d", goods.getListPrice())).append("</span>\n");
						sb.append("						<span style=\"height:40px; line-height:40px; color: #fd4802; font-size: 16px; font-weight: bold;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.05em; display: table-cell; width:40%; float: right; text-align: right;\">").append(goods.getDcRate()).append("%</span>\n");
					}
					sb.append("					</span>\n");

					// 상품혜택정보
					if (StringUtils.isNotBlank(goods.getBenefits())) {
						sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");

						String[] arrBenefit = goods.getBenefits().split(",");
						for (String benefit : arrBenefit) {
							sb.append("						<span style=\"width: auto; height: 20px;text-align: center;line-height:18px; display: inline-block; padding-top:0;padding-bottom:0;padding-left:5px;padding-right:5px; font-size:9px; color: #888888; background: #f5f5f5; border-width: 1px; border-style: solid; border-color: #f5f5f5; box-sizing: border-box;\">").append(benefit).append("</span>\n");
						}

						sb.append("					</span>\n");
					}

					// 상품타이틀명(세일링문구)
					sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
					sb.append("						<span style=\"color: #fd4802;font-size: 14px; line-height: 40px; height: 40px; font-weight: bold;letter-spacing: -0.05em;\">").append(StringUtils.isNotBlank(goods.getGoodsTnm()) ? goods.getGoodsTnm() : "&nbsp;").append("</span>\n");
					sb.append("					</span>\n");

					sb.append("				</a>\n");
					sb.append("			</td>\n");

					if (idx % 3 == 0) {
						sb.append("		</tr>\n");
						sb.append("		<tr>\n");
						sb.append("			<td height=\"20px;\" style=\"height: 20px;\"></td>\n");
						sb.append("		</tr>\n");
					}

					idx++;
				}
			}

			sb.append("	</tbody>\n");
			sb.append("</table>\n");
		} else if (coner.getGoodsCpr() == 4) { //상품노출갯수
			// 상품 목록
			if (goodsList != null && !goodsList.isEmpty()) {
				int idx = 1;

				for (MailTemplateGoods goods : goodsList) {
					if (idx % 4 == 1) {
						sb.append("<!-- 4컷혼합 : 상품 1-Horizontal 진열 테이블 -->\n");
						sb.append("<table style=\"width:100%;height:100%;margin:0;padding:0;font-size: 14px;\" cellspacing=\"0\" cellpadding=\"0\">\n");
						sb.append("	<tbody>\n");
						sb.append("		<tr>\n");
						sb.append("			<td width=\"240px;\" height=\"360px;\" rowspan=\"6\" valign=\"top\" style=\"margin:0;padding:0;border-spacing:0;\">\n");
						sb.append("				<a href=\"#none\" target=\"_blank\" style=\"margin:0;padding:0\">\n");
						sb.append("					<span style=\"background: #ffffff;width: 240px;height: 360px;margin:0;padding:0\">\n");
						sb.append("						<img src=\"http:").append(uploadGoodsView).append("/").append(goods.getSysImgNm()).append("\" alt=\"이미지\" style=\"width: 240px;height: auto;display:block;margin:0;padding:0\"/>\n");
						sb.append("					</span>\n");
						sb.append("				</a>\n");
						sb.append("			</td>\n");
						sb.append("			<td width=\"40px;\" style=\"height: 40px;\"></td>\n");
						sb.append("			<td valign=\"top\" height=\"0px;\" style=\"height:0px; padding: 0; margin: 0;border-spacing:0;\"></td>\n");
						sb.append("		</tr>\n");
						sb.append("		<tr>\n");
						sb.append("			<td width=\"40px;\" style=\"height: 20px;\"></td>\n");
						sb.append("			<td valign=\"top\" height=\"20px;\" style=\"padding: 0; margin: 0;border-spacing:0;\">\n");
						sb.append("				<span style=\"color: #888888; line-height: 40px; font-size: 11px;font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;line-height:20px;letter-spacing: -0.025em; text-align: left; margin:0;\">").append(goods.getBrandNm()).append("</span>\n");
						sb.append("			</td>\n");
						sb.append("		</tr>\n");
						sb.append("		<tr>\n");
						sb.append("			<td width=\"40px;\" style=\"height: 20px;\"></td>\n");
						sb.append("			<td valign=\"top\" style=\"padding: 0; margin: 0;border-spacing:0;\">\n");
						sb.append("				<a href=\"#none\" target=\"_blank\" style=\"display: block; color: #222222; font-size: 14px; font-weight: normal; letter-spacing: -0.025em; text-decoration: none;\">\n");
						sb.append("					<span style=\"height:40px;color: #222222; font-size: 14px;font-weight: normal;line-height:20px;letter-spacing: -0.025em; text-align: left; margin:0; overflow: hidden; text-overflow: ellipsis; -webkit-line-clamp: 2; -webkit-box-orient: vertical; display: block; display: -webkit-box;\">").append(goods.getGoodsNm()).append("</span>\n");
						sb.append("				</a>\n");
						sb.append("			</td>\n");
						sb.append("		</tr>\n");
						sb.append("		<tr>\n");
						sb.append("			<td width=\"40px;\" style=\"height: 20px;\"></td>\n");
						sb.append("			<td valign=\"top\" style=\"padding: 0; margin: 0;border-spacing:0;\">\n");
						sb.append("				<span style=\"color: #222222; font-size: 16px; font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;line-height:20px;letter-spacing: -0.025em; display: inline-block; width:auto; padding-top:0; padding-bottom:0; padding-left:0; padding-right: 6px; text-align: left;\">").append(String.format("%,d", goods.getCurrPrice())).append("</span>\n");
						if(goods.getDcRate() > 0) {
							sb.append("				<span style=\"color: #888888; font-size: 14px; text-decoration: line-through;font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;line-height:20px;letter-spacing: -0.025em; display: inline-block; width:auto; text-align: left;\">").append(String.format("%,d", goods.getListPrice())).append("</span>\n");
							sb.append("				<span style=\"color: #fd4802; font-size: 16px; font-weight: bold;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;line-height:20px;letter-spacing: -0.05em; display: inline-block; width:50px; text-align: right;\">").append(goods.getDcRate()).append("%</span>\n");
						}
						sb.append("			</td>\n");
						sb.append("		</tr>\n");

						// 상품혜택정보
						if (StringUtils.isNotBlank(goods.getBenefits())) {
							sb.append("		<tr>\n");
							sb.append("			<td width=\"40px;\" style=\"height: 20px;\"></td>\n");
							sb.append("			<td valign=\"top\" height=\"35px;\" colspan=\"3\" style=\"padding: 0; margin: 0;border-spacing:0;\">\n");

							String[] arrBenefit = goods.getBenefits().split(",");
							for (String benefit : arrBenefit) {
								sb.append("				<span style=\"width: auto; height: 20px;text-align: center;line-height:18px; display: inline-block; padding-top:0;padding-bottom:0;padding-left:5px;padding-right:5px; font-size:9px; color: #888888; background: #f5f5f5; border-width: 1px; border-style: solid; border-color: #f5f5f5; box-sizing: border-box;\">").append(benefit).append("</span>\n");
							}

							sb.append("			</td>\n");
							sb.append("		</tr>\n");
						}

						// 상품타이틀명(세일링문구)
						if (StringUtils.isNotBlank(goods.getGoodsTnm())) {
							sb.append("		<tr>\n");
							sb.append("			<td width=\"40px;\" style=\"height: 20px;\"></td>\n");
							sb.append("			<td valign=\"top\" height=\"100px;\" colspan=\"3\" height=\"100px;\" style=\"height: 100px; padding: 0; margin: 0;border-spacing:0;\">\n");
							sb.append("				<span style=\"color: #fd4802;font-size: 14px; line-height: 18px; font-weight: bold;letter-spacing: -0.05em;\">").append(goods.getGoodsTnm()).append("</span>\n");
							sb.append("			</td>\n");
							sb.append("		</tr>\n");
						}

						sb.append("		<tr>\n");
						sb.append("			<td height=\"30px;\" colspan=\"3\" style=\"height: 30px;border-spacing:0;\"></td>\n");
						sb.append("		</tr>\n");
						sb.append("	</tbody>\n");
						sb.append("</table>\n");
					} else {
						if (idx % 4 == 2) {
							sb.append("<!-- 4컷혼합 : 상품 3-Column 진열 테이블 -->\n");
							sb.append("<table style=\"width:100%;margin:0;padding:0;font-size: 14px;\" cellspacing=\"0\" cellpadding=\"0\">\n");
							sb.append("	<tbody>\n");
							sb.append("		<tr>\n");
						} else {
							sb.append("			<td width=\"20px;\" height=\"420px;\" style=\"width: 20px;display: block;margin:0;padding:0\"></td>\n");
						}

						sb.append("			<td width=\"180px;\" height=\"430px;\" colspan=\"2\" style=\"margin:0;padding:0\">\n");
						sb.append("				<a href=\"#none\" target=\"_blank\" style=\"margin:0;padding:0;text-decoration: none;\">\n");
						sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:0; padding-right: 0px;\">\n");
						sb.append("						<span style=\"background: #ffffff;width: 180px;height: 270px;display: block;margin:0;padding:0\">\n");
						sb.append("							<img src=\"http:").append(uploadGoodsView).append("/").append(goods.getSysImgNm()).append("\" alt=\"이미지\" style=\"width: 180px;height: auto; margin:0;padding:0\"/>\n");
						sb.append("						</span>\n");
						sb.append("					</span>\n");
						sb.append("					<span style=\"display: block; width: 100%; height: 40px; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
						sb.append("						<span style=\"color: #888888; height:40px; line-height: 40px; font-size: 11px;font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.025em; text-align: left; margin:0;padding-left:0px; padding-right: 0px;\">").append(goods.getBrandNm()).append("</span>\n");
						sb.append("					</span>\n");
						sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
						sb.append("						<span style=\"height:40px; color: #222222; font-size: 14px;font-weight: normal;line-height:20px;letter-spacing: -0.025em; text-align: left; margin:0; overflow: hidden; text-overflow: ellipsis; -webkit-line-clamp: 2; -webkit-box-orient: vertical; display: block; display: -webkit-box;\">").append(goods.getGoodsNm()).append("</span>\n");
						sb.append("					</span>\n");
						sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
						sb.append("						<span style=\"height:40px; line-height:40px; color: #222222; font-size: 16px; font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.025em; display: inline-block; width:auto; padding-top:0; padding-bottom:0; padding-left:0px; padding-right: 10px; text-align: left;\">").append(String.format("%,d", goods.getCurrPrice())).append("</span>\n");
						if(goods.getDcRate() > 0) {
							sb.append("						<span style=\"height:40px; line-height:40px; color: #888888; font-size: 14px; text-decoration: line-through;font-weight: normal;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.025em; display: inline-block; width:auto; text-align: left;\">").append(String.format("%,d", goods.getListPrice())).append("</span>\n");
							sb.append("						<span style=\"height:40px; line-height:40px; color: #fd4802; font-size: 16px; font-weight: bold;font-family:'Trebuchet MS','Bahnschrift','Times New', verdana,Arial, Helvetica, sans-serif;letter-spacing: -0.05em; display: table-cell; width:40%; float: right; text-align: right;\">").append(goods.getDcRate()).append("%</span>\n");
						}
						sb.append("					</span>\n");

						// 상품혜택정보
						if (StringUtils.isNotBlank(goods.getBenefits())) {
							sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");

							String[] arrBenefit = goods.getBenefits().split(",");
							for (String benefit : arrBenefit) {
								sb.append("						<span style=\"width: auto; height: 20px;text-align: center;line-height:18px; display: inline-block; padding-top:0;padding-bottom:0;padding-left:5px;padding-right:5px; font-size:9px; color: #888888; background: #f5f5f5; border-width: 1px; border-style: solid; border-color: #f5f5f5; box-sizing: border-box;\">").append(benefit).append("</span>\n");
							}

							sb.append("					</span>\n");
						}

						// 상품타이틀명(세일링문구)
						sb.append("					<span style=\"display: block; width: 100%; padding-top:0; padding-bottom:0; padding-left:5px; padding-right: 5px;\">\n");
						sb.append("						<span style=\"color: #fd4802;font-size: 14px; line-height: 40px; height: 40px; font-weight: bold;letter-spacing: -0.05em;\">").append(StringUtils.isNotBlank(goods.getGoodsTnm()) ? goods.getGoodsTnm() : "&nbsp;").append("</span>\n");
						sb.append("					</span>\n");

						sb.append("				</a>\n");
						sb.append("			</td>\n");

						if (idx % 4 == 0) {
							sb.append("		</tr>\n");
							sb.append("		<tr>\n");
							sb.append("			<td height=\"20px;\" style=\"height: 20px;\" colspan=\"5\"></td>\n");
							sb.append("		</tr>\n");
							sb.append("	</tbody>\n");
							sb.append("</table>\n");
						}
					}

					idx++;
				}
			}
		}

		// 버튼
		sb.append("<table style=\"width:100%;margin:0;padding:0\" cellspacing=\"0\" cellpadding=\"0\">\n");
		sb.append("	<tbody>\n");
		sb.append("		<tr>\n");
		sb.append("			<td colspan=\"3\" width=\"179px;\" height=\"60px\" valign=\"bottom\"></td>\n");
		sb.append("			<td width=\"222px;\" height=\"60px;\" valign=\"bottom\" bgcolor=\"#ffffff\" style=\"background: #ffffff; text-align: center;margin:0;padding:0;border-spacing:0;\">\n");
		sb.append("				<a href=\"").append(coner.getMailtcLink()).append("\" target=\"_blank\" style=\"width:100%; height:100%; display: block; border-width: 1px; border-bottom-width: 1px;border-right-width: 1px;border-top-width: 1px;border-left-width: 1px; border-style: solid; border-color: #a7a7a7; box-sizing: border-box; padding: 20px; color: #222222; font-size: 17px; font-weight: 600; letter-spacing: -0.025em; text-decoration: none;\">더많은 상품 보기</a>\n");
		sb.append("			</td>\n");
		sb.append("			<td colspan=\"3\" width=\"179px;\" height=\"60px\" valign=\"bottom\"></td>\n");
		sb.append("		</tr>\n");
		sb.append("		<tr>\n");
		sb.append("			<td style=\"height: 60px;\"></td>\n");
		sb.append("		</tr>\n");
		sb.append("	</tbody>\n");
		sb.append("</table>\n");

		return sb.toString();
	}

}
