package com.madeu.service.mobile.mobilereservation.impl;

import com.madeu.constants.Constants;
import com.madeu.dao.mobile.mobileuser.MobileUserSqlMapDAO;
import com.madeu.dao.mobile.mobileReservation.MobileReservationSqlMapDAO;
import com.madeu.dao.mobile.mobileconsultingchart.MobileConsultingChartSqlMapDAO;
import com.madeu.dao.mobile.mobilemedicalchart.MobileMedicalChartSqlMapDAO;
import com.madeu.dao.mobile.mobiledevice.MobileDeviceSqlMapDAO;
import com.madeu.dao.mobile.mobilekakao.MobileKakaoSqlMapDAO;
import com.madeu.dao.mobile.mobilekakaotemplate.MobileKakaoTemplateSqlMapDAO;
import com.madeu.dao.mobile.mobilepush.MobilePushSqlMapDAO;
import com.madeu.service.common.autokakao.AutoKakaoService;
import com.madeu.service.common.autopush.AutoPushService;
import com.madeu.service.mobile.mobilereservation.MobileReservationService;
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 java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service("MobileReservationService")
public class MobileReservationServiceImpl implements MobileReservationService {
    @Autowired
    private MobileReservationSqlMapDAO mobileReservationSqlMapDAO;

    @Autowired
    private MobileConsultingChartSqlMapDAO mobileConsultingChartSqlMapDAO;

    @Autowired
    private MobileMedicalChartSqlMapDAO mobileMedicalChartSqlMapDAO;

    @Autowired
    private MobileUserSqlMapDAO mobileUserSqlMapDAO;

	@Autowired
	private MobileKakaoSqlMapDAO mobileKakaoSqlMapDAO;

	@Autowired
	private MobileKakaoTemplateSqlMapDAO mobileKakaoTemplateSqlMapDAO;

	@Autowired
	private MobileDeviceSqlMapDAO mobileDeviceSqlMapDAO;

	@Autowired
	private MobilePushSqlMapDAO mobilePushSqlMapDAO;

	@Autowired
	private AutoPushService autoPushService;

	@Autowired
	private AutoKakaoService autoKakaoService;

	@Value("${popbill.sender-phone-num}")
	String senderPhoneNumber;


