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

import java.text.SimpleDateFormat;
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.webauth.WebAuthSqlMapDAO;
import com.madeu.dao.web.webauthmenurelation.WebAuthMenuRelationSqlMapDAO;
import com.madeu.dao.web.webmember.WebMemberSqlMapDAO;
import com.madeu.service.web.webauthmenurelation.WebAuthMenuRelationService;
import com.madeu.util.ValidationCheckUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("WebAuthMenuRelationService")
public class WebAuthMenuRelationServiceImpl implements WebAuthMenuRelationService {
	
	@Autowired
	private WebAuthSqlMapDAO webAuthSqlMapDAO;
	
	@Autowired
	private WebMemberSqlMapDAO webMemberSqlMapDAO;
	
	@Autowired
	private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;
	
	/**
	 * 권한별 메뉴 리스트 조회
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListAuthMenuRelation(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebAuthMenuRelationServiceImpl selectListAuthMenuRelation 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>> authListMap = webAuthSqlMapDAO.selectAuth(paramMap);
				int authListMapSize = authListMap.size();
				
				if(1 == authListMapSize){
					paramMap.put("authName", authListMap.get(0).get("authName"));
				}
				else {
					check = false;
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc", "권한 정보가 없습니다.");
				}
			}
			
			if(true == check){
				List<Map<String, Object>> listMap = webAuthMenuRelationSqlMapDAO.selectListAuthMenuRelation(paramMap);

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

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

		log.debug("WebAuthMenuRelationServiceImpl selectListAuthMenuRelation END");
		return map;
	}
	
	/**
	 * 권한별 메뉴 정보 저장
	 * 
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	@Override
	@Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
	public HashMap<String, Object> updateAuthMenuRelation(
			HashMap<String, Object> paramMap) throws Exception{
		log.debug("WebAuthMenuRelationServiceImpl updateAuthMenuRelation 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("selectAuthArr       :"+paramMap.get("selectAuthArr"));
		
		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 selectAuthArr = String.valueOf(paramMap.get("selectAuthArr"));
			
			if(true != ValidationCheckUtil.emptyCheck(muAuthId)){
				check = false;
				map.put("msgCode", Constants.FAIL);
				map.put("msgDesc","권한 정보가 없습니다.");
			}
			
			if(true != ValidationCheckUtil.emptyCheck(selectAuthArr)){
				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){
				// 권한 정보 확인
				List<Map<String, Object>> authListMap = webAuthSqlMapDAO.selectAuth(paramMap);
				int authListMapSize = authListMap.size();
				
				if(1 == authListMapSize){
					
				}
				else {
					check = false;
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc", "권한 정보가 없습니다.");
				}
			}
			
			if(true == check){
				paramMap.put("tDate", tDate);
				paramMap.put("tId", tId);
				
				// 삭제
				webAuthMenuRelationSqlMapDAO.deleteAuthMenuRelation(paramMap);
				
				// 권한별 메뉴 연결 등록
				StringTokenizer selectData = new StringTokenizer(selectAuthArr, ",");
				while(selectData.hasMoreTokens()){//토큰이 계속 있을때 까지
					String muMenuId = selectData.nextToken();
					String [] menuArr = muMenuId.split("\\|");
					
					HashMap<String, Object> insertParamMap = new HashMap<String, Object>();
					String muAuthMenuRelationId = ("AMR").concat(String.valueOf(System.currentTimeMillis()));
					insertParamMap.put("muAuthMenuRelationId",muAuthMenuRelationId);
					insertParamMap.put("muAuthId", muAuthId);
					insertParamMap.put("muMenuId", menuArr[0]);
					insertParamMap.put("selectUseYn", menuArr[1]);
					insertParamMap.put("insertUseYn", menuArr[2]);
					insertParamMap.put("updateUseYn", menuArr[3]);
					insertParamMap.put("deleteUseYn", menuArr[4]);
					insertParamMap.put("downloadUseYn", menuArr[5]);
					insertParamMap.put("regId", paramMap.get("regId"));
					insertParamMap.put("modId", paramMap.get("modId"));
					insertParamMap.put("tId", paramMap.get("tId"));
					insertParamMap.put("tDate", paramMap.get("tDatetDate"));
					webAuthMenuRelationSqlMapDAO.insertAuthMenuRelation(insertParamMap);
				}
				
				map.put("msgCode", Constants.OK);
				map.put("msgDesc", "저장되었습니다.");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
		log.debug("WebAuthMenuRelationServiceImpl updateAuthMenuRelation END");
		return map;
	}
}
