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

import com.madeu.constants.Constants;
import com.madeu.dao.web.webevent.WebEventSqlMapDAO;
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.webevent.WebEventService;
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("WebEventService")
public class WebEventServiceImpl implements WebEventService {

    @Autowired
    private WebEventSqlMapDAO webEventSqlMapDAO;

    @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> selectListWebEventIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebEventServiceImpl selectListWebEventIntro 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("WebEventServiceImpl selectListWebEventIntro END");
        return map;
    }

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

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

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

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

        log.debug("webEventSearchKeywordParam  :"+paramMap.get("webEventSearchKeywordParam"));
        log.debug("webEventSort                :"+paramMap.get("webEventSort"));
        log.debug("webEventStart               :"+paramMap.get("webEventStart"));
        log.debug("webEventLimit               :"+paramMap.get("webEventLimit"));
        log.debug("webEventSort                :"+paramMap.get("webEventSort"));
        log.debug("webEventDir                 :"+paramMap.get("webEventDir"));
        log.debug("webEventCurrentPage         :"+paramMap.get("webEventCurrentPage"));

        log.debug("webEventSearchStartDate     :"+paramMap.get("webEventSearchStartDate"));
        log.debug("webEventSearchEndDate       :"+paramMap.get("webEventSearchEndDate"));

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

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

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

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

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

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

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

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

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

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

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

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(muWebEventId)){
                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 = webEventSqlMapDAO.selectWebEvent(paramMap);

                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("rows",listMap);
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebEventServiceImpl selectWebEvent 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("WebEventServiceImpl 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("WebEventServiceImpl uploadFile END");
        return map;
    }

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

    /**
     * 홈페이지 이벤트 정보 등록
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> insertWebEvent(
            HashMap<String, Object> paramMap, MultipartFile file) throws Exception{
        log.debug("WebEventServiceImpl 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("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 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","제목 정보가 없습니다.");
            }

            if(true != ValidationCheckUtil.emptyCheck(content)){
                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(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 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("eventStartDate", eventStartDate);
                    paramMap.put("eventEndDate", eventEndDate);
                }
                paramMap.put("tDate", tDate);
                paramMap.put("tId", tId);

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

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

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

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

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

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

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

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

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

        try{
            boolean check  = true;

            String muWebEventId       = String.valueOf(paramMap.get("muWebEventId"));
            String title              = String.valueOf(paramMap.get("title"));
            String content             = String.valueOf(paramMap.get("content"));
            String eventStartDate     = String.valueOf(paramMap.get("eventStartDate"));
            String eventEndDate       = String.valueOf(paramMap.get("eventEndDate"));

            if(true != ValidationCheckUtil.emptyCheck(muWebEventId)){
                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","제목 정보가 없습니다.");
            }

            if(true != ValidationCheckUtil.emptyCheck(content)){
                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", "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 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("eventStartDate", eventStartDate);
                    paramMap.put("eventEndDate", eventEndDate);
                }
                paramMap.put("muWebEventId",muWebEventId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webEventSqlMapDAO.updateWebEvent(paramMap);
                map.put("muWebEventId", paramMap.get("muWebEventId"));

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

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

                    HashMap<String, Object> deleteParamMap = new HashMap<String, Object>();
                    deleteParamMap.put("muWebEventId", muWebEventId);
                    deleteParamMap.put("modId", paramMap.get("loginMemberId"));
                    webEventSqlMapDAO.deleteWebEvent(deleteParamMap);
                }

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

        log.debug("WebEventServiceImpl deleteWebEvent END");
        return map;
    }
}
