package com.style24.admin.biz.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gagaframework.web.parameter.GagaMap;
import com.gagaframework.web.util.GagaDateUtil;
import com.gagaframework.web.util.GagaFileUtil;
import com.gagaframework.web.util.GagaStringUtil;
import com.style24.admin.biz.dao.TsaPlanDao;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.persistence.domain.Notice;
import com.style24.persistence.domain.Plan;

import lombok.extern.slf4j.Slf4j;

/**
 * 기획전 Service
 * 
 * @author sowon
 * @since 2021. 02. 04
 */
@Service
@Slf4j
public class TsaPlanService {

	@Autowired
	private TsaPlanDao planDao;
	
	@Autowired
	private Environment env;
	
	@Autowired
	private ObjectMapper mapper;
	
	/**
	 * 기획전 코너 전체 조회
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 4
	 */
	public Collection<Plan> getPlanAllList(Plan param) {
		return planDao.getPlanAllList(param);
	}
	
	/**
	 * 기획전 웹 등록
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 8
	 */
	@Transactional("shopTxnManager")
	public void createPlanInfo(Plan param) {
		Plan plan = new Plan();
		int ind = 1;
		param.setRegNo(TsaSession.getInfo().getUserNo());
		param.setUpdNo(TsaSession.getInfo().getUserNo());
		if(param.getMainPimg() != null && !"".equals(param.getMainPimg())){
			String imgInd = String.valueOf(ind);
			if (ind < 10) {
				imgInd = "0" + imgInd;
			}
			String planUploadPath = env.getProperty("upload.default.target.path");
			String yearMonth = GagaDateUtil.getToday().substring(0, 6);
			planUploadPath = GagaFileUtil.getConcatenationPath(planUploadPath, "/planning");
			File newFile = new File(GagaFileUtil.getConcatenationPath(planUploadPath, param.getMainPimg()));
			planUploadPath = GagaFileUtil.getConcatenationPath(planUploadPath, yearMonth);
			String newFilename = "PLANNING_" + GagaDateUtil.getTodayDateTime() + "_" + imgInd + "." + StringUtils.getFilenameExtension(param.getMainPimg());
			File uniqueFile = GagaFileUtil.getUniqueFile(new File(GagaFileUtil.getConcatenationPath(planUploadPath, newFilename)));
			// Rename a file
			File path = new File(planUploadPath);
			if (!path.exists()) {
				path.mkdir();
			}
			newFile.renameTo(uniqueFile);
			param.setMainPimg("/planning/" + yearMonth + "/" + newFilename);
			param.setMainMimg("/planning/" + yearMonth + "/" + newFilename);
		}

		planDao.createPlanInfo(param);
		
		String[] brand = param.getMultiBrand();
		// 기획전 브랜드 insert
		for (int j = 0; j < brand.length; j++) {
			plan.setBrandGroupNo(brand[j]);
			plan.setPlanSq(param.getPlanSq());
			plan.setRegNo(TsaSession.getInfo().getUserNo());
			plan.setUpdNo(TsaSession.getInfo().getUserNo());
			planDao.createPlanBrand(plan);
		}
		    
		String[] cate = param.getMultiCate();
		// 기획전 카테고리insert
		for (int j = 0; j < cate.length; j++) {
			plan.setCateNo(cate[j]);
			plan.setPlanSq(param.getPlanSq());
			plan.setRegNo(TsaSession.getInfo().getUserNo());
			plan.setUpdNo(TsaSession.getInfo().getUserNo());
			planDao.createPlanCate(plan);
		}
		
		String[] channel = param.getMultiChannel();
		// 기획전 제휴채널 insert
		for (int j = 0; j < channel.length; j++) {
			plan.setAfLinkCd(channel[j]);
			plan.setPlanSq(param.getPlanSq());
			plan.setRegNo(TsaSession.getInfo().getUserNo());
			planDao.createPlanAfChannel(plan);
		}
		
		// 기획전 회원구분 insert
		String[] custGb = param.getUsableCustGbArr();
		for (int j = 0; j < custGb.length; j++) {
			plan.setCustGb(custGb[j]);
			plan.setPlanSq(param.getPlanSq());
			plan.setRegNo(TsaSession.getInfo().getUserNo());
			planDao.createPlanCustGb(plan);
		}
		
		// 기획전 회원등급 insert
		String[] custGrade = param.getUsableCustGradeArr();
		for (int j = 0; j < custGrade.length; j++) {
			plan.setCustGrade(custGrade[j]);
			plan.setPlanSq(param.getPlanSq());
			plan.setRegNo(TsaSession.getInfo().getUserNo());
			planDao.createPlanCustGrade(plan);
		}
		
		// 이벤트 출석체크
		Collection<Plan> attendData = param.getAttendData();
		for (Plan attendInfo : attendData) {
			attendInfo.setRegNo(TsaSession.getInfo().getUserNo());
			attendInfo.setUpdNo(TsaSession.getInfo().getUserNo());
			attendInfo.setPlanSq(param.getPlanSq());
			planDao.savePlanAttendBenefit(attendInfo);
		}

	}

	/**
	 * 기획전 목록
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 5
	 */
	public Collection<Plan> getPlanList(Plan param) {
		return planDao.getPlanList(param);
	}
	
	/**
	 * 기획전 목록 카운트 조회
	 * @param  param
	 * @return int
	 * @author sowon
	 * @since 2021. 2. 5
	 */
	public int getPlanListCount(Plan param) {
		return planDao.getPlanListCount(param);
	}
	
	/**
	 * 기획전 삭제 처리
	 *
	 * @param
	 * @return
	 * @author rladbwnd5
	 * @since 2021. 2. 5
	 */
	@Transactional("shopTxnManager")
	public void deletePlan(Collection<Plan> paramList) {
		for (Plan plan : paramList) {
			plan.setUpdNo(TsaSession.getInfo().getUserNo());
			planDao.deletePlan(plan);
		}
	}
	
