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

import com.madeu.constants.Constants;
import com.madeu.dao.web.webtreatmentdiet.WebTreatmentDietSqlMapDAO;
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.webtreatmentdiet.WebTreatmentDietService;
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("WebTreatmentDietService")
public class WebTreatmentDietServiceImpl implements WebTreatmentDietService {

    @Autowired
    private WebTreatmentDietSqlMapDAO webTreatmentDietSqlMapDAO;

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

    /**
     * 홈페이지 시술예약(다이어트) 리스트 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListWebTreatmentDiet(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebTreatmentDietServiceImpl selectListWebTreatmentDiet START");

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

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

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

        log.debug("webTreatmentDietSearchKeywordParam  :"+paramMap.get("webTreatmentDietSearchKeywordParam"));
        log.debug("webTreatmentDietSort                :"+paramMap.get("webTreatmentDietSort"));
        log.debug("webTreatmentDietStart               :"+paramMap.get("webTreatmentDietStart"));
        log.debug("webTreatmentDietLimit               :"+paramMap.get("webTreatmentDietLimit"));
        log.debug("webTreatmentDietSort                :"+paramMap.get("webTreatmentDietSort"));
        log.debug("webTreatmentDietDir                 :"+paramMap.get("webTreatmentDietDir"));
        log.debug("webTreatmentDietCurrentPage         :"+paramMap.get("webTreatmentDietCurrentPage"));

        log.debug("webTreatmentDietSearchStartDate     :"+paramMap.get("webTreatmentDietSearchStartDate"));
        log.debug("webTreatmentDietSearchEndDate       :"+paramMap.get("webTreatmentDietSearchEndDate"));

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

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

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

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

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

    /**
     * 홈페이지 시술예약(다이어트)정보 상세 조회 (one)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectWebTreatmentDiet(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebTreatmentDietServiceImpl selectTreatmentDiet START");

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

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

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

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

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

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(muWebTreatmentDietId)){
                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 = webTreatmentDietSqlMapDAO.selectWebTreatmentDiet(paramMap);
                List<Map<String, Object>> listMap2 = webTreatmentDietSqlMapDAO.selectWebTreatmentListDiet(paramMap);

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

    /**
     * 홈페이지 시술예약(다이어트) 콘텐츠 등록 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> insertWebTreatmentDietIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebTreatmentDietServiceImpl insertWebTreatmentDietIntro START");

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

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

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

        try{
            boolean check  = true;

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

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

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

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

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

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

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

    /**
     * 홈페이지 시술예약(다이어트) 정보 등록
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> insertWebTreatmentDiet(
            HashMap<String, Object> paramMap, MultipartFile file, MultipartFile content_file) throws Exception{
        log.debug("WebTreatmentDietServiceImpl insertTreatmentDiet START");

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

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

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

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

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

            String title           = String.valueOf(paramMap.get("title"));
            String content          = String.valueOf(paramMap.get("content"));
            String hashtag          = String.valueOf(paramMap.get("hashtag"));
            String thumbnail_bottom_txt          = String.valueOf(paramMap.get("thumbnail_bottom_txt"));
            String[] treatmentlist          = String.valueOf(paramMap.get("treatmentlist")).split(",");
            String[] treatmentprocedurelist          = String.valueOf(paramMap.get("treatmentprocedurelist")).split(",");
            
//            String treatmentdietStartDate  = String.valueOf(paramMap.get("treatmentdietStartDate"));
//            String treatmentdietEndDate    = String.valueOf(paramMap.get("treatmentdietEndDate"));

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

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

//            if(true != ValidationCheckUtil.emptyCheck(treatmentdietStartDate)){
//                check = false;
//                map.put("msgCode", Constants.FAIL);
//                map.put("msgDesc","시술예약(다이어트) 시작일자 정보가 없습니다.");
//            }
//
//            if(true != ValidationCheckUtil.emptyCheck(treatmentdietEndDate)){
//                check = false;
//                map.put("msgCode", Constants.FAIL);
//                map.put("msgDesc","시술예약(다이어트) 종료일자 정보가 없습니다.");
//            }

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

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

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

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

                if(1==authMenuRelationlistMapSize){

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

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

            if(true == check){

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

                String muWebTreatmentDietId = ("E").concat(String.valueOf(System.currentTimeMillis()));
                paramMap.put("muWebTreatmentDietId",muWebTreatmentDietId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webTreatmentDietSqlMapDAO.insertWebTreatmentDiet(paramMap);
                log.debug("muWebTreatmentDietId :"+paramMap.get("id"));
                paramMap.put("muWebTreatmentDietId", paramMap.get("id"));
                map.put("muWebTreatmentDietId", paramMap.get("muWebTreatmentDietId"));
                
                webTreatmentDietSqlMapDAO.updateWebTreatmentListDiet(paramMap);
                for(int i = 0; i < treatmentlist.length; i++) {
                	paramMap.put("treatmentid", treatmentlist[i]);
                	paramMap.put("treatmentprocedureid", treatmentprocedurelist[i]);                	
                	webTreatmentDietSqlMapDAO.insertWebTreatmentListDiet(paramMap);
                }
                

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

    /**
     * 홈페이지 시술예약(다이어트) 수정 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> updateWebTreatmentDietIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebTreatmentDietServiceImpl updateWebTreatmentDietIntro START");

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

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

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

        try{
            boolean check  = true;

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

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

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

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

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

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

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

    /**
     * 홈페이지 시술예약(다이어트) 정보 수정
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> updateWebTreatmentDiet(
            HashMap<String, Object> paramMap, MultipartFile file, MultipartFile content_file) throws Exception{
        log.debug("WebTreatmentDietServiceImpl updateWebTreatmentDiet START");

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

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

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

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

        try{
            boolean check  = true;

            String muWebTreatmentDietId       = String.valueOf(paramMap.get("muWebTreatmentDietId"));
            String title           = String.valueOf(paramMap.get("title"));
            String content          = String.valueOf(paramMap.get("content"));
            String hashtag          = String.valueOf(paramMap.get("hashtag"));
            String thumbnail_bottom_txt          = String.valueOf(paramMap.get("thumbnail_bottom_txt"));
            String[] treatmentlist          = String.valueOf(paramMap.get("treatmentlist")).split(",");
            String[] treatmentprocedurelist          = String.valueOf(paramMap.get("treatmentprocedurelist")).split(",");
            
//            String treatmentdietStartDate  = String.valueOf(paramMap.get("treatmentdietStartDate"));
//            String treatmentdietEndDate    = String.valueOf(paramMap.get("treatmentdietEndDate"));

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

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

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

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

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

                if(1==authMenuRelationlistMapSize){

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

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

            if(true == check){
                if (null != file && !file.isEmpty()) {
                	paramMap.put("title", title);
                    paramMap.put("content", content);
                    paramMap.put("hashtag", hashtag);
                    paramMap.put("thumbnail_bottom_txt", thumbnail_bottom_txt);
                    paramMap.put("treatmentlist", treatmentlist);
                    paramMap.put("treatmentprocedurelist", treatmentprocedurelist);
                	HashMap<String, Object> uploadFileMap = new HashMap<String, Object>();
                    uploadFileMap.put("type", "WE");
                    uploadFileMap.put("fileType", "P");
                    uploadFileMap.put("fileName", file.getOriginalFilename());
                    HashMap<String, Object> fileMap = fileService.uploadFile(uploadFileMap, file);
                    String cdnUrl       = String.valueOf(fileMap.get("cdnUrl"));
                    String filePath     = String.valueOf(fileMap.get("filePath"));
                    String fileName     = String.valueOf(fileMap.get("fileName"));
                    String chgFileName  = file.getOriginalFilename();
                    
                    paramMap.put("cdnUrl", cdnUrl);
                    paramMap.put("filePath", filePath);
                    paramMap.put("fileName", fileName);
                    paramMap.put("originalFileName", chgFileName);
                    paramMap.put("fileType", "img");
                    
                    webTreatmentDietSqlMapDAO.insertWebTreatmentDietThumbnailFile(paramMap);
                    log.debug("thumbnailId :"+paramMap.get("thumbnailId"));
                    paramMap.put("thumbnailId", paramMap.get("thumbnailId"));  
//                    paramMap.put("treatmentdietStartDate", treatmentdietStartDate);
//                    paramMap.put("treatmentdietEndDate", treatmentdietEndDate);          
                }else {
                	paramMap.put("thumbnailId", "");
                }
                
                if (null != content_file && !content_file.isEmpty()) {
                	paramMap.put("title", title);
                    paramMap.put("content", content);
                    paramMap.put("hashtag", hashtag);
                    paramMap.put("thumbnail_bottom_txt", thumbnail_bottom_txt);
                    paramMap.put("treatmentlist", treatmentlist);
                    paramMap.put("treatmentprocedurelist", treatmentprocedurelist);
                    HashMap<String, Object> uploadFileMap2 = new HashMap<String, Object>();
                    uploadFileMap2.put("type", "WE");
                    uploadFileMap2.put("fileType", "P");
                    uploadFileMap2.put("fileName", content_file.getOriginalFilename());                   
                    HashMap<String, Object> fileMap2 = fileService.uploadFile(uploadFileMap2, content_file);
                    String cdnUrl2       = String.valueOf(fileMap2.get("cdnUrl"));
                    String filePath2     = String.valueOf(fileMap2.get("filePath"));
                    String fileName2     = String.valueOf(fileMap2.get("fileName"));
                    String chgFileName2  = content_file.getOriginalFilename();
                    paramMap.put("cdnUrl2", cdnUrl2);
                    paramMap.put("filePath2", filePath2);
                    paramMap.put("fileName2", fileName2);
                    paramMap.put("originalFileName2", chgFileName2);
                    paramMap.put("fileType2", "img");
                    
                    webTreatmentDietSqlMapDAO.insertWebTreatmentDietContentFile(paramMap);
                    log.debug("contentId :"+paramMap.get("contentId"));
                    paramMap.put("contentId", paramMap.get("contentId"));
                	
                }else {
                	paramMap.put("contentId", "");                	
                }
                
                paramMap.put("muWebTreatmentDietId",muWebTreatmentDietId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webTreatmentDietSqlMapDAO.updateWebTreatmentDiet(paramMap);
                map.put("muWebTreatmentDietId", paramMap.get("muWebTreatmentDietId"));
                webTreatmentDietSqlMapDAO.updateWebTreatmentListDiet(paramMap);
                for(int i = 0; i < treatmentlist.length; i++) {
                	paramMap.put("treatmentid", treatmentlist[i]);
                	paramMap.put("treatmentprocedureid", treatmentprocedurelist[i]);
                	webTreatmentDietSqlMapDAO.insertWebTreatmentListDiet(paramMap);
                }
                
                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "수정되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("WebTreatmentDietServiceImpl updateWebTreatmentDiet END");
        return map;
    }

    /**
     * 홈페이지 시술예약(다이어트) 정보 삭제
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> deleteWebTreatmentDiet(
            HashMap<String, Object> paramMap) throws Exception{
        log.debug("WebTreatmentDietServiceImpl deleteWebTreatmentDiet 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 muWebTreatmentDietId = selectData.nextToken();

                    HashMap<String, Object> deleteParamMap = new HashMap<String, Object>();
                    deleteParamMap.put("muWebTreatmentDietId", muWebTreatmentDietId);
                    deleteParamMap.put("modId", paramMap.get("loginMemberId"));
                    webTreatmentDietSqlMapDAO.deleteWebTreatmentDiet(deleteParamMap);
                }

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

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

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

        try{
            boolean check  = true;

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

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

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

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

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

                int totalCount = webTreatmentDietSqlMapDAO.selectTotalTreatmentCount(paramMap);
                if(0<totalCount){
                    listMap = webTreatmentDietSqlMapDAO.selectListTreatment(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("WebTreatmentDietServiceImpl selectListTreatmentOption END");
        return map;
    }
}
