package com.madeu.service.web.webauth.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 com.madeu.service.web.webmenu.WebMenuService;
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.webauth.WebAuthSqlMapDAO;
import com.madeu.dao.web.webauthmenurelation.WebAuthMenuRelationSqlMapDAO;
import com.madeu.dao.web.webmember.WebMemberSqlMapDAO;
import com.madeu.service.web.webauth.WebAuthService;
import com.madeu.util.ValidationCheckUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("WebAuthService")
public class WebAuthServiceImpl implements WebAuthService {
	
	@Autowired
	private WebAuthSqlMapDAO webAuthSqlMapDAO;
	
	@Autowired
	private WebMemberSqlMapDAO webMemberSqlMapDAO;
	
	@Autowired
	private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;
	@Autowired
	private WebMenuService webMenuService;
	
	/**
	 * 권한 리스트 조회 (option)
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListAuthOption(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebAuthServiceImpl selectListAuthOption 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 = webAuthSqlMapDAO.selectListAuthOption(paramMap);

				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("rows",listMap);
			}

		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}

		log.debug("WebAuthServiceImpl selectListAuthOption END");
		return map;
	}
	
	/**
	 * 권한 리스트 조회
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListAuth(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebAuthServiceImpl selectListAuth START");

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

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

		log.debug("menuClass           :"+paramMap.get("menuClass"));
		
		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){
				List<Map<String, Object>> totalCountListMap = webAuthSqlMapDAO.selectTotalAuthCount(paramMap);
				int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));
				
				if(0 < totalCount) {
					// 리스트 조회
					listMap = webAuthSqlMapDAO.selectListAuth(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("WebAuthServiceImpl selectListAuth END");
		return map;
	}
	/**
	 * 권한그룹 정보 상세 조회 (one)
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectAuth(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebAuthServiceImpl selectAuth START");

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

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

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

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

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

			// 정렬
			if(true != ValidationCheckUtil.emptyCheck(muAuthId)){
				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){
				List<Map<String, Object>> listMap = webAuthSqlMapDAO.selectAuth(paramMap);

				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("rows",listMap);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		log.debug("WebAuthServiceImpl selectAuth END");
		return map;
	}
	/**
	 * 헤더 메뉴 권한 조회(option)
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectHeaderRelation(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebAuthServiceImpl selectAuth START");

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

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

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

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

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

			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", webMenuService.selectMenuCode(paramMap).get("menuCode"));
				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("rows",authMenuRelationlistMap.get(0));
				}
				else {
					check = false;
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc", "권한 정보가 없습니다.");
				}
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		log.debug("WebAuthServiceImpl selectHeaderRelation END");
		return map;
	}

	/**
	 * 권한 정보 등록
	 * 
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	@Override
	@Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
	public HashMap<String, Object> insertAuth(
			HashMap<String, Object> paramMap) throws Exception{
		log.debug("WebAuthServiceImpl insertAuth START");
		
		HashMap<String, Object> map = new HashMap<String, Object>();

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

		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 authName      = String.valueOf(paramMap.get("authName"));
			
			if(true != ValidationCheckUtil.emptyCheck(authName)){
				check = false;
				map.put("msgCode", Constants.FAIL);
				map.put("msgDesc","권한명 정보가 없습니다.");
			}
			else{
				if(true != ValidationCheckUtil.lengthCheck(authName,35)){
					check = false;
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc","권한명 정보는 35자를 넘을 수 없습니다.");
				}
			}
			
			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 muAuthId = ("A").concat(String.valueOf(System.currentTimeMillis()));
				paramMap.put("muAuthId",muAuthId);
				paramMap.put("muMemberId", paramMap.get("loginMemberId"));
				webAuthSqlMapDAO.insertAuth(paramMap);
				log.debug("muAuthId :"+paramMap.get("id"));
				paramMap.put("muAuthId", paramMap.get("id"));
				map.put("muAuthId", paramMap.get("muAuthId"));
				
				map.put("msgCode", Constants.OK);
				map.put("msgDesc", "등록되었습니다.");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
		log.debug("WebAuthServiceImpl insertAuth END");
		return map;
	}
	/**
	 * 권한그룹 정보 수정
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
	public HashMap<String, Object> updateAuth(
			HashMap<String, Object> paramMap) throws Exception{
		log.debug("WebAuthServiceImpl updateAuth START");

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

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

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

		log.debug("muAuthId            :"+paramMap.get("muAuthId"));
		log.debug("authName            :"+paramMap.get("authName"));
		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 muAuthId       = String.valueOf(paramMap.get("muAuthId"));
			String authName       = String.valueOf(paramMap.get("authName"));
			String etc            = String.valueOf(paramMap.get("etc"));

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

			if(true != ValidationCheckUtil.emptyCheck(authName)){
				check = false;
				map.put("msgCode", Constants.FAIL);
				map.put("msgDesc","권한 그룹명 정보가 없습니다.");
			}
			else{
				if(true != ValidationCheckUtil.lengthCheck(authName,35)){
					check = false;
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc","권한 그룹명 정보는 35자를 넘을 수 없습니다.");
				}
			}

			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){
				webAuthSqlMapDAO.updateAuth(paramMap);
				map.put("muAuthId", paramMap.get("muAuthId"));

				map.put("msgCode", Constants.OK);
				map.put("msgDesc", "수정되었습니다.");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		log.debug("WebAuthServiceImpl updateAuth END");
		return map;
	}
	/**
	 * 권한 정보 삭제
	 * 
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	@Override
	@Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
	public HashMap<String, Object> deleteAuth(
			HashMap<String, Object> paramMap) throws Exception{
		log.debug("WebAuthServiceImpl deleteAuth START");
		
		HashMap<String, Object> map = new HashMap<String, Object>();

		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
		
		log.debug("menuClass           :"+paramMap.get("menuClass"));
		
		log.debug("muAuthId            :"+paramMap.get("muAuthId"));
		
		try{
			boolean check  = true;
			
			if(null== paramMap.get("muAuthId") || ("").equals(paramMap.get("muAuthId"))){
				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){
				webAuthSqlMapDAO.deleteAuth(paramMap);
				webAuthMenuRelationSqlMapDAO.deleteAuthMenuRelation(paramMap);
				
				map.put("msgCode", Constants.OK);
				map.put("msgDesc", "삭제 되었습니다.");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
		log.debug("WebAuthServiceImpl deleteAuth END");
		return map;
	}
}