	/**
	 * 기획전 상세 정보
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 8
	 */
	public Plan getPlanDetailInfo(Plan param) {
		Collection<Plan> planList = planDao.getPlanList(param);
		Plan planInfo = new Plan();

		if (!planList.isEmpty()) {
			for (Plan plan : planList) {
				planInfo = plan;
			}
		}

		// 날짜 설정
		String[] stDate = planInfo.getDispStdt().split(" ");
		planInfo.setStartSearchDate(stDate[0]);
		planInfo.setStartSearchTime(GagaStringUtil.replace(stDate[1], ":", "").substring(0, 6));

		String[] edDate = planInfo.getDispEddt().split(" ");
		planInfo.setEndSearchDate(edDate[0]);
		planInfo.setEndSearchTime(GagaStringUtil.replace(edDate[1], ":", "").substring(0, 6));

		return planInfo;
	}
	
	
	/**
	 * 기획전 웹 수정
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 8
	 */
	@Transactional("shopTxnManager")
	public void updatePlanInfo(Plan param) {
		Plan plan = new Plan();
		log.debug("param {}", param);
		String targetPath = env.getProperty("upload.default.target.path") + "/planning";
		param.setRegNo(TsaSession.getInfo().getUserNo());
		param.setUpdNo(TsaSession.getInfo().getUserNo());

		// 이미지 삭제 여부에 따른 이미지 삭제 처리
		if (("Y").equals(param.getOrgMainPimgDelYn())) {
			deleteFile(targetPath, param.getOrgMainPimg());
		}

		if (("Y").equals(param.getOrgMainMimgDelYn())) {
			deleteFile(targetPath, param.getOrgMainMimg());
		}

		if (("Y").equals(param.getOrgDtlPimgDelYn())) {
			deleteFile(targetPath, param.getOrgDtlPimg());
		}

		if (("Y").equals(param.getOrgDtlMimgDelYn())) {
			deleteFile(targetPath, param.getOrgDtlMimg());
		}

		if (("Y").equals(param.getOrgReplyImgDelYn())) {
			//param.setReplyImg(null);
			deleteFile(targetPath, param.getOrgReplyImg());
		}

		// 기존 기획전 카테고리 삭제 
		planDao.deletePlanCate(param);
		
		String[] cate = param.getMultiCate();
		// 기획전 카테고리insert
		for (int j = 0; j < cate.length; j++) {
			plan.setCateNo(cate[j]);
			plan.setPlanSq(param.getPlanSq());
			plan.setRegNo(TsaSession.getInfo().getUserNo());
			plan.setUpdNo(TsaSession.getInfo().getUserNo());
			planDao.createPlanCate(plan);
		}
		
		// 기존 기획전 브랜드 삭제
		planDao.deletePlanBrand(param);;
		
		
		String[] brand = param.getMultiBrand();
		// 기획전 브랜드 insert
		for (int j = 0; j < brand.length; j++) {
			plan.setBrandGroupNo(brand[j]);
			plan.setPlanSq(param.getPlanSq());
			plan.setRegNo(TsaSession.getInfo().getUserNo());
			plan.setUpdNo(TsaSession.getInfo().getUserNo());
			planDao.createPlanBrand(plan);
		}
		
		// 기존 기획전 제휴채널 삭제
		planDao.deletePlanAfChannel(param);
		
		
		String[] channel = param.getMultiChannel();
		// 기획전 제휴채널 insert
		for (int j = 0; j < channel.length; j++) {
			plan.setAfLinkCd(channel[j]);
			plan.setPlanSq(param.getPlanSq());
			plan.setRegNo(TsaSession.getInfo().getUserNo());
			planDao.createPlanAfChannel(plan);
		}
		
		//기존 회원구분 삭제
		planDao.deletePlanCustGb(param);
		
		
		// 기획전 회원구분 insert
		String[] custGb = param.getUsableCustGbArr();
		for (int j = 0; j < custGb.length; j++) {
			plan.setCustGb(custGb[j]);
			plan.setPlanSq(param.getPlanSq());
			plan.setRegNo(TsaSession.getInfo().getUserNo());
			planDao.createPlanCustGb(plan);
		}
		
		//기존 회원등급 삭제
		planDao.deletePlanCustGrade(param);
		
		
		// 기획전 회원등급 insert
		String[] custGrade = param.getUsableCustGradeArr();
		for (int j = 0; j < custGrade.length; j++) {
			plan.setCustGrade(custGrade[j]);
			plan.setPlanSq(param.getPlanSq());
			plan.setRegNo(TsaSession.getInfo().getUserNo());
			planDao.createPlanCustGrade(plan);
		}
		
		int ind = 1;
		param.setMainMimg(param.getMainPimg());
		if(param.getMainPimg() != null && !"".equals(param.getMainPimg())){
			String imgInd = String.valueOf(ind);
			if (ind < 10) {
				imgInd = "0" + imgInd;
			}
			if (!param.getMainPimg().contains("planning")) {
				String planUploadPath = env.getProperty("upload.default.target.path");
				String yearMonth = GagaDateUtil.getToday().substring(0, 6);
				planUploadPath = GagaFileUtil.getConcatenationPath(planUploadPath, "/planning");
				File newFile = new File(GagaFileUtil.getConcatenationPath(planUploadPath, param.getMainPimg()));
				planUploadPath = GagaFileUtil.getConcatenationPath(planUploadPath, yearMonth);
				String newFilename = "PLANNING_" + GagaDateUtil.getTodayDateTime() + "_" + imgInd + "." + StringUtils.getFilenameExtension(param.getMainPimg());
				File uniqueFile = GagaFileUtil.getUniqueFile(new File(GagaFileUtil.getConcatenationPath(planUploadPath, newFilename)));
				// Rename a file
				File path = new File(planUploadPath);
				if (!path.exists()) {
					path.mkdir();
				}
				newFile.renameTo(uniqueFile);
				param.setMainPimg("/planning/" + yearMonth + "/" + newFilename);
				param.setMainMimg("/planning/" + yearMonth + "/" + newFilename);
			}
			
		}

		// 이벤트 출석체크
		planDao.deletePlanAttendBenefitList(param);
		
		Collection<Plan> attendData = param.getAttendData();
		for (Plan attendInfo : attendData) {
			attendInfo.setRegNo(TsaSession.getInfo().getUserNo());
			attendInfo.setUpdNo(TsaSession.getInfo().getUserNo());
			attendInfo.setPlanSq(param.getPlanSq());
			planDao.savePlanAttendBenefit(attendInfo);
		}
		
		// 최종업데이트 
		planDao.updatePlanInfo(param);

	}
	
	/**
	 * 기획전 복사
	 *
	 * @param 복사대상 기획전 번호
	 * @return 복사된 기획전 번호
	 * @author sowon
	 * @since 2021. 02. 09
	 */
	@Transactional("shopTxnManager")
	public Plan copyPlan(Plan param) {
		Plan plan = new Plan();
		int userNo = TsaSession.getInfo().getUserNo();
		// 01. 기획전 복사
		param.setRegNo(userNo);
		param.setUpdNo(userNo);
		planDao.createPlanCopy(param);
		Integer prePlanSq = param.getPlanSq();
		
		plan.setRegNo(userNo);
		plan.setUpdNo(userNo);
		
		// 02. 기획전 브랜드 복사
		plan.setPlanSq(param.getCopyPlanSq());
		Collection <Plan> brandList = planDao.getPlanBrandList(plan);
		
		for(Plan brandCd : brandList){
			plan.setPlanSq(param.getPlanSq());
			plan.setBrandGroupNo(brandCd.getBrandGroupNo());
			plan.setCopyPlanSq(param.getCopyPlanSq()); 
			planDao.createPlanBrandCopy(plan);
		}
		
		// 02. 기획전 카테고리 복사
		plan.setPlanSq(param.getCopyPlanSq());
		Collection <Plan> cateList = planDao.getPlanCateList(plan);
		
		for(Plan cateNo : cateList){
			plan.setPlanSq(param.getPlanSq());
			plan.setCateNo(cateNo.getCateNo());
			plan.setCopyPlanSq(param.getCopyPlanSq()); 
			planDao.createPlanCateCopy(plan);
		}
		
		// 03.기획전 제휴채널 복사
		plan.setPlanSq(param.getCopyPlanSq());
		Collection <Plan> afChannelList = planDao.getPlanAfChannelList(plan);
		
		for(Plan afChannel : afChannelList){
			plan.setPlanSq(param.getPlanSq());
			plan.setAfLinkCd(afChannel.getAfLinkCd());
			plan.setCopyPlanSq(param.getCopyPlanSq()); 
			planDao.createPlanAfChannelCopy(plan);
		}
		
		// 04.고객 유형복사
		plan.setPlanSq(param.getCopyPlanSq());
		Collection <Plan> custGbList = planDao.getPlanCustGbList(plan);
		
		for(Plan custGb : custGbList){
			plan.setPlanSq(param.getPlanSq());
			plan.setCustGb(custGb.getCustGb());
			plan.setCopyPlanSq(param.getCopyPlanSq()); 
			planDao.createPlanCustGbCopy(plan);
		}
		
		// 05.고객 등급 복사
		plan.setPlanSq(param.getCopyPlanSq());
		Collection <Plan> custGradeList = planDao.getPlanCustGradeList(plan);
		
		for(Plan custGrade : custGradeList){
			plan.setPlanSq(param.getPlanSq());
			plan.setCustGrade(custGrade.getCustGrade());
			plan.setCopyPlanSq(param.getCopyPlanSq()); 
			planDao.createPlanCustGradeCopy(plan);
		}
		
		// 06. 이벤트 출석체크 복사
		plan.setPlanSq(param.getCopyPlanSq());
		Collection<Plan> attendData = planDao.getPlanAttendBenefitList(plan);
		for (Plan attendInfo : attendData) {
			plan.setPlanSq(param.getPlanSq());
			plan.setBasDays(attendInfo.getBasDays());
			planDao.createPlanAttendBenefitCopy(plan);
		}
		// 복사한 기획전 번호 셋팅
		plan.setPlanSq(prePlanSq);
		return plan;
	}
	
