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

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.madeu.constants.Constants;
import com.madeu.dao.web.webauthmenurelation.WebAuthMenuRelationSqlMapDAO;
import com.madeu.dao.web.webmember.WebMemberSqlMapDAO;
import com.madeu.dao.web.webtreatmentpetit.WebTreatmentPetitSqlMapDAO;
import com.madeu.service.common.file.FileService;
import com.madeu.service.web.webtreatmentpetit.WebTreatmentPetitService;
import com.madeu.util.ValidationCheckUtil;
import com.madeu.common.dao.CommonDao;


import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("WebTreatmentPetitService")
public class WebTreatmentPetitServiceImpl implements WebTreatmentPetitService {

    private final DataSourceTransactionManager transactionManager;

    @Autowired
    private WebTreatmentPetitSqlMapDAO webTreatmentPetitSqlMapDAO;
    
    @Autowired
    private CommonDao cmmnDAO;
    
    @Autowired
    private WebMemberSqlMapDAO webMemberSqlMapDAO;

    @Autowired
    private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;

    @Autowired
    private FileService fileService;

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

    WebTreatmentPetitServiceImpl(DataSourceTransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    /**
     * 홈페이지 시술예약(쁘띠) 리스트 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListWebTreatmentPetitIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebTreatmentPetitServiceImpl selectListWebTreatmentPetitIntro 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("WebTreatmentPetitServiceImpl selectListWebTreatmentPetitIntro END");
        return map;
    }

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

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

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

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

        log.debug("webTreatmentPetitSearchKeywordParam  :"+paramMap.get("webTreatmentPetitSearchKeywordParam"));
        log.debug("webTreatmentPetitSort                :"+paramMap.get("webTreatmentPetitSort"));
        log.debug("webTreatmentPetitStart               :"+paramMap.get("webTreatmentPetitStart"));
        log.debug("webTreatmentPetitLimit               :"+paramMap.get("webTreatmentPetitLimit"));
        log.debug("webTreatmentPetitSort                :"+paramMap.get("webTreatmentPetitSort"));
        log.debug("webTreatmentPetitDir                 :"+paramMap.get("webTreatmentPetitDir"));
        log.debug("webTreatmentPetitCurrentPage         :"+paramMap.get("webTreatmentPetitCurrentPage"));

        log.debug("webTreatmentPetitSearchStartDate     :"+paramMap.get("webTreatmentPetitSearchStartDate"));
        log.debug("webTreatmentPetitSearchEndDate       :"+paramMap.get("webTreatmentPetitSearchEndDate"));

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

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

                paramMap.put("useYn", "Y");
                
                //List<Map<String, Object>> totalCountListMap = webTreatmentPetitSqlMapDAO.selectTotalWebTreatmentPetitCount(paramMap);
                int totalCount = cmmnDAO.selectCount("WebTreatmentPetit.selectTotalWebTreatmentPetitCount", paramMap);

                if(0 < totalCount) {
                    // 리스트 조회
                    //listMap = webTreatmentPetitSqlMapDAO.selectListWebTreatmentPetit(paramMap);
                    listMap = cmmnDAO.selectList("WebTreatmentPetit.selectListWebTreatmentPetit", 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("WebTreatmentPetitServiceImpl selectListWebTreatmentPetit END");
        return map;
    }

    /**
     * 홈페이지 시술예약(쁘띠)정보 상세 조회 (one)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectWebTreatmentPetit(
            HashMap<String, Object> paramMap) throws Exception {

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

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

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(postNo)){
                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){
                
                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("detail", cmmnDAO.select("WebTreatmentPetit.selectWebTreatmentPetit", paramMap));
                map.put("treatmentList", cmmnDAO.selectList("WebTreatmentPetit.selectWebTreatmentListPetit", paramMap));
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebTreatmentPetitServiceImpl selectWebTreatmentPetit 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("WebTreatmentPetitServiceImpl 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("WebTreatmentPetitServiceImpl uploadFile END");
        return map;
    }

    /**
     * 홈페이지 시술예약(쁘띠) 콘텐츠 등록 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> insertWebTreatmentPetitIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebTreatmentPetitServiceImpl insertWebTreatmentPetitIntro 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 = webTreatmentPetitSqlMapDAO.selectWebTreatmentPetitCategory(paramMap);
            map.put("categorylist", listMap);
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebTreatmentPetitServiceImpl insertWebTreatmentPetitIntro END");
        return map;
    }

    /**
     * 홈페이지 시술예약(쁘띠) 정보 등록
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> insertWebTreatmentPetit(
            HashMap<String, Object> paramMap, MultipartFile file, MultipartFile content_file) throws Exception{
        log.debug("WebTreatmentPetitServiceImpl insertTreatmentPetit 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 treatmentpetitStartDate  = String.valueOf(paramMap.get("treatmentpetitStartDate"));
//            String treatmentpetitEndDate    = String.valueOf(paramMap.get("treatmentpetitEndDate"));

            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(treatmentpetitStartDate)){
//                check = false;
//                map.put("msgCode", Constants.FAIL);
//                map.put("msgDesc","시술예약(쁘띠) 시작일자 정보가 없습니다.");
//            }
//
//            if(true != ValidationCheckUtil.emptyCheck(treatmentpetitEndDate)){
//                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("treatmentpetitStartDate", treatmentpetitStartDate);
//                    paramMap.put("treatmentpetitEndDate", treatmentpetitEndDate);
                     
                     webTreatmentPetitSqlMapDAO.insertWebTreatmentPetitContentFile(paramMap);
                     log.debug("contentId :"+paramMap.get("contentId"));
                     paramMap.put("contentId", paramMap.get("contentId"));
                     webTreatmentPetitSqlMapDAO.insertWebTreatmentPetitThumbnailFile(paramMap);
                     log.debug("thumbnailId :"+paramMap.get("thumbnailId"));
                     paramMap.put("thumbnailId", paramMap.get("thumbnailId"));
                }
                paramMap.put("tDate", tDate);
                paramMap.put("tId", tId);

                String muWebTreatmentPetitId = ("E").concat(String.valueOf(System.currentTimeMillis()));
                paramMap.put("muWebTreatmentPetitId",muWebTreatmentPetitId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webTreatmentPetitSqlMapDAO.insertWebTreatmentPetit(paramMap);
                log.debug("muWebTreatmentPetitId :"+paramMap.get("id"));
                paramMap.put("muWebTreatmentPetitId", paramMap.get("id"));
                map.put("muWebTreatmentPetitId", paramMap.get("muWebTreatmentPetitId"));
                webTreatmentPetitSqlMapDAO.updateWebTreatmentListDiet(paramMap);
                
                for(int i = 0; i < treatmentlist.length; i++) {
                	paramMap.put("treatmentid", treatmentlist[i]);
                	paramMap.put("treatmentprocedureid", treatmentprocedurelist[i]);
                	webTreatmentPetitSqlMapDAO.insertWebTreatmentListDiet(paramMap);
                }
                
                
                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "등록되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("WebTreatmentPetitServiceImpl insertWebTreatmentPetit END");
        return map;
    }

    /**
     * 홈페이지 시술예약(쁘띠) 수정 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> updateWebTreatmentPetitIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebTreatmentPetitServiceImpl updateWebTreatmentPetitIntro 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 = webTreatmentPetitSqlMapDAO.selectWebTreatmentPetitCategory(paramMap);
            map.put("category", cmmnDAO.selectList("WebTreatmentPetit.selectListTreatmentCategory", paramMap));

        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebTreatmentPetitServiceImpl updateWebTreatmentPetitIntro END");
        return map;
    }

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

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

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

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

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

        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 muWebTreatmentPetitId       = String.valueOf(paramMap.get("muWebTreatmentPetitId"));
            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 treatmentpetitStartDate  = String.valueOf(paramMap.get("treatmentpetitStartDate"));
//            String treatmentpetitEndDate    = String.valueOf(paramMap.get("treatmentpetitEndDate"));

            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");
                    
                    webTreatmentPetitSqlMapDAO.insertWebTreatmentPetitThumbnailFile(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");
                    
                    webTreatmentPetitSqlMapDAO.insertWebTreatmentPetitContentFile(paramMap);
                    log.debug("contentId :"+paramMap.get("contentId"));
                    paramMap.put("contentId", paramMap.get("contentId"));
                	
                }else {
                	paramMap.put("contentId", "");                	
                }
                paramMap.put("muWebTreatmentPetitId",muWebTreatmentPetitId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webTreatmentPetitSqlMapDAO.updateWebTreatmentPetit(paramMap);
                map.put("muWebTreatmentPetitId", paramMap.get("muWebTreatmentPetitId"));
                
                webTreatmentPetitSqlMapDAO.updateWebTreatmentListDiet(paramMap);
                for(int i = 0; i < treatmentlist.length; i++) {
                	paramMap.put("treatmentid", treatmentlist[i]);
                	paramMap.put("treatmentprocedureid", treatmentprocedurelist[i]);
                	webTreatmentPetitSqlMapDAO.insertWebTreatmentListDiet(paramMap);
                }
                
                
                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "수정되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("WebTreatmentPetitServiceImpl updateWebTreatmentPetit END");
        return map;
    }

    /**
     * 홈페이지 시술예약(쁘띠) 정보 삭제
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> deleteWebTreatmentPetit(
            HashMap<String, Object> paramMap) throws Exception{

    	HashMap<String, Object> map = new HashMap<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);
                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", "권한 정보가 없습니다.");
                }
            }
            int delCnt = 0;
            if(true == check) {
            	
            	
            	delCnt = cmmnDAO.delete("WebTreatmentPetit.deleteContentsBbs", paramMap);
            	map.put("msgCode", Constants.OK);
                map.put("msgDesc", delCnt + "건 삭제 되었습니다.");
            }
            
//            if(true == check){
//                String selectId = String.valueOf(paramMap.get("selectId"));
//                StringTokenizer selectData = new StringTokenizer(selectId, ",");
//                while(selectData.hasMoreTokens()){//토큰이 계속 있을때 까지
//                    String muWebTreatmentPetitId = selectData.nextToken();
//
//                    HashMap<String, Object> deleteParamMap = new HashMap<String, Object>();
//                    deleteParamMap.put("muWebTreatmentPetitId", muWebTreatmentPetitId);
//                    deleteParamMap.put("modId", paramMap.get("loginMemberId"));
//                    webTreatmentPetitSqlMapDAO.deleteWebTreatmentPetit(deleteParamMap);
//                }
//
//                map.put("msgCode", Constants.OK);
//                map.put("msgDesc", delCnt + "건 삭제 되었습니다.");
//            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        log.debug("WebTreatmentPetitServiceImpl deleteWebTreatmentPetit END");
        return map;
    }
    
    /**
     * 고객 리스트 조회.(List,Option)
     * 예약팝업에서 고객 조회
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListTreatmentOption(HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebTreatmentPetitServiceImpl 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 = webTreatmentPetitSqlMapDAO.selectTotalTreatmentCount(paramMap);
                if(0<totalCount){
                    listMap = webTreatmentPetitSqlMapDAO.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("WebTreatmentPetitServiceImpl selectListTreatmentOption END");
        return map;
    }
}
