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

import com.madeu.constants.Constants;
import com.madeu.dao.web.webappevent.WebAppEventSqlMapDAO;
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.webappevent.WebAppEventService;
import com.madeu.util.ValidationCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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("WebAppEventService")
public class WebAppEventServiceImpl implements WebAppEventService {

    @Autowired
    private WebAppEventSqlMapDAO webAppEventSqlMapDAO;

    @Autowired
    private WebMemberSqlMapDAO webMemberSqlMapDAO;

    @Autowired
    private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;

    @Autowired
    private FileService fileService;

    /**
     * 이벤트 리스트 조회 (option)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListAppEventOption(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebAppEventServiceImpl selectListAppEventOption START");

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

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

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

        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){

                listMap = webAppEventSqlMapDAO.selectListAppEventOption(paramMap);

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

    /**
     * 이벤트 콘텐츠 리스트 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListAppEventIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebAppEventServiceImpl selectListAppEventIntro 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("WebAppEventServiceImpl selectListAppEventIntro END");
        return map;
    }

    /**
     * 이벤트 리스트 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListAppEvent(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebAppEventServiceImpl selectListAppEvent START");

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

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

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

        log.debug("appEventSearchKeywordParam  :"+paramMap.get("appEventSearchKeywordParam"));
        log.debug("appEventSort                :"+paramMap.get("appEventSort"));
        log.debug("appEventStart               :"+paramMap.get("appEventStart"));
        log.debug("appEventLimit               :"+paramMap.get("appEventLimit"));
        log.debug("appEventSort                :"+paramMap.get("appEventSort"));
        log.debug("appEventDir                 :"+paramMap.get("appEventDir"));
        log.debug("appEventCurrentPage         :"+paramMap.get("appEventCurrentPage"));

        log.debug("appEventSearchStartDate     :"+paramMap.get("appEventSearchStartDate"));
        log.debug("appEventSearchEndDate       :"+paramMap.get("appEventSearchEndDate"));

        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("appEventDir")||("").equals(paramMap.get("appEventDir"))){

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

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

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

                if(0 < totalCount) {
                    // 리스트 조회
                    listMap = webAppEventSqlMapDAO.selectListAppEvent(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("WebAppEventServiceImpl selectListAppEvent END");
        return map;
    }

    /**
     * 이벤트정보 상세 조회 (one)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectAppEvent(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebAppEventServiceImpl selectEvent START");

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

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

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

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

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

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(muAppEventId)){
                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 = webAppEventSqlMapDAO.selectAppEvent(paramMap);

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

    /**
     * 이벤트 콘텐츠 등록 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> insertAppEventIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebAppEventServiceImpl insertAppEventIntro 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("WebAppEventServiceImpl insertAppEventIntro END");
        return map;
    }

    /**
     * 이벤트정보 등록
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> insertAppEvent(
            HashMap<String, Object> paramMap, MultipartFile file) throws Exception{
        log.debug("WebAppEventServiceImpl insertEvent 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("detail              :"+paramMap.get("detail"));
        log.debug("exposedYn          :"+paramMap.get("exposedYn"));
        log.debug("file                :"+file);
        log.debug("eventStartDate      :"+paramMap.get("eventStartDate"));
        log.debug("eventEndDate        :"+paramMap.get("eventEndDate"));

        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 detail          = String.valueOf(paramMap.get("detail"));
            String exposedYn      = String.valueOf(paramMap.get("exposedYn"));
            String eventStartDate  = String.valueOf(paramMap.get("eventStartDate"));
            String eventEndDate    = String.valueOf(paramMap.get("eventEndDate"));

            if(true != ValidationCheckUtil.emptyCheck(title)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","제목 정보가 없습니다.");
            }
            else{
                if(true != ValidationCheckUtil.lengthCheck(title,100)){
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc","제목 정보는 100자를 넘을 수 없습니다.");
                }
            }

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

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

            if(true != ValidationCheckUtil.emptyCheck(exposedYn)) {
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc", "메인 홈 표출여부 정보가 없습니다.");
            }
            else if("Y".equals(exposedYn)){
                List<Map<String, Object>> exposedCountListMap = webAppEventSqlMapDAO.selectAppEventExposedCount(paramMap);
                int exposedCount = Integer.parseInt(String.valueOf(exposedCountListMap.get(0).get("exposedCount")));

                if(4 < exposedCount) {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "이벤트 메인 노출은 최대 5개까지 가능합니다.");
                }
            }

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

            if(true != ValidationCheckUtil.emptyCheck(eventEndDate)){
                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(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", "EI");
                    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 chgFileNmae  = file.getOriginalFilename();
                    paramMap.put("cdnUrl", cdnUrl);
                    paramMap.put("filePath", filePath);
                    paramMap.put("fileName", fileName);
                    paramMap.put("originalFileName", chgFileNmae);
                    paramMap.put("title", title);
                    paramMap.put("content", content);
                    paramMap.put("detail", detail);
                    paramMap.put("exposedYn", exposedYn);
                    paramMap.put("eventStartDate", eventStartDate);
                    paramMap.put("eventEndDate", eventEndDate);
                }
                paramMap.put("tDate", tDate);
                paramMap.put("tId", tId);

                String muAppEventId = ("E").concat(String.valueOf(System.currentTimeMillis()));
                paramMap.put("muAppEventId",muAppEventId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webAppEventSqlMapDAO.insertAppEvent(paramMap);
                log.debug("muAppEventId :"+paramMap.get("id"));
                paramMap.put("muAppEventId", paramMap.get("id"));
                map.put("muAppEventId", paramMap.get("muAppEventId"));

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

    /**
     * 이벤트 콘텐츠 수정 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> updateAppEventIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebAppEventServiceImpl updateAppEventIntro 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("WebAppEventServiceImpl updateAppEventIntro END");
        return map;
    }

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

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

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

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

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

        log.debug("title               :"+paramMap.get("title"));
        log.debug("content             :"+paramMap.get("content"));
        log.debug("detail              :"+paramMap.get("detail"));
        log.debug("exposedYn          :"+paramMap.get("exposedYn"));
        log.debug("file                :"+file);
        log.debug("eventStartDate      :"+paramMap.get("eventStartDate"));
        log.debug("eventEndDate        :"+paramMap.get("eventEndDate"));

        try{
            boolean check  = true;

            String muAppEventId       = String.valueOf(paramMap.get("muAppEventId"));
            String title              = String.valueOf(paramMap.get("title"));
            String content            = String.valueOf(paramMap.get("content"));
            String detail             = String.valueOf(paramMap.get("detail"));
            String exposedYn         = String.valueOf(paramMap.get("exposedYn"));
            String eventStartDate     = String.valueOf(paramMap.get("eventStartDate"));
            String eventEndDate       = String.valueOf(paramMap.get("eventEndDate"));

            if(true != ValidationCheckUtil.emptyCheck(muAppEventId)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","이벤트식별자 정보가 없습니다.");
            }

            if(true != ValidationCheckUtil.emptyCheck(title)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","제목 정보가 없습니다.");
            }
            else{
                if(true != ValidationCheckUtil.lengthCheck(title,100)){
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc","제목 정보는 100자를 넘을 수 없습니다.");
                }
            }

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

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

            if(true != ValidationCheckUtil.emptyCheck(exposedYn)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","메인 홈 표출여부 정보가 없습니다.");
            }

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

            if(true != ValidationCheckUtil.emptyCheck(eventEndDate)){
                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()) {
                HashMap<String, Object> uploadFileMap = new HashMap<String, Object>();
                    uploadFileMap.put("type", "EI");
                    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 chgFileNmae  = file.getOriginalFilename();
                    paramMap.put("cdnUrl", cdnUrl);
                    paramMap.put("filePath", filePath);
                    paramMap.put("fileName", fileName);
                    paramMap.put("originalFileName", chgFileNmae);
                    paramMap.put("title", title);
                    paramMap.put("content", content);
                    paramMap.put("detail", detail);
                    paramMap.put("exposedYn", exposedYn);
                    paramMap.put("eventStartDate", eventStartDate);
                    paramMap.put("eventEndDate", eventEndDate);
                }
                paramMap.put("muAppEventId",muAppEventId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webAppEventSqlMapDAO.updateAppEvent(paramMap);
                map.put("muAppEventId", paramMap.get("muAppEventId"));

                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "수정되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("WebAppEventServiceImpl updateAppEvent END");
        return map;
    }

    /**
     * 이벤트정보 삭제
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> deleteAppEvent(
            HashMap<String, Object> paramMap) throws Exception{
        log.debug("WebAppEventServiceImpl deleteAppEvent 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 muAppEventId = selectData.nextToken();

                    HashMap<String, Object> deleteParamMap = new HashMap<String, Object>();
                    deleteParamMap.put("muAppEventId", muAppEventId);
                    deleteParamMap.put("modId", paramMap.get("loginMemberId"));
                    webAppEventSqlMapDAO.deleteAppEvent(deleteParamMap);
                }

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

        log.debug("WebAppEventServiceImpl deleteAppEvent END");
        return map;
    }
}