	/**
	 * 파일 삭제
	 *
	 * @param targetPath
	 * @param excelFilename
	 * @author sowon
	 * @since 2021. 2. 8
	 */
	private void deleteFile(String targetPath, String excelFilename) {
		// 파일 삭제
		try {
			GagaFileUtil.deleteFile(GagaFileUtil.getConcatenationPath(targetPath, excelFilename));
		} catch (IOException e) {
			// Nothing Do
		}
	}
	
	/**
	 * 기획전 브랜드 목록
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 2. 9
	 */
	public Collection<Plan> getPlanBrandList(Plan param){
		return planDao.getPlanBrandList(param);
	}
	
	/**
	 * 기획전 카테고리 목록
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 2. 15
	 */
	public Collection<Plan> getPlanCateList(Plan param){
		return planDao.getPlanCateList(param);
	}
	

	/**
	 * 기획전 제휴채널 목록
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 3. 4
	 */
	public Collection<Plan> getPlanAfChannelList(Plan param){
		return planDao.getPlanAfChannelList(param);
	}
	
	/**
	 * 기획전 코너 목록 조회
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 17
	 */
	public Collection<Plan> getPlanCornerList(Plan param) {
		
		Collection<Plan> cornerList = planDao.getPlanCornerList(param);
		for (Plan corner : cornerList) {
			// 날짜 설정
			String[] stDate = corner.getDispStdt().split(" ");
			corner.setStartSearchDate(stDate[0]);                                                 
			corner.setStartSearchTime(stDate[1].substring(0, 8));
			
			String[] edDate = corner.getDispEddt().split(" ");
			corner.setEndSearchDate(edDate[0]);                                                 
			corner.setEndSearchTime(edDate[1].substring(0, 8));
		}
		return cornerList;
	}
	
	/**
	 * 기획전 코너 시퀀스 조회
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 15
	 */
	public Integer getPlanDtlSq(Plan param) {
		return planDao.getPlanDtlSq(param);
	}
	
	/**
	 * 기획전 코너 삭제 처리
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 17
	 */
	@Transactional("shopTxnManager")
	public void deletePlanCornerInfo(Collection<Plan> paramList) {
		for (Plan cornerInfo : paramList) {
			planDao.deletePlanCornerInfo(cornerInfo);
			planDao.deletePlanCornerGoods(cornerInfo);
		}
	}
	

	/**
	 * 기획전 코너 상품 삭제 처리
	 *
	 * @param
	 * @return
	 * @author rladbwnd5
	 * @since 2021. 2. 18
	 */
	@Transactional("shopTxnManager")
	public void deletePlanCornerGoods(Collection<Plan> paramList) {
		for (Plan cornerInfo : paramList) {
			planDao.deletePlanCornerGoods(cornerInfo);
		}
	}
	
	/**
	 * 기획전 코너 저장
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 17
	 */
	@Transactional("shopTxnManager")
	public void savePlanCornerInfo(Collection<Plan> paramList) {
		for (Plan cornerInfo : paramList) {
			cornerInfo.setRegNo(TsaSession.getInfo().getUserNo());
			cornerInfo.setUpdNo(TsaSession.getInfo().getUserNo());
			planDao.savePlanCornerInfo(cornerInfo);
		}

	}
	
	/**
	 * 기획전 코너 상품 조회
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 17
	 */
	public Collection<Plan> getPlanGoodsList(Plan param) {
		return planDao.getPlanGoodsList(param);
	}
	
	/**
	 * 기획전 코너 상품 조회 건수
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 17
	 */
	public int getPlanGoodsListCount(Plan param) {
		return planDao.getPlanGoodsListCount(param);
	}
	
	/**
	 * 기획전 코너 상품 추가
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 17
	 */
	@Transactional("shopTxnManager")
	public void savePlanGoods(Collection<Plan> paramList) {
		for (Plan param : paramList) {
			savePlanCornerGoods(param);
		}
	}

	/**
	 * 기획전 코너 상품 저장
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 17
	 */
	@Transactional("shopTxnManager")
	public void savePlanCornerGoods(Plan param) {
		Plan plan = new Plan();
		
		plan.setRegNo(TsaSession.getInfo().getUserNo());
		plan.setUpdNo(TsaSession.getInfo().getUserNo());
		plan.setGoodsCd(param.getGoodsCd());
		plan.setDispOrd(param.getDispOrd());
		plan.setPlanDtlSq(param.getPlanDtlSq());
		planDao.savePlanCornerGoods(plan);
	}
	
	/**
	 * 기획전 코너 복사
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 18
	 */
	@Transactional("shopTxnManager")
	public void copyPlanCorner(Collection<Plan> paramList) {
		try {
			for (Plan planInfo : paramList) {
				planInfo.setRegNo(TsaSession.getInfo().getUserNo());
				planInfo.setUpdNo(TsaSession.getInfo().getUserNo());
				planInfo.setPlanDtlSq(planDao.getPlanDtlSq(planInfo));
				// 코너 복사
				planDao.createPlanCornerCopy(planInfo);
				
				// 상품 복사 시 적용 할 상세일련번호
				Integer planDtlSq = planInfo.getPlanDtlSq();

				// 상품 조회
				Plan planSearch = new Plan();
				planSearch.setPlanDtlSq(planInfo.getCopyPlanDtlSq());
				Collection<Plan> goodsList = planDao.getPlanGoodsList(planSearch);

				// 상품 복사
				if (goodsList != null && !goodsList.isEmpty()) {
					for (Plan goodsInfo : goodsList) {
						goodsInfo.setPlanDtlSq(planDtlSq);
						goodsInfo.setRegNo(planInfo.getRegNo());
						goodsInfo.setUpdNo(planInfo.getUpdNo());
						planDao.createPlanGoodsCopy(goodsInfo);
					}
				}
			}
		} catch (Exception e) {
			log.info("[기획전 복사 중 Error] {}", e.toString());
			throw new IllegalStateException("기획전 복사 실패");
		}
	}

