package com.madeu.service.web.webvacation.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.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.madeu.constants.Constants;
import com.madeu.dao.web.webauthmenurelation.WebAuthMenuRelationSqlMapDAO;
import com.madeu.dao.web.webmember.WebMemberSqlMapDAO;
import com.madeu.dao.web.webvacation.WebVacationSqlMapDAO;
import com.madeu.service.web.webvacation.WebVacationService;
import com.madeu.util.ValidationCheckUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("WebVacationService")
public class WebVacationServiceImpl implements WebVacationService {
	
	@Autowired
	private WebVacationSqlMapDAO webVacationSqlMapDAO;
	
	@Autowired
	private WebMemberSqlMapDAO webMemberSqlMapDAO;
	
	@Autowired
	private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;
	
	/**
	 * 연차설정 조회 (option)
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListVacationOption(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebVacationServiceImpl selectListVacationOption 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;
			
			if(true == check){
				List<Map<String, Object>> listMap = webVacationSqlMapDAO.selectListVacationOption(paramMap);
				
				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("rows",listMap);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}

		log.debug("WebVacationServiceImpl selectListVacationOption END");
		return map;
	}
	
	/**
	 * 연차설정 리스트 화면으로 이동.
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListVacationIntro(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebVacationServiceImpl selectListVacationIntro 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("WebVacationServiceImpl selectListVacationIntro END");
		return map;
	}
	
	/**
	 * 연차설정 리스트 조회
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListVacation(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebVacationServiceImpl selectListVacation START");

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

		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
		
		log.debug("menuClass           :"+paramMap.get("menuClass"));
		
		log.debug("vacationSearchKeywordParam  :"+paramMap.get("vacationSearchKeywordParam"));
		log.debug("vacationStart               :"+paramMap.get("vacationStart"));
		log.debug("vacationLimit               :"+paramMap.get("vacationLimit"));
		log.debug("vacationSort                :"+paramMap.get("vacationSort"));
		log.debug("vacationDir                 :"+paramMap.get("vacationDir"));
		
		log.debug("vacationSearchGroupId       :"+paramMap.get("vacationSearchGroupId"));
		log.debug("vacationSearchDutyId        :"+paramMap.get("vacationSearchDutyId"));

		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("vacationDir")||("").equals(paramMap.get("vacationDir"))){
					
				}
				else{
					String dir = String.valueOf(paramMap.get("dir"));
					if(("A").equals(dir)){
						paramMap.put("vacationDir", "DESC");
					}
					else if(("B").equals(dir)){
						paramMap.put("vacationDir", "ASC");
					}
					else {
						paramMap.put("vacationDir", "DESC");
					}
				}
				
				paramMap.put("useYn", "Y");
				
				List<Map<String, Object>> totalCountListMap = webVacationSqlMapDAO.selectTotalVacationCount(paramMap);
				int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));
				
				if(0 < totalCount) {
					// 리스트 조회
					listMap = webVacationSqlMapDAO.selectListVacation(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("WebVacationServiceImpl selectListVacation END");
		return map;
	}
	
	/**
	 * 연차설정 정보 상세 조회 (one)
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectVacation(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebVacationServiceImpl selectVacation START");

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

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

		try{
			boolean check  = true;
			String muVacationId    = String.valueOf(paramMap.get("muVacationId"));
			
			// 정렬
			if(true != ValidationCheckUtil.emptyCheck(muVacationId)){
				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 = webVacationSqlMapDAO.selectVacation(paramMap);
				
				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("rows",listMap);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		log.debug("WebVacationServiceImpl selectVacation END");
		return map;
	}
	
	/**
	 * 연차설정 정보 등록
	 * 
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	@Override
	@Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
	public HashMap<String, Object> insertVacation(
			HashMap<String, Object> paramMap) throws Exception{
		log.debug("WebVacationServiceImpl insertVacation START");
		
		HashMap<String, Object> map = new HashMap<String, Object>();

		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
		
		log.debug("menuClass           :"+paramMap.get("menuClass"));
		
		log.debug("vacationName        :"+paramMap.get("vacationName"));
		log.debug("hourType            :"+paramMap.get("hourType"));
		log.debug("startTime           :"+paramMap.get("startTime"));
		log.debug("endTime             :"+paramMap.get("endTime"));
		log.debug("availableDays       :"+paramMap.get("availableDays"));
		log.debug("etc                 :"+paramMap.get("etc"));

		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 vacationName    = String.valueOf(paramMap.get("vacationName"));
			String hourType        = String.valueOf(paramMap.get("hourType"));
//			String startTime       = String.valueOf(paramMap.get("startTime"));
//			String endTime         = String.valueOf(paramMap.get("endTime"));
			String hour            = String.valueOf(paramMap.get("hour"));
			String availableDays   = String.valueOf(paramMap.get("availableDays"));
			String etc             = String.valueOf(paramMap.get("etc"));
			
			if(true != ValidationCheckUtil.emptyCheck(vacationName)){
				check = false;
				map.put("msgCode", Constants.FAIL);
				map.put("msgDesc","연차유형명 정보가 없습니다.");
			}
			else{
				if(true != ValidationCheckUtil.lengthCheck(vacationName,255)){
					check = false;
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc","연차유형명 정보는 100자를 넘을 수 없습니다.");
				}
			}

			if(true != ValidationCheckUtil.emptyCheck(hourType)){
				check = false;
				map.put("msgCode", Constants.FAIL);
				map.put("msgDesc","시간유형 정보가 없습니다.");
			}
			else {
				if(("A").equals(hourType)) {
					
				}
				else {
					if(true != ValidationCheckUtil.emptyCheck(hour)){
						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){
				paramMap.put("tDate", tDate);
				paramMap.put("tId", tId);
				
				String muVacationId = ("V").concat(String.valueOf(System.currentTimeMillis()));
				paramMap.put("muVacationId",muVacationId);
				paramMap.put("muMemberId", paramMap.get("loginMemberId"));
				webVacationSqlMapDAO.insertVacation(paramMap);
				log.debug("muVacationId :"+paramMap.get("id"));
				paramMap.put("muVacationId", paramMap.get("id"));
				map.put("muVacationId", paramMap.get("muVacationId"));

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

	/**
	 * 연차설정 정보 수정
	 * 
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	@Override
	@Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
	public HashMap<String, Object> updateVacation(
			HashMap<String, Object> paramMap) throws Exception{
		log.debug("WebVacationServiceImpl updateVacation START");
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
		
		log.debug("menuClass           :"+paramMap.get("menuClass"));
		
		log.debug("muVacationId        :"+paramMap.get("muVacationId"));

		log.debug("vacationName        :"+paramMap.get("vacationName"));
		log.debug("hourType            :"+paramMap.get("hourType"));
		log.debug("hour                :"+paramMap.get("hour"));
		log.debug("availableDays       :"+paramMap.get("availableDays"));
		log.debug("etc                 :"+paramMap.get("etc"));

		try{
			boolean check  = true;

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

			String vacationName    = String.valueOf(paramMap.get("vacationName"));
			String hourType        = String.valueOf(paramMap.get("hourType"));
			String hour            = String.valueOf(paramMap.get("hour"));
			String availableDays   = String.valueOf(paramMap.get("availableDays"));
			String etc             = String.valueOf(paramMap.get("etc"));

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

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

			if(true != ValidationCheckUtil.emptyCheck(hourType)){
				check = false;
				map.put("msgCode", Constants.FAIL);
				map.put("msgDesc","시간유형 정보가 없습니다.");
			}
			else {
				if(("A").equals(hourType)) {
					
				}
				else {
					if(true != ValidationCheckUtil.emptyCheck(hour)){
						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){
				webVacationSqlMapDAO.updateVacation(paramMap);
				map.put("muVacationId", paramMap.get("muVacationId"));

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

	/**
	 * 연차설정 정보 삭제
	 * 
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	@Override
	@Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
	public HashMap<String, Object> deleteVacation(
			HashMap<String, Object> paramMap) throws Exception{
		log.debug("WebVacationServiceImpl deleteVacation 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 muVacationId = selectData.nextToken();
					
					HashMap<String, Object> deleteParamMap = new HashMap<String, Object>();
					deleteParamMap.put("muVacationId", muVacationId);
					deleteParamMap.put("modId", paramMap.get("loginMemberId"));
					webVacationSqlMapDAO.deleteVacation(deleteParamMap);
				}
				
				map.put("msgCode", Constants.OK);
				map.put("msgDesc", "삭제 되었습니다.");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		log.debug("WebVacationServiceImpl deleteVacation END");
		return map;
	}
}
