package com.madeu.service.web.webeventpetit.impl;

import com.madeu.constants.Constants;
import com.madeu.dao.web.webeventpetit.WebEventPetitSqlMapDAO;
import com.madeu.dao.web.webauthmenurelation.WebAuthMenuRelationSqlMapDAO;
import com.madeu.dao.web.webmember.WebMemberSqlMapDAO;
import com.madeu.service.common.file.FileService;
import com.madeu.service.web.webeventpetit.WebEventPetitService;
import com.madeu.util.ValidationCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service("WebEventPetitService")
public class WebEventPetitServiceImpl implements WebEventPetitService {

    @Autowired
    private WebEventPetitSqlMapDAO webEventPetitSqlMapDAO;

    @Autowired
    private WebMemberSqlMapDAO webMemberSqlMapDAO;

    @Autowired
    private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;

    @Autowired
    private FileService fileService;

	@Value("${url.cdn}")
	String CDN_URL;

    /**
     * 홈페이지 이벤트(쁘띠) 리스트 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListWebEventPetitIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebEventPetitServiceImpl selectListWebEventPetitIntro START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        try{
            boolean check  = true;

            String menuClass    = String.valueOf(paramMap.get("menuClass"));

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(menuClass)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","메뉴 정보가 없습니다.");
            }

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);
                int authMenuRelationlistMapSize = authMenuRelationlistMap.size();

                if(1==authMenuRelationlistMapSize){
                    map.put("msgCode", Constants.OK);
                    map.put("success","true");

                    map.put("selectUseYn", authMenuRelationlistMap.get(0).get("selectUseYn"));
                    map.put("insertUseYn", authMenuRelationlistMap.get(0).get("insertUseYn"));
                    map.put("updateUseYn", authMenuRelationlistMap.get(0).get("updateUseYn"));
                    map.put("deleteUseYn", authMenuRelationlistMap.get(0).get("deleteUseYn"));
                    map.put("downloadUseYn", authMenuRelationlistMap.get(0).get("downloadUseYn"));
                }
                else {
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebEventPetitServiceImpl selectListWebEventPetitIntro END");
        return map;
    }

    /**
     * 홈페이지 이벤트(쁘띠) 리스트 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListWebEventPetit(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebEventPetitServiceImpl selectListWebEventPetit START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        log.debug("webEventPetitSearchKeywordParam  :"+paramMap.get("webEventPetitSearchKeywordParam"));
        log.debug("webEventPetitSort                :"+paramMap.get("webEventPetitSort"));
        log.debug("webEventPetitStart               :"+paramMap.get("webEventPetitStart"));
        log.debug("webEventPetitLimit               :"+paramMap.get("webEventPetitLimit"));
        log.debug("webEventPetitSort                :"+paramMap.get("webEventPetitSort"));
        log.debug("webEventPetitDir                 :"+paramMap.get("webEventPetitDir"));
        log.debug("webEventPetitCurrentPage         :"+paramMap.get("webEventPetitCurrentPage"));

        log.debug("webEventPetitSearchStartDate     :"+paramMap.get("webEventPetitSearchStartDate"));
        log.debug("webEventPetitSearchEndDate       :"+paramMap.get("webEventPetitSearchEndDate"));

        List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();

        try{
            boolean check  = true;

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);

                if(1==authMenuRelationlistMap.size()){

                    if(("Y").equals(authMenuRelationlistMap.get(0).get("selectUseYn"))) {

                    }
                    else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "조회 권한 정보가 없습니다.");
                    }
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }

            if(true == check){
                // 정렬 
                if(null==paramMap.get("webEventPetitDir")||("").equals(paramMap.get("webEventPetitDir"))){

                }
                else{
                    String dir = String.valueOf(paramMap.get("webEventPetitDir"));
                    if(("A").equals(dir)){
                        paramMap.put("webEventPetitDir", "DESC");
                    }
                    else if(("B").equals(dir)){
                        paramMap.put("webEventPetitDir", "ASC");
                    }
                    else {
                        paramMap.put("webEventPetitDir", "DESC");
                    }
                }

                paramMap.put("useYn", "Y");

                List<Map<String, Object>> totalCountListMap = webEventPetitSqlMapDAO.selectTotalWebEventPetitCount(paramMap);
                int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));

                if(0 < totalCount) {
                    // 리스트 조회
                    listMap = webEventPetitSqlMapDAO.selectListWebEventPetit(paramMap);
                }

                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("totalCount", totalCount);
                map.put("rows",listMap);
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebEventPetitServiceImpl selectListWebEventPetit END");
        return map;
    }

    /**
     * 홈페이지 이벤트(쁘띠)정보 상세 조회 (one)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectWebEventPetit(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebEventPetitServiceImpl selectEventPetit START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        log.debug("muWebEventPetitId          :"+paramMap.get("muWebEventPetitId"));
        
        log.debug("categorylist          :"+paramMap.get("categorylist"));

        try{
            boolean check  = true;
            String muWebEventPetitId    = String.valueOf(paramMap.get("muWebEventPetitId"));

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(muWebEventPetitId)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","이벤트(쁘띠)정보가 없습니다.");
            }

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);

                if(1==authMenuRelationlistMap.size()){

                    if(("Y").equals(authMenuRelationlistMap.get(0).get("selectUseYn"))) {

                    }
                    else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "조회 권한 정보가 없습니다.");
                    }
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }

            if(true == check){
                List<Map<String, Object>> listMap = webEventPetitSqlMapDAO.selectWebEventPetit(paramMap);
                List<Map<String, Object>> listMap2 = webEventPetitSqlMapDAO.selectWebEventListPetit(paramMap);

                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("rows",listMap);
                map.put("eventlist", listMap2);
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebEventPetitServiceImpl selectWebEventPetit END");
        return map;
    }

    /**
     * 홈페이지 메인 배너 상세 이미지파일 저장
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> uploadFile(
            HashMap<String, Object> paramMap, MultipartFile file) throws Exception{
        log.debug("WebEventPetitServiceImpl uploadFile START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        log.debug("file                :"+file);

        try{
            boolean check  = true;
            String tId = String.valueOf(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c1 = Calendar.getInstance();
            String tDate = sdf.format(c1.getTime());

            if(null==file|| file.isEmpty()){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","이미지 정보가 없습니다.");
            }

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);
                int authMenuRelationlistMapSize = authMenuRelationlistMap.size();

                if(1==authMenuRelationlistMapSize){

                    if(("Y").equals(authMenuRelationlistMap.get(0).get("insertUseYn"))) {

                    }
                    else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "등록 권한 정보가 없습니다.");
                    }
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }

            if(true == check){

                if (null != file && !file.isEmpty()) {
                    HashMap<String, Object> uploadFileMap = new HashMap<String, Object>();
                    uploadFileMap.put("type", "WE");
                    uploadFileMap.put("fileType", "P");
                    uploadFileMap.put("fileName", file.getOriginalFilename());
                    HashMap<String, Object> fileMap = fileService.uploadFile(uploadFileMap, file);
                    String cdnUrl       = String.valueOf(fileMap.get("cdnUrl"));
                    String filePath     = String.valueOf(fileMap.get("filePath"));
                    String fileName     = String.valueOf(fileMap.get("fileName"));
                    String chgFileName  = file.getOriginalFilename();
                    paramMap.put("cdnUrl", cdnUrl);
                    paramMap.put("filePath", (CDN_URL).concat(filePath));
                    paramMap.put("fileName", fileName);
                    paramMap.put("originalFileName", chgFileName);
                }
                paramMap.put("tDate", tDate);
                paramMap.put("tId", tId);

                map.put("rows",paramMap);
                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "등록되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("WebEventPetitServiceImpl uploadFile END");
        return map;
    }

    /**
     * 홈페이지 이벤트(쁘띠) 콘텐츠 등록 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> insertWebEventPetitIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebEventPetitServiceImpl insertWebEventPetitIntro START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        try{
            boolean check  = true;

            String menuClass    = String.valueOf(paramMap.get("menuClass"));

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(menuClass)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","메뉴 정보가 없습니다.");
            }

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);
                int authMenuRelationlistMapSize = authMenuRelationlistMap.size();

                if(1==authMenuRelationlistMapSize){
                    map.put("msgCode", Constants.OK);
                    map.put("success","true");

                    map.put("selectUseYn", authMenuRelationlistMap.get(0).get("selectUseYn"));
                    map.put("insertUseYn", authMenuRelationlistMap.get(0).get("insertUseYn"));
                    map.put("updateUseYn", authMenuRelationlistMap.get(0).get("updateUseYn"));
                    map.put("deleteUseYn", authMenuRelationlistMap.get(0).get("deleteUseYn"));
                    map.put("downloadUseYn", authMenuRelationlistMap.get(0).get("downloadUseYn"));
                }
                else {
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }
            List<Map<String, Object>> listMap = webEventPetitSqlMapDAO.selectWebEventPetitCategory(paramMap);
            map.put("categorylist", listMap);
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebEventPetitServiceImpl insertWebEventPetitIntro END");
        return map;
    }

    /**
     * 홈페이지 이벤트(쁘띠) 정보 등록
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> insertWebEventPetit(
            HashMap<String, Object> paramMap, MultipartFile file, MultipartFile content_file) throws Exception{
        log.debug("WebEventPetitServiceImpl insertEventPetit START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        log.debug("title               :"+paramMap.get("title"));
        log.debug("content              :"+paramMap.get("content"));
        log.debug("hashtag              :"+paramMap.get("hashtag"));
        log.debug("thumbnail_bottom_txt              :"+paramMap.get("thumbnail_bottom_txt"));
        log.debug("file                :"+file);
        log.debug("content_file                :"+content_file);
        log.debug("eventlist                :"+paramMap.get("eventlist"));
        log.debug("eventprocedurelist                :"+paramMap.get("eventprocedurelist"));
        

        try{
            boolean check  = true;
            String tId = String.valueOf(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c1 = Calendar.getInstance();
            String tDate = sdf.format(c1.getTime());

            String title           = String.valueOf(paramMap.get("title"));
            String content          = String.valueOf(paramMap.get("content"));
            String hashtag          = String.valueOf(paramMap.get("hashtag"));
            String thumbnail_bottom_txt          = String.valueOf(paramMap.get("thumbnail_bottom_txt"));
            String[] eventlist          = String.valueOf(paramMap.get("eventlist")).split(",");
            String[] eventprocedurelist          = String.valueOf(paramMap.get("eventprocedurelist")).split(",");
            
//            String eventpetitStartDate  = String.valueOf(paramMap.get("eventpetitStartDate"));
//            String eventpetitEndDate    = String.valueOf(paramMap.get("eventpetitEndDate"));

            if(true != ValidationCheckUtil.emptyCheck(title)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","제목 정보가 없습니다.");
            }

            if(true != ValidationCheckUtil.emptyCheck(content)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","이벤트(쁘띠) 상세 정보가 없습니다.");
            }
            if(true != ValidationCheckUtil.emptyCheck(hashtag)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","이벤트(쁘띠) 해시태그 정보가 없습니다.");
            }
            if(true != ValidationCheckUtil.emptyCheck(thumbnail_bottom_txt)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","이벤트(쁘띠) 썸네일 하단 정보가 없습니다.");
            }

//            if(true != ValidationCheckUtil.emptyCheck(eventpetitStartDate)){
//                check = false;
//                map.put("msgCode", Constants.FAIL);
//                map.put("msgDesc","이벤트(쁘띠) 시작일자 정보가 없습니다.");
//            }
//
//            if(true != ValidationCheckUtil.emptyCheck(eventpetitEndDate)){
//                check = false;
//                map.put("msgCode", Constants.FAIL);
//                map.put("msgDesc","이벤트(쁘띠) 종료일자 정보가 없습니다.");
//            }

            if(null==file|| file.isEmpty()){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","배너 이미지 정보가 없습니다.");
            }
            if(null==content_file|| content_file.isEmpty()){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","첨부파일 정보가 없습니다.");
            }

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);
                int authMenuRelationlistMapSize = authMenuRelationlistMap.size();

                if(1==authMenuRelationlistMapSize){

                    if(("Y").equals(authMenuRelationlistMap.get(0).get("insertUseYn"))) {

                    }
                    else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "등록 권한 정보가 없습니다.");
                    }
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }

            if(true == check){

                if (null != file && !file.isEmpty()) {
                    HashMap<String, Object> uploadFileMap = new HashMap<String, Object>();
                    uploadFileMap.put("type", "WE");
                    uploadFileMap.put("fileType", "P");
                    uploadFileMap.put("fileName", file.getOriginalFilename());
                    HashMap<String, Object> fileMap = fileService.uploadFile(uploadFileMap, file);
                    String cdnUrl       = String.valueOf(fileMap.get("cdnUrl"));
                    String filePath     = String.valueOf(fileMap.get("filePath"));
                    String fileName     = String.valueOf(fileMap.get("fileName"));
                    String chgFileName  = file.getOriginalFilename();
                    HashMap<String, Object> uploadFileMap2 = new HashMap<String, Object>();
                    uploadFileMap2.put("type", "WE");
                    uploadFileMap2.put("fileType", "P");
                    uploadFileMap2.put("fileName", content_file.getOriginalFilename());                   
                    HashMap<String, Object> fileMap2 = fileService.uploadFile(uploadFileMap2, content_file);
                    String cdnUrl2       = String.valueOf(fileMap.get("cdnUrl"));
                    String filePath2     = String.valueOf(fileMap.get("filePath"));
                    String fileName2     = String.valueOf(fileMap.get("fileName"));
                    String chgFileName2  = content_file.getOriginalFilename();
                    paramMap.put("cdnUrl", cdnUrl);
                    paramMap.put("filePath", filePath);
                    paramMap.put("fileName", fileName);
                    paramMap.put("originalFileName", chgFileName);
                    paramMap.put("fileType", "img");
                    paramMap.put("cdnUrl2", cdnUrl2);
                    paramMap.put("filePath2", filePath2);
                    paramMap.put("fileName2", fileName2);
                    paramMap.put("originalFileName2", chgFileName2);
                    paramMap.put("fileType2", "img");
                    paramMap.put("title", title);
                    paramMap.put("content", content);
                    paramMap.put("hashtag", hashtag);
                    paramMap.put("thumbnail_bottom_txt", thumbnail_bottom_txt);
                    paramMap.put("eventlist", eventlist);
                    paramMap.put("eventprocedurelist", eventprocedurelist);
//                    paramMap.put("eventpetitStartDate", eventpetitStartDate);
//                    paramMap.put("eventpetitEndDate", eventpetitEndDate);
                    
                    webEventPetitSqlMapDAO.insertWebEventPetitContentFile(paramMap);
                    log.debug("contentId :"+paramMap.get("contentId"));
                    paramMap.put("contentId", paramMap.get("contentId"));
                    webEventPetitSqlMapDAO.insertWebEventPetitThumbnailFile(paramMap);
                    log.debug("thumbnailId :"+paramMap.get("thumbnailId"));
                    paramMap.put("thumbnailId", paramMap.get("thumbnailId"));
                }
                paramMap.put("tDate", tDate);
                paramMap.put("tId", tId);

                String muWebEventPetitId = ("E").concat(String.valueOf(System.currentTimeMillis()));
                paramMap.put("muWebEventPetitId",muWebEventPetitId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webEventPetitSqlMapDAO.insertWebEventPetit(paramMap);
                log.debug("muWebEventPetitId :"+paramMap.get("id"));
                paramMap.put("muWebEventPetitId", paramMap.get("id"));
                map.put("muWebEventPetitId", paramMap.get("muWebEventPetitId"));
                
                webEventPetitSqlMapDAO.updateWebEventListPetit(paramMap);
                for(int i = 0; i < eventlist.length; i++) {
                	paramMap.put("eventid", eventlist[i]);
                	paramMap.put("eventprocedureid", eventprocedurelist[i]);                	
                	webEventPetitSqlMapDAO.insertWebEventListPetit(paramMap);
                }
                

                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "등록되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("WebEventPetitServiceImpl insertWebEventPetit END");
        return map;
    }

    /**
     * 홈페이지 이벤트(쁘띠) 수정 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> updateWebEventPetitIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebEventPetitServiceImpl updateWebEventPetitIntro START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        try{
            boolean check  = true;

            String menuClass    = String.valueOf(paramMap.get("menuClass"));

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(menuClass)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","메뉴 정보가 없습니다.");
            }

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);
                int authMenuRelationlistMapSize = authMenuRelationlistMap.size();

                if(1==authMenuRelationlistMapSize){
                    map.put("msgCode", Constants.OK);
                    map.put("success","true");

                    map.put("selectUseYn", authMenuRelationlistMap.get(0).get("selectUseYn"));
                    map.put("insertUseYn", authMenuRelationlistMap.get(0).get("insertUseYn"));
                    map.put("updateUseYn", authMenuRelationlistMap.get(0).get("updateUseYn"));
                    map.put("deleteUseYn", authMenuRelationlistMap.get(0).get("deleteUseYn"));
                    map.put("downloadUseYn", authMenuRelationlistMap.get(0).get("downloadUseYn"));
                }
                else {
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }
            List<Map<String, Object>> listMap = webEventPetitSqlMapDAO.selectWebEventPetitCategory(paramMap);
            map.put("categorylist", listMap);
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebEventPetitServiceImpl updateWebEventPetitIntro END");
        return map;
    }

    /**
     * 홈페이지 이벤트(쁘띠) 정보 수정
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> updateWebEventPetit(
            HashMap<String, Object> paramMap, MultipartFile file, MultipartFile content_file) throws Exception{
        log.debug("WebEventPetitServiceImpl updateWebEventPetit START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        log.debug("title               :"+paramMap.get("title"));
        log.debug("content              :"+paramMap.get("content"));
        log.debug("hashtag              :"+paramMap.get("hashtag"));
        log.debug("thumbnail_bottom_txt              :"+paramMap.get("thumbnail_bottom_txt"));
        log.debug("file                :"+file);
        log.debug("content_file                :"+content_file);
        log.debug("eventlist                :"+paramMap.get("eventlist"));
        log.debug("eventprocedurelist                :"+paramMap.get("eventprocedurelist"));

        try{
            boolean check  = true;

            String muWebEventPetitId       = String.valueOf(paramMap.get("muWebEventPetitId"));
            String title           = String.valueOf(paramMap.get("title"));
            String content          = String.valueOf(paramMap.get("content"));
            String hashtag          = String.valueOf(paramMap.get("hashtag"));
            String thumbnail_bottom_txt          = String.valueOf(paramMap.get("thumbnail_bottom_txt"));
            String[] eventlist          = String.valueOf(paramMap.get("eventlist")).split(",");
            String[] eventprocedurelist          = String.valueOf(paramMap.get("eventprocedurelist")).split(",");
            
//            String eventpetitStartDate  = String.valueOf(paramMap.get("eventpetitStartDate"));
//            String eventpetitEndDate    = String.valueOf(paramMap.get("eventpetitEndDate"));

            if(true != ValidationCheckUtil.emptyCheck(title)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","제목 정보가 없습니다.");
            }

            if(true != ValidationCheckUtil.emptyCheck(content)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","이벤트(쁘띠) 상세 정보가 없습니다.");
            }
            if(true != ValidationCheckUtil.emptyCheck(hashtag)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","이벤트(쁘띠) 해시태그 정보가 없습니다.");
            }
            if(true != ValidationCheckUtil.emptyCheck(thumbnail_bottom_txt)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","이벤트(쁘띠) 썸네일 하단 정보가 없습니다.");
            }

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);
                int authMenuRelationlistMapSize = authMenuRelationlistMap.size();

                if(1==authMenuRelationlistMapSize){

                    if(("Y").equals(authMenuRelationlistMap.get(0).get("updateUseYn"))) {

                    }
                    else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "수정 권한 정보가 없습니다.");
                    }
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }

            if(true == check){
            	if (null != file && !file.isEmpty()) {
                	paramMap.put("title", title);
                    paramMap.put("content", content);
                    paramMap.put("hashtag", hashtag);
                    paramMap.put("thumbnail_bottom_txt", thumbnail_bottom_txt);
                    paramMap.put("eventlist", eventlist);
                    paramMap.put("eventprocedurelist", eventprocedurelist);
                	HashMap<String, Object> uploadFileMap = new HashMap<String, Object>();
                    uploadFileMap.put("type", "WE");
                    uploadFileMap.put("fileType", "P");
                    uploadFileMap.put("fileName", file.getOriginalFilename());
                    HashMap<String, Object> fileMap = fileService.uploadFile(uploadFileMap, file);
                    String cdnUrl       = String.valueOf(fileMap.get("cdnUrl"));
                    String filePath     = String.valueOf(fileMap.get("filePath"));
                    String fileName     = String.valueOf(fileMap.get("fileName"));
                    String chgFileName  = file.getOriginalFilename();
                    
                    paramMap.put("cdnUrl", cdnUrl);
                    paramMap.put("filePath", filePath);
                    paramMap.put("fileName", fileName);
                    paramMap.put("originalFileName", chgFileName);
                    paramMap.put("fileType", "img");
                    
                    webEventPetitSqlMapDAO.insertWebEventPetitThumbnailFile(paramMap);
                    log.debug("thumbnailId :"+paramMap.get("thumbnailId"));
                    paramMap.put("thumbnailId", paramMap.get("thumbnailId"));  
//                    paramMap.put("treatmentdietStartDate", treatmentdietStartDate);
//                    paramMap.put("treatmentdietEndDate", treatmentdietEndDate);          
                }else {
                	paramMap.put("thumbnailId", "");
                }
                
                if (null != content_file && !content_file.isEmpty()) {
                	paramMap.put("title", title);
                    paramMap.put("content", content);
                    paramMap.put("hashtag", hashtag);
                    paramMap.put("thumbnail_bottom_txt", thumbnail_bottom_txt);
                    paramMap.put("eventlist", eventlist);
                    paramMap.put("eventprocedurelist", eventprocedurelist);
                    HashMap<String, Object> uploadFileMap2 = new HashMap<String, Object>();
                    uploadFileMap2.put("type", "WE");
                    uploadFileMap2.put("fileType", "P");
                    uploadFileMap2.put("fileName", content_file.getOriginalFilename());                   
                    HashMap<String, Object> fileMap2 = fileService.uploadFile(uploadFileMap2, content_file);
                    String cdnUrl2       = String.valueOf(fileMap2.get("cdnUrl"));
                    String filePath2     = String.valueOf(fileMap2.get("filePath"));
                    String fileName2     = String.valueOf(fileMap2.get("fileName"));
                    String chgFileName2  = content_file.getOriginalFilename();
                    paramMap.put("cdnUrl2", cdnUrl2);
                    paramMap.put("filePath2", filePath2);
                    paramMap.put("fileName2", fileName2);
                    paramMap.put("originalFileName2", chgFileName2);
                    paramMap.put("fileType2", "img");
                    
                    webEventPetitSqlMapDAO.insertWebEventPetitContentFile(paramMap);
                    log.debug("contentId :"+paramMap.get("contentId"));
                    paramMap.put("contentId", paramMap.get("contentId"));
                	
                }else {
                	paramMap.put("contentId", "");                	
                }
                paramMap.put("muWebEventPetitId",muWebEventPetitId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webEventPetitSqlMapDAO.updateWebEventPetit(paramMap);
                map.put("muWebEventPetitId", paramMap.get("muWebEventPetitId"));
                
                webEventPetitSqlMapDAO.updateWebEventListPetit(paramMap);
                for(int i = 0; i < eventlist.length; i++) {
                	paramMap.put("eventid", eventlist[i]);
                	paramMap.put("eventprocedureid", eventprocedurelist[i]);
                	webEventPetitSqlMapDAO.insertWebEventListPetit(paramMap);
                }
                
                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "수정되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("WebEventPetitServiceImpl updateWebEventPetit END");
        return map;
    }

    /**
     * 홈페이지 이벤트(쁘띠) 정보 삭제
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> deleteWebEventPetit(
            HashMap<String, Object> paramMap) throws Exception{
        log.debug("WebEventPetitServiceImpl deleteWebEventPetit START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        // ,로 구분
        log.debug("selectId            :"+paramMap.get("selectId"));

        try{
            boolean check  = true;

            if(null== paramMap.get("selectId") || ("").equals(paramMap.get("selectId"))){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc", "삭제 대상이 없습니다.");
            }

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);
                int authMenuRelationlistMapSize = authMenuRelationlistMap.size();

                if(1==authMenuRelationlistMapSize){

                    if(("Y").equals(authMenuRelationlistMap.get(0).get("deleteUseYn"))) {

                    }
                    else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "삭제 권한 정보가 없습니다.");
                    }
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }

            if(true == check){
                String selectId = String.valueOf(paramMap.get("selectId"));
                StringTokenizer selectData = new StringTokenizer(selectId, ",");
                while(selectData.hasMoreTokens()){//토큰이 계속 있을때 까지
                    String muWebEventPetitId = selectData.nextToken();

                    HashMap<String, Object> deleteParamMap = new HashMap<String, Object>();
                    deleteParamMap.put("muWebEventPetitId", muWebEventPetitId);
                    deleteParamMap.put("modId", paramMap.get("loginMemberId"));
                    webEventPetitSqlMapDAO.deleteWebEventPetit(deleteParamMap);
                }

                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "삭제 되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        log.debug("WebEventPetitServiceImpl deleteWebEventPetit END");
        return map;
    }
    
    /**
     * 고객 리스트 조회.(List,Option)
     * 예약팝업에서 고객 조회
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListEventOption(HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebEventPetitServiceImpl selectListEventOption START");

        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> listMap = new ArrayList<>();
        log.debug("userSearchKeywordParam   :"+paramMap.get("userSearchKeywordParam"));
        log.debug("userTypeCode             :"+paramMap.get("userTypeCode"));
        log.debug("membershipYn             :"+paramMap.get("membershipYn"));
        log.debug("nationalityCode          :"+paramMap.get("nationalityCode"));
        log.debug("loginMemberId            :"+paramMap.get("loginMemberId"));
        log.debug("userSort                 :"+paramMap.get("userSort"));
        log.debug("userStart                :"+paramMap.get("userStart"));
        log.debug("nationality              :"+paramMap.get("nationality"));
        log.debug("menuClass                :"+paramMap.get("menuClass"));
        log.debug("userType                 :"+paramMap.get("userType"));
        log.debug("userLimit                :"+paramMap.get("userLimit"));

        try{
            boolean check  = true;

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                // 정렬
                if(null==paramMap.get("userDir")||("").equals(paramMap.get("userDir"))){

                }
                else{
                    String dir = String.valueOf(paramMap.get("userDir"));
                    if(("A").equals(dir)){
                        paramMap.put("userDir", "DESC");
                    }
                    else if(("B").equals(dir)){
                        paramMap.put("userDir", "ASC");
                    }
                    else {
                        paramMap.put("userDir", "DESC");
                    }
                }

                paramMap.put("useYn", "Y");

                int totalCount = webEventPetitSqlMapDAO.selectTotalEventCount(paramMap);
                if(0<totalCount){
                    listMap = webEventPetitSqlMapDAO.selectListEvent(paramMap);
                }

                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("totalCount", totalCount);
                map.put("rows",listMap);
            }

        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }

        log.debug("WebEventPetitServiceImpl selectListEventOption END");
        return map;
    }
}