	/**
	 * 기획전 코너 상품 엑셀 저장
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 18
	 */
	@Transactional("shopTxnManager")
	public void createExceluploadGoods(Collection<GagaMap> ecxelGoodsList, Plan param) {
		String targetPath = env.getProperty("upload.excel.target.path");

		if ((ecxelGoodsList == null || ecxelGoodsList.isEmpty())) {
			deleteFile(targetPath, param.getExcelFileNm());
			throw new IllegalStateException("저장 할 데이터가 없습니다.");
		}

		Collection<Plan> dtlPlan = new ArrayList<>();
		Collection<Plan> goodsList = new ArrayList<>();

		long tempPlanDtlSq = 0;
		for (GagaMap gagaMap : ecxelGoodsList) {
			Plan planGoods = mapper.convertValue(gagaMap, Plan.class);
			planGoods.setPlanSq(param.getPlanSq());

			String planDtlSq = String.valueOf(planGoods.getPlanDtlSq());
			if (org.apache.commons.lang3.StringUtils.isBlank(planDtlSq)) {
				throw new IllegalStateException("상품코드 : " + planGoods.getGoodsCd() + " 기획전 코너 번호가 없습니다.");
			}

			if (org.apache.commons.lang3.StringUtils.isBlank(planGoods.getGoodsCd())) {
				throw new IllegalStateException("비어 있는 상품코드가 존재합니다.");
			}

			// 중복 확인 위한 데이터 적제
			if (tempPlanDtlSq != planGoods.getPlanDtlSq()) {
				dtlPlan.add(planGoods);
			}

			// 상품 데이터 적제
			goodsList.add(planGoods);
		}

		// 기획전 유효성 체크
		for (Plan plan : dtlPlan) {
			Plan searchCorner = new Plan();
			searchCorner.setPlanSq(plan.getPlanSq());
			searchCorner.setPlanDtlSq(plan.getPlanDtlSq());

			Collection<Plan> cornerList = planDao.getPlanCornerList(searchCorner);

			if (cornerList == null || cornerList.isEmpty()) {
				throw new IllegalStateException("상품코드 : " + plan.getGoodsCd() + "의 " + plan.getPlanDtlSq() + " 기획전 코너가 존재 하지 않습니다.");
			}
		}

		// 기획전 상품 저장
		for (Plan goods : goodsList) {
			savePlanCornerGoods(goods);
		}
	}
	
	/**
	 * 기획전 회원구분 조회
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 23
	 */
	public Collection<Plan> getPlanCustGbList(Plan param) {
		return planDao.getPlanCustGbList(param);	
	}
	
	/**
	 * 기획전 회원등급 조회
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 23
	 */
	public Collection<Plan> getPlanCustGradeList(Plan param) {
		return planDao.getPlanCustGradeList(param);	
	}
	
