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

import com.madeu.constants.Constants;
import com.madeu.dao.web.webappcontents.WebAppContentsSqlMapDAO;
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.webappcontents.WebAppContentsService;
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("WebAppContentsService")
public class WebAppContentsServiceImpl implements WebAppContentsService {

    @Autowired
    private WebAppContentsSqlMapDAO webAppContentsSqlMapDAO;

    @Autowired
    private WebMemberSqlMapDAO webMemberSqlMapDAO;

    @Autowired
    private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;

    @Autowired
    private FileService fileService;

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

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

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

    /**
     * 콘텐츠리스트 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListAppContents(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebAppContentsServiceImpl selectListAppContents START");

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

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

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

        log.debug("appContentsSearchKeywordParam  :"+paramMap.get("appContentsSearchKeywordParam"));
        log.debug("appContentsSort                :"+paramMap.get("appContentsSort"));
        log.debug("appContentsStart               :"+paramMap.get("appContentsStart"));
        log.debug("appContentsLimit               :"+paramMap.get("appContentsLimit"));
        log.debug("appContentsSort                :"+paramMap.get("appContentsSort"));
        log.debug("appContentsDir                 :"+paramMap.get("appContentsDir"));
        log.debug("appContentsCurrentPage         :"+paramMap.get("appContentsCurrentPage"));
        log.debug("appContentappContentCategoryName         :"+paramMap.get("appContentappContentCategoryName"));

        log.debug("appContentsSearchStartDate     :"+paramMap.get("appContentsSearchStartDate"));
        log.debug("appContentsSearchEndDate       :"+paramMap.get("appContentsSearchEndDate"));

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

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

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

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

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

    /**
     * 시술 콘텐츠 정보 상세 조회 (one)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectAppContents(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebAppContentsServiceImpl selectContents START");

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

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

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

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

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

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(muAppContentId)){
                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 = webAppContentsSqlMapDAO.selectAppContents(paramMap);

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

    /**
     * 시술 콘텐츠 정보 등록
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> insertAppContents(
            HashMap<String, Object> paramMap, MultipartFile file, MultipartFile file2) throws Exception{
        log.debug("WebAppContentsServiceImpl insertContents 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("file                :"+file);
        log.debug("categoryName        :"+paramMap.get("categoryName"));
        log.debug("categoryCode        :"+paramMap.get("categoryCode"));

        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 categoryName    = String.valueOf(paramMap.get("categoryName"));
            String categoryCode    = String.valueOf(paramMap.get("categoryCode"));

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

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

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

            if(true != ValidationCheckUtil.emptyCheck(categoryName)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","카테고리 정보가 없습니다.");
            }

            if(true != ValidationCheckUtil.emptyCheck(categoryCode)){
                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", "SI");
                    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);
                }

                if (null != file2 && !file2.isEmpty()) {
                    HashMap<String, Object> uploadFileMap = new HashMap<String, Object>();
                    uploadFileMap.put("type", "SI");
                    uploadFileMap.put("fileType", "P");
                    uploadFileMap.put("fileName", file2.getOriginalFilename());
                    HashMap<String, Object> fileMap = fileService.uploadFile(uploadFileMap, file2);
                    String cdnUrl       = String.valueOf(fileMap.get("cdnUrl"));
                    String filePath     = String.valueOf(fileMap.get("filePath"));
                    String fileName     = String.valueOf(fileMap.get("fileName"));
                    String chgFileNmae  = file2.getOriginalFilename();
                    paramMap.put("cdnUrl2", cdnUrl);
                    paramMap.put("filePath2", filePath);
                    paramMap.put("fileName2", fileName);
                    paramMap.put("originalFileName2", chgFileNmae);
                }

                paramMap.put("tDate", tDate);
                paramMap.put("tId", tId);

                String muAppContentId = ("C").concat(String.valueOf(System.currentTimeMillis()));
                paramMap.put("muAppContentId",muAppContentId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webAppContentsSqlMapDAO.insertAppContents(paramMap);
                log.debug("muAppContentId :"+paramMap.get("id"));
                paramMap.put("muAppContentId", paramMap.get("id"));
                map.put("muAppContentId", paramMap.get("muAppContentId"));

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

    /**
     * 콘텐츠정보 수정
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> updateAppContents(
            HashMap<String, Object> paramMap, MultipartFile file, MultipartFile file2) throws Exception{
        log.debug("WebAppContentsServiceImpl updateAppContents START");

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

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

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

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

        log.debug("title               :"+paramMap.get("title"));
        log.debug("content             :"+paramMap.get("content"));
        log.debug("detail              :"+paramMap.get("detail"));
        log.debug("file                :"+file);
        log.debug("categoryName        :"+paramMap.get("categoryName"));
        log.debug("categoryCode        :"+paramMap.get("categoryCode"));

        try{
            boolean check  = true;

            String muAppContentId       = String.valueOf(paramMap.get("muAppContentId"));
            String title              = String.valueOf(paramMap.get("title"));
            String content            = String.valueOf(paramMap.get("content"));
            String detail             = String.valueOf(paramMap.get("detail"));
            String categoryCode       = String.valueOf(paramMap.get("categoryCode"));
            String categoryName       = String.valueOf(paramMap.get("categoryName"));

            if(true != ValidationCheckUtil.emptyCheck(muAppContentId)){
                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,20)){
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc","제목 정보는 20자를 넘을 수 없습니다.");
                }
            }

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

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

            if(true != ValidationCheckUtil.emptyCheck(categoryCode)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","카테고리 정보가 없습니다.");
            }

            if(true != ValidationCheckUtil.emptyCheck(categoryName)){
                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", "SI");
                    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);
                }

                if (null != file2 && !file2.isEmpty()) {
                    HashMap<String, Object> uploadFileMap = new HashMap<String, Object>();
                    uploadFileMap.put("type", "SI");
                    uploadFileMap.put("fileType", "P");
                    uploadFileMap.put("fileName", file2.getOriginalFilename());
                    HashMap<String, Object> fileMap = fileService.uploadFile(uploadFileMap, file2);
                    String cdnUrl       = String.valueOf(fileMap.get("cdnUrl"));
                    String filePath     = String.valueOf(fileMap.get("filePath"));
                    String fileName     = String.valueOf(fileMap.get("fileName"));
                    String chgFileNmae  = file2.getOriginalFilename();
                    paramMap.put("cdnUrl2", cdnUrl);
                    paramMap.put("filePath2", filePath);
                    paramMap.put("fileName2", fileName);
                    paramMap.put("originalFileName2", chgFileNmae);
                }
                paramMap.put("muAppContentId",muAppContentId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webAppContentsSqlMapDAO.updateAppContents(paramMap);
                map.put("muAppContentId", paramMap.get("muAppContentId"));

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

    /**
     * 콘텐츠정보 삭제
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> deleteAppContents(
            HashMap<String, Object> paramMap) throws Exception{
        log.debug("WebAppContentsServiceImpl deleteAppContents 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 muAppContentId = selectData.nextToken();

                    HashMap<String, Object> deleteParamMap = new HashMap<String, Object>();
                    deleteParamMap.put("muAppContentId", muAppContentId);
                    deleteParamMap.put("modId", paramMap.get("loginMemberId"));
                    webAppContentsSqlMapDAO.deleteAppContents(deleteParamMap);
                }

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

        log.debug("WebAppContentsServiceImpl deleteAppContents END");
        return map;
    }
}