    /**
     * 예약내역 정보 리스트 조회 (list)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListReservation(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("MobileReservationServiceImpl selectListReservation START");

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

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

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

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

        try{
            boolean check  = true;
            String muAppBoardId    = String.valueOf(paramMap.get("muAppBoardId"));
            List<Map<String, Object>> listMap = new ArrayList<>();

            if(true == check){
                List<Map<String, Object>> totalCountListMap = mobileReservationSqlMapDAO.selectListReservationCount(paramMap);
                int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));

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

    /**
     * 이전예약내역 리스트 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListReservationHistory(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("MobileReservationServiceImpl selectListReservationHistory START");

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

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

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

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

        try{
            boolean check  = true;
            String muAppBoardId    = String.valueOf(paramMap.get("muAppBoardId"));
            if(true == check){
                List<Map<String, Object>> listMap = new ArrayList<>();
                List<Map<String, Object>> totalCountListMap = mobileReservationSqlMapDAO.selectListReservationHistoryCount(paramMap);
                int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));

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

    /**
     * 홈 상단 예약 알림 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectReservationHeader(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("MobileReservationServiceImpl selectReservationHeader START");

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

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

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

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

        try{
            boolean check  = true;
            String muAppBoardId    = String.valueOf(paramMap.get("muAppBoardId"));
            if(true == check){
                List<Map<String, Object>> listMap = mobileReservationSqlMapDAO.selectReservationHeader(paramMap);

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

    /**
     * 예약현황 정보 등록 (예약등록)
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> insertReserve(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("MobileReservationServiceImpl insertReserve START");

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

        log.debug("loginUserId       :"+paramMap.get("loginUserId"));
        log.debug("muUserId          :"+paramMap.get("muUserId"));
        log.debug("userName          :"+paramMap.get("userName"));

        log.debug("reserveDate         :"+paramMap.get("reserveDate"));
        log.debug("reserveTime         :"+paramMap.get("reserveTime"));
        log.debug("muTreatmentId       :"+paramMap.get("muTreatmentId"));
        log.debug("treatmentName       :"+paramMap.get("treatmentName"));
        log.debug("muProcedureId       :"+paramMap.get("muProcedureId"));
        log.debug("procedureName       :"+paramMap.get("procedureName"));

        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 muUserId               = String.valueOf(paramMap.get("muUserId"));
            String userName               = String.valueOf(paramMap.get("userName"));
            String reserveDate            = String.valueOf(paramMap.get("reserveDate"));
            String reserveTime            = String.valueOf(paramMap.get("reserveTime"));
            String muTreatmentId          = String.valueOf(paramMap.get("muTreatmentId"));
            String treatmentName          = String.valueOf(paramMap.get("treatmentName"));
            String muProcedureId          = String.valueOf(paramMap.get("muProcedureId"));
            String procedureName          = String.valueOf(paramMap.get("procedureName"));
            String etc                    = String.valueOf(paramMap.get("etc"));

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

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

            if(true != ValidationCheckUtil.emptyCheck(reserveDate)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","예약일자 정보가 없습니다.");
            }else {
                if(true != ValidationCheckUtil.dateCheck(reserveDate)){
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc","예약일자 정보가 YYYY-MM-DD 형식에 맞지 않습니다.");
                }
            }

            if(true != ValidationCheckUtil.emptyCheck(reserveTime)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","예약시간 정보가 없습니다.");
            }else {
                if(true != ValidationCheckUtil.timeCheck(reserveTime)){
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc","예약시간 정보가 HH:mm 형식에 맞지 않습니다.");
                }
            }

            if(true != ValidationCheckUtil.emptyCheck(muTreatmentId)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","진료 정보가 없습니다.");
            }
            else if(true != ValidationCheckUtil.emptyCheck(treatmentName)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","진료 정보가 없습니다.");
            }

            if(true != ValidationCheckUtil.emptyCheck(muProcedureId)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","시술 정보가 없습니다.");
            }
            else if(true != ValidationCheckUtil.emptyCheck(procedureName)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","시술 정보가 없습니다.");
            }

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

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

            String[] muProcedureIds = muProcedureId.split(", ");
            String[] procedureNames = procedureName.split(", ");



            if(true == check){
                for (String muTreatmentProcedureId : muProcedureIds) {
                    // 예약 중복 확인
                    HashMap<String, Object> selectParamMap = new HashMap<String, Object>();
                    selectParamMap.put("reserveDate", reserveDate);
                    selectParamMap.put("reserveTime", reserveTime);
                    selectParamMap.put("userName", userName);
                    selectParamMap.put("phoneNumber", paramMap.get("phoneNumber"));
                    selectParamMap.put("muTreatmentProcedureId", muTreatmentProcedureId);
                    List<Map<String, Object>> reserveYnList = mobileReservationSqlMapDAO.selectListReserveYn(selectParamMap);
                    if(0 == reserveYnList.size()) {
                    }
                    else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "해당시간에 진료예약건이 있습니다.");
                    }
                }
                if(true == check){
                    // 휴일 확인
                    HashMap<String, Object> selectHolidayMap = new HashMap<String, Object>();
                    selectHolidayMap.put("searchDate", reserveDate);
                    List<Map<String, Object>> hospitalListMap = mobileReservationSqlMapDAO.selectHospitalHoliday(selectHolidayMap);

                    if(1 == hospitalListMap.size()) {
                        String openYn = String.valueOf(hospitalListMap.get(0).get("openYn"));
                        String holidayYn = String.valueOf(hospitalListMap.get(0).get("holidayYn"));

                        if(("N").equals(openYn)||("Y").equals(holidayYn)){
                            check = false;
                            map.put("msgCode", Constants.FAIL);
                            map.put("msgDesc", "운영시간이 아닙니다.");
                        }
                    }
                }
            }

            if(true == check){
                paramMap.put("tDate", tDate);
                paramMap.put("tId", tId);

				// 자동발송 (알림톡 + 푸시)
				// 템플릿 조회
				HashMap<String, Object> insertKakaoMap = new HashMap<String, Object>();
				insertKakaoMap.put("templateCode", "024110000457");
				List<Map<String, Object>> templateListMap = mobileKakaoTemplateSqlMapDAO.selectKakaoTemplate(insertKakaoMap);

				// 디바이스 리스트 조회
				List<Map<String, Object>> deviceListMap = mobileDeviceSqlMapDAO.selectListDevice2(paramMap);
				int deviceListMapSize = deviceListMap.size();

				// 고객명, 예약날짜, 예약시간, 시술명
				String KakaoTemplateTitle = String.valueOf(templateListMap.get(0).get("title"));
				String KakaoTemplateContent = String.valueOf(templateListMap.get(0).get("content"));

                for (int i = 0; i < muProcedureIds.length; i++) {

                    HashMap<String, Object> treatmentProcedureParamMap = new HashMap<String, Object>();
                    treatmentProcedureParamMap.put("muTreatmentProcedureId", muProcedureIds[i]);
                    List<Map<String, Object>> treatmentProcedureList = mobileReservationSqlMapDAO.selectTreatmentProcedure(treatmentProcedureParamMap);

                    if(1==treatmentProcedureList.size()) {
                        String treatmentId = String.valueOf(treatmentProcedureList.get(0).get("muTreatmentId"));
                        treatmentName = String.valueOf(treatmentProcedureList.get(0).get("treatmentName"));

                        String muReserveId = ("R").concat(String.valueOf(System.currentTimeMillis()));
                        paramMap.put("muReserveId",muReserveId);
                        paramMap.put("reserveDate", reserveDate);
                        paramMap.put("reserveTime", reserveTime);
                        paramMap.put("muTreatmentProcedureId", muProcedureIds[i]);
                        paramMap.put("treatmentProcedureName", procedureNames[i]);


                        paramMap.put("muTreatmentId", treatmentId);
                        paramMap.put("treatmentName", treatmentName);
                        paramMap.put("etc", etc);
                        paramMap.put("status", "T");
                        paramMap.put("status2", "N");
                        mobileReservationSqlMapDAO.insertReserve(paramMap);
                        paramMap.put("muReserveId",paramMap.get("id"));

                        // 상담차트 등록
                        List<Map<String, Object>> userConsultingChartNumberList = mobileConsultingChartSqlMapDAO.selectConsultingChartNumber(paramMap);
                        String chartNumber1 = String.valueOf(userConsultingChartNumberList.get(0).get("chartNumber"));

                        String muUserConsultingChartId = ("UCC").concat(String.valueOf(System.currentTimeMillis()));
                        paramMap.put("muUserConsultingChartId",muUserConsultingChartId);
                        paramMap.put("chartNumber",chartNumber1);
                        mobileConsultingChartSqlMapDAO.insertConsultingChart(paramMap);

                        // 시술차트 등록
                        List<Map<String, Object>> userMedicalChartNumberList = mobileMedicalChartSqlMapDAO.selectMedicalChartNumber(paramMap);
                        String chartNumber2 = String.valueOf(userMedicalChartNumberList.get(0).get("chartNumber"));

                        String muUserMedicalChartId = ("UMC").concat(String.valueOf(System.currentTimeMillis()));
                        paramMap.put("muUserMedicalChartId",muUserMedicalChartId);
                        paramMap.put("chartNumber",chartNumber2);
                        mobileMedicalChartSqlMapDAO.insertMedicalChart(paramMap);



						// 자동발송 (알림톡 + 푸시)
						// 고객명, 예약날짜, 예약시간, 시술명
						String kakaoContent = KakaoTemplateContent;
						kakaoContent = kakaoContent.replace("#{고객명}", userName);
						kakaoContent = kakaoContent.replace("#{예약날짜}", reserveDate);
						kakaoContent = kakaoContent.replace("#{예약시간}", reserveTime);
						kakaoContent = kakaoContent.replace("#{시술명}", treatmentName.concat("-").concat(procedureNames[i]));


						insertKakaoMap.put("muKakaoId", ("K").concat(String.valueOf(System.currentTimeMillis())));
						insertKakaoMap.put("muUserId", paramMap.get("muUserId"));
						insertKakaoMap.put("userName", paramMap.get("userName"));
						insertKakaoMap.put("phoneNumber", paramMap.get("phoneNumber"));
						insertKakaoMap.put("content", kakaoContent);
						insertKakaoMap.put("state", "R");
						insertKakaoMap.put("popbillState", "0");
						insertKakaoMap.put("sender", senderPhoneNumber);
						insertKakaoMap.put("regId", "SYSTEM");
						insertKakaoMap.put("modId", "SYSTEM");
						insertKakaoMap.put("tId", paramMap.get("tId"));
						insertKakaoMap.put("tDate", paramMap.get("tDate"));

						mobileKakaoSqlMapDAO.insertKakao(insertKakaoMap);
//                        autoKakaoService.send("reserve", insertKakaoMap);

						// 푸시
						if(0<deviceListMapSize){
							HashMap<String, Object> insertPushMap = new HashMap<String, Object>();
							for(int j=0; j<deviceListMapSize; j++){
								insertPushMap.put("muPushId", ("P").concat(String.valueOf(System.currentTimeMillis())));
								insertPushMap.put("muUserId", paramMap.get("muUserId"));
								insertPushMap.put("userName", paramMap.get("userName"));
								insertPushMap.put("muDeviceId", deviceListMap.get(j).get("muDeviceId"));
								insertPushMap.put("deviceUuid", deviceListMap.get(j).get("deviceUuid"));
								insertPushMap.put("pushKey", deviceListMap.get(j).get("pushKey"));
								insertPushMap.put("pushType", "A");
								insertPushMap.put("phoneType", deviceListMap.get(j).get("phoneType"));
								insertPushMap.put("title", KakaoTemplateTitle);
								insertPushMap.put("content", kakaoContent);
								insertPushMap.put("state", "R");
								insertPushMap.put("regId", "SYSTEM");
								insertPushMap.put("modId", "SYSTEM");
								insertPushMap.put("tId", paramMap.get("tId"));
								insertPushMap.put("tDate", paramMap.get("tDate"));

								mobilePushSqlMapDAO.insertPush(insertPushMap);
//                                autoPushService.send("reserve", insertPushMap);
							}
						}
                    }else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "시술 정보가 없습니다.");
                        throw new Exception("시술 정보가 없습니다.");
                    }
                }
                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "등록되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("MobileReservationServiceImpl insertReserve END");
        return map;
    }

    /**
     * 예약현황 정보 상태 수정 (진료상태)
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> updateReserveStatus(
            HashMap<String, Object> paramMap) throws Exception{
        log.debug("MobileReservationServiceImpl updateReserveStatus START");

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

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

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

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

        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());
            paramMap.put("tDate", tDate);
            paramMap.put("tId", tId);

            String muReserveId       = String.valueOf(paramMap.get("muReserveId"));
            String status            = String.valueOf(paramMap.get("status"));
            String userName = "";
			String phoneNumber = "";
			String reserveDate = "";
			String reserveTime = "";
			String treatmentName = "";
			String treatmentProcedureName = "";

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

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

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

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


            if(true == check){
                // 예약 조회
                List<Map<String, Object>> listMap = mobileReservationSqlMapDAO.selectReserve(paramMap);

                if(1==listMap.size()) {
                    paramMap.put("muUserId", listMap.get(0).get("muUserId"));
                    paramMap.put("userName", listMap.get(0).get("userName"));
					paramMap.put("phoneNumber", listMap.get(0).get("phoneNumber"));
					paramMap.put("reserveDate", listMap.get(0).get("reserveDate"));
					paramMap.put("reserveTime", listMap.get(0).get("reserveTime"));
					paramMap.put("treatmentName", listMap.get(0).get("treatmentName"));
					paramMap.put("treatmentProcedureName", listMap.get(0).get("treatmentProcedureName"));

                    userName = String.valueOf(listMap.get(0).get("userName"));
					phoneNumber = String.valueOf(listMap.get(0).get("phoneNumber"));
					reserveDate = String.valueOf(listMap.get(0).get("reserveDate"));
					reserveTime = String.valueOf(listMap.get(0).get("reserveTime"));
					treatmentName = String.valueOf(listMap.get(0).get("treatmentName"));
                    treatmentProcedureName = String.valueOf(listMap.get(0).get("treatmentProcedureName"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "예약 정보가 없습니다.");
                }
            }

            if(true == check){
                //시술차트 및 상담차트 삭제
                mobileMedicalChartSqlMapDAO.deleteMedicalChart(paramMap);
                mobileConsultingChartSqlMapDAO.deleteConsultingChart(paramMap);
                mobileReservationSqlMapDAO.updateReserveStatus(paramMap);



                // 자동발송 (알림톡 + 푸시)
                // 템플릿 조회
                HashMap<String, Object> insertKakaoMap = new HashMap<String, Object>();
                insertKakaoMap.put("templateCode", "024110000456");
                List<Map<String, Object>> templateListMap = mobileKakaoTemplateSqlMapDAO.selectKakaoTemplate(insertKakaoMap);

                // 디바이스 리스트 조회
                List<Map<String, Object>> deviceListMap = mobileDeviceSqlMapDAO.selectListDevice2(paramMap);
                int deviceListMapSize = deviceListMap.size();

                // 고객명, 예약날짜, 예약시간, 시술명
                String KakaoTemplateTitle = String.valueOf(templateListMap.get(0).get("title"));
                String KakaoTemplateContent = String.valueOf(templateListMap.get(0).get("content"));



                // 자동발송 (알림톡 + 푸시)
                // 고객명, 예약날짜, 예약시간, 시술명
                String kakaoContent = KakaoTemplateContent;
                kakaoContent = kakaoContent.replace("#{고객명}", userName);
                kakaoContent = kakaoContent.replace("#{예약날짜}", reserveDate);
                kakaoContent = kakaoContent.replace("#{예약시간}", reserveTime);
                kakaoContent = kakaoContent.replace("#{시술명}", treatmentName.concat("-").concat(treatmentProcedureName));


                insertKakaoMap.put("muKakaoId", ("K").concat(String.valueOf(System.currentTimeMillis())));
                insertKakaoMap.put("muUserId", paramMap.get("muUserId"));
                insertKakaoMap.put("userName", paramMap.get("userName"));
                insertKakaoMap.put("phoneNumber", paramMap.get("phoneNumber"));
                insertKakaoMap.put("content", kakaoContent);
                insertKakaoMap.put("state", "R");
                insertKakaoMap.put("popbillState", "0");
                insertKakaoMap.put("sender", senderPhoneNumber);
                insertKakaoMap.put("regId", "SYSTEM");
                insertKakaoMap.put("modId", "SYSTEM");
                insertKakaoMap.put("tId", paramMap.get("tId"));
                insertKakaoMap.put("tDate", paramMap.get("tDate"));

                mobileKakaoSqlMapDAO.insertKakao(insertKakaoMap);
//                autoKakaoService.send("reserve", insertKakaoMap);

                // 푸시
                if(0<deviceListMapSize){
                    HashMap<String, Object> insertPushMap = new HashMap<String, Object>();
                    for(int j=0; j<deviceListMapSize; j++){
                        insertPushMap.put("muPushId", ("P").concat(String.valueOf(System.currentTimeMillis())));
                        insertPushMap.put("muUserId", paramMap.get("muUserId"));
                        insertPushMap.put("userName", paramMap.get("userName"));
                        insertPushMap.put("muDeviceId", deviceListMap.get(j).get("muDeviceId"));
                        insertPushMap.put("deviceUuid", deviceListMap.get(j).get("deviceUuid"));
                        insertPushMap.put("pushKey", deviceListMap.get(j).get("pushKey"));
                        insertPushMap.put("pushType", "A");
                        insertPushMap.put("phoneType", deviceListMap.get(j).get("phoneType"));
                        insertPushMap.put("title", KakaoTemplateTitle);
                        insertPushMap.put("content", kakaoContent);
                        insertPushMap.put("state", "R");
                        insertPushMap.put("regId", "SYSTEM");
                        insertPushMap.put("modId", "SYSTEM");
                        insertPushMap.put("tId", paramMap.get("tId"));
                        insertPushMap.put("tDate", paramMap.get("tDate"));

                        mobilePushSqlMapDAO.insertPush(insertPushMap);
//                        autoPushService.send("reserve", insertPushMap);
                    }
                }



                map.put("muReserveId", paramMap.get("muReserveId"));
                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "처리되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("MobileReservationServiceImpl updateReserveStatus END");
        return map;
    }

    /**
     * 예약현황 정보 상태 수정 (진료상태)
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> updateReserveUserCheckYn(
            HashMap<String, Object> paramMap) throws Exception{
        log.debug("MobileReservationServiceImpl updateReserveUserCheckYn START");

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

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

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

        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());
            paramMap.put("tDate", tDate);
            paramMap.put("tId", tId);

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

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

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

            if(true == check){

                mobileReservationSqlMapDAO.updateReserveUserCheckYn(paramMap);

                map.put("muReserveId", paramMap.get("muReserveId"));
                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "처리되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("MobileReservationServiceImpl updateReserveUserCheckYn END");
        return map;
    }

}