	/**
	 * 기획전 템플릿 등록
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 25
	 */
	@Transactional("shopTxnManager")
	public void createPlanContents(Plan param) {
		Integer regNo = TsaSession.getInfo().getUserNo();
		
		param.setRegNo(regNo);
		
		// 리뷰 등록
		Plan rePlan = new Plan();
		//리뷰 등록 - tb_plan_contents
//		if(param.getReTitle() != "" || !org.apache.commons.lang3.StringUtils.isAllEmpty(param.getMultiPlanReview())) {
			rePlan.setRegNo(regNo);
			rePlan.setUpdNo(regNo);
			rePlan.setTmplType("G082_10");
			rePlan.setPlanSq(param.getPlanSq());
			rePlan.setTitle(param.getReTitle());
			rePlan.setDispYn(param.getReDispYn());
			rePlan.setDispOrd(param.getReDispOrd());
			rePlan.setPlanContSq(param.getRePlanContSq());
			planDao.createPlanContents(rePlan);

			// 기존 리뷰아이템 삭제
			planDao.deletePlanContentItem(param.getRePlanContSq());
			//리뷰 아이템 등록
			if(param.getMultiPlanReview().length>0) {
				String[] reItemVal = param.getMultiPlanReview();
				String[] startDate = param.getMultiPlanReviewStartD();
				String[] startTime = param.getMultiPlanReviewStartT();
				String[] endDate = param.getMultiPlanReviewEndD();
				String[] endTime = param.getMultiPlanReviewEndT();
				// 기획전 브랜드 insert
				for (int j = 0; j < reItemVal.length; j++) {
					rePlan.setItemVal(reItemVal[j]);
					rePlan.setReviewDispStdt(startDate[j] + ' ' +startTime[j]);
					rePlan.setReviewDispEddt(endDate[j] + ' ' +endTime[j]);
					planDao.createPlanContentsItem(rePlan);
				}
			}
//		}
		
		// 쿠폰등록
		Plan cuPlan = new Plan();
		//쿠폰 등록 - tb_plan_contents
//		if(param.getCuTitle() != "" || param.getMultiPlanCoupon().length>0) {
			cuPlan.setRegNo(regNo);
			cuPlan.setUpdNo(regNo);
			cuPlan.setTmplType("G082_20");
			cuPlan.setPlanSq(param.getPlanSq());
			cuPlan.setTitle(param.getCuTitle());
			cuPlan.setDispYn(param.getCuDispYn());
			cuPlan.setDispOrd(param.getCuDispOrd());
			cuPlan.setPlanContSq(param.getCuPlanContSq());
			String[] cpnNote = param.getMultiPlanCouponNotice();
			for (int i = 0; i < cpnNote.length; i++) {
				if(i==0) 
					cuPlan.setCpnNote0(cpnNote[i]);
				else if(i==1) 
					cuPlan.setCpnNote1(cpnNote[i]);
				else if(i==2) 
					cuPlan.setCpnNote2(cpnNote[i]);
				else if(i==3) 
					cuPlan.setCpnNote3(cpnNote[i]);
				else if(i==4) 
					cuPlan.setCpnNote4(cpnNote[i]);
				else if(i==5) 
					cuPlan.setCpnNote5(cpnNote[i]);
				else if(i==6) 
					cuPlan.setCpnNote6(cpnNote[i]);
				else if(i==7) 
					cuPlan.setCpnNote7(cpnNote[i]);
				else if(i==8) 
					cuPlan.setCpnNote8(cpnNote[i]);
				else if(i==9) 
					cuPlan.setCpnNote9(cpnNote[i]);
			}
			planDao.createPlanContents(cuPlan);
			
			// 기존 쿠폰아이템 삭제
			planDao.deletePlanContentItem(param.getCuPlanContSq());
						
			//쿠폰아이템 등록
			if(param.getMultiPlanCoupon().length>0) {
				String[] cuItemVal = param.getMultiPlanCoupon();
				
				// 기획전 브랜드 insert
				for (int j = 0; j < cuItemVal.length; j++) {
					cuPlan.setItemVal(cuItemVal[j]);
					planDao.createPlanContentsItem(cuPlan);
				}
			}
//		}
		
		//이미지등록
		Plan filePlan = new Plan();
		if(!org.apache.commons.lang3.StringUtils.isAllEmpty(param.getMultiPlanFile()) || param.getFileDispOrd() >=0) {
			filePlan.setRegNo(regNo);
			filePlan.setUpdNo(regNo);
			filePlan.setTmplType("G082_30");
			filePlan.setPlanSq(param.getPlanSq());
			filePlan.setDispYn(param.getFileDispYn());
			filePlan.setDispOrd(param.getFileDispOrd());
			filePlan.setPlanContSq(param.getFilePlanContSq());
			
			planDao.createPlanContents(filePlan);
			
			// 기존  파일 삭제
			planDao.deletePlanContentItem(param.getFilePlanContSq()); 
			
			//파일아이템 등록
			String[] fileItemVal = param.getMultiPlanFile();
			String[] fileItemVal2 = param.getMultiPlanFile2();
			String[] fileItemVal3 = param.getMultiPlanFile3();
			int ind = 1;
			for (int j = 0; j < fileItemVal.length; j++) {
				String imgInd = String.valueOf(ind);
				if (ind < 10) {
					imgInd = "0" + imgInd;
				}
				String planUploadPath = env.getProperty("upload.default.target.path");
				String yearMonth = GagaDateUtil.getToday().substring(0, 6);
				planUploadPath = GagaFileUtil.getConcatenationPath(planUploadPath, "/planning");
				File newFile = new File(GagaFileUtil.getConcatenationPath(planUploadPath, fileItemVal[j]));
				planUploadPath = GagaFileUtil.getConcatenationPath(planUploadPath, yearMonth);
				String newFilename = "PLANNING_" + GagaDateUtil.getTodayDateTime() + "_" + imgInd + "." + StringUtils.getFilenameExtension(fileItemVal[j]);
				File uniqueFile = GagaFileUtil.getUniqueFile(new File(GagaFileUtil.getConcatenationPath(planUploadPath, newFilename)));
				// Rename a file
				File path = new File(planUploadPath);
				if (!path.exists()) {
					path.mkdir();
				}
				newFile.renameTo(uniqueFile);
				if (fileItemVal[j].contains("/planning")) {
					filePlan.setItemVal(fileItemVal[j]);
				}else {
					filePlan.setItemVal("/planning/" + yearMonth + "/" + newFilename);
				}
				
				filePlan.setLinkUrl(fileItemVal2[j]);
				filePlan.setLinkOpenGb((fileItemVal3[j]));
				planDao.createPlanContentsItem(filePlan);
				ind++;
			}
		}
		
		// 유의사항등록
		Plan noticePlan = new Plan();
		//유의사항 등록 - tb_plan_contents
		if(!org.apache.commons.lang3.StringUtils.isAllEmpty(param.getMultiPlanNotice()) || param.getNoticeDispOrd() >=0) {
			noticePlan.setRegNo(regNo);
			noticePlan.setUpdNo(regNo);
			noticePlan.setTmplType("G082_60");
			noticePlan.setPlanSq(param.getPlanSq());
			noticePlan.setDispYn(param.getNoticeDispYn());
			noticePlan.setDispOrd(param.getNoticeDispOrd());
			noticePlan.setPlanContSq(param.getNoticePlanContSq());
			planDao.createPlanContents(noticePlan);
			
			// 기존 유의사항삭제
			planDao.deletePlanContentItem(param.getNoticePlanContSq());
						
			//유의사항아이템 등록
			if(param.getMultiPlanNotice().length>0) {
				String[] noticeItemVal = param.getMultiPlanNotice();
				
				// 유의사항 insert
				for (int j = 0; j < noticeItemVal.length; j++) {
					noticePlan.setItemVal(noticeItemVal[j]);
					planDao.createPlanContentsItem(noticePlan);
				}
			}
		}
		
		//HTML[PC] 상단 하단 등록
		if(param.getFsrcMobileTop() != "" || param.getFsrcPcTop() != "") {

			
			// 웹용 상단PC 등록
			if (org.apache.commons.lang3.StringUtils.isNotBlank(param.getFsrcPcTop())) {
				
				createPlanFsrcPcTop(param);
			}
		}
		//HTML[하단] 등록
		if(param.getFsrcMobileBtm() != "" || param.getFsrcPcBtm() != "") {
			
			
			// 웹용 상단PC 등록
			if (org.apache.commons.lang3.StringUtils.isNotBlank(param.getFsrcPcBtm())) {
				createPlanFsrcPcBtm(param);
			}

		}
		
		//상품 등록 -- 1
		for (Plan multiPlanGoodsContent1 : param.getMultiPlanGoodsContent1()) {
			
			if (!StringUtils.isEmpty(multiPlanGoodsContent1.getGoodsType())) {
				String goodsTitle = multiPlanGoodsContent1.getGoodsTitle();
				String goodsUrl = multiPlanGoodsContent1.getGoodsUrl();
				String goodsType = multiPlanGoodsContent1.getGoodsType();
				int goodsDispOrd = multiPlanGoodsContent1.getGoodsDispOrd();
				String goodsDispYn = multiPlanGoodsContent1.getGoodsDispYn();
				Integer goodsPlanContSq = multiPlanGoodsContent1.getGoodsPlanContSq();
				String linkOpenGb = multiPlanGoodsContent1.getGoodsLinkOpenGb();
				
				Plan goods1 = new Plan();
				goods1.setRegNo(regNo);
				goods1.setUpdNo(regNo);
				goods1.setTitle(goodsTitle);
				goods1.setDispOrd(goodsDispOrd);
				goods1.setPlanSq(param.getPlanSq());
				goods1.setDispYn(goodsDispYn);
				goods1.setTmplType(goodsType);
				goods1.setLinkOpenGb(linkOpenGb);
				goods1.setLinkUrl(goodsUrl);
				goods1.setPlanContSq(goodsPlanContSq);
				
				planDao.createPlanContents(goods1);
				
				// 기존  상품1 삭제
				planDao.deletePlanContentItem(goodsPlanContSq); 
				
				for (Plan multiPlanGoodsContentVal1 : param.getMultiPlanGoodsContentVal1()) {
					if(param.getMultiPlanGoodsContentVal1().size()>0) {
						goods1.setItemVal(multiPlanGoodsContentVal1.getGoodsItemVal());
						planDao.createPlanContentsItem(goods1);
					}
					
				}
			}
		}
		
		//상품 등록 -- 2
		for (Plan multiPlanGoodsContent2 : param.getMultiPlanGoodsContent2()) {
			
			if (!StringUtils.isEmpty(multiPlanGoodsContent2.getGoodsType())) {
				String goodsTitle = multiPlanGoodsContent2.getGoodsTitle();
				String goodsUrl = multiPlanGoodsContent2.getGoodsUrl();
				String goodsType = multiPlanGoodsContent2.getGoodsType();
				int goodsDispOrd = multiPlanGoodsContent2.getGoodsDispOrd();
				String goodsDispYn = multiPlanGoodsContent2.getGoodsDispYn();
				Integer goodsPlanContSq = multiPlanGoodsContent2.getGoodsPlanContSq();
				String linkOpenGb = multiPlanGoodsContent2.getGoodsLinkOpenGb();
				
				Plan goods2 = new Plan();
				goods2.setRegNo(regNo);
				goods2.setUpdNo(regNo);
				goods2.setTitle(goodsTitle);
				goods2.setDispOrd(goodsDispOrd);
				goods2.setPlanSq(param.getPlanSq());
				goods2.setDispYn(goodsDispYn);
				goods2.setTmplType(goodsType);
				goods2.setLinkUrl(goodsUrl);
				goods2.setLinkOpenGb(linkOpenGb);
				goods2.setPlanContSq(goodsPlanContSq);
				
				planDao.createPlanContents(goods2);
				
				// 기존  상품2 삭제
				planDao.deletePlanContentItem(goodsPlanContSq); 
				
				for (Plan multiPlanGoodsContentVal2 : param.getMultiPlanGoodsContentVal2()) {
					if(param.getMultiPlanGoodsContentVal2().size()>0) {
						goods2.setItemVal(multiPlanGoodsContentVal2.getGoodsItemVal());
						planDao.createPlanContentsItem(goods2);
					}
					
				}
			}
		}
		
		//상품 등록 -- 3
		for (Plan multiPlanGoodsContent3 : param.getMultiPlanGoodsContent3()) {
			
			if (!StringUtils.isEmpty(multiPlanGoodsContent3.getGoodsType())) {
				String goodsTitle = multiPlanGoodsContent3.getGoodsTitle();
				String goodsUrl = multiPlanGoodsContent3.getGoodsUrl();
				String goodsType = multiPlanGoodsContent3.getGoodsType();
				int goodsDispOrd = multiPlanGoodsContent3.getGoodsDispOrd();
				String goodsDispYn = multiPlanGoodsContent3.getGoodsDispYn();
				Integer goodsPlanContSq = multiPlanGoodsContent3.getGoodsPlanContSq();
				String linkOpenGb = multiPlanGoodsContent3.getGoodsLinkOpenGb();
				
				Plan goods3 = new Plan();
				goods3.setRegNo(regNo);
				goods3.setUpdNo(regNo);
				goods3.setTitle(goodsTitle);
				goods3.setDispOrd(goodsDispOrd);
				goods3.setPlanSq(param.getPlanSq());
				goods3.setDispYn(goodsDispYn);
				goods3.setTmplType(goodsType);
				goods3.setLinkUrl(goodsUrl);
				goods3.setLinkOpenGb(linkOpenGb);
				goods3.setPlanContSq(goodsPlanContSq);
				
				planDao.createPlanContents(goods3);
				
				// 기존  상품3 삭제
				planDao.deletePlanContentItem(goodsPlanContSq); 
				
				for (Plan multiPlanGoodsContentVal3 : param.getMultiPlanGoodsContentVal3()) {
					if(param.getMultiPlanGoodsContentVal3().size()>0) {
						goods3.setItemVal(multiPlanGoodsContentVal3.getGoodsItemVal());
						planDao.createPlanContentsItem(goods3);
					}
					
				}
			}
		}
		
		//상품 등록 -- 4
		for (Plan multiPlanGoodsContent4 : param.getMultiPlanGoodsContent4()) {
			
			if (!StringUtils.isEmpty(multiPlanGoodsContent4.getGoodsType())) {
				String goodsTitle = multiPlanGoodsContent4.getGoodsTitle();
				String goodsUrl = multiPlanGoodsContent4.getGoodsUrl();
				String goodsType = multiPlanGoodsContent4.getGoodsType();
				int goodsDispOrd = multiPlanGoodsContent4.getGoodsDispOrd();
				String goodsDispYn = multiPlanGoodsContent4.getGoodsDispYn();
				Integer goodsPlanContSq = multiPlanGoodsContent4.getGoodsPlanContSq();
				String linkOpenGb = multiPlanGoodsContent4.getGoodsLinkOpenGb();
				
				Plan goods4 = new Plan();
				goods4.setRegNo(regNo);
				goods4.setUpdNo(regNo);
				goods4.setTitle(goodsTitle);
				goods4.setDispOrd(goodsDispOrd);
				goods4.setPlanSq(param.getPlanSq());
				goods4.setDispYn(goodsDispYn);
				goods4.setTmplType(goodsType);
				goods4.setLinkUrl(goodsUrl);
				goods4.setPlanContSq(goodsPlanContSq);
				goods4.setLinkOpenGb(linkOpenGb);
				
				planDao.createPlanContents(goods4);
				
				// 기존  상품4 삭제
				planDao.deletePlanContentItem(goodsPlanContSq); 
				
				for (Plan multiPlanGoodsContentVal4 : param.getMultiPlanGoodsContentVal4()) {
					if(param.getMultiPlanGoodsContentVal4().size()>0) {
						goods4.setItemVal(multiPlanGoodsContentVal4.getGoodsItemVal());
						planDao.createPlanContentsItem(goods4);
					}
					
				}
			}
		}
		
		//상품 등록 -- 5
		for (Plan multiPlanGoodsContent5 : param.getMultiPlanGoodsContent5()) {
			
			if (!StringUtils.isEmpty(multiPlanGoodsContent5.getGoodsType())) {
				String goodsTitle = multiPlanGoodsContent5.getGoodsTitle();
				String goodsUrl = multiPlanGoodsContent5.getGoodsUrl();
				String goodsType = multiPlanGoodsContent5.getGoodsType();
				int goodsDispOrd = multiPlanGoodsContent5.getGoodsDispOrd();
				String goodsDispYn = multiPlanGoodsContent5.getGoodsDispYn();
				Integer goodsPlanContSq = multiPlanGoodsContent5.getGoodsPlanContSq();
				String linkOpenGb = multiPlanGoodsContent5.getGoodsLinkOpenGb();

				
				Plan goods5 = new Plan();
				goods5.setRegNo(regNo);
				goods5.setUpdNo(regNo);
				goods5.setTitle(goodsTitle);
				goods5.setDispOrd(goodsDispOrd);
				goods5.setPlanSq(param.getPlanSq());
				goods5.setDispYn(goodsDispYn);
				goods5.setTmplType(goodsType);
				goods5.setLinkUrl(goodsUrl);
				goods5.setPlanContSq(goodsPlanContSq);
				goods5.setLinkOpenGb(linkOpenGb);
				
				planDao.createPlanContents(goods5);
				
				// 기존  상품5 삭제
				planDao.deletePlanContentItem(goodsPlanContSq); 
				
				for (Plan multiPlanGoodsContentVal5 : param.getMultiPlanGoodsContentVal5()) {
					if(param.getMultiPlanGoodsContentVal5().size()>0) {
						goods5.setItemVal(multiPlanGoodsContentVal5.getGoodsItemVal());
						planDao.createPlanContentsItem(goods5);
					}
					
				}
			}
		}
		
		//상품 등록 -- 6
		for (Plan multiPlanGoodsContent6 : param.getMultiPlanGoodsContent6()) {
			
			if (!StringUtils.isEmpty(multiPlanGoodsContent6.getGoodsType())) {
				String goodsTitle = multiPlanGoodsContent6.getGoodsTitle();
				String goodsUrl = multiPlanGoodsContent6.getGoodsUrl();
				String goodsType = multiPlanGoodsContent6.getGoodsType();
				int goodsDispOrd = multiPlanGoodsContent6.getGoodsDispOrd();
				String goodsDispYn = multiPlanGoodsContent6.getGoodsDispYn();
				Integer goodsPlanContSq = multiPlanGoodsContent6.getGoodsPlanContSq();
				String linkOpenGb = multiPlanGoodsContent6.getGoodsLinkOpenGb();
				
				Plan goods6 = new Plan();
				goods6.setRegNo(regNo);
				goods6.setUpdNo(regNo);
				goods6.setTitle(goodsTitle);
				goods6.setDispOrd(goodsDispOrd);
				goods6.setPlanSq(param.getPlanSq());
				goods6.setDispYn(goodsDispYn);
				goods6.setTmplType(goodsType);
				goods6.setLinkUrl(goodsUrl);
				goods6.setPlanContSq(goodsPlanContSq);
				goods6.setLinkOpenGb(linkOpenGb);
				
				planDao.createPlanContents(goods6);
				
				// 기존  상품6 삭제
				planDao.deletePlanContentItem(goodsPlanContSq); 
				
				for (Plan multiPlanGoodsContentVal6 : param.getMultiPlanGoodsContentVal6()) {
					if(param.getMultiPlanGoodsContentVal6().size()>0) {
						goods6.setItemVal(multiPlanGoodsContentVal6.getGoodsItemVal());
						planDao.createPlanContentsItem(goods6);
					}
					
				}
			}
		}
		
		//상품 등록 -- 7
		for (Plan multiPlanGoodsContent7 : param.getMultiPlanGoodsContent7()) {
			
			if (!StringUtils.isEmpty(multiPlanGoodsContent7.getGoodsType())) {
				String goodsTitle = multiPlanGoodsContent7.getGoodsTitle();
				String goodsUrl = multiPlanGoodsContent7.getGoodsUrl();
				String goodsType = multiPlanGoodsContent7.getGoodsType();
				int goodsDispOrd = multiPlanGoodsContent7.getGoodsDispOrd();
				String goodsDispYn = multiPlanGoodsContent7.getGoodsDispYn();
				Integer goodsPlanContSq = multiPlanGoodsContent7.getGoodsPlanContSq();
				String linkOpenGb = multiPlanGoodsContent7.getGoodsLinkOpenGb();
				
				Plan goods7 = new Plan();
				goods7.setRegNo(regNo);
				goods7.setUpdNo(regNo);
				goods7.setTitle(goodsTitle);
				goods7.setDispOrd(goodsDispOrd);
				goods7.setPlanSq(param.getPlanSq());
				goods7.setDispYn(goodsDispYn);
				goods7.setTmplType(goodsType);
				goods7.setLinkUrl(goodsUrl);
				goods7.setPlanContSq(goodsPlanContSq);
				goods7.setLinkOpenGb(linkOpenGb);
				
				planDao.createPlanContents(goods7);
				
				// 기존  상품7 삭제
				planDao.deletePlanContentItem(goodsPlanContSq); 
				
				for (Plan multiPlanGoodsContentVal7 : param.getMultiPlanGoodsContentVal7()) {
					if(param.getMultiPlanGoodsContentVal7().size()>0) {
						goods7.setItemVal(multiPlanGoodsContentVal7.getGoodsItemVal());
						planDao.createPlanContentsItem(goods7);
					}
					
				}
			}
		}
		
		//상품 등록 -- 8
		for (Plan multiPlanGoodsContent8 : param.getMultiPlanGoodsContent8()) {
			
			if (!StringUtils.isEmpty(multiPlanGoodsContent8.getGoodsType())) {
				String goodsTitle = multiPlanGoodsContent8.getGoodsTitle();
				String goodsUrl = multiPlanGoodsContent8.getGoodsUrl();
				String goodsType = multiPlanGoodsContent8.getGoodsType();
				int goodsDispOrd = multiPlanGoodsContent8.getGoodsDispOrd();
				String goodsDispYn = multiPlanGoodsContent8.getGoodsDispYn();
				Integer goodsPlanContSq = multiPlanGoodsContent8.getGoodsPlanContSq();
				String linkOpenGb = multiPlanGoodsContent8.getGoodsLinkOpenGb();
				
				Plan goods8 = new Plan();
				goods8.setRegNo(regNo);
				goods8.setUpdNo(regNo);
				goods8.setTitle(goodsTitle);
				goods8.setDispOrd(goodsDispOrd);
				goods8.setPlanSq(param.getPlanSq());
				goods8.setDispYn(goodsDispYn);
				goods8.setTmplType(goodsType);
				goods8.setLinkUrl(goodsUrl);
				goods8.setPlanContSq(goodsPlanContSq);
				goods8.setLinkOpenGb(linkOpenGb);
				
				planDao.createPlanContents(goods8);
				
				// 기존  상품8 삭제
				planDao.deletePlanContentItem(goodsPlanContSq); 
				
				for (Plan multiPlanGoodsContentVal8 : param.getMultiPlanGoodsContentVal8()) {
					if(param.getMultiPlanGoodsContentVal8().size()>0) {
						goods8.setItemVal(multiPlanGoodsContentVal8.getGoodsItemVal());
						planDao.createPlanContentsItem(goods8);
					}
					
				}
			}
		}
		
		//상품 등록 -- 9
		for (Plan multiPlanGoodsContent9 : param.getMultiPlanGoodsContent9()) {
			
			if (!StringUtils.isEmpty(multiPlanGoodsContent9.getGoodsType())) {
				String goodsTitle = multiPlanGoodsContent9.getGoodsTitle();
				String goodsUrl = multiPlanGoodsContent9.getGoodsUrl();
				String goodsType = multiPlanGoodsContent9.getGoodsType();
				int goodsDispOrd = multiPlanGoodsContent9.getGoodsDispOrd();
				String goodsDispYn = multiPlanGoodsContent9.getGoodsDispYn();
				Integer goodsPlanContSq = multiPlanGoodsContent9.getGoodsPlanContSq();
				String linkOpenGb = multiPlanGoodsContent9.getGoodsLinkOpenGb();
				
				Plan goods9 = new Plan();
				goods9.setRegNo(regNo);
				goods9.setUpdNo(regNo);
				goods9.setTitle(goodsTitle);
				goods9.setDispOrd(goodsDispOrd);
				goods9.setPlanSq(param.getPlanSq());
				goods9.setDispYn(goodsDispYn);
				goods9.setTmplType(goodsType);
				goods9.setLinkUrl(goodsUrl);
				goods9.setPlanContSq(goodsPlanContSq);
				goods9.setLinkOpenGb(linkOpenGb);
				
				planDao.createPlanContents(goods9);
				
				// 기존  상품9 삭제
				planDao.deletePlanContentItem(goodsPlanContSq); 
				
				for (Plan multiPlanGoodsContentVal9 : param.getMultiPlanGoodsContentVal9()) {
					if(param.getMultiPlanGoodsContentVal9().size()>0) {
						goods9.setItemVal(multiPlanGoodsContentVal9.getGoodsItemVal());
						planDao.createPlanContentsItem(goods9);
					}
					
				}
			}
		}
		
		//상품 등록 -- 10
		for (Plan multiPlanGoodsContent10 : param.getMultiPlanGoodsContent10()) {
			
			if (!StringUtils.isEmpty(multiPlanGoodsContent10.getGoodsType())) {
				String goodsTitle = multiPlanGoodsContent10.getGoodsTitle();
				String goodsUrl = multiPlanGoodsContent10.getGoodsUrl();
				String goodsType = multiPlanGoodsContent10.getGoodsType();
				int goodsDispOrd = multiPlanGoodsContent10.getGoodsDispOrd();
				String goodsDispYn = multiPlanGoodsContent10.getGoodsDispYn();
				Integer goodsPlanContSq = multiPlanGoodsContent10.getGoodsPlanContSq();
				String linkOpenGb = multiPlanGoodsContent10.getGoodsLinkOpenGb();
				
				Plan goods10 = new Plan();
				goods10.setRegNo(regNo);
				goods10.setUpdNo(regNo);
				goods10.setTitle(goodsTitle);
				goods10.setDispOrd(goodsDispOrd);
				goods10.setPlanSq(param.getPlanSq());
				goods10.setDispYn(goodsDispYn);
				goods10.setTmplType(goodsType);
				goods10.setLinkUrl(goodsUrl);
				goods10.setPlanContSq(goodsPlanContSq);
				goods10.setLinkOpenGb(linkOpenGb);
				
				planDao.createPlanContents(goods10);
				
				// 기존  상품10 삭제
				planDao.deletePlanContentItem(goodsPlanContSq); 
				
				for (Plan multiPlanGoodsContentVal10 : param.getMultiPlanGoodsContentVal10()) {
					if(param.getMultiPlanGoodsContentVal10().size()>0) {
						goods10.setItemVal(multiPlanGoodsContentVal10.getGoodsItemVal());
						planDao.createPlanContentsItem(goods10);
					}
					
				}
			}
		}
	}
	
	/**
	 * 기획전 템플릿 상세조회 - 리뷰
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 3 .2
	 */
	public Collection<Plan> getPlanContentReviewList(Plan param){
		return planDao.getPlanContentReviewList(param);
	}
	
	/**
	 * 기획전 템플릿 상세조회 - 쿠폰
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 3 .2
	 */
	public Collection<Plan> getPlanContentCouponList(Plan param){
		return planDao.getPlanContentCouponList(param);
	}
	
	/**
	 * 기획전 템플릿 상세조회 - 쿠폰유의사항
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 5 .26
	 */
	public Plan getPlanContentCouponNotice(Plan param){
		return planDao.getPlanContentCouponNotice(param);
	}
	
	
	/**
	 * 기획전 템플릿 상세조회 - 파일
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 3 .2
	 */
	public Collection<Plan> getPlanContentFileList(Plan param){
		return planDao.getPlanContentFileList(param);
	}
	
	/**
	 * 기획전 템플릿 상세조회 - 유의사항
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 5 .28
	 */
	public Collection<Plan> getPlanContentNoticeList(Plan param){
		return planDao.getPlanContentNoticeList(param);
	}
	
	
	/**
	 * 기획전 템플릿 상세조회 - html
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 3 .2
	 */
	public Collection<Plan> getPlanContentFsrcList(Plan param){
		return planDao.getPlanContentFsrcList(param);
	}
	
	/**
	 * 기획전 템플릿 상세조회 - goods
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 3 .2
	 */
	public Collection<Plan> getPlanContentGoodsList(Plan param){
		return planDao.getPlanContentGoodsList(param);
	}
	
	/**
	 * 기획전 템플릿 상세조회 - goods 목록조회
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 3 .2
	 */
	public Collection<Plan> getPlanContentGoodsDetailList(Plan param){
		return planDao.getPlanContentGoodsDetailList(param);
	}
	/**
	 * 기획전 템플릿 이미지 첨부파일 삭제
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 3. 2
	 */
	@Transactional("shopTxnManager")
	public void deletePlanTemplateFile(Plan plan) {
		planDao.deletePlanTemplateFile(plan);
	}
	
	
	
	/*
	 * 기획전 템플릿 HTML[상단] PC 모바일 소스 등록
	 */
	@Transactional("shopTxnManager")
	public Plan createPlanFsrcPcTop(Plan param) {
		Plan fsrcTop = new Plan();
		
		fsrcTop.setFrontGb("T");                 
		fsrcTop.setRegNo(param.getRegNo());      
		fsrcTop.setPlanSq(param.getPlanSq());    
		fsrcTop.setDispYn(param.getTopFsrcDispYn());
		
		fsrcTop.setFsrcPc(param.getFsrcPcTop());
		fsrcTop.setFsrcMob(param.getFsrcMobileTop());
		planDao.savePlanFsrcInfo(fsrcTop);
		
		return param;
	}


	/*
	 * 기획전 템플릿 HTML[하단] PC 모바일 소스 등록
	 */
	@Transactional("shopTxnManager")
	public Plan createPlanFsrcPcBtm(Plan param) {
		Plan fsrcBtm = new Plan();
		
		fsrcBtm.setFrontGb("B");                 
		fsrcBtm.setRegNo(param.getRegNo());      
		fsrcBtm.setPlanSq(param.getPlanSq());    
		fsrcBtm.setDispYn(param.getBtmFsrcDispYn());
		fsrcBtm.setFsrcPc(param.getFsrcPcBtm());
		fsrcBtm.setFsrcMob(param.getFsrcMobileBtm());
		planDao.savePlanFsrcInfo(fsrcBtm);

		return param;
	}

	
	/**
	 * 기획전 템플릿 목록 카운트 조회
	 * @param  param
	 * @return int
	 * @author sowon
	 * @since 2021. 2. 25
	 */
	public int getPlanContentsCount(Plan param) {
		return planDao.getPlanContentsCount(param);
	}

	/**
	 * 기획전 템플릿 상품목록 삭제
	 * @param  
	 * @return 
	 * @author sowon
	 * @since 2021. 3. 3
	 */
	@Transactional("shopTxnManager")
	public void deletePlanContentItemGoods(Integer planContSq) {
		
		planDao.deletePlanContentItemGoods(planContSq);
	
	}

	/**
	 * 기획전 템플릿 컨텐츠 삭제
	 * @param  
	 * @return 
	 * @author sowon
	 * @since 2021. 3. 3
	 */

	@Transactional("shopTxnManager")
	public void deletePlanContentItem(Integer planContSq) {
		planDao.deletePlanContentItem(planContSq);
		
	}
	
	/**
	 * 기획전 브랜드 목록
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 4. 6
	 */
	public Collection<Plan> getPlanAttendBenefitList(Plan plan){
		return planDao.getPlanAttendBenefitList(plan);
	}

	/**
	 * 기획전 댓글 목록
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 5. 24
	 */
	public Collection<Plan> getPlanReplyList(Plan param) {
		return planDao.getPlanReplyList(param);
	}
	
	/**
	 * 기획전 리스트 카운트 조회
	 * @param  param
	 * @return int
	 * @author sowon
	 * @since 2021. 5. 24
	 */
	public int getPlanReplyListCount(Plan param) {
		return planDao.getPlanReplyListCount(param);
	}

	/**
	 * 기획전 댓글 사진 목록
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 5. 24
	 */
	public Collection<Plan> getPlanReplyAttachList(Plan param){
		return planDao.getPlanReplyAttachList(param);
	}

	/**
	 * 기획전 댓글 삭제
	 * @param 
	 * @author sowon
	 * @since 2021. 5. 24
	 */
	@Transactional("shopTxnManager")
	public void deleteReply(Collection<Plan> paramList) {
		
		for (Plan plan : paramList) {
			planDao.deleteReply(plan);
			planDao.deleteReplyAttach(plan);
		}
	}
		
	/**
	 * 기획전 카테고리 목록
	 *
	 * @param plan
	 * @return
	 * @author sowon
	 * @since 2021. 05. 05
	 */
	public Collection<Plan> getPlanningCategoryList(Plan param){
		return planDao.getPlanningCategoryList(param);
	}
	
	/**
	 * 출석체크 쿠폰 유효검사
	 *
	 * @param plan
	 * @return
	 * @author sowon
	 * @since 2021. 06. 09
	 */
	public Collection<Plan> getPlanAttendCoupon(Plan param){
		return planDao.getPlanAttendCoupon(param);
	}
	
	/**
	 * 총알배송 그룹브랜드 목록
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 6. 29
	 */
	public Collection<Plan> getShotSelfBrandList(){
		return planDao.getShotSelfBrandList();
	}
	
	/**
	 * 총알배송 브랜드 업데이트
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 6. 29
	 */
	@Transactional("shopTxnManager")
	public void updateShotBrand(Collection<Plan> paramList){
		for (Plan cornerInfo : paramList) {
			cornerInfo.setRegNo(TsaSession.getInfo().getUserNo());
			cornerInfo.setUpdNo(TsaSession.getInfo().getUserNo());
			planDao.updateShotBrand(cornerInfo);
		}
	}
	
	/**
	 * 총알배송 브랜드 상품 카운트
	 *
	 * @param plan
	 * @return
	 * @author sowon
	 * @since 2021. 6. 29
	 */
	public int getShotBrandGoodsListCount(Plan param) {
		return planDao.getShotBrandGoodsListCount(param);
	}
	
	/**
	 * 총알배송 그룹브랜드 상품리스트
	 *
	 * @param 
	 * @return
	 * @author sowon
	 * @since 2021. 6. 29
	 */
	public Collection<Plan> getShotBrandGoodsList(Plan param){
		return planDao.getShotBrandGoodsList(param);
	}
	

	/**
	 * 총알배송 그룹브랜드 상품 추가
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 6. 129
	 */
	@Transactional("shopTxnManager")
	public void saveShotBrandGoods(Collection<Plan> paramList) {
		for (Plan param : paramList) {
			saveShotBrandGoods(param);
		}
	}
	
	/**
	 * 총알배송 그룹브랜드 상품 저장
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 2. 17
	 */
	@Transactional("shopTxnManager")
	public void saveShotBrandGoods(Plan param) {
		Plan plan = new Plan();
		
		plan.setRegNo(TsaSession.getInfo().getUserNo());
		plan.setUpdNo(TsaSession.getInfo().getUserNo());
		plan.setDelYn("N");
		plan.setGoodsCd(param.getGoodsCd());
		plan.setDispOrd(param.getDispOrd());
		plan.setBrandGroupNo(param.getBrandGroupNo());
		planDao.saveShotBrandGoods(plan);
	}
	
	/**
	 * 총알배송 그룹브랜드 상품삭제
	 *
	 * @param
	 * @return
	 * @author sowon
	 * @since 2021. 6. 29
	 */
	@Transactional("shopTxnManager")
	public void deleteShotBrandGoods(Collection<Plan> paramList) {
		for (Plan brandInfo : paramList) {
			brandInfo.setUpdNo(TsaSession.getInfo().getUserNo());
			planDao.deleteShotBrandGoods(brandInfo);
		}
	}
	

}
